-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
779 lines (666 loc) · 26.7 KB
/
Copy pathapi.ts
File metadata and controls
779 lines (666 loc) · 26.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/**
* @file api.ts
* @description This file contains the main API endpoints for the application.
*/
import {http, Marketplace, RatingSystem, Resources, Sdk, validChains} from "media-sdk"
import express from "express"
import bodyParser from "body-parser"
import cors from "cors"
import {DealsController} from "./database/controllers/dealsController"
import {ResourcesController} from "./database/controllers/resourcesController"
import {OffersController} from "./database/controllers/offersController"
import {parseFilter} from "./utils/filter"
import {createRelationsBetweenTables} from "./database/utils"
import {ProvidersController} from "./database/controllers/providersController"
import {EventsController} from "./database/controllers/eventsController"
import {ProvidersMetadata} from "./database/models/Providers/ProvidersMetadata"
import {httpNetworks} from "./networks"
import {ProviderClient} from "./database/models/manyToMany/ProviderClient"
import {ChainClient} from "./database/models/manyToMany/ChainClient"
import {Deal} from "./database/models/deals/Deal"
import {ChainProvider} from "./database/models/manyToMany/ChainProvider"
import {Op} from "sequelize"
import {RatingController} from "./database/controllers/ratingController"
import {Offer} from "./database/models/offers/Offer"
import {validateParams, ValidatorType} from "./middlewares/validation"
import {validateChain} from "./middlewares/validChain"
// Initialize express app
export const app = express()
// Middleware
app.use(bodyParser.json()) // for parsing application/json
app.use(cors()) // for enabling CORS
/**
* @function manageIncomingFilterRequest
* @description Manage incoming request and parse filters from query parameters
* @param {object} req - The request object
*/
function manageIncomingFilterRequest(req: any) {
// Parse filters from query parameters
const filters = JSON.parse(req.query.filters ? req.query.filters as string : "{}")
// Get page number and size from filters
const page = req.query.page ? Number(req.query.page) : undefined
const pageSize = req.query.pageSize ? Number(req.query.pageSize) : undefined
// Parse individual filters
const genericFilter = parseFilter(filters.genericFilter ? filters.genericFilter : {})
const metadataFilter = parseFilter(filters.metadataFilter ? filters.metadataFilter : {})
const bandwidthFilter = parseFilter(filters.bandwidthFilter ? filters.bandwidthFilter : {})
const nodeLocationFilter = parseFilter(filters.nodeLocationFilter ? filters.nodeLocationFilter : {})
const clientFilter = parseFilter(filters.clientFilter ? filters.clientFilter : {})
return {
page,
pageSize,
genericFilter,
metadataFilter,
bandwidthFilter,
nodeLocationFilter,
clientFilter
}
}
/**
* @route GET /resources
* @description Retrieves all resources.
*/
app.get("/resources", validateParams({
chainId: [ValidatorType.NUMBER_ARRAY_OPTIONAL, ValidatorType.NUMBER_OPTIONAL]
}), async (req, res) => {
const chainId = req.query.chainId
try{
const isNumber = !isNaN(Number(chainId))
const formattedChainId = chainId ? (isNumber ? Number(chainId) : JSON.parse(chainId as string)) : undefined
const resources = await ResourcesController.getResources(formattedChainId)
res.json(resources)
} catch (e) {
console.log("Error:", e)
res.status(500).json({error: "Something went wrong"})
}
})
/**
* @route GET /deals
* @description Retrieves deals based on provided filters, page number and page size.
*/
app.get("/deals", validateParams({
chainId: [ValidatorType.NUMBER_OPTIONAL, ValidatorType.NUMBER_ARRAY_OPTIONAL]
}), validateChain(Object.keys(validChains).map(chain => Number(chain))), async (req, res) => {
const chainId = req.query.chainId
const managedFilters = manageIncomingFilterRequest(req)
try{
// Get deals from DealsController
const isNumber = !isNaN(Number(chainId))
const formattedChainId = chainId ? (isNumber ? Number(chainId) : JSON.parse(chainId as string)) : undefined
console.log("ChainId", formattedChainId)
const deals = await DealsController.getDeals(formattedChainId, managedFilters.genericFilter, managedFilters.metadataFilter, managedFilters.bandwidthFilter, managedFilters.nodeLocationFilter, managedFilters.page, managedFilters.pageSize)
// Send response
res.json(deals)
} catch (e) {
console.log({error: e})
res.status(500).json({error: "Something went wrong"})
}
})
/**
* @route GET /deals/:id/chainId/:chainId
* @description Retrieves a deal by its id.
*/
app.get("/deals/:id/chainId/:chainId", validateParams({
id: ValidatorType.NUMBER,
chainId: ValidatorType.NUMBER
}), validateChain(Object.keys(validChains).map(chain => Number(chain))), async (req, res) => {
const deal = await DealsController.getDealByIdAndChain(Number(req.params.id), Number(req.params.chainId))
res.json(deal)
})
/**
* @route GET /offers
* @description Retrieves offers based on provided filters, page number and page size.
*/
app.get("/offers", validateParams({
chainId: [ValidatorType.NUMBER_OPTIONAL, ValidatorType.NUMBER_ARRAY_OPTIONAL],
minRating: ValidatorType.NUMBER_OPTIONAL
}), validateChain(Object.keys(validChains).map(validateChain => Number(validateChain))), async (req, res) => {
const chainId = req.query.chainId
const minRating = req.query.minRating ? Number(req.query.minRating) : undefined
const managedFilters = manageIncomingFilterRequest(req)
try {
const isNumber = !isNaN(Number(chainId))
const formattedChainId = chainId ? (isNumber ? Number(chainId) : JSON.parse(chainId as string)) : undefined
const offers = await OffersController.getOffers(formattedChainId, managedFilters.genericFilter, managedFilters.metadataFilter, managedFilters.bandwidthFilter, managedFilters.nodeLocationFilter, minRating, managedFilters.page, managedFilters.pageSize)
res.json(offers)
} catch (e) {
console.log(e)
res.status(500).json({error: "Something went wrong"})
}
})
/**
* @route GET /providers
* @description Retrieves providers based on provided filters, page number and page size.
*/
app.get("/providers", validateParams({
chainId: ValidatorType.NUMBER_ARRAY_OPTIONAL,
provider: ValidatorType.STRING_OPTIONAL,
rating: ValidatorType.NUMBER_OPTIONAL,
page: ValidatorType.NUMBER_OPTIONAL,
pageSize: ValidatorType.NUMBER_OPTIONAL
}), validateChain(Object.keys(validChains).map(chain => Number(chain))), async(req, res) => {
try {
const result = []
const page = req.query.page ? Number(req.query.page) : undefined
const pageSize = req.query.pageSize ? Number(req.query.pageSize) : undefined
const chainId = req.query.chainId
const account = req.query.provider
const rating = req.query.rating ? Number(req.query.rating) : undefined
const isNumber = !isNaN(Number(chainId))
const formattedChainId = chainId ? (isNumber ? Number(chainId) : JSON.parse(chainId as string)) : undefined
const providers = await ProvidersController.getProviders({chainId : formattedChainId, page : page, pageSize : pageSize, account : account as string | undefined, minRating : rating})
for (const provider of providers) {
const dealsCount = await ProvidersController.countDeals(provider.account, formattedChainId)
const offersCount = await ProvidersController.countOffers(provider.account, formattedChainId)
const clientCount = await ProvidersController.countClients(provider.account, formattedChainId)
let providerRating = {}
let providerMetadata: { [index: number]: any } | ProvidersMetadata | null = {}
let registryTime: { [index: number]: any } | number = {}
if(chainId) {
providerMetadata = await ProvidersController.getMetadata(provider.account, formattedChainId)
registryTime = await ProvidersController.getProviderStartTime(provider.account, formattedChainId)
providerRating = await RatingController.getAverageRating(provider.account, formattedChainId)
}
else {
const chains = provider.Chains
for (const chain of chains!) {
providerMetadata![chain] = await ProvidersController.getMetadata(provider.account, [Number(chain)])
registryTime[chain] = (await ProvidersController.getProviderStartTime(provider.account, [Number(chain)]))[chain]
providerRating = await RatingController.getAverageRating(provider.account, chains!)
}
}
const providerResult = {
"address": provider.account,
"chains": provider.Chains,
"deals": dealsCount,
"offers": offersCount,
"clients": clientCount,
"metadata": providerMetadata,
"registerTime": registryTime,
"rating": providerRating
}
result.push(providerResult)
}
res.json(result)
} catch (e) {
console.log(e)
res.status(500).json({error: "Something went wrong"})
}
})
/**
* @route GET /providers/countNewDeals
* @description Retrieves the count of new deals for a provider.
*/
app.get("/providers/countNewDeals", validateParams({
provider: ValidatorType.STRING,
chainId: ValidatorType.NUMBER_ARRAY_OPTIONAL,
from: ValidatorType.NUMBER_OPTIONAL,
to: ValidatorType.NUMBER_OPTIONAL
}), validateChain(Object.keys(validChains).map(chain => Number(chain))), async (req, res) => {
const provider = req.query.provider
const chainId = req.query.chainId && Array.isArray(JSON.parse(req.query.chainId as string)) ? JSON.parse(req.query.chainId as string).map((value: number) => parseInt(value.toString())) : undefined
const fromDate = req.query.from ? Number(req.query.from) : undefined
const toDate = req.query.to ? Number(req.query.to) : undefined
try {
const amount = await EventsController.calculateProviderNewDeals(provider!.toString(), chainId, fromDate, toDate)
res.json({
"dealsCount": amount
})
} catch (e) {
console.log(e)
res.send(e)
}
})
/**
* @route GET /providers/totalRevenue
* @description Retrieves the total revenue for a provider.
*/
app.get("/providers/totalRevenue", validateParams({
provider: ValidatorType.STRING,
chainId: [ValidatorType.NUMBER_ARRAY_OPTIONAL, ValidatorType.NUMBER_OPTIONAL],
from: ValidatorType.NUMBER_OPTIONAL,
to: ValidatorType.NUMBER_OPTIONAL
}), async (req, res) => {
try {
const provider = req.query.provider
const chainId = req.query.chainId
const from: number | undefined = req.query.from ? Number(req.query.from) : undefined
const to: number | undefined = req.query.to ? Number(req.query.to) : undefined
const isNumber = !isNaN(Number(chainId))
const formattedChainId: number[] = chainId ? (isNumber ? [Number(chainId)] : JSON.parse(chainId as string)) : Object.keys(validChains).map(chain => Number(chain))
const response: {[index: number]: any} = {}
for (const chain of formattedChainId) {
const queryResult = await EventsController.calculateProviderRevenue(provider!.toString(), Number(chain), from, to)
const dailyRevenue = queryResult.dailyRevenue
const formattedData: { [key: string]: string } = {}
for (const key in dailyRevenue) {
const timestamp = Number(key)
const bigNumber = dailyRevenue[key]
formattedData[timestamp] = bigNumber.toString()
}
response[Number(chain)] = {
dailyRevenue: formattedData,
totalRevenue: queryResult.totalRevenue.toString(),
collectedRevenue: queryResult.collectedRevenue.toString(),
uncollectedRevenue: queryResult.uncollectedRevenue.toString()
}
}
res.json(response)
} catch (e) {
console.log(e)
res.status(500).json({error: e})
}
})
/**
* @route GET /providers/countNewClients
* @description Retrieves the count of new clients for a provider.
*/
app.get("/providers/countNewClients", validateParams({
provider: ValidatorType.STRING,
chainId: [ValidatorType.NUMBER_ARRAY_OPTIONAL, ValidatorType.NUMBER_OPTIONAL],
from: ValidatorType.NUMBER_OPTIONAL,
to: ValidatorType.NUMBER_OPTIONAL
}), async (req, res) => {
const provider = req.query.provider
const chainId = req.query.chainId
const fromTimestamp = req.query.from ? Number(req.query.from) : undefined
const toTimestamp = req.query.to ? Number(req.query.to) : undefined
/*if(!provider || !chainId || !Array.isArray(JSON.parse(req.query.chainId as string))) {
res.status(500).json({error: "No provider or chainId provided"})
return
}*/
const isNumber = !isNaN(Number(chainId))
const formattedChainId: number[] = chainId ? (isNumber ? [Number(chainId)] : JSON.parse(chainId as string)) : Object.keys(validChains).map(chain => Number(chain))
try {
const result = await ProvidersController.getProviderNewClients(provider!.toString(), formattedChainId, fromTimestamp, toTimestamp)
res.json({
"clients": result
})
} catch (e) {
console.log(e)
res.status(500).json({error: e})
}
})
/**
* @route GET /providers/countActiveClients
* @description Retrieves the count of active clients for a provider.
*/
app.get("/providers/countActiveClients", validateParams({
provider: ValidatorType.STRING,
chainId: [ValidatorType.NUMBER_ARRAY_OPTIONAL, ValidatorType.NUMBER_OPTIONAL],
from: ValidatorType.NUMBER_OPTIONAL,
to: ValidatorType.NUMBER_OPTIONAL
}), async (req, res) => {
const provider = req.query.provider
const chainId = req.query.chainId && Array.isArray(JSON.parse(req.query.chainId as string)) ? JSON.parse(req.query.chainId as string).map((value: number) => parseInt(value.toString())) : undefined
const fromTimestamp = req.query.from ? Number(req.query.from) : undefined
const toTimestamp = req.query.to ? Number(req.query.to) : undefined
/*if(!provider) {
res.status(500).json({error: "No provider provided"})
return
}*/
const isNumber = !isNaN(Number(chainId))
const formattedChainId: number[] = chainId ? (isNumber ? [Number(chainId)] : JSON.parse(chainId as string)) : Object.keys(validChains).map(chain => Number(chain))
try {
const result = await ProvidersController.getProviderActiveClients(provider!.toString(), formattedChainId, fromTimestamp, toTimestamp)
res.json(result)
} catch (e) {
console.log(e)
res.status(500).json({error: e})
}
})
/**
* @route GET /providerClient
* @description Retrieves deal details for a specific provider and client.
* @param {object} req - The request object containing query parameters.
* @param {string} req.query.provider - The provider identifier.
* @param {string} req.query.client - The client identifier.
* @param {object} res - The response object.
* @returns {JSON} - The deal details associated with the given provider and client or an error message in case of failure.
*/
app.get("/providerClient", validateParams({
provider: ValidatorType.STRING,
client: ValidatorType.STRING
}), async (req, res) => {
const {provider, client} = req.query
const formattedProvider = provider ? provider.toString() : ""
const formattedClient = client ? client.toString() : ""
const clientDeals: {[index: number]: { deals: Deal[], dealsCount: number }} = {}
try {
// Check if client and provider association exists
await ProviderClient.findOne({
where: {
provider: formattedProvider,
client: formattedClient
},
rejectOnEmpty: true,
})
const chainClient = await ChainClient.findAll({
where: {
client: formattedClient
},
raw: true
})
for (const chain of chainClient) {
const deals = await Deal.findAll({
where: {
chainId: chain.chainId,
client: formattedClient
},
raw: true
})
clientDeals[chain.chainId] = {
deals: deals,
dealsCount: deals.length
}
}
res.json({
provider: provider,
dealDetails: clientDeals
})
} catch (e) {
console.log("Error", e)
res.json(e)
}
})
/**
* @route GET /allProvidersClients
* @description Retrieves all clients and their deals for a specific provider across all chains.
* @param {object} req - The request object containing query parameters.
* @param {string} req.query.provider - The provider identifier.
* @param {object} res - The response object.
* @returns {JSON} - The clients and their deals associated with the given provider or an error message in case of failure.
*/
app.get("/allProvidersClients", validateParams({
provider: ValidatorType.STRING
}), async (req, res) => {
const {provider} = req.query
const formattedProvider = provider ? provider.toString() : ""
const response: {
[index: string]: { //ChainId
[index: string]: { //Client
deals: Deal[],
dealsCount: number
}
}
} = {}
try {
// Check if client and provider association exists
const providerClients = await ProviderClient.findAll({
where: {
provider: formattedProvider
},
raw: true
})
const clients = providerClients.map(providerClient => providerClient.client)
const providerChain = await ChainProvider.findAll({
where: {
provider: formattedProvider
},
raw: true
})
for (const chain of providerChain) {
const deals = await Deal.findAll({
where: {
chainId: chain.chainId,
client: {
[Op.in]: clients
},
provider: formattedProvider
},
attributes: {
exclude: ["offerId"]
},
include: [
{
model: Offer,
as: "Offer",
attributes: ["offerId"],
}
],
nest: true,
raw: true
})
const rawClient = deals.map(deal => deal.client)
const client = [...new Set(rawClient)]
console.log(client, chain.chainId)
const clientsDeals: {[index: string]: {
deals: Deal[],
dealsCount: number
}
} = {}
for (const clientElement of client) {
const clientDeals = deals.filter(deal => deal.client === clientElement)
clientsDeals[clientElement] = {
deals: clientDeals,
dealsCount: clientDeals.length
}
}
response[chain.chainId] = clientsDeals
}
res.json({
provider: provider,
dealDetails: response
})
} catch (e) {
console.log("Error", e)
res.json(e)
}
})
/**
* @route GET /upsertOffer
* @description This endpoint is used to update or insert an offer in the database.
*/
app.get("/upsertOffer", validateParams({
chainId: ValidatorType.NUMBER,
offerId: ValidatorType.NUMBER
}), async (req, res) => {
const {chainId, offerId} = req.query
const chainIdFormatted = chainId ? chainId.toString() : ""
const offerIdFormatted = offerId ? offerId.toString() : ""
const chains = Object.keys(validChains)
if(!chains.includes(chainIdFormatted)) {
res.status(401).json({error: "ChainId does not belong to any valid chain"})
return
}
try {
const chain = validChains[chainIdFormatted as unknown as keyof typeof validChains]
const transports = httpNetworks ? httpNetworks[chain.name].map(transport => http(transport)) : undefined
const sdk = new Sdk({chain: chain, transport: transports})
const marketplace = new Marketplace(sdk)
const offer = await marketplace.getOfferById({
marketplaceId: Number(process.env.MARKETPLACE_ID),
offerId: Number(offerIdFormatted)
})
const formattedOffer = OffersController.formatOffer(offer)
const providerData = await marketplace.getProvider({
marketplaceId: Number(process.env.MARKETPLACE_ID),
provider: formattedOffer.provider
})
await OffersController.upsertOffer(formattedOffer, Number(chainIdFormatted), providerData.metadata, providerData.publicKey)
res.status(200).json({message: "Offer Updated"})
} catch (e) {
console.log(e)
res.status(500).json({error: e})
}
})
/**
* @route GET /upsertDeal
* @description This endpoint is used to update or insert a deal in the database.
*/
app.get("/upsertDeal", validateParams({
chainId: ValidatorType.NUMBER,
dealId: ValidatorType.NUMBER
}), async (req, res) => {
const {chainId, dealId} = req.query
const chainIdFormatted = chainId ? chainId.toString() : ""
const dealIdFormatted = dealId ? Number(dealId) : 0
const chains = Object.keys(validChains)
if(!chains.includes(chainIdFormatted)) {
res.status(401).json({error: "ChainId does not belong to any valid chain"})
return
}
try {
const chain = validChains[chainIdFormatted as unknown as keyof typeof validChains]
const transports = httpNetworks ? httpNetworks[chain.name].map(transport => http(transport)) : undefined
const sdk = new Sdk({chain: chain, transport: transports})
const marketplace = new Marketplace(sdk)
const deal = await marketplace.getDealById({
marketplaceId: Number(process.env.MARKETPLACE_ID),
dealId: Number(dealIdFormatted)
})
await DealsController.upsertDeal(DealsController.formatDeal(deal), Number(chainIdFormatted))
res.status(200).json({message: "Deal updated"})
} catch (e) {
console.log(e)
res.status(500).json({error: e})
}
})
/**
* @route GET /upsertResource
* @description This endpoint is used to update or insert a resource in the database.
*/
app.get("/upsertResource", validateParams({
address: ValidatorType.STRING,
resourceId: ValidatorType.NUMBER,
chainId: ValidatorType.NUMBER
}), async (req, res) => {
const {address, resourceId, chainId} = req.query
const addressFormatted = address ? address.toString() : ""
const resourceIdFormatted = resourceId ? Number(resourceId) : 0
const chainIdFormatted = chainId ? chainId.toString() : ""
const chains = Object.keys(validChains)
if(!chains.includes(chainIdFormatted)) {
res.status(401).json({error: "ChainId does not belong to any valid chain"})
return
}
try {
const chain = validChains[chainIdFormatted as unknown as keyof typeof validChains]
const transports = httpNetworks ? httpNetworks[chain.name].map(transport => http(transport)) : undefined
const sdk = new Sdk({chain: chain, transport: transports})
const resources = new Resources(sdk)
const resource = await resources.getResource({
id: resourceIdFormatted,
address: addressFormatted
})
await ResourcesController.upsertResource(ResourcesController.formatResource(resource), Number(chainIdFormatted))
res.status(200).json({message: "Resource Updated"})
} catch (e) {
console.log(e)
res.status(500).json({error: e})
}
})
/**
* @route GET /upsertProvider
* @description This endpoint is used to update or insert a provider in the database.
*/
app.get("/upsertProvider", validateParams({
provider: ValidatorType.STRING,
chainId: ValidatorType.NUMBER
}), async (req, res) => {
const {provider, chainId} = req.query
const providerFormatted = provider ? provider.toString() : ""
const chainIdFormatted = chainId ? chainId.toString() : ""
const chains = Object.keys(validChains)
if(!chains.includes(chainIdFormatted)) {
res.status(401).json({error: "ChainId does not belong to any valid chain"})
return
}
try {
const chain = validChains[chainIdFormatted as unknown as keyof typeof validChains]
const transports = httpNetworks ? httpNetworks[chain.name].map(transport => http(transport)) : undefined
const sdk = new Sdk({chain: chain, transport: transports})
const marketplace = new Marketplace(sdk)
const providerData = await marketplace.getProvider({
marketplaceId: Number(process.env.MARKETPLACE_ID),
provider: providerFormatted
})
await ProvidersController.upsertProvider(providerFormatted, Number(chainIdFormatted), undefined, providerData.metadata, providerData.publicKey)
res.status(200).json({message: "Provider Updated"})
} catch (e) {
console.log(e)
res.status(500).json({error: e})
}
})
/**
* @route GET /account/events
* @description Retrieves event logs for a specific account and chain ID.
* @param {string} account - The account identifier from the query parameters.
* @param {number | undefined} chainId - The chain ID from the query parameters, parsed to a number if provided.
* @returns {JSON} - The events associated with the given account and chainId or an error message in case of failure.
*/
app.get("/account/events", validateParams({
account: ValidatorType.STRING,
chainId: ValidatorType.NUMBER,
page: ValidatorType.NUMBER_OPTIONAL,
pageSize: ValidatorType.NUMBER_OPTIONAL
}), async (req, res) => {
const account = req.query.account
const chainId = req.query.chainId ? Number(req.query.chainId) : undefined
const page = req.query.page ? Number(req.query.page) : undefined
const pageSize = req.query.pageSize ? Number(req.query.pageSize) : undefined
/*if(!account || !chainId) {
res.status(500).json({error: "No account provided or chainId provided"})
return
}*/
try {
const events = await EventsController.getAccountEvents(account!.toString(), chainId!, page, pageSize)
res.json(events)
} catch (e) {
console.log(e)
res.status(500).json({error: e})
}
})
app.post("/rateProvider", validateParams({
provider: ValidatorType.STRING,
chainId: ValidatorType.NUMBER
}), async (req, res) => {
const {provider, chainId} = req.body
const chainIdFormatted = chainId ? chainId.toString() : ""
const chains = Object.keys(validChains)
if(!chains.includes(chainIdFormatted)) {
res.status(401).json({error: "ChainId does not belong to any valid chain"})
return
}
try {
const chain = validChains[chainIdFormatted as unknown as keyof typeof validChains]
const transports = httpNetworks ? httpNetworks[chain.name].map(transport => http(transport)) : undefined
const sdk = new Sdk({chain: chain, transport: transports})
const rating = new RatingSystem(sdk)
/*const providerRating = await rating.getAverageRating({
marketplaceId: process.env.MARKETPLACE_ID!,
provider: provider
})*/
const providerRating = await rating.getProviderRating({
marketplaceId: process.env.MARKETPLACE_ID!,
provider: provider
})
await RatingController.rateProvider(provider, chainId, Number(providerRating.sum), Number(providerRating.count))
res.status(200).json({message: "Rating added"})
} catch (e) {
console.log(e)
res.status(500).json({error: "Something went wrong"})
}
})
app.get("/provider/rating", validateParams({
provider: ValidatorType.STRING,
chainIds: ValidatorType.NUMBER_ARRAY_OPTIONAL
}), async (req, res) => {
const {provider, chainIds} = req.query
const chainId = chainIds && Array.isArray(JSON.parse(chainIds as string)) ? JSON.parse(chainIds as string).map((value: number) => parseInt(value.toString())) : undefined
try {
const rating = await RatingController.getAverageRating(provider!.toString(), chainId)
res.status(200).json(rating)
} catch (e) {
console.log(e)
res.status(500).json({error: "Something went wrong"})
}
})
// Create relations between tables
createRelationsBetweenTables()
.then(() => {
console.log("Tables created")
app.listen(5000, () => {
console.log("Server running on port 5000")
})
})