balancer-v2-pools
Simple SDK for simulating the exact on-chain behaviour of various Balancer v2 pools.
Usage instructions
import { JsonRpcProvider } from "@ethersproject/providers";
import { WeightedPool } from "@georgeroman/balancer-v2-pools";
const provider = new JsonRpcProvider(process.env.RPC_ENDPOINT);
const pool = await WeightedPool.initFromOnchain(
provider,
"0x0b09dea16768f0799065c475be02919503cb2a3500020000000000000000001a",
"mainnet"
);
const amountOut = pool.swapGivenIn("WETH", "DAI", "100");
const bptOut = pool.joinExactTokensInForBptOut({
WETH: "1",
DAI: "2000",
});
const tokensOut = pool.exitExactBptInForTokensOut("1.23");
Setup instructions
Mainnet forking is used for testing in order to make sure the SDK exactly match the EVM. Make sure to have a .env
file in the root directory, containing the following definitions (the given block number must include the instances of the pools used in the tests):
RPC_URL=
BLOCK_NUMBER=
The setup I recommend for deterministic tests is the following:
RPC_URL=https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_KEY}
BLOCK_NUMBER=13128465
To execute the tests locally, simply run:
npm install
npm test