From baa29cf340814cac0295d868d6686ae81fd13784 Mon Sep 17 00:00:00 2001 From: TheJuze Date: Tue, 19 Mar 2024 13:00:21 +0300 Subject: [PATCH] add frontage service and getAggregatedMetrics request --- package-lock.json | 4 +- package.json | 2 +- src/Orion/index.ts | 28 ++++-- src/Unit/index.ts | 13 ++- src/config/envs.json | 93 +++++++++++++++++++ src/config/schemas/pureEnvSchema.ts | 3 + src/services/Frontage/index.ts | 26 ++++++ .../schemas/aggregated-metrics-schema.ts | 29 ++++++ src/services/Frontage/schemas/error-schema.ts | 9 ++ src/services/Frontage/schemas/index.ts | 2 + src/types.ts | 3 + 11 files changed, 198 insertions(+), 14 deletions(-) create mode 100644 src/services/Frontage/index.ts create mode 100644 src/services/Frontage/schemas/aggregated-metrics-schema.ts create mode 100644 src/services/Frontage/schemas/error-schema.ts create mode 100644 src/services/Frontage/schemas/index.ts diff --git a/package-lock.json b/package-lock.json index 98ae352..7b80503 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.72", + "version": "0.20.74-rc100", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.20.72", + "version": "0.20.74-rc100", "hasInstallScript": true, "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index 8d2b156..1e385ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.73", + "version": "0.20.74-rc100", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Orion/index.ts b/src/Orion/index.ts index 7245c00..7a266d5 100644 --- a/src/Orion/index.ts +++ b/src/Orion/index.ts @@ -3,7 +3,14 @@ import { chains, envs } from '../config/index.js'; import type { networkCodes } from '../constants/index.js'; import Unit from '../Unit/index.js'; import { ReferralSystem } from '../services/ReferralSystem/index.js'; -import type { SupportedChainId, DeepPartial, VerboseUnitConfig, KnownEnv, EnvConfig, AggregatedAssets } from '../types.js'; +import type { + SupportedChainId, + DeepPartial, + VerboseUnitConfig, + KnownEnv, + EnvConfig, + AggregatedAssets +} from '../types.js'; import { isValidChainId } from '../utils/index.js'; import { simpleFetch } from 'simple-typed-fetch'; import Bridge from './bridge/index.js'; @@ -60,6 +67,9 @@ export default class Orion { }, indexer: { api: networkConfig.api + networkConfig.services.indexer?.http, + }, + frontage: { + api: networkConfig.api + networkConfig.services.frontage.http, } }, }; @@ -118,7 +128,7 @@ export default class Orion { if (!unit) { throw new Error( `Invalid network code: ${networkCodeOrChainId}. ` + - `Available network codes: ${this.unitsArray.map((u) => u.networkCode).join(', ')}`); + `Available network codes: ${this.unitsArray.map((u) => u.networkCode).join(', ')}`); } return unit; } @@ -156,7 +166,7 @@ export default class Orion { const networks = chainIds.map((chainId) => chains[chainId]?.label).join(', '); console.error( `Asset found in Aggregator, but not in BlockchainService (base): ${baseAsset} (${pair}).` + - ` Networks: ${networks}` + ` Networks: ${networks}` ); } else { tradableAggregatedAssets[baseAsset] = aggregatedBaseAsset; @@ -166,7 +176,7 @@ export default class Orion { const networks = chainIds.map((chainId) => chains[chainId]?.label).join(', '); console.error( `Asset found in Aggregator, but not in BlockchainService (quote): ${quoteAsset} (${pair}).` + - ` Networks: ${networks}` + ` Networks: ${networks}` ); } else { tradableAggregatedAssets[quoteAsset] = aggregatedQuoteAsset; @@ -178,11 +188,11 @@ export default class Orion { async getPairs(...params: Parameters) { const result: Partial< - Record< - string, - SupportedChainId[] - > - > = {}; + Record< + string, + SupportedChainId[] + > + > = {}; await Promise.all(this.unitsArray.map(async (unit) => { const pairs = await simpleFetch(unit.aggregator.getPairsList)(...params); diff --git a/src/Unit/index.ts b/src/Unit/index.ts index 85d7052..79deda1 100644 --- a/src/Unit/index.ts +++ b/src/Unit/index.ts @@ -2,6 +2,8 @@ import { JsonRpcProvider } from 'ethers'; import { Aggregator } from '../services/Aggregator'; import { BlockchainService } from '../services/BlockchainService'; import { PriceFeed } from '../services/PriceFeed'; +import { IndexerService } from '../services/Indexer'; +import { FrontageService } from '../services/Frontage'; import type { KnownEnv, SupportedChainId, @@ -10,8 +12,7 @@ import type { import Exchange from './Exchange/index.js'; import { chains, envs } from '../config'; import type { networkCodes } from '../constants/index.js'; -import { IndexerService } from '../services/Indexer'; -import Pmm from "./Pmm"; +import Pmm from './Pmm'; type KnownConfig = { env: KnownEnv @@ -29,6 +30,8 @@ export default class Unit { public readonly indexer: IndexerService | undefined; + public readonly frontage: FrontageService | undefined; + public readonly aggregator: Aggregator; public readonly pmm: Pmm; @@ -88,6 +91,9 @@ export default class Unit { indexer: { api: networkConfig.api + networkConfig.services.indexer?.http, }, + frontage: { + api: networkConfig.api + networkConfig.services.frontage?.http, + }, }, }; } else { @@ -115,6 +121,9 @@ export default class Unit { intNetwork ) : undefined; + this.frontage = new FrontageService( + this.config.services.frontage.api, + ); this.aggregator = new Aggregator( this.config.services.aggregator.http, this.config.services.aggregator.ws, diff --git a/src/config/envs.json b/src/config/envs.json index 6628b38..55a72bf 100644 --- a/src/config/envs.json +++ b/src/config/envs.json @@ -17,6 +17,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } }, "liquidityMigratorAddress": "0x23a1820a47BcD022E29f6058a5FD224242F50D1A" @@ -36,6 +39,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -54,6 +60,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -72,6 +81,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -90,6 +102,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -108,6 +123,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -126,6 +144,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -144,6 +165,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -162,6 +186,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } } @@ -185,6 +212,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } }, "liquidityMigratorAddress": "0x01b10dds12478C88A5E18e2707E729906bC25CfF6" @@ -204,6 +234,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -222,6 +255,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -240,6 +276,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -258,6 +297,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -276,6 +318,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } } @@ -299,6 +344,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -317,6 +365,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -335,6 +386,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -353,6 +407,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -371,6 +428,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -389,6 +449,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -407,6 +470,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -425,6 +491,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -443,6 +512,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } } @@ -466,6 +538,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -484,6 +559,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } } @@ -507,6 +585,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } }, "liquidityMigratorAddress": "0x23a1820a47BcD022E29f6058a5FD224242F50D1A" @@ -526,6 +607,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -544,6 +628,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -562,6 +649,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } }, @@ -580,6 +670,9 @@ }, "indexer": { "http": "/orion-indexer/" + }, + "frontage": { + "http": "/frontage/" } } } diff --git a/src/config/schemas/pureEnvSchema.ts b/src/config/schemas/pureEnvSchema.ts index 9c799c4..31f503c 100644 --- a/src/config/schemas/pureEnvSchema.ts +++ b/src/config/schemas/pureEnvSchema.ts @@ -17,6 +17,9 @@ export const pureEnvNetworksSchema = z.object({ indexer: z.object({ http: z.string(), }).optional(), + frontage: z.object({ + http: z.string(), + }), }), rpc: z.string().optional(), liquidityMigratorAddress: z.string().optional(), diff --git a/src/services/Frontage/index.ts b/src/services/Frontage/index.ts new file mode 100644 index 0000000..9542efd --- /dev/null +++ b/src/services/Frontage/index.ts @@ -0,0 +1,26 @@ +import { fetchWithValidation } from 'simple-typed-fetch'; +import { aggregatedMetricsSchema } from './schemas'; + +export class FrontageService { + private readonly apiUrl: string; + + get api() { + return this.apiUrl; + } + + constructor(apiUrl: string) { + this.apiUrl = apiUrl; + + this.getAggregatedMetrics = this.getAggregatedMetrics.bind(this); + } + + readonly getAggregatedMetrics = () => { + const url = new URL(`${this.apiUrl}/api/v1/metrics/aggregated`); + return fetchWithValidation( + url.toString(), + aggregatedMetricsSchema, + ); + }; +} + +export * as schemas from './schemas/index.js'; diff --git a/src/services/Frontage/schemas/aggregated-metrics-schema.ts b/src/services/Frontage/schemas/aggregated-metrics-schema.ts new file mode 100644 index 0000000..f68a1ad --- /dev/null +++ b/src/services/Frontage/schemas/aggregated-metrics-schema.ts @@ -0,0 +1,29 @@ +import { z } from 'zod'; +import { networkCodes } from '../../../constants'; + +const volumeInfoSchema = z.object({ + volume24: z.number(), + volume7d: z.number(), + volumeAllTime: z.number(), + networks: z.array(z.enum(networkCodes)), +}) + +const supplyMetricsSchema = z.object({ + circulatingSupply: z.number(), + totalSupply: z.number(), + maxSupply: z.number(), +}) + +const governanceMetricsSchema = z.object({ + totalLumiaLocked: z.number(), + totalVeLumia: z.number(), + totalVeLumiaInVoting: z.number(), + weeklyLumiaReward: z.number(), + networks: z.array(z.enum(networkCodes)), +}) + +export const aggregatedMetricsSchema = z.object({ + volumeInfo: volumeInfoSchema, + supplyMetrics: supplyMetricsSchema, + governanceMetrics: governanceMetricsSchema +}); diff --git a/src/services/Frontage/schemas/error-schema.ts b/src/services/Frontage/schemas/error-schema.ts new file mode 100644 index 0000000..1163d5f --- /dev/null +++ b/src/services/Frontage/schemas/error-schema.ts @@ -0,0 +1,9 @@ +import { z } from 'zod'; + +export const errorSchema = z.object({ + error: z.object({ + code: z.number(), + reason: z.string(), + }), + timestamp: z.string(), +}); diff --git a/src/services/Frontage/schemas/index.ts b/src/services/Frontage/schemas/index.ts new file mode 100644 index 0000000..6edc708 --- /dev/null +++ b/src/services/Frontage/schemas/index.ts @@ -0,0 +1,2 @@ +export * from './aggregated-metrics-schema'; +export * from './error-schema'; diff --git a/src/types.ts b/src/types.ts index 0f1bbb3..c4a935f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -264,6 +264,9 @@ export type VerboseUnitConfig = { // http://10.23.5.11:3003/, // https://price-feed:3003/ } + frontage: { + api: string + } indexer?: { api: string // For example: