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

Sor update 4.1.1 beta.12 #480

Merged
merged 5 commits into from
Jul 18, 2023
Merged
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
2 changes: 1 addition & 1 deletion balancer-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"typescript": "^4.0.2"
},
"dependencies": {
"@balancer-labs/sor": "4.1.1-beta.9",
"@balancer-labs/sor": "4.1.1-beta.12",
"@ethersproject/abi": "^5.4.0",
"@ethersproject/abstract-signer": "^5.4.0",
"@ethersproject/address": "^5.4.0",
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use zipObject here

Copy link
Contributor

@lgahdl lgahdl Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, forget about the zipObject, you don't even need the zipObject, Object.fromEntries and/or Object.values, you can remove duplicates using lodash uniqBy function:
const abis: any = _.uniqBy([...(FXPool__factory.abi as readonly JsonFragment[])], "name")

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;
}
8 changes: 4 additions & 4 deletions balancer-js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,10 @@
"@babel/helper-validator-identifier" "^7.19.1"
to-fast-properties "^2.0.0"

"@balancer-labs/[email protected].9":
version "4.1.1-beta.9"
resolved "https://registry.yarnpkg.com/@balancer-labs/sor/-/sor-4.1.1-beta.9.tgz#68ea312f57d43595a0156642b56e7713f87cf4bb"
integrity sha512-jwqEkjOgc9qTnuQGne/ZnG+ynx4ca4qEIgRClJVH2tCCf+pXL7jVPWv/v3I+7dJs2aCyet4uIsXwU/vi2jK+/Q==
"@balancer-labs/[email protected].12":
version "4.1.1-beta.12"
resolved "https://registry.yarnpkg.com/@balancer-labs/sor/-/sor-4.1.1-beta.12.tgz#35a77b14c4cbe99a9a4cd1b538c9615b002d2c30"
integrity sha512-H0k6Zv3KH79wncPXs0iamRwv2MmRJBazk0WIAmnXE1opZAPOSxTPgf1YJh4/hAZlFbHuI2LXOZAVY7ueSCRewQ==
dependencies:
isomorphic-fetch "^2.2.1"

Expand Down