forked from compound-finance/comet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupply-test.ts
665 lines (577 loc) · 28.2 KB
/
supply-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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import { ethers, event, expect, exp, makeProtocol, portfolio, ReentryAttack, setTotalsBasic, wait, fastForward, defaultAssets } from './helpers';
import { EvilToken, EvilToken__factory, NonStandardFaucetFeeToken__factory, NonStandardFaucetFeeToken } from '../build/types';
describe('supplyTo', function () {
it('supplies base from sender if the asset is base', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [alice, bob] } = protocol;
const { USDC } = tokens;
const _i0 = await USDC.allocateTo(bob.address, 100e6);
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
const t0 = await comet.totalsBasic();
const p0 = await portfolio(protocol, alice.address);
const q0 = await portfolio(protocol, bob.address);
const _a0 = await wait(baseAsB.approve(comet.address, 100e6));
const s0 = await wait(cometAsB.supplyTo(alice.address, USDC.address, 100e6));
const t1 = await comet.totalsBasic();
const p1 = await portfolio(protocol, alice.address);
const q1 = await portfolio(protocol, bob.address);
expect(event(s0, 0)).to.be.deep.equal({
Transfer: {
from: bob.address,
to: comet.address,
amount: BigInt(100e6),
}
});
expect(event(s0, 1)).to.be.deep.equal({
Supply: {
from: bob.address,
dst: alice.address,
amount: BigInt(100e6),
}
});
expect(event(s0, 2)).to.be.deep.equal({
Transfer: {
from: ethers.constants.AddressZero,
to: alice.address,
amount: BigInt(100e6),
}
});
expect(p0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(p0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q0.external).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(p1.internal).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(p1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(t1.totalSupplyBase).to.be.equal(t0.totalSupplyBase.add(100e6));
expect(t1.totalBorrowBase).to.be.equal(t0.totalBorrowBase);
expect(Number(s0.receipt.gasUsed)).to.be.lessThan(124000);
});
it('supplies max base borrow balance (including accrued) from sender if the asset is base', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [alice, bob] } = protocol;
const { USDC } = tokens;
await USDC.allocateTo(bob.address, 100e6);
await setTotalsBasic(comet, {
totalSupplyBase: 100e6,
totalBorrowBase: 50e6, // non-zero borrow to accrue interest
});
await comet.setBasePrincipal(alice.address, -50e6);
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
// Fast forward to accrue some interest
await fastForward(86400);
await ethers.provider.send('evm_mine', []);
const t0 = await comet.totalsBasic();
const a0 = await portfolio(protocol, alice.address);
const b0 = await portfolio(protocol, bob.address);
await wait(baseAsB.approve(comet.address, 100e6));
const aliceAccruedBorrowBalance = (await comet.callStatic.borrowBalanceOf(alice.address)).toBigInt();
const s0 = await wait(cometAsB.supplyTo(alice.address, USDC.address, ethers.constants.MaxUint256));
const t1 = await comet.totalsBasic();
const a1 = await portfolio(protocol, alice.address);
const b1 = await portfolio(protocol, bob.address);
expect(s0.receipt['events'].length).to.be.equal(2);
expect(event(s0, 0)).to.be.deep.equal({
Transfer: {
from: bob.address,
to: comet.address,
amount: aliceAccruedBorrowBalance,
}
});
expect(event(s0, 1)).to.be.deep.equal({
Supply: {
from: bob.address,
dst: alice.address,
amount: aliceAccruedBorrowBalance,
}
});
expect(-aliceAccruedBorrowBalance).to.not.equal(exp(-50, 6));
expect(a0.internal).to.be.deep.equal({ USDC: -aliceAccruedBorrowBalance, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(a0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(b0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(b0.external).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(a1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(a1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(b1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(b1.external).to.be.deep.equal({ USDC: exp(100, 6) - aliceAccruedBorrowBalance, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(t1.totalSupplyBase).to.be.equal(t0.totalSupplyBase);
expect(t1.totalBorrowBase).to.be.equal(0n);
expect(Number(s0.receipt.gasUsed)).to.be.lessThan(120000);
});
it('supply max base should supply 0 if user has no borrow position', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [alice, bob] } = protocol;
const { USDC } = tokens;
await USDC.allocateTo(bob.address, 100e6);
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
const t0 = await comet.totalsBasic();
const a0 = await portfolio(protocol, alice.address);
const b0 = await portfolio(protocol, bob.address);
await wait(baseAsB.approve(comet.address, 100e6));
const s0 = await wait(cometAsB.supplyTo(alice.address, USDC.address, ethers.constants.MaxUint256));
const t1 = await comet.totalsBasic();
const a1 = await portfolio(protocol, alice.address);
const b1 = await portfolio(protocol, bob.address);
expect(s0.receipt['events'].length).to.be.equal(2);
expect(event(s0, 0)).to.be.deep.equal({
Transfer: {
from: bob.address,
to: comet.address,
amount: 0n,
}
});
expect(event(s0, 1)).to.be.deep.equal({
Supply: {
from: bob.address,
dst: alice.address,
amount: 0n,
}
});
expect(a0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(a0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(b0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(b0.external).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(a1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(a1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(b1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(b1.external).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(t1.totalSupplyBase).to.be.equal(t0.totalSupplyBase);
expect(t1.totalBorrowBase).to.be.equal(t0.totalBorrowBase);
expect(Number(s0.receipt.gasUsed)).to.be.lessThan(120000);
});
it('does not emit Transfer for 0 mint', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [alice, bob] } = protocol;
const { USDC } = tokens;
await USDC.allocateTo(bob.address, 100e6);
await comet.setBasePrincipal(alice.address, -100e6);
await setTotalsBasic(comet, {
totalBorrowBase: 100e6,
});
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
const _a0 = await wait(baseAsB.approve(comet.address, 100e6));
const s0 = await wait(cometAsB.supplyTo(alice.address, USDC.address, 100e6));
expect(s0.receipt['events'].length).to.be.equal(2);
expect(event(s0, 0)).to.be.deep.equal({
Transfer: {
from: bob.address,
to: comet.address,
amount: BigInt(100e6),
}
});
expect(event(s0, 1)).to.be.deep.equal({
Supply: {
from: bob.address,
dst: alice.address,
amount: BigInt(100e6),
}
});
});
// This is an edge-case that can occur when a user supplies 0 base.
// When `amount=0` in `supplyBase`, `dstPrincipalNew = principalValue(presentValue(dstPrincipal))`
// In some cases, `dstPrincipalNew` can actually be less than `dstPrincipal` due to the fact
// that the principal value and present value functions round down. This breaks our assumption
// in `repayAndSupplyAmount` that `newPrincipal >= oldPrincipal` MUST be true. In the old code,
// this would cause `supplyAmount` to be an extremely large number (uint104(-1)), which would
// later cause an overflow during an addition operation. The new code now explicitly checks
// this assumption and sets both `repayAmount` and `supplyAmount` to 0 if the assumption is
// violated.
it('supplies 0 and does not revert when dstPrincipalNew < dstPrincipal', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [alice] } = protocol;
const { USDC } = tokens;
await comet.setBasePrincipal(alice.address, 99999992291226);
await setTotalsBasic(comet, {
totalSupplyBase: 699999944771920,
baseSupplyIndex: 1000000131467072,
});
const s0 = await wait(comet.connect(alice).supply(USDC.address, 0));
expect(s0.receipt['events'].length).to.be.equal(2);
expect(event(s0, 0)).to.be.deep.equal({
Transfer: {
from: alice.address,
to: comet.address,
amount: BigInt(0),
}
});
expect(event(s0, 1)).to.be.deep.equal({
Supply: {
from: alice.address,
dst: alice.address,
amount: BigInt(0),
}
});
});
it('user supply is same as total supply', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [bob] } = protocol;
const { USDC } = tokens;
await setTotalsBasic(comet, {
totalSupplyBase: 100,
baseSupplyIndex: exp(1.085, 15),
});
const _i0 = await USDC.allocateTo(bob.address, 10);
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
const t0 = await comet.totalsBasic();
const p0 = await portfolio(protocol, bob.address);
const _a0 = await wait(baseAsB.approve(comet.address, 10));
const s0 = await wait(cometAsB.supplyTo(bob.address, USDC.address, 10));
const t1 = await comet.totalsBasic();
const p1 = await portfolio(protocol, bob.address);
expect(p0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(p0.external).to.be.deep.equal({ USDC: 10n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(p1.internal).to.be.deep.equal({ USDC: 9n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(p1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(t1.totalSupplyBase).to.be.equal(109);
expect(t1.totalBorrowBase).to.be.equal(t0.totalBorrowBase);
expect(Number(s0.receipt.gasUsed)).to.be.lessThan(124000);
});
it('supplies collateral from sender if the asset is collateral', async () => {
const protocol = await makeProtocol();
const { comet, tokens, users: [alice, bob] } = protocol;
const { COMP } = tokens;
const _i0 = await COMP.allocateTo(bob.address, 8e8);
const baseAsB = COMP.connect(bob);
const cometAsB = comet.connect(bob);
const t0 = await comet.totalsCollateral(COMP.address);
const p0 = await portfolio(protocol, alice.address);
const q0 = await portfolio(protocol, bob.address);
const _a0 = await wait(baseAsB.approve(comet.address, 8e8));
const s0 = await wait(cometAsB.supplyTo(alice.address, COMP.address, 8e8));
const t1 = await comet.totalsCollateral(COMP.address);
const p1 = await portfolio(protocol, alice.address);
const q1 = await portfolio(protocol, bob.address);
expect(event(s0, 0)).to.be.deep.equal({
Transfer: {
from: bob.address,
to: comet.address,
amount: BigInt(8e8),
}
});
expect(event(s0, 1)).to.be.deep.equal({
SupplyCollateral: {
from: bob.address,
dst: alice.address,
asset: COMP.address,
amount: BigInt(8e8),
}
});
expect(p0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(p0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q0.external).to.be.deep.equal({ USDC: 0n, COMP: exp(8, 8), WETH: 0n, WBTC: 0n });
expect(p1.internal).to.be.deep.equal({ USDC: 0n, COMP: exp(8, 8), WETH: 0n, WBTC: 0n });
expect(p1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(t1.totalSupplyAsset).to.be.equal(t0.totalSupplyAsset.add(8e8));
expect(Number(s0.receipt.gasUsed)).to.be.lessThan(153000);
});
it('calculates base principal correctly', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [alice, bob] } = protocol;
const { USDC } = tokens;
await USDC.allocateTo(bob.address, 100e6);
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
const totals0 = await setTotalsBasic(comet, {
baseSupplyIndex: 2e15,
});
const alice0 = await portfolio(protocol, alice.address);
const bob0 = await portfolio(protocol, bob.address);
const aliceBasic0 = await comet.userBasic(alice.address);
await wait(baseAsB.approve(comet.address, 100e6));
await wait(cometAsB.supplyTo(alice.address, USDC.address, 100e6));
const t1 = await comet.totalsBasic();
const alice1 = await portfolio(protocol, alice.address);
const bob1 = await portfolio(protocol, bob.address);
const aliceBasic1 = await comet.userBasic(alice.address);
expect(alice0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(alice0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(bob0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(bob0.external).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(alice1.internal).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(alice1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(bob1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(bob1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(t1.totalSupplyBase).to.be.equal(totals0.totalSupplyBase.add(50e6)); // 100e6 in present value
expect(t1.totalBorrowBase).to.be.equal(totals0.totalBorrowBase);
expect(aliceBasic1.principal).to.be.equal(aliceBasic0.principal.add(50e6)); // 100e6 in present value
});
it('reverts if supplying collateral exceeds the supply cap', async () => {
const protocol = await makeProtocol({
assets: {
COMP: { initial: 1e7, decimals: 18, supplyCap: 0 },
USDC: { initial: 1e6, decimals: 6 },
}
});
const { comet, tokens, users: [alice, bob] } = protocol;
const { COMP } = tokens;
const _i0 = await COMP.allocateTo(bob.address, 8e8);
const baseAsB = COMP.connect(bob);
const cometAsB = comet.connect(bob);
const _a0 = await wait(baseAsB.approve(comet.address, 8e8));
await expect(cometAsB.supplyTo(alice.address, COMP.address, 8e8)).to.be.revertedWith("custom error 'SupplyCapExceeded()'");
});
it('reverts if the asset is neither collateral nor base', async () => {
const protocol = await makeProtocol();
const { comet, users: [alice, bob], unsupportedToken: USUP } = protocol;
const _i0 = await USUP.allocateTo(bob.address, 1);
const baseAsB = USUP.connect(bob);
const cometAsB = comet.connect(bob);
const _a0 = await wait(baseAsB.approve(comet.address, 1));
await expect(cometAsB.supplyTo(alice.address, USUP.address, 1)).to.be.reverted;
});
it('reverts if supply is paused', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, pauseGuardian, users: [alice, bob] } = protocol;
const { USDC } = tokens;
await USDC.allocateTo(bob.address, 1);
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
// Pause supply
await wait(comet.connect(pauseGuardian).pause(true, false, false, false, false));
expect(await comet.isSupplyPaused()).to.be.true;
await wait(baseAsB.approve(comet.address, 1));
await expect(cometAsB.supplyTo(alice.address, USDC.address, 1)).to.be.revertedWith("custom error 'Paused()'");
});
it('reverts if supply max for a collateral asset', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [alice, bob] } = protocol;
const { COMP } = tokens;
await COMP.allocateTo(bob.address, 100e6);
const baseAsB = COMP.connect(bob);
const cometAsB = comet.connect(bob);
await wait(baseAsB.approve(COMP.address, 100e6));
await expect(cometAsB.supplyTo(alice.address, COMP.address, ethers.constants.MaxUint256)).to.be.revertedWith("custom error 'InvalidUInt128()'");
});
it('supplies base the correct amount in a fee-like situation', async () => {
const assets = defaultAssets();
// Add USDT to assets on top of default assets
assets['USDT'] = {
initial: 1e6,
decimals: 6,
factory: (await ethers.getContractFactory('NonStandardFaucetFeeToken')) as NonStandardFaucetFeeToken__factory,
};
const protocol = await makeProtocol({ base: 'USDT', assets: assets });
const { comet, tokens, users: [alice, bob] } = protocol;
const { USDT } = tokens;
// Set fee to 0.1%
await (USDT as NonStandardFaucetFeeToken).setParams(10, 10);
const _i0 = await USDT.allocateTo(bob.address, 1000e6);
const baseAsB = USDT.connect(bob);
const cometAsB = comet.connect(bob);
const t0 = await comet.totalsBasic();
const p0 = await portfolio(protocol, alice.address);
const q0 = await portfolio(protocol, bob.address);
const _a0 = await wait(baseAsB.approve(comet.address, 1000e6));
const s0 = await wait(cometAsB.supplyTo(alice.address, USDT.address, 1000e6));
const t1 = await comet.totalsBasic();
const p1 = await portfolio(protocol, alice.address);
const q1 = await portfolio(protocol, bob.address);
expect(event(s0, 0)).to.be.deep.equal({
Transfer: {
from: bob.address,
to: comet.address,
amount: BigInt(999e6),
}
});
expect(event(s0, 1)).to.be.deep.equal({
Supply: {
from: bob.address,
dst: alice.address,
amount: BigInt(999e6),
}
});
expect(event(s0, 2)).to.be.deep.equal({
Transfer: {
from: ethers.constants.AddressZero,
to: alice.address,
amount: BigInt(999e6),
}
});
expect(p0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, USDT: 0n });
expect(p0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, USDT: 0n });
expect(q0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, USDT: 0n });
expect(q0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, USDT: exp(1000, 6) });
expect(p1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, USDT: exp(999, 6) });
expect(p1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, USDT: 0n });
expect(q1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, USDT: 0n });
expect(q1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, USDT: 0n });
expect(t1.totalSupplyBase).to.be.equal(t0.totalSupplyBase.add(999e6));
expect(t1.totalBorrowBase).to.be.equal(t0.totalBorrowBase);
// Fee Token logics will cost a bit more gas than standard ERC20 token with no fee calculation
expect(Number(s0.receipt.gasUsed)).to.be.lessThan(151000);
});
it('supplies collateral the correct amount in a fee-like situation', async () => {
const assets = defaultAssets();
// Add FeeToken Collateral to assets on top of default assets
assets['FeeToken'] = {
initial: 1e8,
decimals: 18,
factory: (await ethers.getContractFactory('NonStandardFaucetFeeToken')) as NonStandardFaucetFeeToken__factory,
};
const protocol = await makeProtocol({ base: 'USDC', assets: assets });
const { comet, tokens, users: [alice, bob] } = protocol;
const { FeeToken } = tokens;
// Set fee to 0.1%
await (FeeToken as NonStandardFaucetFeeToken).setParams(10, 10);
const _i0 = await FeeToken.allocateTo(bob.address, 2000e8);
const baseAsB = FeeToken.connect(bob);
const cometAsB = comet.connect(bob);
const t0 = await comet.totalsCollateral(FeeToken.address);
const p0 = await portfolio(protocol, alice.address);
const q0 = await portfolio(protocol, bob.address);
const _a0 = await wait(baseAsB.approve(comet.address, 2000e8));
const s0 = await wait(cometAsB.supplyTo(alice.address, FeeToken.address, 2000e8));
const t1 = await comet.totalsCollateral(FeeToken.address);
const p1 = await portfolio(protocol, alice.address);
const q1 = await portfolio(protocol, bob.address);
expect(event(s0, 0)).to.be.deep.equal({
Transfer: {
from: bob.address,
to: comet.address,
amount: BigInt(1998e8),
}
});
expect(event(s0, 1)).to.be.deep.equal({
SupplyCollateral: {
from: bob.address,
dst: alice.address,
asset: FeeToken.address,
amount: BigInt(1998e8),
}
});
expect(p0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, FeeToken: 0n });
expect(p0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, FeeToken: 0n });
expect(q0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, FeeToken: 0n });
expect(q0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, FeeToken: exp(2000, 8) });
expect(p1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, FeeToken: exp(1998, 8) });
expect(p1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, FeeToken: 0n });
expect(q1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, FeeToken: 0n });
expect(q1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n, FeeToken: 0n });
expect(t1.totalSupplyAsset).to.be.equal(t0.totalSupplyAsset.add(1998e8));
// Fee Token logics will cost a bit more gas than standard ERC20 token with no fee calculation
expect(Number(s0.receipt.gasUsed)).to.be.lessThan(186000);
});
it('blocks reentrancy from exceeding the supply cap', async () => {
const { comet, tokens, users: [alice, bob] } = await makeProtocol({
assets: {
USDC: {
decimals: 6
},
EVIL: {
decimals: 6,
initialPrice: 2,
factory: await ethers.getContractFactory('EvilToken') as EvilToken__factory,
supplyCap: 100e6
}
}
});
const { EVIL } = <{ EVIL: EvilToken }>tokens;
const attack = Object.assign({}, await EVIL.getAttack(), {
attackType: ReentryAttack.SupplyFrom,
source: alice.address,
destination: bob.address,
asset: EVIL.address,
amount: 75e6,
maxCalls: 1
});
await EVIL.setAttack(attack);
await comet.connect(alice).allow(EVIL.address, true);
await wait(EVIL.connect(alice).approve(comet.address, 75e6));
await EVIL.allocateTo(alice.address, 75e6);
await expect(
comet.connect(alice).supplyTo(bob.address, EVIL.address, 75e6)
).to.be.revertedWithCustomError(comet, 'ReentrantCallBlocked');
});
});
describe('supply', function () {
it('supplies to sender by default', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, users: [bob] } = protocol;
const { USDC } = tokens;
const _i0 = await USDC.allocateTo(bob.address, 100e6);
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
const _t0 = await comet.totalsBasic();
const q0 = await portfolio(protocol, bob.address);
const _a0 = await wait(baseAsB.approve(comet.address, 100e6));
const _s0 = await wait(cometAsB.supply(USDC.address, 100e6));
const _t1 = await comet.totalsBasic();
const q1 = await portfolio(protocol, bob.address);
expect(q0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q0.external).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q1.internal).to.be.deep.equal({ USDC: exp(100, 6), COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
});
it('reverts if supply is paused', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const { comet, tokens, pauseGuardian, users: [bob] } = protocol;
const { USDC } = tokens;
await USDC.allocateTo(bob.address, 100e6);
const baseAsB = USDC.connect(bob);
const cometAsB = comet.connect(bob);
// Pause supply
await wait(comet.connect(pauseGuardian).pause(true, false, false, false, false));
expect(await comet.isSupplyPaused()).to.be.true;
await wait(baseAsB.approve(comet.address, 100e6));
await expect(cometAsB.supply(USDC.address, 100e6)).to.be.revertedWith("custom error 'Paused()'");
});
});
describe('supplyFrom', function () {
it('supplies from `from` if specified and sender has permission', async () => {
const protocol = await makeProtocol();
const { comet, tokens, users: [alice, bob, charlie] } = protocol;
const { COMP } = tokens;
const _i0 = await COMP.allocateTo(bob.address, 7);
const baseAsB = COMP.connect(bob);
const cometAsB = comet.connect(bob);
const cometAsC = comet.connect(charlie);
const _a0 = await wait(baseAsB.approve(comet.address, 7));
const _a1 = await wait(cometAsB.allow(charlie.address, true));
const p0 = await portfolio(protocol, alice.address);
const q0 = await portfolio(protocol, bob.address);
const _s0 = await wait(cometAsC.supplyFrom(bob.address, alice.address, COMP.address, 7));
const p1 = await portfolio(protocol, alice.address);
const q1 = await portfolio(protocol, bob.address);
expect(p0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(p0.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q0.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q0.external).to.be.deep.equal({ USDC: 0n, COMP: 7n, WETH: 0n, WBTC: 0n });
expect(p1.internal).to.be.deep.equal({ USDC: 0n, COMP: 7n, WETH: 0n, WBTC: 0n });
expect(p1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q1.internal).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
expect(q1.external).to.be.deep.equal({ USDC: 0n, COMP: 0n, WETH: 0n, WBTC: 0n });
});
it('reverts if `from` is specified and sender does not have permission', async () => {
const protocol = await makeProtocol();
const { comet, tokens, users: [alice, bob, charlie] } = protocol;
const { COMP } = tokens;
const _i0 = await COMP.allocateTo(bob.address, 7);
const cometAsC = comet.connect(charlie);
await expect(cometAsC.supplyFrom(bob.address, alice.address, COMP.address, 7))
.to.be.revertedWith("custom error 'Unauthorized()'");
});
it('reverts if supply is paused', async () => {
const protocol = await makeProtocol();
const { comet, tokens, pauseGuardian, users: [alice, bob, charlie] } = protocol;
const { COMP } = tokens;
await COMP.allocateTo(bob.address, 7);
const baseAsB = COMP.connect(bob);
const cometAsB = comet.connect(bob);
const cometAsC = comet.connect(charlie);
// Pause supply
await wait(comet.connect(pauseGuardian).pause(true, false, false, false, false));
expect(await comet.isSupplyPaused()).to.be.true;
await wait(baseAsB.approve(comet.address, 7));
await wait(cometAsB.allow(charlie.address, true));
await expect(cometAsC.supplyFrom(bob.address, alice.address, COMP.address, 7)).to.be.revertedWith("custom error 'Paused()'");
});
});