Skip to content

Commit

Permalink
Refactor test to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoguerios committed Sep 1, 2023
1 parent 8747aa2 commit 6733647
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 56 deletions.
25 changes: 23 additions & 2 deletions test/lib/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
// 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,
Expand Down
110 changes: 56 additions & 54 deletions test/weightedJoin.integration.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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

Expand All @@ -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();
Expand All @@ -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);
});
});
});

Expand Down

0 comments on commit 6733647

Please sign in to comment.