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

Change to eslint #19

Merged
merged 5 commits into from
Jul 24, 2024
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: 2 additions & 0 deletions testData/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
6 changes: 6 additions & 0 deletions testData/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
};
7 changes: 7 additions & 0 deletions testData/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 80,
"tabWidth": 4,
"singleQuote": true,
"bracketSpacing": true,
"semi": true
}
Binary file modified testData/bun.lockb
Binary file not shown.
14 changes: 12 additions & 2 deletions testData/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
"module": "index.ts",
"type": "module",
"devDependencies": {
"bun-types": "latest"
"@types/bun": "^1.1.6",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"bun-types": "latest",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"prettier": "^3.3.3"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand All @@ -14,6 +21,9 @@
},
"scripts": {
"generate": "bun index.ts false",
"generate:overwrite": "bun index.ts true"
"generate:overwrite": "bun index.ts true",
"format": "prettier --config .prettierrc 'src/**/*.ts' --write",
"lint": "eslint ./src --ext .ts",
"lint:fix": "eslint ./src --ext .ts --fix"
}
}
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;
}
Loading
Loading