Skip to content

Commit

Permalink
chore: Apply formatting to testData.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Jul 24, 2024
1 parent 6aa276d commit 9945c30
Show file tree
Hide file tree
Showing 8 changed files with 602 additions and 586 deletions.
111 changes: 61 additions & 50 deletions testData/src/generatePoolTestData.ts
Original file line number Diff line number Diff line change
@@ -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<TestOutput> {
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,
};
}
184 changes: 92 additions & 92 deletions testData/src/getAdds.ts
Original file line number Diff line number Diff line change
@@ -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: <explanation>
} 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: <explanation>
} 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: <explanation>
} 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: <explanation>
} 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<AddLiquidityQueryOutput> {
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<AddLiquidityResult[] | undefined> {
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;
}
54 changes: 27 additions & 27 deletions testData/src/getPool.ts
Original file line number Diff line number Diff line change
@@ -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<PoolBase> {
// 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,
};
}
Loading

0 comments on commit 9945c30

Please sign in to comment.