Skip to content

Commit

Permalink
sync FX quote tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Nov 20, 2024
1 parent 760c57c commit ca93d8d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/actions/pool/v2/add-pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BalancerPoolFragment } from '../../../subgraphs/balancer-subgraph/gener
import { subgraphToPrismaCreate } from '../../../pool/subgraph-mapper';
import { upsertBptBalancesV2 } from '../../user/upsert-bpt-balances-v2';
import _ from 'lodash';
import { syncPoolTypeOnchainData } from './sync-pool-type-onchain-data';

export const addPools = async (subgraphService: V2SubgraphClient, chain: Chain): Promise<string[]> => {
const { block } = await subgraphService.legacyService.getMetadata();
Expand All @@ -28,6 +29,13 @@ export const addPools = async (subgraphService: V2SubgraphClient, chain: Chain):
const created = await createPoolRecord(subgraphPool, chain, block.number, allNestedTypePools);
if (created) {
createdPools.push(subgraphPool.id);
// When new FX pool is added, we need to get the quote token
if (subgraphPool.poolType === 'FX') {
await syncPoolTypeOnchainData(
[{ id: subgraphPool.id, address: subgraphPool.address, type: subgraphPool.poolType }],
chain,
);
}
}
}

Expand Down
45 changes: 45 additions & 0 deletions modules/actions/pool/v2/sync-pool-type-onchain-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Abi } from 'abitype';
import FX from '../../../pool/abi/FxPool.json';
import { getViemClient, ViemClient } from '../../../sources/viem-client';
import { Chain, PrismaPoolType } from '@prisma/client';
import { update } from '../v3/type-data/update';

Check failure on line 5 in modules/actions/pool/v2/sync-pool-type-onchain-data.ts

View workflow job for this annotation

GitHub Actions / Build

Cannot find module '../v3/type-data/update' or its corresponding type declarations.

export const syncPoolTypeOnchainData = async (
pools: { id: string; address: string; type: PrismaPoolType }[],
chain: Chain,
) => {
const viemClient = getViemClient(chain);

// Get FX pools
const fxPools = pools.filter((pool) => pool.type === 'FX');
const quoteTokens = await fetchFxQuoteTokens(fxPools, viemClient);
await update(quoteTokens);

return true;
};

export const fetchFxQuoteTokens = async (pools: { id: string; address: string }[], viemClient: ViemClient) => {
// Fetch the tokens from the subgraph
const contracts = pools.map(({ address }) => {
return {
address: address as `0x${string}`,
abi: FX as Abi,
functionName: 'derivatives',
args: [1],
};
});

const results = await viemClient.multicall({ contracts, allowFailure: true });

return results
.map((call, index) => {
// If the call failed, return null
if (call.status === 'failure') return null;

return {
id: pools[index].id,
typeData: { quoteToken: (call.result as string).toLowerCase() },
};
})
.filter((quoteToken): quoteToken is { id: string; typeData: { quoteToken: string } } => quoteToken !== null);
};
9 changes: 9 additions & 0 deletions modules/controllers/fx-pools-controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import config from '../../config';
import { prisma } from '../../prisma/prisma-client';
import { syncPoolTypeOnchainData } from '../actions/pool/v2/sync-pool-type-onchain-data';
import { syncLatestFXPrices } from '../token/latest-fx-price';
import { Chain } from '@prisma/client';

Expand All @@ -11,5 +13,12 @@ export function FXPoolsController() {

return syncLatestFXPrices(balancer, chain);
},
async syncQuoteTokens(chain: Chain) {
const pools = await prisma.prismaPool.findMany({
where: { chain, type: 'FX' },
});

return syncPoolTypeOnchainData(pools, chain);
},
};
}
4 changes: 4 additions & 0 deletions tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ async function run(job: string = process.argv[2], chainId: string = process.argv
} else if (job === 'sync-hook-data') {
return PoolController().syncHookData(chain);
}
// Maintenance
else if (job === 'sync-fx-quote-tokens') {
return FXPoolsController().syncQuoteTokens(chain);
}
return Promise.reject(new Error(`Unknown job: ${job}`));
}

Expand Down

0 comments on commit ca93d8d

Please sign in to comment.