From 53ed710ad6b0775b8e91424b49113e2ebf2078a6 Mon Sep 17 00:00:00 2001 From: ReflectiveChimp <55021052+ReflectiveChimp@users.noreply.github.com> Date: Tue, 11 Jun 2024 20:03:47 +0100 Subject: [PATCH] Fix/timeline stitching 2 (#13) * use separate sdk instance per chain --- src/routes/v1/investor.ts | 13 ++++------ src/routes/v1/vault.ts | 52 +++++++++++++++------------------------ src/routes/v1/vaults.ts | 21 +++++++--------- src/utils/sdk.ts | 6 ++++- 4 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/routes/v1/investor.ts b/src/routes/v1/investor.ts index 482ae8c..35f586b 100644 --- a/src/routes/v1/investor.ts +++ b/src/routes/v1/investor.ts @@ -3,7 +3,7 @@ import S from 'fluent-json-schema'; import { allChainIds } from '../../config/chains'; import { addressSchema } from '../../schema/address'; import { GraphQueryError } from '../../utils/error'; -import { sdk } from '../../utils/sdk'; +import { getSdkForChain } from '../../utils/sdk'; import { interpretAsDecimal } from '../../utils/decimal'; export default async function ( @@ -49,13 +49,10 @@ export default async function ( const getTimeline = async (investor_address: string) => { const res = await Promise.all( allChainIds.map(chain => - sdk - .InvestorTimeline( - { - investor_address, - }, - { chainName: chain } - ) + getSdkForChain(chain) + .InvestorTimeline({ + investor_address, + }) .then(res => ({ chain, ...res })) .catch((e: unknown) => { // we have nothing to leak here diff --git a/src/routes/v1/vault.ts b/src/routes/v1/vault.ts index 11c03f8..9e7eea8 100644 --- a/src/routes/v1/vault.ts +++ b/src/routes/v1/vault.ts @@ -3,7 +3,7 @@ import S from 'fluent-json-schema'; import { ChainId } from '../../config/chains'; import { addressSchema } from '../../schema/address'; import { GraphQueryError } from '../../utils/error'; -import { sdk } from '../../utils/sdk'; +import { getSdkForChain } from '../../utils/sdk'; import { getPeriodSeconds, Period, periodSchema } from '../../schema/period'; import { chainSchema } from '../../schema/chain'; import { bigintSchema } from '../../schema/bigint'; @@ -173,13 +173,10 @@ export default async function ( } const getVaultPrice = async (chain: ChainId, vault_address: string) => { - const res = await sdk - .VaultPrice( - { - vault_address, - }, - { chainName: chain } - ) + const res = await getSdkForChain(chain) + .VaultPrice({ + vault_address, + }) .catch((e: unknown) => { // we have nothing to leak here throw new GraphQueryError(e); @@ -198,13 +195,10 @@ const getVaultPrice = async (chain: ChainId, vault_address: string) => { }; const getVaultHarvests = async (chain: ChainId, vault_address: string) => { - const res = await sdk - .VaultHarvests( - { - vault_address, - }, - { chainName: chain } - ) + const res = await getSdkForChain(chain) + .VaultHarvests({ + vault_address, + }) .catch((e: unknown) => { // we have nothing to leak here throw new GraphQueryError(e); @@ -263,15 +257,12 @@ const getVaultHistoricPrices = async ( period: Period, since: string ) => { - const res = await sdk - .VaultHistoricPrices( - { - vault_address, - period: getPeriodSeconds(period), - since, - }, - { chainName: chain } - ) + const res = await getSdkForChain(chain) + .VaultHistoricPrices({ + vault_address, + period: getPeriodSeconds(period), + since, + }) .catch((e: unknown) => { // we have nothing to leak here throw new GraphQueryError(e); @@ -301,14 +292,11 @@ const getVaultHistoricPricesRange = async ( vault_address: string, period: Period ) => { - const res = await sdk - .VaultHistoricPricesRange( - { - vault_address, - period: getPeriodSeconds(period), - }, - { chainName: chain } - ) + const res = await getSdkForChain(chain) + .VaultHistoricPricesRange({ + vault_address, + period: getPeriodSeconds(period), + }) .catch((e: unknown) => { // we have nothing to leak here throw new GraphQueryError(e); diff --git a/src/routes/v1/vaults.ts b/src/routes/v1/vaults.ts index 2ca3570..37b01e7 100644 --- a/src/routes/v1/vaults.ts +++ b/src/routes/v1/vaults.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyPluginOptions, FastifySchema } from 'fastify'; import S from 'fluent-json-schema'; import { ChainId } from '../../config/chains'; import { GraphQueryError } from '../../utils/error'; -import { sdk } from '../../utils/sdk'; +import { getSdkForChain } from '../../utils/sdk'; import { chainSchema } from '../../schema/chain'; import { getPeriodSeconds, Period, periodSchema } from '../../schema/period'; import { VaultsQuery } from '../../../.graphclient'; @@ -104,8 +104,8 @@ const getVaults = async (chain: ChainId, period: Period) => { const now = new Date(); const periodSeconds = getPeriodSeconds(period); const since = BigInt(Math.floor(now.getTime() / 1000) - periodSeconds); - const rawVaults = await sdk - .Vaults({ since: since.toString() }, { chainName: chain }) + const rawVaults = await getSdkForChain(chain) + .Vaults({ since: since.toString() }) .then(res => [...res.clms, ...res.beta_clms]) .catch((e: unknown) => { throw new GraphQueryError(e); @@ -158,16 +158,13 @@ const getVaultsHarvests = async ( since: number, vaults: Address[] ): Promise => { - const options = { chainName: chain }; + const sdk = getSdkForChain(chain); const queryPromise = vaults.length - ? sdk.VaultsHarvestsFiltered( - { - since: since.toString(), - vaults: vaults.map(v => v.toLowerCase()), - }, - options - ) - : sdk.VaultsHarvests({ since: since.toString() }, options); + ? sdk.VaultsHarvestsFiltered({ + since: since.toString(), + vaults: vaults.map(v => v.toLowerCase()), + }) + : sdk.VaultsHarvests({ since: since.toString() }); const rawVaults = await queryPromise .then(res => [...res.clms, ...res.beta_clms]) diff --git a/src/utils/sdk.ts b/src/utils/sdk.ts index 953a285..b2d01a7 100644 --- a/src/utils/sdk.ts +++ b/src/utils/sdk.ts @@ -1,3 +1,7 @@ import { getBuiltGraphSDK } from '../../.graphclient'; +import { createCachedFactoryByChainId } from './factory'; +import { ChainId } from '../config/chains'; -export const sdk = getBuiltGraphSDK(); +export const getSdkForChain = createCachedFactoryByChainId((chainId: ChainId) => + getBuiltGraphSDK({ chainName: chainId }) +);