-
Notifications
You must be signed in to change notification settings - Fork 25
/
test.fork.ts
335 lines (268 loc) · 12.2 KB
/
test.fork.ts
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import hre from 'hardhat';
import { expect } from 'chai';
import { BigNumber, Contract } from 'ethers';
import * as expectEvent from '@helpers/expectEvent';
import { describeForkTest } from '@src';
import { Task, TaskMode } from '@src';
import { getForkedNetwork } from '@src';
import { getSigner, impersonate } from '@src';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { MAX_UINT256, ZERO_ADDRESS } from '@helpers/constants';
import { bn, fp } from '@helpers/numbers';
import { BasePoolEncoder } from '@helpers/models/pools/utils/encoder';
import { StablePoolEncoder } from '@helpers/models/pools/stable/encoder';
import { SwapKind } from '@helpers/models/types/types';
import { actionId } from '@helpers/models/misc/actions';
import { expectEqualWithError } from '@helpers/relativeError';
describeForkTest.skip('ComposableStablePool', 'mainnet', 16000000, function () {
let task: Task;
let factory: Contract;
let owner: SignerWithAddress;
let whale: SignerWithAddress;
let govMultisig: SignerWithAddress;
let vault: Contract;
let authorizer: Contract;
let busd: Contract;
let usdt: Contract;
const GOV_MULTISIG = '0x10A19e7eE7d7F8a52822f6817de8ea18204F2e4f';
const LARGE_TOKEN_HOLDER = '0x47ac0fb4f2d84898e4d9e7b4dab3c24507a6d503';
const USDT_SCALING = bn(1e12); // USDT has 6 decimals, so its scaling factor is 1e12
const USDT = '0xdac17f958d2ee523a2206206994597c13d831ec7';
const BUSD = '0x4Fabb145d64652a948d72533023f6E7A623C7C53';
const tokens = [BUSD, USDT];
const amplificationParameter = bn(400);
const swapFeePercentage = fp(0.01);
const initialBalanceBUSD = fp(1e6);
const initialBalanceUSDT = fp(1e6).div(USDT_SCALING);
const initialBalances = [initialBalanceBUSD, initialBalanceUSDT];
before('run task', async () => {
task = new Task('20221122-composable-stable-pool-v2', TaskMode.TEST, getForkedNetwork(hre));
await task.run({ force: true });
factory = await task.deployedInstance('ComposableStablePoolFactory');
});
before('load signers', async () => {
owner = await getSigner();
whale = await impersonate(LARGE_TOKEN_HOLDER, fp(100));
govMultisig = await impersonate(GOV_MULTISIG, fp(100));
});
before('load vault and tokens', async () => {
const vaultTask = new Task('20210418-vault', TaskMode.READ_ONLY, getForkedNetwork(hre));
vault = await vaultTask.instanceAt('Vault', await factory.getVault());
authorizer = await new Task('20210418-authorizer', TaskMode.READ_ONLY, getForkedNetwork(hre)).deployedInstance(
'Authorizer'
);
busd = await task.instanceAt('ERC20', BUSD);
usdt = await task.instanceAt('ERC20', USDT);
await busd.connect(whale).approve(vault.address, MAX_UINT256);
await usdt.connect(whale).approve(vault.address, MAX_UINT256);
});
async function createPool(tokens: string[], initialize = true): Promise<Contract> {
const rateProviders: string[] = Array(tokens.length).fill(ZERO_ADDRESS);
const cacheDurations: BigNumber[] = Array(tokens.length).fill(bn(0));
const exemptFlags: boolean[] = Array(tokens.length).fill(false);
const tx = await factory.create(
'CSP',
'CSBPT',
tokens,
amplificationParameter,
rateProviders,
cacheDurations,
exemptFlags,
swapFeePercentage,
owner.address
);
const event = expectEvent.inReceipt(await tx.wait(), 'PoolCreated');
const pool = await task.instanceAt('ComposableStablePool', event.args.pool);
expect(await factory.isPoolFromFactory(pool.address)).to.be.true;
if (initialize) {
const bptIndex = await pool.getBptIndex();
const poolId = await pool.getPoolId();
const registeredBalances = getRegisteredBalances(bptIndex, initialBalances);
const { tokens: registeredTokens } = await vault.getPoolTokens(poolId);
const userData = StablePoolEncoder.joinInit(registeredBalances);
// Use this for maxAmountsIn
registeredBalances[bptIndex] = MAX_UINT256;
await vault.connect(whale).joinPool(poolId, whale.address, owner.address, {
assets: registeredTokens,
maxAmountsIn: registeredBalances,
fromInternalBalance: false,
userData,
});
}
return pool;
}
function getRegisteredBalances(bptIndex: number, balances: BigNumber[]): BigNumber[] {
return Array.from({ length: balances.length + 1 }).map((_, i) =>
i == bptIndex ? bn(0) : i < bptIndex ? balances[i] : balances[i - 1]
);
}
describe('getters', () => {
it('check factory version', async () => {
const expectedFactoryVersion = {
name: 'ComposableStablePoolFactory',
version: 2,
deployment: '20221122-composable-stable-pool-v2',
};
expect(await factory.version()).to.equal(JSON.stringify(expectedFactoryVersion));
});
it('check pool version', async () => {
const pool = await createPool(tokens);
const expectedPoolVersion = {
name: 'ComposableStablePool',
version: 2,
deployment: '20221122-composable-stable-pool-v2',
};
expect(await pool.version()).to.equal(JSON.stringify(expectedPoolVersion));
});
});
describe('pool operations', () => {
const amount = fp(500);
let pool: Contract;
let poolId: string;
let bptIndex: number;
context('swap', () => {
before('deploy a composable stable pool', async () => {
expect(await factory.isPoolFromFactory(ZERO_ADDRESS)).to.be.false;
pool = await createPool(tokens);
poolId = pool.getPoolId();
const [registeredAddress] = await vault.getPool(poolId);
expect(registeredAddress).to.equal(pool.address);
bptIndex = await pool.getBptIndex();
});
it('performs a swap', async () => {
await busd.connect(whale).transfer(owner.address, amount);
await busd.connect(owner).approve(vault.address, amount);
await vault
.connect(owner)
.swap(
{ kind: SwapKind.GivenIn, poolId, assetIn: BUSD, assetOut: USDT, amount, userData: '0x' },
{ sender: owner.address, recipient: owner.address, fromInternalBalance: false, toInternalBalance: false },
0,
MAX_UINT256
);
// Assert pool swap
const expectedUSDT = amount.div(USDT_SCALING);
expectEqualWithError(await busd.balanceOf(owner.address), 0, 0.0001);
expectEqualWithError(await usdt.balanceOf(owner.address), bn(expectedUSDT), 0.1);
});
});
context('proportional join', () => {
before('deploy a composable stable pool', async () => {
expect(await factory.isPoolFromFactory(ZERO_ADDRESS)).to.be.false;
pool = await createPool(tokens);
poolId = pool.getPoolId();
const [registeredAddress] = await vault.getPool(poolId);
expect(registeredAddress).to.equal(pool.address);
bptIndex = await pool.getBptIndex();
});
it('joins proportionally', async () => {
const ownerBptBalance = await pool.balanceOf(owner.address);
const bptOut = ownerBptBalance.div(5);
const { tokens: registeredTokens } = await vault.getPoolTokens(poolId);
// Given the bptOut, the max amounts in should be slightly more than 1/5. Decimals make it a bit complicated.
const adjustedBalances = [
initialBalanceBUSD.div(fp(4.99)).mul(fp(1)),
initialBalanceUSDT.div(bn(4.99e6)).mul(1e6),
];
const maxAmountsIn = getRegisteredBalances(bptIndex, adjustedBalances);
const tx = await vault.connect(whale).joinPool(poolId, whale.address, whale.address, {
assets: registeredTokens,
maxAmountsIn: maxAmountsIn,
fromInternalBalance: false,
userData: StablePoolEncoder.joinAllTokensInForExactBptOut(bptOut),
});
const receipt = await (await tx).wait();
const { deltas: amountsIn } = expectEvent.inReceipt(receipt, 'PoolBalanceChanged').args;
// Amounts in should be ~ 1/5 the initial balances
expect(amountsIn).to.equalWithError(maxAmountsIn, 0.01);
// Make sure received BPT is close to what we expect
const currentBptBalance = await pool.balanceOf(whale.address);
expect(currentBptBalance).to.be.equalWithError(bptOut, 0.001);
});
});
context('proportional exit', () => {
before('deploy a composable stable pool', async () => {
expect(await factory.isPoolFromFactory(ZERO_ADDRESS)).to.be.false;
pool = await createPool(tokens);
poolId = pool.getPoolId();
const [registeredAddress] = await vault.getPool(poolId);
expect(registeredAddress).to.equal(pool.address);
bptIndex = await pool.getBptIndex();
});
it('exits proportionally', async () => {
const previousBptBalance = await pool.balanceOf(owner.address);
const bptIn = previousBptBalance.div(4);
const { tokens: registeredTokens, balances: registeredBalances } = await vault.getPoolTokens(poolId);
const tx = await vault.connect(owner).exitPool(poolId, owner.address, owner.address, {
assets: registeredTokens,
minAmountsOut: Array(registeredTokens.length).fill(0),
fromInternalBalance: false,
userData: StablePoolEncoder.exitExactBptInForTokensOut(bptIn),
});
const receipt = await (await tx).wait();
const { deltas } = expectEvent.inReceipt(receipt, 'PoolBalanceChanged').args;
const amountsOut = deltas.map((x: BigNumber) => x.mul(-1));
const expectedAmountsOut = (registeredBalances as BigNumber[]).map((b) => b.div(4));
expectedAmountsOut[bptIndex] = bn(0);
// Amounts out should be 1/4 the initial balances
expect(amountsOut).to.equalWithError(expectedAmountsOut, 0.00001);
// Make sure sent BPT is close to what we expect
const currentBptBalance = await pool.balanceOf(owner.address);
expect(currentBptBalance).to.be.equalWithError(bn(previousBptBalance).sub(bptIn), 0.001);
});
});
});
describe('recovery mode', () => {
let pool: Contract;
let poolId: string;
before('deploy and initialize a composable stable pool', async () => {
pool = await createPool(tokens);
poolId = await pool.getPoolId();
});
before('enter recovery mode', async () => {
await authorizer.connect(govMultisig).grantRole(await actionId(pool, 'enableRecoveryMode'), govMultisig.address);
await pool.connect(govMultisig).enableRecoveryMode();
expect(await pool.inRecoveryMode()).to.be.true;
});
it('can exit via recovery mode', async () => {
const bptBalance = await pool.balanceOf(owner.address);
expect(bptBalance).to.gt(0);
const vaultUSDTBalanceBeforeExit = await usdt.balanceOf(vault.address);
const ownerUSDTBalanceBeforeExit = await usdt.balanceOf(owner.address);
const { tokens: registeredTokens } = await vault.getPoolTokens(poolId);
const userData = BasePoolEncoder.recoveryModeExit(bptBalance);
await vault.connect(owner).exitPool(poolId, owner.address, owner.address, {
assets: registeredTokens,
minAmountsOut: Array(registeredTokens.length).fill(0),
fromInternalBalance: false,
userData,
});
const remainingBalance = await pool.balanceOf(owner.address);
expect(remainingBalance).to.equal(0);
const vaultUSDTBalanceAfterExit = await usdt.balanceOf(vault.address);
const ownerUSDTBalanceAfterExit = await usdt.balanceOf(owner.address);
expect(vaultUSDTBalanceAfterExit).to.lt(vaultUSDTBalanceBeforeExit);
expect(ownerUSDTBalanceAfterExit).to.gt(ownerUSDTBalanceBeforeExit);
});
});
describe('factory disable', () => {
it('the factory can be disabled', async () => {
await authorizer.connect(govMultisig).grantRole(await actionId(factory, 'disable'), govMultisig.address);
await factory.connect(govMultisig).disable();
expect(await factory.isDisabled()).to.be.true;
await expect(
factory.create(
'CSP',
'CSBPT',
tokens,
amplificationParameter,
Array(tokens.length).fill(ZERO_ADDRESS),
Array(tokens.length).fill(0),
Array(tokens.length).fill(false),
swapFeePercentage,
owner.address
)
).to.be.revertedWith('BAL#211');
});
});
});