Skip to content

Commit

Permalink
Merge pull request #532 from balancer/swaps-v3-integration-tests
Browse files Browse the repository at this point in the history
Refactor swaps v3 integration tests
  • Loading branch information
brunoguerios authored Dec 17, 2024
2 parents 7054cef + 79c8144 commit c818d3d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 277 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-dragons-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@balancer/sdk": patch
---

Refactor swaps v3 integration tests
277 changes: 2 additions & 275 deletions test/entities/swaps/v3/swapV3.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import {
CHAINS,
ChainId,
SwapKind,
Token,
Swap,
ExactInQueryOutput,
ExactOutQueryOutput,
BALANCER_ROUTER,
BALANCER_BATCH_ROUTER,
PERMIT2,
Expand Down Expand Up @@ -57,7 +54,6 @@ describe('SwapV3', () => {
let rpcUrl: string;
let snapshot: Hex;
let pathBalWeth: Path;
let pathBPTBAL: Path;
let pathMultiSwap: Path;
let pathUsdcWethMulti: Path;
let pathWithExit: Path;
Expand All @@ -83,24 +79,6 @@ describe('SwapV3', () => {
outputAmountRaw: 100000000000n,
};

// bpt [swap] weth
pathBPTBAL = {
protocolVersion: 3,
tokens: [
{
address: '0xce701deAC1B660dA4ee05F6F3F7cbafDDb6a79Fe', //BPT
decimals: 18,
},
{
address: '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
decimals: 18,
},
],
pools: ['0xce701deAC1B660dA4ee05F6F3F7cbafDDb6a79Fe'],
inputAmountRaw: 1000000000000000n,
outputAmountRaw: 1n,
};

// weth [swap] bal [swap] dai [swap] usdc
pathMultiSwap = {
protocolVersion,
Expand Down Expand Up @@ -215,194 +193,6 @@ describe('SwapV3', () => {
snapshot = await client.snapshot();
});

// TODO: double check if comparing query outputs against balanceDeltas isn't redundant with query tests
// if yes, we should be able to remove query tests (and avoid relying on a fixed blockNumber)
describe.skip('query method should return correct updated', () => {
describe('single swap', () => {
test('GivenIn', async () => {
const swap = new Swap({
chainId,
paths: [pathBalWeth],
swapKind: SwapKind.GivenIn,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactInQueryOutput;

const wethToken = new Token(
chainId,
WETH.address,
WETH.decimals,
);
expect(expected.expectedAmountOut.token).to.deep.eq(wethToken);
expect(expected.expectedAmountOut.amount).to.eq(97807604n);
});
test('GivenIn as BPT', async () => {
const swap = new Swap({
chainId,
paths: [pathBPTBAL],
swapKind: SwapKind.GivenIn,
});
const expected = (await swap.query(
rpcUrl,
)) as ExactInQueryOutput;
const balToken = new Token(chainId, BAL.address, BAL.decimals);

expect(expected.expectedAmountOut.token).to.deep.eq(balToken);
expect(expected.expectedAmountOut.amount).to.eq(
1409169048985584n,
);
});
test('GivenOut', async () => {
const swap = new Swap({
chainId,
paths: [pathBalWeth],
swapKind: SwapKind.GivenOut,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactOutQueryOutput;

const balToken = new Token(chainId, BAL.address, BAL.decimals);
expect(expected.expectedAmountIn.token).to.deep.eq(balToken);
expect(expected.expectedAmountIn.amount).to.eq(
102241640811992n,
);
});
test('Given Out as BPT', async () => {
const swap = new Swap({
chainId,
paths: [pathBalWeth],
swapKind: SwapKind.GivenOut,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactOutQueryOutput;

const balToken = new Token(chainId, BAL.address, BAL.decimals);

expect(expected.expectedAmountIn.token).to.deep.eq(balToken);
expect(expected.expectedAmountIn.amount).to.eq(
102241640811992n,
);
});
});
// BAL/DAI pool has no liquidity
describe.skip('multi-hop swap', () => {
describe('path with swaps only', () => {
test('GivenIn', async () => {
const swap = new Swap({
chainId,
paths: [pathMultiSwap],
swapKind: SwapKind.GivenIn,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactInQueryOutput;

const usdcToken = new Token(
chainId,
USDC.address,
USDC.decimals,
);
expect(expected.swapKind).to.eq(SwapKind.GivenIn);
expect(expected.pathAmounts).to.deep.eq([186324n]);
expect(expected.expectedAmountOut.token).to.deep.eq(
usdcToken,
);
expect(expected.expectedAmountOut.amount).to.eq(186324n);
});
test('GivenOut', async () => {
const swap = new Swap({
chainId,
paths: [pathMultiSwap],
swapKind: SwapKind.GivenOut,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactOutQueryOutput;

const wethToken = new Token(
chainId,
WETH.address,
WETH.decimals,
);
expect(expected.swapKind).to.eq(SwapKind.GivenOut);
expect(expected.pathAmounts).to.deep.eq([
1084111861665514n,
]);
expect(expected.expectedAmountIn.token).to.deep.eq(
wethToken,
);
expect(expected.expectedAmountIn.amount).to.eq(
1084111861665514n,
);
});
});
describe('path with exit', () => {
test('GivenIn', async () => {
const swap = new Swap({
chainId,
paths: [pathMultiSwap, pathWithExit],
swapKind: SwapKind.GivenIn,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactInQueryOutput;

const usdcToken = new Token(
chainId,
USDC.address,
USDC.decimals,
);
expect(expected.swapKind).to.eq(SwapKind.GivenIn);
expect(expected.pathAmounts).to.deep.eq([
186324n,
1181508n,
]);
expect(expected.expectedAmountOut.token).to.deep.eq(
usdcToken,
);
expect(expected.expectedAmountOut.amount).to.eq(1367832n);
});
test('GivenOut', async () => {
const swap = new Swap({
chainId,
paths: [pathMultiSwap, pathWithExit],
swapKind: SwapKind.GivenOut,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactOutQueryOutput;

const wethToken = new Token(
chainId,
WETH.address,
WETH.decimals,
);
expect(expected.swapKind).to.eq(SwapKind.GivenOut);
expect(expected.pathAmounts).to.deep.eq([
1084111861665514n,
519629120486462n,
]);
expect(expected.expectedAmountIn.token).to.deep.eq(
wethToken,
);
expect(expected.expectedAmountIn.amount).to.eq(
1603740982151976n,
);
});
});
});
});

describe('permit2 direct approval', () => {
beforeEach(async () => {
await approveTokens(client, testAddress, tokens, protocolVersion);
Expand Down Expand Up @@ -518,8 +308,7 @@ describe('SwapV3', () => {
});
});

// BAL/DAI pool has no liquidity
describe.skip('multi-hop swap', () => {
describe('multi-hop swap', () => {
describe('swap should be executed correctly', () => {
describe('path with swaps only', () => {
describe('wethIsEth: false', () => {
Expand Down Expand Up @@ -812,8 +601,7 @@ describe('SwapV3', () => {
});
});

// BAL/DAI pool has no liquidity
describe.skip('multi-hop swap', () => {
describe('multi-hop swap', () => {
describe('swap should be executed correctly', () => {
describe('path with swaps only', () => {
describe('wethIsEth: false', () => {
Expand Down Expand Up @@ -986,67 +774,6 @@ describe('SwapV3', () => {
isBuffer: [true, false, true],
};

// TODO: same thing about query tests being redundant
describe.skip('query method should return correct updated', () => {
test('GivenIn', async () => {
const swap = new Swap({
chainId,
paths: [
{
...pathWithBuffers,
inputAmountRaw: 100000000n,
outputAmountRaw: 0n,
} as Path,
],
swapKind: SwapKind.GivenIn,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactInQueryOutput;

const usdtToken = new Token(
chainId,
USDT.address,
USDT.decimals,
);
expect(expected.swapKind).to.eq(SwapKind.GivenIn);
expect(expected.pathAmounts).to.deep.eq([99910376n]);
expect(expected.expectedAmountOut.token).to.deep.eq(
usdtToken,
);
expect(expected.expectedAmountOut.amount).to.eq(99910376n);
});
test('GivenOut', async () => {
const swap = new Swap({
chainId,
paths: [
{
...pathWithBuffers,
inputAmountRaw: 0n,
outputAmountRaw: 100000000n,
} as Path,
],
swapKind: SwapKind.GivenOut,
});

const expected = (await swap.query(
rpcUrl,
)) as ExactOutQueryOutput;

const usdcToken = new Token(
chainId,
USDC.address,
USDC.decimals,
);
expect(expected.swapKind).to.eq(SwapKind.GivenOut);
expect(expected.pathAmounts).to.deep.eq([100089705n]);
expect(expected.expectedAmountIn.token).to.deep.eq(
usdcToken,
);
expect(expected.expectedAmountIn.amount).to.eq(100089705n);
});
});
describe('swap should be executed correctly', () => {
beforeEach(async () => {
await approveTokens(
Expand Down
4 changes: 2 additions & 2 deletions test/lib/utils/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ export const POOLS: Record<number, Record<string, TestPool>> = {
slot: 0,
},
MOCK_BAL_DAI_POOL: {
address: '0xe69b70a86a4e1fd33da95693a1ae12be1c26c8ea', // TODO
id: '0xe69b70a86a4e1fd33da95693a1ae12be1c26c8ea',
address: '0x3e099b16beeae7cfcf8a0921941394f1a9b00e0e',
id: '0x3e099b16beeae7cfcf8a0921941394f1a9b00e0e',
type: PoolType.Weighted,
decimals: 18,
slot: 0,
Expand Down

0 comments on commit c818d3d

Please sign in to comment.