From c2066ec83074455a3c32c772c58a16b4f670d4ff Mon Sep 17 00:00:00 2001 From: bronco Date: Tue, 29 Aug 2023 12:25:50 +0200 Subject: [PATCH] onchain data via multicall3 fixes --- .../examples/data/onchain-multicall3.ts | 15 +++++----- balancer-js/src/lib/utils/multiCaller3.ts | 1 + .../src/modules/sor/pool-data/onChainData3.ts | 30 ++++++++++--------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/balancer-js/examples/data/onchain-multicall3.ts b/balancer-js/examples/data/onchain-multicall3.ts index 24ffabc0a..32459fb4b 100644 --- a/balancer-js/examples/data/onchain-multicall3.ts +++ b/balancer-js/examples/data/onchain-multicall3.ts @@ -1,11 +1,11 @@ import { PoolsSubgraphRepository } from '@/modules/data/pool/subgraph' import { getOnChainBalances as getOnChainBalances3 } from '@/modules/sor/pool-data/onChainData3' -import { SubgraphPoolBase } from '@/.' +import { SubgraphPoolBase, Network } from '@/.' import { getOnChainBalances } from '@/modules/sor/pool-data/onChainData' import { JsonRpcProvider } from '@ethersproject/providers' import _ from 'lodash' -const network = Network.MAINNET; +const network = Network.POLYGON; const urls: Record = { [Network.MAINNET]: @@ -37,11 +37,11 @@ const pools = new PoolsSubgraphRepository({ skip: 0, orderBy: 'totalLiquidity', orderDirection: 'desc', - // where: { - // poolType: { - // eq: "MetaStable" - // }, - // }, + where: { + poolType: { + eq: "GyroE" + }, + }, }, attrs: {}, }, @@ -85,6 +85,7 @@ async function main() { const subgraph = await pools.fetch() as SubgraphPoolBase[]; const onchain3 = await getOnChainBalances3(subgraph, '', '0xBA12222222228d8Ba445958a75a0704d566BF2C8', provider); console.log(onchain3.length) + console.log(JSON.stringify(onchain3, null, 2)); // const onchain = await getOnChainBalances(subgraph, '0xeefba1e63905ef1d7acba5a8513c70307c1ce441', '0xBA12222222228d8Ba445958a75a0704d566BF2C8', provider); // console.log(onchain.length) // for(const i in subgraph) { diff --git a/balancer-js/src/lib/utils/multiCaller3.ts b/balancer-js/src/lib/utils/multiCaller3.ts index 9588b4467..db4c87f68 100644 --- a/balancer-js/src/lib/utils/multiCaller3.ts +++ b/balancer-js/src/lib/utils/multiCaller3.ts @@ -93,6 +93,7 @@ export class Multicaller3 { results[callIndex] = []; } } else { + console.error('Failed request in multicall', this.paths[callIndex]); results[callIndex] = []; } } diff --git a/balancer-js/src/modules/sor/pool-data/onChainData3.ts b/balancer-js/src/modules/sor/pool-data/onChainData3.ts index 1232273cf..83f8fc408 100644 --- a/balancer-js/src/modules/sor/pool-data/onChainData3.ts +++ b/balancer-js/src/modules/sor/pool-data/onChainData3.ts @@ -58,11 +58,6 @@ interface OnchainData { tokenRates?: [string, string]; } -type GenericPool = SubgraphPoolBase; -// Omit & { -// tokens: (SubgraphToken | PoolToken)[]; -// }; - const defaultCalls = ( id: string, address: string, @@ -73,6 +68,7 @@ const defaultCalls = ( multicaller.call(`${id}.poolTokens`, vaultAddress, 'getPoolTokens', [id]); multicaller.call(`${id}.totalShares`, address, getTotalSupplyFn(poolType)); multicaller.call(`${id}.swapFee`, address, getSwapFeeFn(poolType)); + // Following where added to the pools query contract, however legacy onchain data didn't have them. // multicaller.call(`${id}.isPaused`, address, 'getPausedState'); // multicaller.call(`${id}.inRecoveryMode`, address, 'inRecoveryMode'); // multicaller.call(`${id}.rate`, address, 'getRate'); @@ -108,7 +104,8 @@ const gyroECalls = (id: string, address: string, multicaller: Multicaller3) => { multicaller.call(`${id}.tokenRates`, address, 'getTokenRates'); }; -const poolTypeCalls = (poolType: string) => { +const poolTypeCalls = (poolType: string, poolTypeVersion = 1) => { + const do_nothing = () => ({}); switch (poolType) { case 'Weighted': case 'LiquidityBootstrapping': @@ -120,17 +117,21 @@ const poolTypeCalls = (poolType: string) => { case 'ComposableStable': return stableCalls; case 'GyroE': - return gyroECalls; + if (poolTypeVersion > 1) { + return gyroECalls; + } else { + return do_nothing; + } default: if (poolType.includes('Linear')) { return linearCalls; } else { - return () => ({}); // do nothing + return do_nothing; } } }; -const merge = (pool: GenericPool, result: OnchainData) => ({ +const merge = (pool: SubgraphPoolBase, result: OnchainData) => ({ ...pool, tokens: pool.tokens.map((token) => { const idx = result.poolTokens[0] @@ -180,6 +181,7 @@ export const fetchOnChainPoolData = async ( id: string; address: string; poolType: string; + poolTypeVersion?: number; }[], vaultAddress: string, provider: Provider @@ -190,9 +192,9 @@ export const fetchOnChainPoolData = async ( const multicaller = new Multicaller3(abi, provider); - pools.forEach(({ id, address, poolType }) => { + pools.forEach(({ id, address, poolType, poolTypeVersion }) => { defaultCalls(id, address, vaultAddress, poolType, multicaller); - poolTypeCalls(poolType)(id, address, multicaller); + poolTypeCalls(poolType, poolTypeVersion)(id, address, multicaller); }); // ZkEVM needs a smaller batch size @@ -204,14 +206,14 @@ export const fetchOnChainPoolData = async ( }; export async function getOnChainBalances( - subgraphPoolsOriginal: GenericPool[], + subgraphPoolsOriginal: SubgraphPoolBase[], _multiAddress: string, vaultAddress: string, provider: Provider -): Promise { +): Promise { if (subgraphPoolsOriginal.length === 0) return subgraphPoolsOriginal; - const poolsWithOnchainData: GenericPool[] = []; + const poolsWithOnchainData: SubgraphPoolBase[] = []; const onchainData = (await fetchOnChainPoolData( subgraphPoolsOriginal,