From 67336474a6ee79161c5e1ee97d7609f73ffb2e3e Mon Sep 17 00:00:00 2001 From: Bruno Eidam Guerios Date: Fri, 1 Sep 2023 16:20:38 -0300 Subject: [PATCH] Refactor test to improve readability --- test/lib/utils/helper.ts | 25 +++++- test/weightedJoin.integration.test.ts | 110 +++++++++++++------------- 2 files changed, 79 insertions(+), 56 deletions(-) diff --git a/test/lib/utils/helper.ts b/test/lib/utils/helper.ts index 0a163951..ae2fb4e3 100644 --- a/test/lib/utils/helper.ts +++ b/test/lib/utils/helper.ts @@ -3,12 +3,33 @@ import { Client, PublicActions, TestActions, - TestClient, TransactionReceipt, WalletActions, } from 'viem'; import { erc20Abi } from '../../../src/abi'; -import { ZERO_ADDRESS } from '../../../src/utils'; +import { BALANCER_VAULT, MAX_UINT256, ZERO_ADDRESS } from '../../../src/utils'; + +export const approveToken = async ( + client: Client & PublicActions & WalletActions, + account: Address, + token: Address, + amount = MAX_UINT256, // approve max by default +): Promise => { + // approve token on the vault + const hash = await client.writeContract({ + account, + chain: client.chain, + address: token, + abi: erc20Abi, + functionName: 'approve', + args: [BALANCER_VAULT, amount], + }); + + const txReceipt = await client.getTransactionReceipt({ + hash, + }); + return txReceipt.status === 'success'; +}; export const getErc20Balance = ( token: Address, diff --git a/test/weightedJoin.integration.test.ts b/test/weightedJoin.integration.test.ts index e45567ee..1340bb6d 100644 --- a/test/weightedJoin.integration.test.ts +++ b/test/weightedJoin.integration.test.ts @@ -1,16 +1,17 @@ // pnpm test -- weightedJoin.integration.test.ts -import { describe, expect, test, beforeAll } from 'vitest'; +import { describe, expect, test, beforeAll, beforeEach } from 'vitest'; import dotenv from 'dotenv'; dotenv.config(); import { + BaseJoin, JoinInput, JoinParser, PoolState, Token, TokenAmount, } from '../src/entities'; -import { CHAINS, ChainId, MAX_UINT256, getPoolAddress } from '../src/utils'; +import { CHAINS, ChainId, getPoolAddress } from '../src/utils'; import { Address } from '../src/types'; import { Client, @@ -22,8 +23,7 @@ import { WalletActions, walletActions, } from 'viem'; -import { erc20Abi } from '../src/abi'; -import { sendTransactionGetBalances } from './lib/utils/helper'; +import { approveToken, sendTransactionGetBalances } from './lib/utils/helper'; const testAddress = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f'; // Balancer DAO Multisig @@ -33,6 +33,10 @@ describe('weighted join test', () => { let rpcUrl: string; let blockNumber: bigint; let client: Client & PublicActions & TestActions & WalletActions; + let poolId: Address; + let poolFromApi: PoolState; + let tokenIn: Token; + let weightedJoin: BaseJoin; beforeAll(async () => { api = new MockApi(); @@ -46,71 +50,69 @@ describe('weighted join test', () => { }) .extend(publicActions) .extend(walletActions); + }); + beforeEach(async () => { await client.reset({ blockNumber, jsonRpcUrl: process.env.ETHEREUM_RPC_URL || 'https://eth.llamarpc.com', }); - await client.impersonateAccount({ address: testAddress }); + await approveToken(client, testAddress, tokenIn.address); + + poolFromApi = await api.getPool(poolId); + const joinParser = new JoinParser(); + weightedJoin = joinParser.getJoin(poolFromApi.type); }); - test('should join', async () => { - const poolId = + + describe('single token join', async () => { + poolId = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014'; // 80BAL-20WETH - // Calls API - const poolFromApi = await api.getPool(poolId); - const joinParser = new JoinParser(); - const weightedJoin = joinParser.getJoin(poolFromApi.type); - const tokenInRaw = poolFromApi.tokens[0]; - const tokenIn = new Token( + tokenIn = new Token( chainId, - tokenInRaw.address, - tokenInRaw.decimals, + '0xba100000625a3754423978a60c9317c58a424e3D', + 18, + 'BAL', ); - const queryInput: JoinInput = { - tokenAmounts: [TokenAmount.fromHumanAmount(tokenIn, '1')], - chainId, - rpcUrl, - }; - const queryResult = await weightedJoin.query(queryInput, poolFromApi); + const amountIn = TokenAmount.fromHumanAmount(tokenIn, '1'); - const { call, to, value } = weightedJoin.buildCall({ - ...queryResult, - slippage: '10', - sender: testAddress, - recipient: testAddress, - }); - - // approve token on the vault - await client.writeContract({ - account: testAddress, - chain: CHAINS[chainId], - address: tokenIn.address, - abi: erc20Abi, - functionName: 'approve', - args: [to, MAX_UINT256], - }); - - const { transactionReceipt, balanceDeltas } = - await sendTransactionGetBalances( - [...queryResult.assets, queryResult.bptOut.token.address], - client, - testAddress, - to, - call, - value, + test('should join', async () => { + const joinInput: JoinInput = { + tokenAmounts: [amountIn], + chainId, + rpcUrl, + }; + const queryResult = await weightedJoin.query( + joinInput, + poolFromApi, ); - console.log('result', balanceDeltas); + const { call, to, value } = weightedJoin.buildCall({ + ...queryResult, + slippage: '10', + sender: testAddress, + recipient: testAddress, + }); + + const { transactionReceipt, balanceDeltas } = + await sendTransactionGetBalances( + [...queryResult.assets, queryResult.bptOut.token.address], + client, + testAddress, + to, + call, + value, + ); - expect(transactionReceipt.status).to.eq('success'); - expect(queryResult.bptOut.amount > 0n).to.be.true; - const expectedDeltas = [ - ...queryResult.amountsIn.map((a) => a.amount), - queryResult.bptOut.amount, - ]; - expect(expectedDeltas).to.deep.eq(balanceDeltas); + expect(transactionReceipt.status).to.eq('success'); + expect(queryResult.bptOut.amount > 0n).to.be.true; + const expectedDeltas = [ + ...queryResult.amountsIn.map((a) => a.amount), + queryResult.bptOut.amount, + ]; + expect(expectedDeltas).to.deep.eq(balanceDeltas); + }); }); });