forked from compound-finance/comet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuy-collateral-test.ts
430 lines (380 loc) · 15.4 KB
/
buy-collateral-test.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import { EvilToken, EvilToken__factory, FaucetToken } from '../build/types';
import { ethers, event, expect, exp, getBlock, makeProtocol, portfolio, ReentryAttack, wait } from './helpers';
describe('buyCollateral', function () {
it('allows buying collateral when reserves < target reserves', async () => {
const protocol = await makeProtocol({
base: 'USDC',
storeFrontPriceFactor: exp(0.5, 18),
targetReserves: 100,
assets: {
USDC: {
initial: 1e6,
decimals: 6,
initialPrice: 1,
},
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1,
liquidationFactor: exp(0.8, 18),
},
}
});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Reserves are at 0 wei
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 100e6);
await COMP.allocateTo(comet.address, exp(60, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(60, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(60, 18)));
const r0 = await comet.getReserves();
const p0 = await portfolio(protocol, alice.address);
await wait(baseAsA.approve(comet.address, exp(50, 6)));
// Alice buys 50e6 wei USDC worth of COMP
const txn = await wait(cometAsA.buyCollateral(COMP.address, exp(50, 18), 50e6, alice.address));
const p1 = await portfolio(protocol, alice.address);
const r1 = await comet.getReserves();
expect(r0).to.be.equal(0n);
expect(r0).to.be.lt(await comet.targetReserves());
expect(p0.internal).to.be.deep.equal({USDC: 0n, COMP: 0n});
expect(p0.external).to.be.deep.equal({USDC: exp(100, 6), COMP: 0n});
expect(p1.internal).to.be.deep.equal({USDC: 0n, COMP: 0n});
expect(p1.external).to.be.deep.equal({USDC: exp(50, 6), COMP: 55555555555555555555n});
expect(r1).to.be.equal(exp(50, 6));
expect(event(txn, 0)).to.be.deep.equal({
Transfer: {
from: alice.address,
to: comet.address,
amount: exp(50, 6),
}
});
expect(event(txn, 1)).to.be.deep.equal({
Transfer: {
from: comet.address,
to: alice.address,
amount: 55555555555555555555n,
}
});
expect(event(txn, 2)).to.be.deep.equal({
WithdrawCollateral: {
src: comet.address,
to: alice.address,
asset: COMP.address,
amount: 55555555555555555555n,
}
});
expect(event(txn, 3)).to.be.deep.equal({
BuyCollateral: {
buyer: alice.address,
asset: COMP.address,
baseAmount: exp(50, 6),
collateralAmount: 55555555555555555555n,
}
});
});
it('allows buying collateral when reserves < 0 and target reserves is 0', async () => {
const protocol = await makeProtocol({
base: 'USDC',
storeFrontPriceFactor: exp(0.5, 18),
targetReserves: 0,
assets: {
USDC: {
initial: 1e6,
decimals: 6,
initialPrice: 1,
},
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1,
liquidationFactor: exp(0.8, 18),
},
}
});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Set reserves to -100 wei
let t0 = await comet.totalsBasic();
t0 = Object.assign({}, t0, {
totalSupplyBase: 100e6,
totalBorrowBase: 0n,
});
await wait(comet.setTotalsBasic(t0));
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 100e6);
await COMP.allocateTo(comet.address, exp(60, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(60, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(60, 18)));
const r0 = await comet.getReserves();
const p0 = await portfolio(protocol, alice.address);
await wait(baseAsA.approve(comet.address, exp(50, 6)));
// Alice buys 50e6 wei USDC worth of COMP
const txn = await wait(cometAsA.buyCollateral(COMP.address, exp(50, 18), 50e6, alice.address));
const p1 = await portfolio(protocol, alice.address);
const r1 = await comet.getReserves();
expect(r0).to.be.equal(-100e6);
expect(r0).to.be.lt(await comet.targetReserves());
expect(p0.internal).to.be.deep.equal({USDC: 0n, COMP: 0n});
expect(p0.external).to.be.deep.equal({USDC: exp(100, 6), COMP: 0n});
expect(p1.internal).to.be.deep.equal({USDC: 0n, COMP: 0n});
expect(p1.external).to.be.deep.equal({USDC: exp(50, 6), COMP: 55555555555555555555n});
expect(r1).to.be.equal(-50e6);
// Only checking the BuyCollateral event in this test case
expect(event(txn, 3)).to.be.deep.equal({
BuyCollateral: {
buyer: alice.address,
asset: COMP.address,
baseAmount: exp(50, 6),
collateralAmount: 55555555555555555555n,
}
});
});
it('reverts if reserves are above target reserves', async () => {
const protocol = await makeProtocol({base: 'USDC', targetReserves: 0});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Set reserves to 100e6 wei
await USDC.allocateTo(comet.address, 100e6);
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 100e6);
await COMP.allocateTo(comet.address, exp(50, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(50, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(50, 18)));
const r0 = await comet.getReserves();
expect(r0).to.be.equal(100e6);
expect(r0).to.be.gt(await comet.targetReserves());
// Alice buys 50e18 wei COMP for 50e6 wei USDC
await wait(baseAsA.approve(comet.address, exp(50, 6)));
await expect(cometAsA.buyCollateral(COMP.address, exp(50, 18), 50e6, alice.address)).to.be.revertedWith("custom error 'NotForSale()'");
});
it('reverts if slippage is too high', async () => {
const protocol = await makeProtocol({base: 'USDC', targetReserves: 100,
assets: {
USDC: {
initial: 1e6,
decimals: 6,
initialPrice: 1,
},
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1,
},
}
});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Reserves are at 0 wei
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 100e6);
await COMP.allocateTo(comet.address, exp(50, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(50, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(50, 18)));
// Alice tries to buy 100e18 wei COMP for 50e6 wei USDC
await wait(baseAsA.approve(comet.address, exp(50, 6)));
await expect(cometAsA.buyCollateral(COMP.address, exp(100, 18), 50e6, alice.address)).to.be.revertedWith("custom error 'TooMuchSlippage()'");
});
it('reverts if not enough collateral to buy', async () => {
const protocol = await makeProtocol({
base: 'USDC',
targetReserves: 100,
assets: {
USDC: {
initial: 1e6,
decimals: 6,
initialPrice: 1,
},
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1,
},
}
});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Reserves are at 0 wei
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 200e6);
await COMP.allocateTo(comet.address, exp(50, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(50, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(50, 18)));
// Alice tries to buy 200e18 wei COMP for 200e6 wei USDC
await wait(baseAsA.approve(comet.address, exp(200, 6)));
await expect(cometAsA.buyCollateral(COMP.address, exp(200, 18), 200e6, alice.address)).to.be.revertedWith('reverted with panic code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)');
});
it('reverts if buy is paused', async () => {
const protocol = await makeProtocol({base: 'USDC', targetReserves: 0});
const { comet, tokens, pauseGuardian, users: [alice] } = protocol;
const { COMP } = tokens;
const cometAsA = comet.connect(alice);
// Pause buy collateral
await wait(comet.connect(pauseGuardian).pause(false, false, false, false, true));
expect(await comet.isBuyPaused()).to.be.true;
await expect(cometAsA.buyCollateral(COMP.address, exp(50, 18), 50e6, alice.address)).to.be.revertedWith("custom error 'Paused()'");
});
it.skip('buys the correct amount in a fee-like situation', async () => {
// Note: fee-tokens are not currently supported (for efficiency) and should not be added
});
describe('reentrancy', function() {
it('is not broken by reentrancy supply ', async () => {
const wethArgs = {
initial: 1e4,
decimals: 18,
initialPrice: 3000,
};
const baseTokenArgs = {
decimals: 6,
initial: 1e6,
initialPrice: 1,
};
// 1. normal scenario, USDC base
const normalProtocol = await makeProtocol({
base: 'USDC',
assets: {
USDC: baseTokenArgs,
WETH: wethArgs,
},
targetReserves: 1
});
const {
comet: normalComet,
tokens: normalTokens,
users: [normalAlice, normalBob, evilAlice, evilBob] // addresses are constant
} = normalProtocol;
const { USDC: normalUSDC, WETH: normalWETH } = normalTokens;
// 2. malicious scenario, EVIL token is base
const evilProtocol = await makeProtocol({
base: 'EVIL',
assets: {
EVIL: {
...baseTokenArgs,
factory: await ethers.getContractFactory('EvilToken') as EvilToken__factory,
},
WETH: wethArgs,
},
targetReserves: 1
});
const {
comet: evilComet,
tokens: evilTokens,
} = evilProtocol;
const { WETH: evilWETH, EVIL } = <{WETH: FaucetToken, EVIL: EvilToken}>evilTokens;
// add attack to EVIL token
const attack = Object.assign({}, await EVIL.getAttack(), {
attackType: ReentryAttack.SupplyFrom,
source: evilAlice.address,
destination: evilBob.address,
asset: EVIL.address,
amount: 1e6,
maxCalls: 1
});
await EVIL.setAttack(attack);
// allocate tokens
await normalWETH.allocateTo(normalComet.address, exp(100, 18));
await normalUSDC.allocateTo(normalAlice.address, exp(5000, 6));
// allocate tokens (evil)
await evilWETH.allocateTo(evilComet.address, exp(100, 18));
await EVIL.allocateTo(evilAlice.address, exp(5000, 6));
// set collateral balance
const t0 = Object.assign({}, await normalComet.totalsCollateral(normalWETH.address), {
totalSupplyAsset: exp(100, 18),
});
await normalComet.setTotalsCollateral(normalWETH.address, t0);
const t1 = Object.assign({}, await evilComet.totalsCollateral(evilWETH.address), {
totalSupplyAsset: exp(100, 18),
});
await evilComet.setTotalsCollateral(evilWETH.address, t1);
// ensure both Comets have the same lastAccrualTime
const start = (await getBlock()).timestamp;
let tb0 = await normalComet.totalsBasic();
tb0 = Object.assign({}, tb0, {
lastAccrualTime: start
});
await normalComet.setTotalsBasic(tb0);
let tb1 = await evilComet.totalsBasic();
tb1 = Object.assign({}, tb1, {
lastAccrualTime: start
});
await evilComet.setTotalsBasic(tb1);
// 5 WETH have been absorbed
await normalComet.setCollateralBalance(
normalComet.address,
normalWETH.address,
exp(5, 18)
);
await evilComet.setCollateralBalance(
evilComet.address,
evilWETH.address,
exp(5, 18)
);
// approve Comet to move funds
await normalUSDC.connect(normalAlice).approve(normalComet.address, exp(5000, 6));
await EVIL.connect(evilAlice).approve(EVIL.address, exp(5000, 6));
// perform the supplies for each protocol in the same block, so that the
// same amount of time elapses for each when calculating interest
await ethers.provider.send('evm_setAutomine', [false]);
// call supply
await normalComet
.connect(normalAlice)
.supplyFrom(
normalAlice.address,
normalBob.address,
normalUSDC.address,
1e6
);
// call buyCollateral
await normalComet
.connect(normalAlice)
.buyCollateral(
normalWETH.address,
exp(.5, 18),
exp(3000, 6),
normalAlice.address
);
// authorize EVIL, since callback will originate from EVIL token address
await evilComet.connect(evilAlice).allow(EVIL.address, true);
// call buyCollateral; supplyFrom is called in in callback
await evilComet
.connect(evilAlice)
.buyCollateral(
evilWETH.address,
exp(.5, 18),
exp(3000, 6),
evilAlice.address
);
// !important; reenable automine
await ethers.provider.send('evm_mine', [start + 1000]);
await ethers.provider.send('evm_setAutomine', [true]);
const normalTotalsBasic = await normalComet.totalsBasic();
const normalTotalsCollateral = await normalComet.totalsCollateral(normalWETH.address);
const evilTotalsBasic = await evilComet.totalsBasic();
const evilTotalsCollateral = await evilComet.totalsCollateral(evilWETH.address);
expect(normalTotalsBasic.baseSupplyIndex).to.equal(evilTotalsBasic.baseSupplyIndex);
expect(normalTotalsBasic.baseBorrowIndex).to.equal(evilTotalsBasic.baseBorrowIndex);
expect(normalTotalsBasic.trackingSupplyIndex).to.equal(evilTotalsBasic.trackingSupplyIndex);
expect(normalTotalsBasic.trackingBorrowIndex).to.equal(evilTotalsBasic.trackingBorrowIndex);
expect(normalTotalsBasic.totalSupplyBase).to.equal(evilTotalsBasic.totalSupplyBase);
expect(normalTotalsBasic.totalBorrowBase).to.equal(evilTotalsBasic.totalBorrowBase);
expect(normalTotalsCollateral.totalSupplyAsset).to.eq(evilTotalsCollateral.totalSupplyAsset);
const normalAlicePortfolio = await portfolio(normalProtocol, normalAlice.address);
const evilAlicePortfolio = await portfolio(evilProtocol, evilAlice.address);
expect(normalAlicePortfolio.internal.USDC).to.deep.equal(evilAlicePortfolio.internal.EVIL);
expect(normalAlicePortfolio.internal.WETH).to.deep.equal(evilAlicePortfolio.internal.WETH);
const normalBobPortfolio = await portfolio(normalProtocol, normalBob.address);
const evilBobPortfolio = await portfolio(evilProtocol, evilBob.address);
expect(normalBobPortfolio.internal.USDC).to.equal(evilBobPortfolio.internal.EVIL);
});
});
});