Skip to content

Commit

Permalink
onchain data via multicall3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Aug 29, 2023
1 parent abc7997 commit c2066ec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
15 changes: 8 additions & 7 deletions balancer-js/examples/data/onchain-multicall3.ts
Original file line number Diff line number Diff line change
@@ -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<number, string> = {
[Network.MAINNET]:
Expand Down Expand Up @@ -37,11 +37,11 @@ const pools = new PoolsSubgraphRepository({
skip: 0,
orderBy: 'totalLiquidity',
orderDirection: 'desc',
// where: {
// poolType: {
// eq: "MetaStable"
// },
// },
where: {
poolType: {
eq: "GyroE"
},
},
},
attrs: {},
},
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions balancer-js/src/lib/utils/multiCaller3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class Multicaller3 {
results[callIndex] = [];
}
} else {
console.error('Failed request in multicall', this.paths[callIndex]);
results[callIndex] = [];
}
}
Expand Down
30 changes: 16 additions & 14 deletions balancer-js/src/modules/sor/pool-data/onChainData3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ interface OnchainData {
tokenRates?: [string, string];
}

type GenericPool = SubgraphPoolBase;
// Omit<SubgraphPoolBase | Pool, 'tokens'> & {
// tokens: (SubgraphToken | PoolToken)[];
// };

const defaultCalls = (
id: string,
address: string,
Expand All @@ -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');
Expand Down Expand Up @@ -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':
Expand All @@ -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]
Expand Down Expand Up @@ -180,6 +181,7 @@ export const fetchOnChainPoolData = async (
id: string;
address: string;
poolType: string;
poolTypeVersion?: number;
}[],
vaultAddress: string,
provider: Provider
Expand All @@ -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
Expand All @@ -204,14 +206,14 @@ export const fetchOnChainPoolData = async (
};

export async function getOnChainBalances(
subgraphPoolsOriginal: GenericPool[],
subgraphPoolsOriginal: SubgraphPoolBase[],
_multiAddress: string,
vaultAddress: string,
provider: Provider
): Promise<GenericPool[]> {
): Promise<SubgraphPoolBase[]> {
if (subgraphPoolsOriginal.length === 0) return subgraphPoolsOriginal;

const poolsWithOnchainData: GenericPool[] = [];
const poolsWithOnchainData: SubgraphPoolBase[] = [];

const onchainData = (await fetchOnChainPoolData(
subgraphPoolsOriginal,
Expand Down

0 comments on commit c2066ec

Please sign in to comment.