Skip to content

Commit

Permalink
Add protocolPercentFee as FX pool swapFee
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoguerios committed Jul 12, 2023
1 parent e91e3d1 commit 7d8eabc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
85 changes: 85 additions & 0 deletions balancer-js/src/modules/sor/pool-data/multicall/fx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { formatFixed } from '@ethersproject/bignumber';
import { Provider } from '@ethersproject/providers';
import { SubgraphPoolBase } from '@balancer-labs/sor';
import { Multicaller } from '@/lib/utils/multiCaller';
import { FXPool__factory, Multicall__factory } from '@/contracts';
import { Pool, PoolType } from '@/types';
import { JsonFragment } from '@ethersproject/abi';
import { BalancerPool } from '../onChainData';
import { Logger } from '@/lib/utils/logger';

type SwapFees = Record<
string,
{
swapFee: string;
}
>;

/**
* Update pool swapFees using mulitcall
* @param pools
* @param multicallAddr
* @param provider
*/
export async function decorateFx<GenericPool extends BalancerPool>(
pools: GenericPool[],
multicallAddr: string,
provider: Provider
): Promise<void> {
const fxPools = pools.filter((p) => {
return p.poolType === 'FX';
});
// Use multicall to get swapFees for all FX pools
const fxSwapFees = await getFxSwapFee(fxPools, multicallAddr, provider);
fxPools.forEach((pool) => {
if (fxSwapFees[pool.id]) {
pool.swapFee = formatFixed(fxSwapFees[pool.id].swapFee, 18);
} else {
console.warn(`FX missing protocolPercentFee: `, pool.id);
}
});
}

async function getFxSwapFee<
GenericPool extends Pick<
SubgraphPoolBase | Pool,
'poolType' | 'id' | 'address'
>
>(
fxPools: GenericPool[],
multiAddress: string,
provider: Provider
): Promise<SwapFees> {
if (fxPools.length === 0) return {} as SwapFees;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const abis: any = Object.values(
// Remove duplicate entries using their names
Object.fromEntries(
[...(FXPool__factory.abi as readonly JsonFragment[])].map((row) => [
row.name,
row,
])
)
);
const multicall = Multicall__factory.connect(multiAddress, provider);
const multiPool = new Multicaller(multicall, abis);
fxPools.forEach((pool) => {
if (pool.poolType !== PoolType.FX) {
const logger = Logger.getInstance();
logger.warn(
`Incorrectly calling protocolPercentFee on pool: ${pool.poolType} ${pool.id}`
);
return;
}
multiPool.call(`${pool.id}.swapFee`, pool.address, 'protocolPercentFee');
});

let swapFees = {} as SwapFees;
try {
swapFees = (await multiPool.execute()) as SwapFees;
} catch (err) {
console.error(`Issue with FX multicall execution.`);
}
return swapFees;
}
4 changes: 3 additions & 1 deletion balancer-js/src/modules/sor/pool-data/multicall/gyroEv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Pool, PoolType } from '@/types';
import { GyroEV2__factory } from '@/contracts';
import { JsonFragment } from '@ethersproject/abi';
import { BalancerPool } from '../onChainData';
import { Logger } from '@/lib/utils/logger';

type TokenRates = Record<
string,
Expand Down Expand Up @@ -74,7 +75,8 @@ async function getGyroTokenRates<
const multiPool = new Multicaller(multicall, abis);
gyroPools.forEach((pool) => {
if (!(pool.poolType === PoolType.GyroE && pool.poolTypeVersion === 2)) {
console.warn(
const logger = Logger.getInstance();
logger.warn(
`Incorrectly calling tokenRates on pool: ${pool.poolType} ${pool.id}`
);
return;
Expand Down
2 changes: 2 additions & 0 deletions balancer-js/src/modules/sor/pool-data/onChainData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Pool, PoolToken, PoolType } from '@/types';
import { decorateGyroEv2 } from './multicall/gyroEv2';
import { getPoolsFromDataQuery } from './poolDataQueries';
import { Logger } from '@/lib/utils/logger';
import { decorateFx } from './multicall/fx';

export type Tokens = (SubgraphToken | PoolToken)[];

Expand Down Expand Up @@ -34,5 +35,6 @@ export async function getOnChainPools<GenericPool extends BalancerPool>(
);
// GyroEV2 requires tokenRates onchain update that dataQueries does not provide
await decorateGyroEv2(onChainPools, multicallAddr, provider);
await decorateFx(onChainPools, multicallAddr, provider);
return onChainPools;
}

0 comments on commit 7d8eabc

Please sign in to comment.