From 9945c30a89adbcc5fad96bd007fd79b60fc1e5c8 Mon Sep 17 00:00:00 2001 From: johngrantuk Date: Wed, 24 Jul 2024 11:39:57 +0100 Subject: [PATCH] chore: Apply formatting to testData. --- testData/src/generatePoolTestData.ts | 111 +++++++------ testData/src/getAdds.ts | 184 ++++++++++----------- testData/src/getPool.ts | 54 +++---- testData/src/getRemoves.ts | 231 ++++++++++++++------------- testData/src/getSwaps.ts | 146 +++++++++-------- testData/src/stablePool.ts | 212 ++++++++++++------------ testData/src/types.ts | 36 ++--- testData/src/weightedPool.ts | 214 ++++++++++++------------- 8 files changed, 602 insertions(+), 586 deletions(-) diff --git a/testData/src/generatePoolTestData.ts b/testData/src/generatePoolTestData.ts index d0f857d..ba758b2 100644 --- a/testData/src/generatePoolTestData.ts +++ b/testData/src/generatePoolTestData.ts @@ -1,57 +1,68 @@ -import type { TestInput, TestOutput } from "./types"; -import { getSwaps } from "./getSwaps"; -import { getPool } from "./getPool"; -import { getAddLiquiditys } from "./getAdds"; -import { getRemoveLiquiditys } from "./getRemoves"; +import type { TestInput, TestOutput } from './types'; +import { getSwaps } from './getSwaps'; +import { getPool } from './getPool'; +import { getAddLiquiditys } from './getAdds'; +import { getRemoveLiquiditys } from './getRemoves'; export async function generatePoolTestData( - input: TestInput, - overwrite = false, + input: TestInput, + overwrite = false, ) { - const path = `./testData/${input.chainId}-${input.blockNumber}-${input.testName}.json`; - if (!overwrite) { - const file = Bun.file(path); - if (await file.exists()) { - console.log("File already exists and overwrite set to false.", path); - return; - } - } - console.log("Generating test data with input:\n", input); - const testData = await fetchTestData(input); - console.log("Saving test data to: ", path); - await Bun.write(path, JSON.stringify(testData, null, 4)); - console.log("Complete"); + const path = `./testData/${input.chainId}-${input.blockNumber}-${input.testName}.json`; + if (!overwrite) { + const file = Bun.file(path); + if (await file.exists()) { + console.log( + 'File already exists and overwrite set to false.', + path, + ); + return; + } + } + console.log('Generating test data with input:\n', input); + const testData = await fetchTestData(input); + console.log('Saving test data to: ', path); + await Bun.write(path, JSON.stringify(testData, null, 4)); + console.log('Complete'); } async function fetchTestData(input: TestInput): Promise { - const { rpcUrl, chainId, poolAddress, poolType, blockNumber, adds, swaps, removes } = - input; - const pool = await getPool( - rpcUrl, - chainId, - blockNumber, - poolType, - poolAddress, - ); - const swapResults = await getSwaps(swaps, rpcUrl, chainId, poolAddress); - const addResults = await getAddLiquiditys( - adds, - rpcUrl, - chainId, - poolAddress, - poolType, - ); - const removeResults = await getRemoveLiquiditys( - removes, - rpcUrl, - chainId, - poolAddress, - poolType - ) - return { - swaps: swapResults, - adds: addResults, - removes: removeResults, - pool, - }; + const { + rpcUrl, + chainId, + poolAddress, + poolType, + blockNumber, + adds, + swaps, + removes, + } = input; + const pool = await getPool( + rpcUrl, + chainId, + blockNumber, + poolType, + poolAddress, + ); + const swapResults = await getSwaps(swaps, rpcUrl, chainId, poolAddress); + const addResults = await getAddLiquiditys( + adds, + rpcUrl, + chainId, + poolAddress, + poolType, + ); + const removeResults = await getRemoveLiquiditys( + removes, + rpcUrl, + chainId, + poolAddress, + poolType, + ); + return { + swaps: swapResults, + adds: addResults, + removes: removeResults, + pool, + }; } diff --git a/testData/src/getAdds.ts b/testData/src/getAdds.ts index fcb6838..84168d3 100644 --- a/testData/src/getAdds.ts +++ b/testData/src/getAdds.ts @@ -1,116 +1,116 @@ import { - AddLiquidityKind, - type AddLiquidityInput, - AddLiquidity, - OnChainProvider, - type AddLiquidityQueryOutput, -} from "@balancer/sdk"; -import type { Address } from "viem"; + AddLiquidityKind, + type AddLiquidityInput, + AddLiquidity, + OnChainProvider, + type AddLiquidityQueryOutput, +} from '@balancer/sdk'; +import type { Address } from 'viem'; type AddTestInputProportional = { - kind: AddLiquidityKind.Proportional; - inputAmountsRaw: bigint[]; - tokens: Address[]; - decimals: number[]; + kind: AddLiquidityKind.Proportional; + inputAmountsRaw: bigint[]; + tokens: Address[]; + decimals: number[]; }; type AddTestInputSingleToken = { - kind: AddLiquidityKind.SingleToken; - bptOutRaw: bigint; - tokenIn: Address; - decimals: number; + kind: AddLiquidityKind.SingleToken; + bptOutRaw: bigint; + tokenIn: Address; + decimals: number; }; export type AddTestInput = AddTestInputProportional | AddTestInputSingleToken; export type AddLiquidityResult = { - kind: AddLiquidityKind; - inputAmountsRaw: string[]; - bptOutRaw: string; + kind: AddLiquidityKind; + inputAmountsRaw: string[]; + bptOutRaw: string; }; function getInput( - addTestInput: AddTestInput, - chainId: number, - rpcUrl: string, + addTestInput: AddTestInput, + chainId: number, + rpcUrl: string, ): AddLiquidityInput { - const { kind } = addTestInput; - if (kind === AddLiquidityKind.Proportional) { - const amounts = addTestInput.inputAmountsRaw.map((a, i) => ({ - rawAmount: a, - decimals: addTestInput.decimals[i], - address: addTestInput.tokens[i], - })); - const addLiquidityInput: AddLiquidityInput = { - amountsIn: amounts, - chainId, - rpcUrl, - kind: AddLiquidityKind.Unbalanced, - }; - return addLiquidityInput; - // biome-ignore lint/style/noUselessElse: - } else if (kind === AddLiquidityKind.SingleToken) { - const bptAmount = { - rawAmount: addTestInput.bptOutRaw, - decimals: addTestInput.decimals, - address: addTestInput.tokenIn, - }; - const addLiquidityInput: AddLiquidityInput = { - bptOut: bptAmount, - tokenIn: addTestInput.tokenIn, - chainId, - rpcUrl, - kind: AddLiquidityKind.SingleToken, - }; - return addLiquidityInput; - // biome-ignore lint/style/noUselessElse: - } else throw new Error("No support for Custom AddLiquidity kinds"); + const { kind } = addTestInput; + if (kind === AddLiquidityKind.Proportional) { + const amounts = addTestInput.inputAmountsRaw.map((a, i) => ({ + rawAmount: a, + decimals: addTestInput.decimals[i], + address: addTestInput.tokens[i], + })); + const addLiquidityInput: AddLiquidityInput = { + amountsIn: amounts, + chainId, + rpcUrl, + kind: AddLiquidityKind.Unbalanced, + }; + return addLiquidityInput; + // biome-ignore lint/style/noUselessElse: + } else if (kind === AddLiquidityKind.SingleToken) { + const bptAmount = { + rawAmount: addTestInput.bptOutRaw, + decimals: addTestInput.decimals, + address: addTestInput.tokenIn, + }; + const addLiquidityInput: AddLiquidityInput = { + bptOut: bptAmount, + tokenIn: addTestInput.tokenIn, + chainId, + rpcUrl, + kind: AddLiquidityKind.SingleToken, + }; + return addLiquidityInput; + // biome-ignore lint/style/noUselessElse: + } else throw new Error('No support for Custom AddLiquidity kinds'); } async function queryAddLiquidity( - rpcUrl: string, - chainId: number, - poolAddress: Address, - poolType: string, - addTestInput: AddTestInput, + rpcUrl: string, + chainId: number, + poolAddress: Address, + poolType: string, + addTestInput: AddTestInput, ): Promise { - const addLiquidityInput = getInput(addTestInput, chainId, rpcUrl); - // Onchain provider is used to fetch pool state - const onchainProvider = new OnChainProvider(rpcUrl, chainId); - const poolState = await onchainProvider.pools.fetchPoolState( - poolAddress, - poolType, - ); - // Simulate addLiquidity to get the amount of BPT out - const addLiquidity = new AddLiquidity(); - return await addLiquidity.query(addLiquidityInput, poolState); + const addLiquidityInput = getInput(addTestInput, chainId, rpcUrl); + // Onchain provider is used to fetch pool state + const onchainProvider = new OnChainProvider(rpcUrl, chainId); + const poolState = await onchainProvider.pools.fetchPoolState( + poolAddress, + poolType, + ); + // Simulate addLiquidity to get the amount of BPT out + const addLiquidity = new AddLiquidity(); + return await addLiquidity.query(addLiquidityInput, poolState); } export async function getAddLiquiditys( - addTestInputs: AddTestInput[], - rpcUrl: string, - chainId: number, - poolAddress: Address, - poolType: string, + addTestInputs: AddTestInput[], + rpcUrl: string, + chainId: number, + poolAddress: Address, + poolType: string, ): Promise { - if (!addTestInputs) return undefined; - const results: AddLiquidityResult[] = []; - console.log("Querying adds..."); - for (const addTestInput of addTestInputs) { - // TODO - put this in a multicall? - const result = await queryAddLiquidity( - rpcUrl, - chainId, - poolAddress, - poolType, - addTestInput, - ); - results.push({ - kind: addTestInput.kind, - inputAmountsRaw: result.amountsIn.map((a) => a.amount.toString()), - bptOutRaw: result.bptOut.amount.toString(), - }); - } - console.log("Done"); - return results; + if (!addTestInputs) return undefined; + const results: AddLiquidityResult[] = []; + console.log('Querying adds...'); + for (const addTestInput of addTestInputs) { + // TODO - put this in a multicall? + const result = await queryAddLiquidity( + rpcUrl, + chainId, + poolAddress, + poolType, + addTestInput, + ); + results.push({ + kind: addTestInput.kind, + inputAmountsRaw: result.amountsIn.map((a) => a.amount.toString()), + bptOutRaw: result.bptOut.amount.toString(), + }); + } + console.log('Done'); + return results; } diff --git a/testData/src/getPool.ts b/testData/src/getPool.ts index e65ee28..a5f45e0 100644 --- a/testData/src/getPool.ts +++ b/testData/src/getPool.ts @@ -1,33 +1,33 @@ -import type { Address } from "viem"; -import { WeightedPool } from "./weightedPool"; -import { StablePool } from "./stablePool"; -import type { PoolBase } from "./types"; +import type { Address } from 'viem'; +import { WeightedPool } from './weightedPool'; +import { StablePool } from './stablePool'; +import type { PoolBase } from './types'; export async function getPool( - rpcUrl: string, - chainId: number, - blockNumber: number, - poolType: string, - poolAddress: Address, + rpcUrl: string, + chainId: number, + blockNumber: number, + poolType: string, + poolAddress: Address, ): Promise { - // Find onchain data fetching via pool type - const poolData = { - Weighted: new WeightedPool(rpcUrl, chainId), - Stable: new StablePool(rpcUrl, chainId), - }; - if (!poolData[poolType]) throw new Error("getPool: Unsupported pool type"); + // Find onchain data fetching via pool type + const poolData = { + Weighted: new WeightedPool(rpcUrl, chainId), + Stable: new StablePool(rpcUrl, chainId), + }; + if (!poolData[poolType]) throw new Error('getPool: Unsupported pool type'); - console.log("Fetching pool data..."); - const immutable = await poolData[poolType].fetchImmutableData(poolAddress); - const mutable = await poolData[poolType].fetchMutableData(poolAddress); - console.log("Done"); + console.log('Fetching pool data...'); + const immutable = await poolData[poolType].fetchImmutableData(poolAddress); + const mutable = await poolData[poolType].fetchMutableData(poolAddress); + console.log('Done'); - return { - chainId, - blockNumber, - poolType, - poolAddress, - ...immutable, - ...mutable, - }; + return { + chainId, + blockNumber, + poolType, + poolAddress, + ...immutable, + ...mutable, + }; } diff --git a/testData/src/getRemoves.ts b/testData/src/getRemoves.ts index ac7527f..cf6bf75 100644 --- a/testData/src/getRemoves.ts +++ b/testData/src/getRemoves.ts @@ -1,141 +1,144 @@ import { - RemoveLiquidityKind, - RemoveLiquidity, - OnChainProvider, - type RemoveLiquidityQueryOutput, - type RemoveLiquidityInput, - type RemoveLiquidityProportionalInput, - type RemoveLiquiditySingleTokenExactInInput, - type RemoveLiquiditySingleTokenExactOutInput, -} from "@balancer/sdk"; -import type { Address } from "viem"; + RemoveLiquidityKind, + RemoveLiquidity, + OnChainProvider, + type RemoveLiquidityQueryOutput, + type RemoveLiquidityInput, + type RemoveLiquidityProportionalInput, + type RemoveLiquiditySingleTokenExactInInput, + type RemoveLiquiditySingleTokenExactOutInput, +} from '@balancer/sdk'; +import type { Address } from 'viem'; type RemoveTestInputProportional = { - bpt: Address; - kind: RemoveLiquidityKind.Proportional; - bptInRaw: bigint; + bpt: Address; + kind: RemoveLiquidityKind.Proportional; + bptInRaw: bigint; }; type RemoveTestInputSingleTokenExactIn = { - bpt: Address; - token: Address; - kind: RemoveLiquidityKind.SingleTokenExactIn; - bptInRaw: bigint; + bpt: Address; + token: Address; + kind: RemoveLiquidityKind.SingleTokenExactIn; + bptInRaw: bigint; }; type RemoveTestInputSingleTokenExactOut = { - token: Address; - kind: RemoveLiquidityKind.SingleTokenExactOut; - amountOutRaw: bigint; - decimals: number + token: Address; + kind: RemoveLiquidityKind.SingleTokenExactOut; + amountOutRaw: bigint; + decimals: number; }; -export type RemoveTestInput = RemoveTestInputProportional | RemoveTestInputSingleTokenExactIn | RemoveTestInputSingleTokenExactOut; +export type RemoveTestInput = + | RemoveTestInputProportional + | RemoveTestInputSingleTokenExactIn + | RemoveTestInputSingleTokenExactOut; export type RemoveLiquidityResult = { - kind: RemoveLiquidityKind; - amountsOutRaw: string[]; - bptInRaw: string; + kind: RemoveLiquidityKind; + amountsOutRaw: string[]; + bptInRaw: string; }; function getInput( - removeTestInput: RemoveTestInput, - chainId: number, - rpcUrl: string, + removeTestInput: RemoveTestInput, + chainId: number, + rpcUrl: string, ): RemoveLiquidityInput { - const { kind } = removeTestInput; + const { kind } = removeTestInput; - if (kind === RemoveLiquidityKind.Proportional) { - const bptIn = { - rawAmount: removeTestInput.bptInRaw, - decimals: 18, - address: removeTestInput.bpt, - }; - const removeLiquidityInput: RemoveLiquidityProportionalInput = { - chainId, - rpcUrl, - bptIn, - kind: RemoveLiquidityKind.Proportional, - }; - return removeLiquidityInput; - // biome-ignore lint/style/noUselessElse: - } else if (kind === RemoveLiquidityKind.SingleTokenExactIn) { - const bptIn = { - rawAmount: removeTestInput.bptInRaw, - decimals: 18, - address: removeTestInput.bpt, - }; - const removeLiquidityInput: RemoveLiquiditySingleTokenExactInInput = { - chainId, - rpcUrl, - tokenOut: removeTestInput.token, - kind: RemoveLiquidityKind.SingleTokenExactIn, - bptIn, - }; - return removeLiquidityInput; - // biome-ignore lint/style/noUselessElse: - } else if (kind === RemoveLiquidityKind.SingleTokenExactOut) { - const amountOut = { - rawAmount: removeTestInput.amountOutRaw, - decimals: removeTestInput.decimals, - address: removeTestInput.token, - }; - const removeLiquidityInput: RemoveLiquiditySingleTokenExactOutInput = { - chainId, - rpcUrl, - kind: RemoveLiquidityKind.SingleTokenExactOut, - amountOut, - }; - return removeLiquidityInput; - } - // biome-ignore lint/style/noUselessElse: - else throw new Error("No support for Custom AddLiquidity kinds"); + if (kind === RemoveLiquidityKind.Proportional) { + const bptIn = { + rawAmount: removeTestInput.bptInRaw, + decimals: 18, + address: removeTestInput.bpt, + }; + const removeLiquidityInput: RemoveLiquidityProportionalInput = { + chainId, + rpcUrl, + bptIn, + kind: RemoveLiquidityKind.Proportional, + }; + return removeLiquidityInput; + // biome-ignore lint/style/noUselessElse: + } else if (kind === RemoveLiquidityKind.SingleTokenExactIn) { + const bptIn = { + rawAmount: removeTestInput.bptInRaw, + decimals: 18, + address: removeTestInput.bpt, + }; + const removeLiquidityInput: RemoveLiquiditySingleTokenExactInInput = { + chainId, + rpcUrl, + tokenOut: removeTestInput.token, + kind: RemoveLiquidityKind.SingleTokenExactIn, + bptIn, + }; + return removeLiquidityInput; + // biome-ignore lint/style/noUselessElse: + } else if (kind === RemoveLiquidityKind.SingleTokenExactOut) { + const amountOut = { + rawAmount: removeTestInput.amountOutRaw, + decimals: removeTestInput.decimals, + address: removeTestInput.token, + }; + const removeLiquidityInput: RemoveLiquiditySingleTokenExactOutInput = { + chainId, + rpcUrl, + kind: RemoveLiquidityKind.SingleTokenExactOut, + amountOut, + }; + return removeLiquidityInput; + } + // biome-ignore lint/style/noUselessElse: + else throw new Error('No support for Custom AddLiquidity kinds'); } async function queryRemoveLiquidity( - rpcUrl: string, - chainId: number, - poolAddress: Address, - poolType: string, - removeTestInput: RemoveTestInput, + rpcUrl: string, + chainId: number, + poolAddress: Address, + poolType: string, + removeTestInput: RemoveTestInput, ): Promise { - const removeLiquidityInput = getInput(removeTestInput, chainId, rpcUrl); - // Onchain provider is used to fetch pool state - const onchainProvider = new OnChainProvider(rpcUrl, chainId); - const poolState = await onchainProvider.pools.fetchPoolState( - poolAddress, - poolType, - ); - // Simulate addLiquidity to get the amount of BPT out - const removeLiquidity = new RemoveLiquidity(); - return await removeLiquidity.query(removeLiquidityInput, poolState); + const removeLiquidityInput = getInput(removeTestInput, chainId, rpcUrl); + // Onchain provider is used to fetch pool state + const onchainProvider = new OnChainProvider(rpcUrl, chainId); + const poolState = await onchainProvider.pools.fetchPoolState( + poolAddress, + poolType, + ); + // Simulate addLiquidity to get the amount of BPT out + const removeLiquidity = new RemoveLiquidity(); + return await removeLiquidity.query(removeLiquidityInput, poolState); } export async function getRemoveLiquiditys( - removeTestInputs: RemoveTestInput[], - rpcUrl: string, - chainId: number, - poolAddress: Address, - poolType: string, + removeTestInputs: RemoveTestInput[], + rpcUrl: string, + chainId: number, + poolAddress: Address, + poolType: string, ): Promise { - if (!removeTestInputs) return undefined; - const results: RemoveLiquidityResult[] = []; - console.log("Querying removes..."); - for (const removeTestInput of removeTestInputs) { - // TODO - put this in a multicall? - const result = await queryRemoveLiquidity( - rpcUrl, - chainId, - poolAddress, - poolType, - removeTestInput, - ); - results.push({ - kind: removeTestInput.kind, - amountsOutRaw: result.amountsOut.map((a) => a.amount.toString()), - bptInRaw: result.bptIn.amount.toString(), - }); - } - console.log("Done"); - return results; + if (!removeTestInputs) return undefined; + const results: RemoveLiquidityResult[] = []; + console.log('Querying removes...'); + for (const removeTestInput of removeTestInputs) { + // TODO - put this in a multicall? + const result = await queryRemoveLiquidity( + rpcUrl, + chainId, + poolAddress, + poolType, + removeTestInput, + ); + results.push({ + kind: removeTestInput.kind, + amountsOutRaw: result.amountsOut.map((a) => a.amount.toString()), + bptInRaw: result.bptIn.amount.toString(), + }); + } + console.log('Done'); + return results; } diff --git a/testData/src/getSwaps.ts b/testData/src/getSwaps.ts index 5c92829..218cafc 100644 --- a/testData/src/getSwaps.ts +++ b/testData/src/getSwaps.ts @@ -1,84 +1,90 @@ -import type { Address } from "viem"; +import type { Address } from 'viem'; import { - SwapKind, - Swap, - type SwapInput as SdkSwapInput, - type ExactInQueryOutput, - type ExactOutQueryOutput, -} from "@balancer/sdk"; + SwapKind, + Swap, + type SwapInput as SdkSwapInput, + type ExactInQueryOutput, + type ExactOutQueryOutput, +} from '@balancer/sdk'; export type SwapInput = { - swapKind: SwapKind; - amountRaw: bigint; - tokenIn: Address; - tokenOut: Address; + swapKind: SwapKind; + amountRaw: bigint; + tokenIn: Address; + tokenOut: Address; }; -export type SwapResult = Omit & { - amountRaw: string; - outputRaw: string; +export type SwapResult = Omit & { + amountRaw: string; + outputRaw: string; }; async function querySwap( - chainId: number, - poolAddress: Address, - rpcUrl: string, - swap: SwapInput, + chainId: number, + poolAddress: Address, + rpcUrl: string, + swap: SwapInput, ): Promise { - const swapInput: SdkSwapInput = { - chainId: chainId, - swapKind: swap.swapKind, - paths: [ - { - pools: [poolAddress], - tokens: [ - { - address: swap.tokenIn, - decimals: 18, - }, // tokenIn - { - address: swap.tokenOut, - decimals: 18, - }, // tokenOut - ], - vaultVersion: 3 as const, - inputAmountRaw: - swap.swapKind === SwapKind.GivenIn ? BigInt(swap.amountRaw) : 0n, - outputAmountRaw: - swap.swapKind === SwapKind.GivenOut ? BigInt(swap.amountRaw) : 0n, - }, - ], - }; - const sdkSwap = new Swap(swapInput); - let result = 0n; - if (swap.swapKind === SwapKind.GivenIn) { - const queryResult = (await sdkSwap.query(rpcUrl)) as ExactInQueryOutput; - result = queryResult.expectedAmountOut.amount; - } else { - const queryResult = (await sdkSwap.query(rpcUrl)) as ExactOutQueryOutput; - result = queryResult.expectedAmountIn.amount; - } - return result; + const swapInput: SdkSwapInput = { + chainId: chainId, + swapKind: swap.swapKind, + paths: [ + { + pools: [poolAddress], + tokens: [ + { + address: swap.tokenIn, + decimals: 18, + }, // tokenIn + { + address: swap.tokenOut, + decimals: 18, + }, // tokenOut + ], + vaultVersion: 3 as const, + inputAmountRaw: + swap.swapKind === SwapKind.GivenIn + ? BigInt(swap.amountRaw) + : 0n, + outputAmountRaw: + swap.swapKind === SwapKind.GivenOut + ? BigInt(swap.amountRaw) + : 0n, + }, + ], + }; + const sdkSwap = new Swap(swapInput); + let result = 0n; + if (swap.swapKind === SwapKind.GivenIn) { + const queryResult = (await sdkSwap.query(rpcUrl)) as ExactInQueryOutput; + result = queryResult.expectedAmountOut.amount; + } else { + const queryResult = (await sdkSwap.query( + rpcUrl, + )) as ExactOutQueryOutput; + result = queryResult.expectedAmountIn.amount; + } + return result; } export async function getSwaps( - swapTestInputs: SwapInput[], - rpcUrl: string, - chainId: number, - poolAddress: Address, + swapTestInputs: SwapInput[], + rpcUrl: string, + chainId: number, + poolAddress: Address, ): Promise { - if (!swapTestInputs) return undefined; - const results: SwapResult[] = []; - console.log("Querying swaps..."); - for (const swap of swapTestInputs) { - // get swap. TODO - put this in a multicall? - const result = await querySwap(chainId, poolAddress, rpcUrl, swap); - results.push({ - ...swap, - amountRaw: swap.amountRaw.toString(), - outputRaw: result.toString(), - }); - } - console.log("Done"); - return results; + if (!swapTestInputs) return undefined; + const results: SwapResult[] = []; + console.log('Querying swaps...'); + for (const swap of swapTestInputs) { + // get swap. TODO - put this in a multicall? + const result = await querySwap(chainId, poolAddress, rpcUrl, swap); + results.push({ + ...swap, + amountRaw: swap.amountRaw.toString(), + outputRaw: result.toString(), + }); + } + console.log('Done'); + return results; } diff --git a/testData/src/stablePool.ts b/testData/src/stablePool.ts index 64dcfa0..dcde422 100644 --- a/testData/src/stablePool.ts +++ b/testData/src/stablePool.ts @@ -1,118 +1,118 @@ import { - type PublicClient, - createPublicClient, - http, - type Address, - parseAbi, - type Chain, -} from "viem"; -import { CHAINS, VAULT_V3, vaultExtensionV3Abi } from "@balancer/sdk"; + type PublicClient, + createPublicClient, + http, + type Address, + parseAbi, + type Chain, +} from 'viem'; +import { CHAINS, VAULT_V3, vaultExtensionV3Abi } from '@balancer/sdk'; import type { - StableImmutable, - StableMutable, -} from "../../typescript/src/stable/data"; + StableImmutable, + StableMutable, +} from '../../typescript/src/stable/data'; type TransformBigintToString = { - [K in keyof T]: T[K] extends bigint - ? string - : T[K] extends bigint[] - ? string[] - : T[K]; + [K in keyof T]: T[K] extends bigint + ? string + : T[K] extends bigint[] + ? string[] + : T[K]; }; export class StablePool { - client: PublicClient; - vault: Address; + client: PublicClient; + vault: Address; - constructor( - public rpcUrl: string, - public chainId: number, - ) { - this.client = createPublicClient({ - transport: http(this.rpcUrl), - chain: CHAINS[this.chainId] as Chain, - }); - this.vault = VAULT_V3[this.chainId]; - } + constructor( + public rpcUrl: string, + public chainId: number, + ) { + this.client = createPublicClient({ + transport: http(this.rpcUrl), + chain: CHAINS[this.chainId] as Chain, + }); + this.vault = VAULT_V3[this.chainId]; + } - async fetchImmutableData( - address: Address, - ): Promise> { - const poolTokensCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getPoolTokenInfo", - args: [address], - } as const; - const tokenRatesCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getPoolTokenRates", - args: [address], - } as const; - const multicallResult = await this.client.multicall({ - contracts: [poolTokensCall, tokenRatesCall], - allowFailure: false, - }); - return { - tokens: multicallResult[0][0].map((token) => token), - scalingFactors: multicallResult[1][0].map((sf) => sf.toString()), - }; - } + async fetchImmutableData( + address: Address, + ): Promise> { + const poolTokensCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getPoolTokenInfo', + args: [address], + } as const; + const tokenRatesCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getPoolTokenRates', + args: [address], + } as const; + const multicallResult = await this.client.multicall({ + contracts: [poolTokensCall, tokenRatesCall], + allowFailure: false, + }); + return { + tokens: multicallResult[0][0].map((token) => token), + scalingFactors: multicallResult[1][0].map((sf) => sf.toString()), + }; + } - async fetchMutableData( - address: Address, - ): Promise> { - const staticSwapFeeCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getStaticSwapFeePercentage", - args: [address], - } as const; - const totalSupplyCall = { - address: this.vault, - abi: parseAbi([ - "function totalSupply(address token) external view returns (uint256)", - ]), - functionName: "totalSupply", - args: [address], - } as const; - const liveBalancesCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getCurrentLiveBalances", - args: [address], - } as const; - const tokenRatesCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getPoolTokenRates", - args: [address], - } as const; - const amplificationParameterCall = { - address, - abi: parseAbi([ - "function getAmplificationParameter() external view returns (uint256 value, bool isUpdating, uint256 precision)", - ]), - functionName: "getAmplificationParameter", - } as const; + async fetchMutableData( + address: Address, + ): Promise> { + const staticSwapFeeCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getStaticSwapFeePercentage', + args: [address], + } as const; + const totalSupplyCall = { + address: this.vault, + abi: parseAbi([ + 'function totalSupply(address token) external view returns (uint256)', + ]), + functionName: 'totalSupply', + args: [address], + } as const; + const liveBalancesCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getCurrentLiveBalances', + args: [address], + } as const; + const tokenRatesCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getPoolTokenRates', + args: [address], + } as const; + const amplificationParameterCall = { + address, + abi: parseAbi([ + 'function getAmplificationParameter() external view returns (uint256 value, bool isUpdating, uint256 precision)', + ]), + functionName: 'getAmplificationParameter', + } as const; - const multicallResult = await this.client.multicall({ - contracts: [ - staticSwapFeeCall, - totalSupplyCall, - liveBalancesCall, - tokenRatesCall, - amplificationParameterCall, - ], - allowFailure: false, - }); - return { - swapFee: multicallResult[0].toString(), - totalSupply: multicallResult[1].toString(), - balancesLiveScaled18: multicallResult[2].map((b) => b.toString()), - tokenRates: multicallResult[3][1].map((b) => b.toString()), - amp: multicallResult[4][0].toString(), - }; - } + const multicallResult = await this.client.multicall({ + contracts: [ + staticSwapFeeCall, + totalSupplyCall, + liveBalancesCall, + tokenRatesCall, + amplificationParameterCall, + ], + allowFailure: false, + }); + return { + swapFee: multicallResult[0].toString(), + totalSupply: multicallResult[1].toString(), + balancesLiveScaled18: multicallResult[2].map((b) => b.toString()), + tokenRates: multicallResult[3][1].map((b) => b.toString()), + amp: multicallResult[4][0].toString(), + }; + } } diff --git a/testData/src/types.ts b/testData/src/types.ts index 66b55d1..003f3fb 100644 --- a/testData/src/types.ts +++ b/testData/src/types.ts @@ -1,35 +1,35 @@ -import type { Address } from "viem"; -import type { AddLiquidityResult, AddTestInput } from "./getAdds"; -import type { SwapResult, SwapInput } from "./getSwaps"; -import type { RemoveLiquidityResult, RemoveTestInput } from "./getRemoves"; +import type { Address } from 'viem'; +import type { AddLiquidityResult, AddTestInput } from './getAdds'; +import type { SwapResult, SwapInput } from './getSwaps'; +import type { RemoveLiquidityResult, RemoveTestInput } from './getRemoves'; export type PoolBase = { - chainId: number; - blockNumber: number; - poolType: string; - poolAddress: Address; + chainId: number; + blockNumber: number; + poolType: string; + poolAddress: Address; }; // Read from main test config file export type Config = { - poolTests: PoolTestConfig[]; + poolTests: PoolTestConfig[]; }; // Each pool/chain/block has its own set of swap/add/remove tests type PoolTestConfig = PoolBase & { - testName: string; - swaps: SwapInput[]; - adds: AddTestInput[]; - removes: RemoveTestInput[]; + testName: string; + swaps: SwapInput[]; + adds: AddTestInput[]; + removes: RemoveTestInput[]; }; export type TestInput = PoolTestConfig & { - rpcUrl: string; + rpcUrl: string; }; export type TestOutput = { - pool: PoolBase; - swaps: SwapResult[] | undefined; - adds: AddLiquidityResult[] | undefined; - removes: RemoveLiquidityResult[] | undefined; + pool: PoolBase; + swaps: SwapResult[] | undefined; + adds: AddLiquidityResult[] | undefined; + removes: RemoveLiquidityResult[] | undefined; }; diff --git a/testData/src/weightedPool.ts b/testData/src/weightedPool.ts index 9a05d2a..7f52521 100644 --- a/testData/src/weightedPool.ts +++ b/testData/src/weightedPool.ts @@ -1,122 +1,118 @@ import { - type PublicClient, - createPublicClient, - http, - type Address, - parseAbi, - type Chain, -} from "viem"; -import { - CHAINS, - VAULT_V3, - vaultExtensionV3Abi, -} from "@balancer/sdk"; + type PublicClient, + createPublicClient, + http, + type Address, + parseAbi, + type Chain, +} from 'viem'; +import { CHAINS, VAULT_V3, vaultExtensionV3Abi } from '@balancer/sdk'; import type { - WeightedImmutable, - WeightedMutable, -} from "../../typescript/src/weighted/data"; + WeightedImmutable, + WeightedMutable, +} from '../../typescript/src/weighted/data'; type TransformBigintToString = { - [K in keyof T]: T[K] extends bigint - ? string - : T[K] extends bigint[] - ? string[] - : T[K]; + [K in keyof T]: T[K] extends bigint + ? string + : T[K] extends bigint[] + ? string[] + : T[K]; }; export class WeightedPool { - client: PublicClient; - vault: Address; + client: PublicClient; + vault: Address; - constructor( - public rpcUrl: string, - public chainId: number, - ) { - this.client = createPublicClient({ - transport: http(this.rpcUrl), - chain: CHAINS[this.chainId] as Chain, - }); - this.vault = VAULT_V3[this.chainId]; - } + constructor( + public rpcUrl: string, + public chainId: number, + ) { + this.client = createPublicClient({ + transport: http(this.rpcUrl), + chain: CHAINS[this.chainId] as Chain, + }); + this.vault = VAULT_V3[this.chainId]; + } - async fetchImmutableData( - address: Address, - ): Promise> { - const poolTokensCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getPoolTokenInfo", - args: [address], - } as const; - const tokenRatesCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getPoolTokenRates", - args: [address], - } as const; - const tokenWeightsCall = { - address, - abi: parseAbi([ - "function getNormalizedWeights() external view returns (uint256[] memory)", - ]), - functionName: "getNormalizedWeights", - } as const; + async fetchImmutableData( + address: Address, + ): Promise> { + const poolTokensCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getPoolTokenInfo', + args: [address], + } as const; + const tokenRatesCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getPoolTokenRates', + args: [address], + } as const; + const tokenWeightsCall = { + address, + abi: parseAbi([ + 'function getNormalizedWeights() external view returns (uint256[] memory)', + ]), + functionName: 'getNormalizedWeights', + } as const; - const multicallResult = await this.client.multicall({ - contracts: [poolTokensCall, tokenRatesCall, tokenWeightsCall], - allowFailure: false, - }); - return { - tokens: multicallResult[0][0].map((token) => token), - scalingFactors: multicallResult[1][0].map((sf) => sf.toString()), - weights: multicallResult[2].map((w) => w.toString()), - }; - } + const multicallResult = await this.client.multicall({ + contracts: [poolTokensCall, tokenRatesCall, tokenWeightsCall], + allowFailure: false, + }); + return { + tokens: multicallResult[0][0].map((token) => token), + scalingFactors: multicallResult[1][0].map((sf) => sf.toString()), + weights: multicallResult[2].map((w) => w.toString()), + }; + } - async fetchMutableData( - address: Address, - ): Promise> { - const staticSwapFeeCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getStaticSwapFeePercentage", - args: [address], - } as const; - const totalSupplyCall = { - address: this.vault, - abi: parseAbi([ - "function totalSupply(address token) external view returns (uint256)", - ]), - functionName: "totalSupply", - args: [address], - } as const; - const liveBalancesCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getCurrentLiveBalances", - args: [address], - } as const; - const tokenRatesCall = { - address: this.vault, - abi: vaultExtensionV3Abi, - functionName: "getPoolTokenRates", - args: [address], - } as const; + async fetchMutableData( + address: Address, + ): Promise> { + const staticSwapFeeCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getStaticSwapFeePercentage', + args: [address], + } as const; + const totalSupplyCall = { + address: this.vault, + abi: parseAbi([ + 'function totalSupply(address token) external view returns (uint256)', + ]), + functionName: 'totalSupply', + args: [address], + } as const; + const liveBalancesCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getCurrentLiveBalances', + args: [address], + } as const; + const tokenRatesCall = { + address: this.vault, + abi: vaultExtensionV3Abi, + functionName: 'getPoolTokenRates', + args: [address], + } as const; - const multicallResult = await this.client.multicall({ - contracts: [ - staticSwapFeeCall, - totalSupplyCall, - liveBalancesCall, - tokenRatesCall - ], - allowFailure: false, - }); - return { - swapFee: multicallResult[0].toString(), - totalSupply: multicallResult[1].toString(), - balancesLiveScaled18: multicallResult[2].map((b) => b.toString()), - tokenRates: multicallResult[3][1].map((b) => b.toString()), - }; - } + const multicallResult = await this.client.multicall({ + contracts: [ + staticSwapFeeCall, + totalSupplyCall, + liveBalancesCall, + tokenRatesCall, + ], + allowFailure: false, + }); + return { + swapFee: multicallResult[0].toString(), + totalSupply: multicallResult[1].toString(), + balancesLiveScaled18: multicallResult[2].map((b) => b.toString()), + tokenRates: multicallResult[3][1].map((b) => b.toString()), + }; + } }