Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop in replacement for pool data via multicall #90

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions debug/multicall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { OnChainPoolDataViaMulticallEnricher } from '../src/data/enrichers/onChainPoolDataViaMulticallEnricher';
import { createPublicClient, http } from 'viem';
// rome-ignore lint/correctness/noUnusedVariables: <this is a test file>
import { mainnet, polygonZkEvm } from 'viem/chains';

const chain = polygonZkEvm;
const rpc = 'https://rpc.ankr.com/polygon_zkevm';

// rome-ignore lint/correctness/noUnusedVariables: <this is a test file>
const client = createPublicClient({
chain,
transport: http(),
});

const poolsQuery = `{
pools(first: 10, orderBy: id, orderDirection: desc, where: { totalShares_gt: 0, poolType_contains: "ComposableStable" }) {
id
address
poolType
wrappedIndex
tokens {
address
decimals
index
}
}
}`;

const pools = await fetch(
'https://api.studio.thegraph.com/query/24660/balancer-polygon-zk-v2/version/latest',
// 'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-v2',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: poolsQuery }),
},
)
.then((res) => res.json())
.then((res) => res.data);

const onChainEnricher = new OnChainPoolDataViaMulticallEnricher(chain.id, rpc);
const data = await onChainEnricher.fetchAdditionalPoolData(pools);
const enrichered = onChainEnricher.enrichPoolsWithData(pools.pools, data);

console.log(enrichered, data.length);
2 changes: 1 addition & 1 deletion src/data/enrichers/onChainPoolDataEnricher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../../utils';
import { HumanAmount, SwapOptions } from '../../types';

interface OnChainPoolData {
export interface OnChainPoolData {
id: string;
balances: readonly bigint[];
totalSupply: bigint;
Expand Down
125 changes: 125 additions & 0 deletions src/data/enrichers/onChainPoolDataViaMulticallEnricher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { createPublicClient, formatUnits, http, PublicClient } from 'viem';
import {
GetPoolsResponse,
PoolDataEnricher,
RawPool,
RawPoolTokenWithRate,
RawWeightedPoolToken,
} from '../types';

import { CHAINS } from '../../utils';
import { HumanAmount } from '../../types';
import { fetchAdditionalPoolData } from '../onChainPoolDataViaMulticall';
import { OnChainPoolData } from './onChainPoolDataEnricher';

export class OnChainPoolDataViaMulticallEnricher implements PoolDataEnricher {
private readonly client: PublicClient;

constructor(
private readonly chainId: number,
private readonly rpcUrl: string,
) {
this.client = createPublicClient({
transport: http(this.rpcUrl),
chain: CHAINS[this.chainId],
});
}

public async fetchAdditionalPoolData(
data: GetPoolsResponse,
): Promise<OnChainPoolData[]> {
return fetchAdditionalPoolData(data.pools, this.client);
}

public enrichPoolsWithData(
pools: RawPool[],
additionalPoolData: OnChainPoolData[],
): RawPool[] {
return pools.map((pool) => {
const data = additionalPoolData.find((item) => item.id === pool.id);

return {
...pool,
tokens: pool.tokens
.sort((a, b) => a.index - b.index)
.map((token) => {
return {
...token,
balance:
data?.balances && data.balances.length > 0
? (formatUnits(
data.balances[token.index],
token.decimals,
) as HumanAmount)
: token.balance,
priceRate: this.getPoolTokenRate({
pool,
token: token as RawPoolTokenWithRate,
data,
index: token.index,
}),
weight: data?.weights
? formatUnits(data.weights[token.index], 18)
: (token as RawWeightedPoolToken).weight,
};
}),
totalShares: data?.totalSupply
? (formatUnits(data.totalSupply, 18) as HumanAmount)
: pool.totalShares,
amp: data?.amp
? formatUnits(data.amp, 3)
: 'amp' in pool
? pool.amp
: undefined,
swapFee: data?.swapFee
? (formatUnits(data.swapFee, 18) as HumanAmount)
: pool.swapFee,
tokenRates: data?.tokenRates
? data.tokenRates.map(
(tokenRate) =>
formatUnits(tokenRate, 18) as HumanAmount,
)
: undefined,
lowerTarget: data?.linearTargets
? data.linearTargets[0]
: 'lowerTarget' in pool
? pool.lowerTarget
: undefined,
upperTarget: data?.linearTargets
? data.linearTargets[0]
: 'upperTarget' in pool
? pool.upperTarget
: undefined,
inRecoveryMode: data?.inRecoveryMode || false,
isPaused: data?.isPaused || false,
queryFailed: data?.queryFailed,
};
});
}

private getPoolTokenRate({
pool,
token,
data,
index,
}: {
pool: RawPool;
token: RawPoolTokenWithRate;
data?: OnChainPoolData;
index: number;
}): string {
if (
data?.wrappedTokenRate &&
'wrappedIndex' in pool &&
pool.wrappedIndex === index
) {
return formatUnits(data.wrappedTokenRate, 18);
}

if (data?.scalingFactors) {
return formatUnits(data.scalingFactors[index], 18);
}

return token.priceRate;
}
}
Loading
Loading