Skip to content

Commit

Permalink
Fix/timeline stitching 2 (#13)
Browse files Browse the repository at this point in the history
* use separate sdk instance per chain
  • Loading branch information
ReflectiveChimp authored Jun 11, 2024
1 parent a2120ed commit 53ed710
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 53 deletions.
13 changes: 5 additions & 8 deletions src/routes/v1/investor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
52 changes: 20 additions & 32 deletions src/routes/v1/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 9 additions & 12 deletions src/routes/v1/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -158,16 +158,13 @@ const getVaultsHarvests = async (
since: number,
vaults: Address[]
): Promise<VaultsHarvests> => {
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])
Expand Down
6 changes: 5 additions & 1 deletion src/utils/sdk.ts
Original file line number Diff line number Diff line change
@@ -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 })
);

0 comments on commit 53ed710

Please sign in to comment.