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

Chores #541

Merged
merged 4 commits into from
Oct 10, 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
1 change: 1 addition & 0 deletions balancer-js/src/lib/constants/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export const BALANCER_NETWORK_CONFIG: Record<Network, BalancerNetworkConfig> = {
},
},
averageBlockTime: 4,
multicallBatchSize: 128,
pools: {},
poolsToIgnore: [],
sorConnectingTokens: [
Expand Down
3 changes: 2 additions & 1 deletion balancer-js/src/modules/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export class Data implements BalancerDataRepositories {
multicall: networkConfig.addresses.contracts.multicall,
vault: networkConfig.addresses.contracts.vault,
},
networkConfig.poolsToIgnore
networkConfig.poolsToIgnore,
networkConfig.multicallBatchSize
);

this.poolShares = new PoolSharesRepository(
Expand Down
28 changes: 18 additions & 10 deletions balancer-js/src/modules/data/pool/onchain-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SubgraphPoolBase } from '@/.';
import { Provider } from '@ethersproject/providers';
import { formatFixed } from '@ethersproject/bignumber';
import { SubgraphToken } from '@balancer-labs/sor';
import { PoolToken, Pool } from '@/types';

const abi = [
'function getSwapFeePercentage() view returns (uint256)',
Expand Down Expand Up @@ -43,6 +44,11 @@ const getSwapFeeFn = (poolType: string) => {
}
};

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

interface OnchainData {
poolTokens: [string[], string[]];
totalShares: string;
Expand Down Expand Up @@ -133,7 +139,7 @@ const poolTypeCalls = (poolType: string, poolTypeVersion = 1) => {
}
};

const merge = (pool: SubgraphPoolBase, result: OnchainData) => ({
const merge = <T extends GenericPool>(pool: T, result: OnchainData) => ({
...pool,
tokens: result.poolTokens
? pool.tokens.map((token) => {
Expand Down Expand Up @@ -188,7 +194,8 @@ export const fetchOnChainPoolData = async (
poolTypeVersion?: number;
}[],
vaultAddress: string,
provider: Provider
provider: Provider,
batchSize = 1024
): Promise<{ [id: string]: OnchainData }> => {
if (pools.length === 0) {
return {};
Expand All @@ -201,28 +208,29 @@ export const fetchOnChainPoolData = async (
poolTypeCalls(poolType, poolTypeVersion)(id, address, multicaller);
});

// ZkEVM needs a smaller batch size
const results = (await multicaller.execute({}, 128)) as {
const results = (await multicaller.execute({}, batchSize)) as {
[id: string]: OnchainData;
};

return results;
};

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

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

const onchainData = (await fetchOnChainPoolData(
subgraphPoolsOriginal,
vaultAddress,
provider
provider,
batchSize
)) as { [id: string]: OnchainData };

subgraphPoolsOriginal.forEach((pool) => {
Expand Down
11 changes: 7 additions & 4 deletions balancer-js/src/modules/data/pool/subgraphOnChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Cacheable, Findable, Searchable } from '../types';
import { Provider } from '@ethersproject/providers';
import { PoolAttribute, PoolsRepositoryFetchOptions } from './types';
import { Pool } from '@/types';
import { getOnChainBalances } from '../../../modules/sor/pool-data/onChainData';
import { getOnChainBalances } from '../../../modules/sor/pool-data/onChainData3';
import { PoolsSubgraphRepository } from './subgraph';
import { isSameAddress } from '@/lib/utils';
import { Logger } from '@/lib/utils/logger';
Expand Down Expand Up @@ -34,7 +34,8 @@ export class PoolsSubgraphOnChainRepository
constructor(
private poolsSubgraph: PoolsSubgraphRepository,
options: PoolsSubgraphOnChainRepositoryOptions,
private readonly poolsToIgnore: string[] | undefined
private readonly poolsToIgnore: string[] | undefined,
private batchSize?: number
) {
this.provider = options.provider;
this.multicall = options.multicall;
Expand Down Expand Up @@ -70,7 +71,8 @@ export class PoolsSubgraphOnChainRepository
filteredPools,
this.multicall,
this.vault,
this.provider
this.provider,
this.batchSize
);

logger.timeEnd(`fetching onchain ${filteredPools.length} pools`);
Expand All @@ -89,7 +91,8 @@ export class PoolsSubgraphOnChainRepository
filteredPools,
this.multicall,
this.vault,
this.provider
this.provider,
this.batchSize
);

logger.timeEnd(`fetching onchain ${filteredPools.length} pools`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,29 +301,29 @@ describe('Migrations', function () {
});
});
});
});

context('polygon', () => {
const { approveRelayer, impersonate, runPool2Pool } = migrations(
Network.POLYGON
);
context('polygon', () => {
const { approveRelayer, impersonate, runPool2Pool } = migrations(
Network.POLYGON
);

beforeEach(() => approveRelayer());
beforeEach(() => approveRelayer());

context('ComposableStable to ComposableStable', () => {
before(() => impersonate('0xe80a6a7b4fdadf0aa59f3f669a8d394d1d4da86b'));
context('ComposableStable to ComposableStable', () => {
before(() => impersonate('0xe80a6a7b4fdadf0aa59f3f669a8d394d1d4da86b'));

it('should build a migration using exit / join', async () => {
const { balanceAfter, minBptOut } = await runPool2Pool(
polygonComposableStable,
polygonComposableStable
);
it('should build a migration using exit / join', async () => {
const { balanceAfter, minBptOut } = await runPool2Pool(
polygonComposableStable,
polygonComposableStable
);

// NOTICE: We don't know the exact amount of BPT that will be minted,
// because swaps from the linear pool are not deterministic due to external rates
expect(BigInt(balanceAfter)).to.satisfy(
(v: bigint) => v > BigInt(minBptOut)
);
});
// NOTICE: We don't know the exact amount of BPT that will be minted,
// because swaps from the linear pool are not deterministic due to external rates
expect(BigInt(balanceAfter)).to.satisfy(
(v: bigint) => v > BigInt(minBptOut)
);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export class SubgraphPoolDataService implements PoolDataService {
mapped,
this.network.addresses.contracts.multicall,
this.network.addresses.contracts.vault,
this.provider
this.provider,
this.network.multicallBatchSize
);

logger.timeEnd(`fetching on-chain balances for ${mapped.length} pools`);
Expand Down
Loading
Loading