Skip to content

Commit

Permalink
Add generalisedJoin scenario to be compared with b-sdk results
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoguerios committed Feb 20, 2024
1 parent 3f381f6 commit 36e9118
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
120 changes: 120 additions & 0 deletions balancer-js/src/modules/pricing/priceImpact.compare.nested.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// yarn test:only ./src/modules/pricing/priceImpact.compare.nested.spec.ts
import dotenv from 'dotenv';

import { BalancerSDK, GraphQLQuery, Network, SimulationType } from '@/.';
import { parseFixed } from '@ethersproject/bignumber';
import { JsonRpcProvider } from '@ethersproject/providers';
import { FORK_NODES, createSubgraphQuery, forkSetup } from '@/test/lib/utils';
import { ADDRESSES, TestAddress, TestAddresses } from '@/test/lib/constants';
import { JsonRpcSigner } from '@ethersproject/providers';
import { RPC_URLS } from '@/test/lib/utils';
import { testGeneralisedJoin } from '../joins/testHelper';

/**
* -- Integration tests for generalisedJoin --
*
* It compares results from local fork transactions with simulated results from
* the Simulation module, which can be of 3 different types:
* 1. Tenderly: uses Tenderly Simulation API (third party service)
* 2. VaultModel: uses TS math, which may be less accurate (min. 99% accuracy)
* 3. Static: uses staticCall, which is 100% accurate but requires vault approval
*/

dotenv.config();

// mainnet
const TEST_JOIN_WITH_ETH_JOIN_FIRST = true;

describe('generalised join execution', async function () {
this.timeout(30000);
const simulationType = SimulationType.Static;
let network: Network;
let blockNumber: number;
let jsonRpcUrl: string;
let rpcUrl: string;
let addresses: TestAddresses;
let subgraphQuery: GraphQLQuery;
let sdk: BalancerSDK;
let signer: JsonRpcSigner;
let userAddress: string;
let tokens: TestAddress[];
let balances: string[];
let testPool: TestAddress;

beforeEach(async () => {
await forkSetup(
signer,
tokens.map((t) => t.address),
tokens.map((t) => t.slot as number),
balances,
jsonRpcUrl,
blockNumber
);
});

context('mainnet', async () => {
before(async () => {
network = Network.MAINNET;
blockNumber = 18559730;
jsonRpcUrl = FORK_NODES[network];
rpcUrl = RPC_URLS[network];
const provider = new JsonRpcProvider(rpcUrl, network);
signer = provider.getSigner(1);
userAddress = await signer.getAddress();
addresses = ADDRESSES[network];
subgraphQuery = createSubgraphQuery(
[
'0x08775ccb6674d6bdceb0797c364c2653ed84f384',
'0x79c58f70905f734641735bc61e45c19dd9ad60bc',
],
blockNumber
);
// // Uncomment and set tenderlyConfig on sdk instantiation in order to test using tenderly simulations
// const tenderlyConfig = {
// accessKey: process.env.TENDERLY_ACCESS_KEY as string,
// user: process.env.TENDERLY_USER as string,
// project: process.env.TENDERLY_PROJECT as string,
// blockNumber,
// };
sdk = new BalancerSDK({
network,
rpcUrl,
subgraphQuery,
});
});

context('join with all tokens - unbalanced', async () => {
if (!TEST_JOIN_WITH_ETH_JOIN_FIRST) return true;

before(async () => {
testPool = addresses.WETH_3POOL;
tokens = [
addresses.DAI,
addresses.USDC,
addresses.USDT,
addresses.WETH,
];
balances = [
parseFixed('100000', addresses.DAI.decimals).toString(),
parseFixed('1000', addresses.USDC.decimals).toString(),
parseFixed('10', addresses.USDT.decimals).toString(),
parseFixed('0.1', addresses.WETH.decimals).toString(),
];
});

it('should join with all tokens', async () => {
const tokensIn = tokens.map((t) => t.address);
const amountsIn = balances;
await testGeneralisedJoin(
sdk,
signer,
userAddress,
testPool,
tokensIn,
amountsIn,
simulationType
);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as fs from 'fs';
import {
Address,
BalancerSDK,
BatchSwapStep,
Network,
PoolToken,
PoolWithMethods,
Expand Down
7 changes: 7 additions & 0 deletions balancer-js/src/test/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ export const ADDRESSES = {
symbol: 'bveth',
slot: 0,
},
WETH_3POOL: {
id: '0x08775ccb6674d6bdceb0797c364c2653ed84f3840002000000000000000004f0',
address: '0x08775ccb6674d6bdceb0797c364c2653ed84f384',
decimals: 18,
symbol: 'weth_3pool',
slot: 0,
},
},
[Network.POLYGON]: {
MATIC: {
Expand Down

0 comments on commit 36e9118

Please sign in to comment.