-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathauraBal.integration.test.ts
More file actions
164 lines (144 loc) · 4.57 KB
/
auraBal.integration.test.ts
File metadata and controls
164 lines (144 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// pnpm test -- auraBal.integration.test.ts
import {
createTestClient,
Hex,
http,
publicActions,
TestActions,
walletActions,
zeroAddress,
} from 'viem';
import {
Relayer,
Slippage,
Token,
TokenAmount,
AuraBalSwap,
BALANCER_RELAYER,
CHAINS,
NATIVE_ASSETS,
SwapKind,
ChainId,
PublicWalletClient,
} from '@/index';
import {
auraBalToken,
BAL,
} from '@/entities/swap/swaps/v2/auraBalSwaps/constants';
import { ANVIL_NETWORKS, startFork } from 'test/anvil/anvil-global-setup';
import { forkSetup, sendTransactionGetBalances } from 'test/lib/utils';
const chainId = ChainId.MAINNET;
const bal = new Token(chainId, BAL, 18);
const weth = new Token(chainId, NATIVE_ASSETS[chainId].wrapped, 18);
describe('auraBalSwaps:Integration tests', () => {
let rpcUrl: string;
let snapshot: Hex;
let client: PublicWalletClient & TestActions;
beforeAll(async () => {
// setup chain and test client
({ rpcUrl } = await startFork(ANVIL_NETWORKS.MAINNET));
client = createTestClient({
mode: 'anvil',
chain: CHAINS[chainId],
transport: http(rpcUrl),
})
.extend(publicActions)
.extend(walletActions);
snapshot = await client.snapshot();
});
beforeEach(async () => {
await client.revert({
id: snapshot,
});
snapshot = await client.snapshot();
});
describe('to auraBal', () => {
test('from bal', async () => {
await testAuraBalSwap(client, bal, auraBalToken, 1, rpcUrl);
});
test('from weth', async () => {
await testAuraBalSwap(client, weth, auraBalToken, 3, rpcUrl);
});
test('from weth, wethIsEth=true', async () => {
await testAuraBalSwap(client, weth, auraBalToken, 3, rpcUrl, true);
});
});
describe('from auraBal', () => {
test('to bal', async () => {
await testAuraBalSwap(client, auraBalToken, bal, 0, rpcUrl);
});
test('to weth', async () => {
await testAuraBalSwap(client, auraBalToken, weth, 0, rpcUrl);
});
test('to weth, wethIsEth=true', async () => {
await testAuraBalSwap(client, auraBalToken, weth, 0, rpcUrl, true);
});
});
});
async function testAuraBalSwap(
client: PublicWalletClient & TestActions,
tokenIn: Token,
tokenOut: Token,
tokenInSlot: number,
rpcUrl: string,
wethIsEth = false,
) {
const testAddress = (await client.getAddresses())[0];
const auraBalSwap = new AuraBalSwap(rpcUrl);
const swapAmount = TokenAmount.fromHumanAmount(tokenIn, '1');
await forkSetup(
client,
testAddress,
[tokenIn.address],
[tokenInSlot],
[swapAmount.amount],
);
const queryOutput = await auraBalSwap.query({
swapAmount,
tokenIn,
tokenOut,
kind: SwapKind.GivenIn,
});
const slippage = Slippage.fromPercentage('1'); // 1%
const relayerApprovalSignature = await Relayer.signRelayerApproval(
BALANCER_RELAYER[chainId],
testAddress,
client,
);
const call = auraBalSwap.buildCall({
queryOutput,
slippage,
wethIsEth,
user: testAddress,
relayerApprovalSignature,
});
const { transactionReceipt, balanceDeltas } =
await sendTransactionGetBalances(
[tokenIn.address, tokenOut.address, zeroAddress],
client,
testAddress,
call.to,
call.callData,
call.value,
);
expect(transactionReceipt.status).to.equal('success');
if (wethIsEth && NATIVE_ASSETS[ChainId.MAINNET].isWrapped(tokenIn)) {
expect(queryOutput.inputAmount.amount).to.equal(balanceDeltas[2]);
expect(queryOutput.expectedAmountOut.amount).to.equal(balanceDeltas[1]);
expect(call.value).to.eq(queryOutput.inputAmount.amount);
expect(balanceDeltas[0]).to.eq(0n);
} else if (
wethIsEth &&
NATIVE_ASSETS[ChainId.MAINNET].isWrapped(tokenOut)
) {
expect(queryOutput.inputAmount.amount).to.equal(balanceDeltas[0]);
expect(queryOutput.expectedAmountOut.amount).to.equal(balanceDeltas[2]);
expect(call.value).to.eq(0n);
expect(balanceDeltas[1]).to.eq(0n);
} else {
expect(queryOutput.inputAmount.amount).to.equal(balanceDeltas[0]);
expect(queryOutput.expectedAmountOut.amount).to.equal(balanceDeltas[1]);
expect(call.value).to.eq(0n);
expect(balanceDeltas[2]).to.eq(0n);
}
}