-
Notifications
You must be signed in to change notification settings - Fork 7
/
TwabController.t.sol
1445 lines (1183 loc) · 47.4 KB
/
TwabController.t.sol
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "forge-std/Test.sol";
import { ERC20 } from "openzeppelin/token/ERC20/ERC20.sol";
import { TwabController, SameDelegateAlreadySet, CannotTransferToSponsorshipAddress, MINIMUM_PERIOD_LENGTH, PeriodLengthTooShort, SPONSORSHIP_ADDRESS, TransferToZeroAddress, PeriodOffsetInFuture } from "../src/TwabController.sol";
import { TwabLib, TimestampNotFinalized, MAX_CARDINALITY, InvalidTimeRange } from "../src/libraries/TwabLib.sol";
import { TwabLib } from "../src/libraries/TwabLib.sol";
import { ObservationLib } from "../src/libraries/ObservationLib.sol";
import { BaseTest } from "./utils/BaseTest.sol";
contract TwabControllerTest is BaseTest {
TwabController public twabController;
address public mockVault = address(0x1234);
ERC20 public token;
uint32 public constant PERIOD_LENGTH = 1 days;
uint32 public constant PERIOD_OFFSET = 10 days;
event IncreasedBalance(
address indexed vault,
address indexed user,
uint96 amount,
uint96 delegateAmount
);
event DecreasedBalance(
address indexed vault,
address indexed user,
uint96 amount,
uint96 delegateAmount
);
event ObservationRecorded(
address indexed vault,
address indexed user,
uint96 balance,
uint96 delegateBalance,
bool isNew,
ObservationLib.Observation observation
);
event Delegated(address indexed vault, address indexed delegator, address indexed delegate);
event IncreasedTotalSupply(address indexed vault, uint96 amount, uint96 delegateAmount);
event DecreasedTotalSupply(address indexed vault, uint96 amount, uint96 delegateAmount);
event TotalSupplyObservationRecorded(
address indexed vault,
uint96 balance,
uint96 delegateBalance,
bool isNew,
ObservationLib.Observation observation
);
function setUp() public override {
super.setUp();
// Ensure time is >= the hardcoded offset.
vm.warp(PERIOD_OFFSET);
twabController = new TwabController(PERIOD_LENGTH, PERIOD_OFFSET);
token = new ERC20("Test", "TST");
}
function testConstructor_periodOffsetInFuture() public {
// After current timestamp
vm.expectRevert(abi.encodeWithSelector(PeriodOffsetInFuture.selector, block.timestamp + 1));
new TwabController(PERIOD_LENGTH, uint32(block.timestamp + 1));
}
function testConstructor_PeriodLengthTooShort() public {
vm.expectRevert(abi.encodeWithSelector(PeriodLengthTooShort.selector));
new TwabController(MINIMUM_PERIOD_LENGTH - 1, PERIOD_OFFSET);
}
function testGetAccount() external {
TwabLib.Account memory account = twabController.getAccount(mockVault, alice);
TwabLib.AccountDetails memory accountDetails = account.details;
assertEq(accountDetails.balance, 0);
assertEq(accountDetails.delegateBalance, 0);
assertEq(accountDetails.cardinality, 0);
for (uint256 i = 0; i < MAX_CARDINALITY; i++) {
assertEq(account.observations[i].cumulativeBalance, 0);
assertEq(account.observations[i].timestamp, 0);
}
}
function testGetTotalSupplyAccount() external {
TwabLib.Account memory account = twabController.getTotalSupplyAccount(mockVault);
TwabLib.AccountDetails memory accountDetails = account.details;
assertEq(accountDetails.balance, 0);
assertEq(accountDetails.delegateBalance, 0);
assertEq(accountDetails.cardinality, 0);
for (uint256 i = 0; i < MAX_CARDINALITY; i++) {
assertEq(account.observations[i].cumulativeBalance, 0);
assertEq(account.observations[i].timestamp, 0);
}
}
function testPeriodEndOnOrAfter_beforeOffset() public {
assertEq(twabController.periodEndOnOrAfter(PERIOD_OFFSET - 1), PERIOD_OFFSET);
}
function testPeriodEndOnOrAfter_atOffset() public {
assertEq(twabController.periodEndOnOrAfter(PERIOD_OFFSET), PERIOD_OFFSET);
}
function testPeriodEndOnOrAfter_midPeriod() public {
assertEq(
twabController.periodEndOnOrAfter(PERIOD_OFFSET + PERIOD_LENGTH / 2),
PERIOD_OFFSET + PERIOD_LENGTH
);
}
function testPeriodEndOnOrAfter_firstPeriod() public {
assertEq(
twabController.periodEndOnOrAfter(PERIOD_OFFSET + PERIOD_LENGTH),
PERIOD_OFFSET + PERIOD_LENGTH
);
}
function testPeriodEndOnOrAfter_secondPeriod() public {
assertEq(
twabController.periodEndOnOrAfter(PERIOD_OFFSET + PERIOD_LENGTH * 2),
PERIOD_OFFSET + PERIOD_LENGTH * 2
);
}
function testBalanceOf() external {
assertEq(twabController.balanceOf(mockVault, alice), 0);
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
vm.stopPrank();
}
function testIsShutdownAt() public {
assertEq(twabController.isShutdownAt(PERIOD_OFFSET), false, "at beginning");
assertEq(twabController.isShutdownAt(PERIOD_OFFSET + PERIOD_LENGTH), false, "after first period");
assertEq(twabController.isShutdownAt((type(uint32).max/PERIOD_LENGTH)*PERIOD_LENGTH + uint256(PERIOD_OFFSET)), false, "at end of last period");
assertEq(twabController.isShutdownAt(type(uint32).max + uint256(PERIOD_OFFSET)), true, "at end");
assertEq(twabController.isShutdownAt(type(uint32).max + uint256(PERIOD_OFFSET) + 1), true, "after end");
}
function testLastObservationAt() public {
assertEq(twabController.lastObservationAt(), uint256(PERIOD_OFFSET) + (type(uint32).max/PERIOD_LENGTH)*PERIOD_LENGTH);
}
function testGetBalanceAt_beforeHistoryStarted() public {
// ensure times are finalized
vm.warp(PERIOD_OFFSET + PERIOD_LENGTH);
// Before history started
assertEq(twabController.getBalanceAt(mockVault, alice, 0), 0);
// At history start
assertEq(twabController.getBalanceAt(mockVault, alice, PERIOD_OFFSET), 0);
}
function testGetBalanceAt_safeBalance_success() public {
vm.startPrank(mockVault);
// Mint at history start
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
// In second period
vm.warp(PERIOD_OFFSET + PERIOD_LENGTH);
// balance is safe and non-zero
assertEq(twabController.getBalanceAt(mockVault, alice, PERIOD_OFFSET), _amount);
}
function testGetBalanceAt_safeBalance_and_unsafeBalance() public {
vm.startPrank(mockVault);
// Mint at history start
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
// In second period
vm.warp(PERIOD_OFFSET + PERIOD_LENGTH);
// mint again
twabController.mint(alice, _amount);
// only safe balance
assertEq(twabController.getBalanceAt(mockVault, alice, PERIOD_OFFSET), _amount);
}
function testTotalSupply_empty() public {
assertEq(twabController.totalSupply(address(this)), 0);
}
function testTotalSupply_single() public {
twabController.mint(alice, 10e18);
assertEq(twabController.totalSupply(address(this)), 10e18);
}
function testTotalSupplyDelegateBalance_empty() public {
assertEq(twabController.totalSupplyDelegateBalance(address(this)), 0);
}
function testTotalSupplyDelegateBalance_single() public {
twabController.mint(alice, 10e18);
assertEq(twabController.totalSupplyDelegateBalance(address(this)), 10e18);
}
function testGetTotalSupplyAt_empty() public {
assertEq(twabController.getTotalSupplyAt(mockVault, uint32(block.timestamp) - 1), 0);
}
function testGetTotalSupplyAt_single_before() public {
vm.warp(PERIOD_OFFSET);
twabController.mint(bob, 10e18);
assertEq(twabController.getTotalSupplyAt(mockVault, PERIOD_OFFSET - 1), 0);
}
function testGetTotalSupplyAt_single_on() public {
vm.warp(PERIOD_OFFSET);
twabController.mint(bob, 10e18);
assertEq(twabController.getTotalSupplyAt(mockVault, PERIOD_OFFSET), 0);
}
function testGetTwabBetween_noSnap() public {
uint32 secondPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(secondPeriodStart);
twabController.mint(alice, 1000e18);
vm.warp(secondPeriodStart + PERIOD_LENGTH);
twabController.mint(alice, 1000e18);
assertEq(
twabController.getTwabBetween(
address(this),
alice,
secondPeriodStart,
secondPeriodStart + PERIOD_LENGTH
),
1000e18
);
}
function testGetTwabBetween_notFinalized_partial() public {
uint32 firstPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(firstPeriodStart);
vm.expectRevert(
abi.encodeWithSelector(
TimestampNotFinalized.selector,
firstPeriodStart + PERIOD_LENGTH,
firstPeriodStart
)
);
twabController.getTwabBetween(
address(this),
alice,
firstPeriodStart,
firstPeriodStart + PERIOD_LENGTH / 2
);
}
function testGetTwabBetween_finalized() public {
uint32 firstPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(firstPeriodStart + PERIOD_LENGTH);
twabController.mint(alice, 1000e18);
assertEq(
twabController.getTwabBetween(
address(this),
alice,
firstPeriodStart,
firstPeriodStart + PERIOD_LENGTH
),
0
);
}
function testGetTwabBetween_snapStart() public {
uint32 firstPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(firstPeriodStart);
twabController.mint(alice, 1000e18);
vm.warp(firstPeriodStart + PERIOD_LENGTH / 2);
twabController.mint(alice, 1000e18);
vm.warp(firstPeriodStart + PERIOD_LENGTH);
assertEq(
twabController.getTwabBetween(
address(this),
alice,
firstPeriodStart - PERIOD_LENGTH / 2,
firstPeriodStart + PERIOD_LENGTH
),
1500e18
);
}
function testGetTwabBetween_snapEnd() public {
uint32 firstPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(firstPeriodStart);
twabController.mint(alice, 1000e18);
vm.warp(firstPeriodStart + PERIOD_LENGTH / 2);
twabController.mint(alice, 1000e18);
vm.warp(firstPeriodStart + PERIOD_LENGTH);
assertEq(
twabController.getTwabBetween(address(this), alice, firstPeriodStart, firstPeriodStart + 1),
1500e18
);
}
function testGetTimestampPeriod() public {
assertEq(twabController.getTimestampPeriod(PERIOD_OFFSET), 0);
assertEq(twabController.getTimestampPeriod(PERIOD_OFFSET - 1), 0);
assertEq(twabController.getTimestampPeriod(PERIOD_OFFSET + PERIOD_LENGTH), 1);
}
function testCurrentOverwritePeriodStartedAt_beginning() public {
assertEq(twabController.currentOverwritePeriodStartedAt(), PERIOD_OFFSET);
}
function testCurrentOverwritePeriodStartedAt_partway() public {
vm.warp(PERIOD_OFFSET + PERIOD_LENGTH / 2);
assertEq(twabController.currentOverwritePeriodStartedAt(), PERIOD_OFFSET);
}
function testCurrentOverwritePeriodStartedAt_next() public {
vm.warp(PERIOD_OFFSET + PERIOD_LENGTH);
assertEq(twabController.currentOverwritePeriodStartedAt(), PERIOD_OFFSET + PERIOD_LENGTH);
}
/// It should not be possible for a twab measurement to change after the period has finalized.
function testGetTotalSupplyTwabBetween_regression() public {
uint32 secondPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(secondPeriodStart);
twabController.mint(alice, 1000e18);
vm.warp(secondPeriodStart + PERIOD_LENGTH);
twabController.mint(alice, 1000e18);
vm.warp(secondPeriodStart + PERIOD_LENGTH * 2);
uint256 balance = twabController.getTotalSupplyTwabBetween(
address(this),
secondPeriodStart + PERIOD_LENGTH,
secondPeriodStart + PERIOD_LENGTH * 2
);
assertEq(balance, 2000e18);
vm.warp(secondPeriodStart + PERIOD_LENGTH * 2 + PERIOD_LENGTH / 4);
twabController.mint(alice, 2222e18);
vm.warp(secondPeriodStart + PERIOD_LENGTH * 2 + PERIOD_LENGTH / 2);
twabController.mint(bob, 44444e18);
vm.warp(secondPeriodStart + PERIOD_LENGTH * 3);
assertEq(
balance,
twabController.getTotalSupplyTwabBetween(
address(this),
secondPeriodStart + PERIOD_LENGTH,
secondPeriodStart + PERIOD_LENGTH * 2
),
"mint after"
);
}
function testGetTotalSupplyTwabBetween_noSnap() public {
uint32 secondPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(secondPeriodStart);
twabController.mint(alice, 1000e18);
vm.warp(secondPeriodStart + PERIOD_LENGTH);
twabController.mint(alice, 1000e18);
assertEq(
twabController.getTotalSupplyTwabBetween(
address(this),
secondPeriodStart,
secondPeriodStart + PERIOD_LENGTH
),
1000e18
);
}
function testGetTotalSupplyTwabBetween_notFinalized_partial() public {
uint32 firstPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(firstPeriodStart);
vm.expectRevert(
abi.encodeWithSelector(
TimestampNotFinalized.selector,
firstPeriodStart + PERIOD_LENGTH,
firstPeriodStart
)
);
twabController.getTotalSupplyTwabBetween(
address(this),
firstPeriodStart,
firstPeriodStart + PERIOD_LENGTH / 2
);
}
function testGetTotalSupplyTwabBetween_finalized() public {
uint32 firstPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(firstPeriodStart + PERIOD_LENGTH);
twabController.mint(alice, 1000e18);
assertEq(
twabController.getTotalSupplyTwabBetween(
address(this),
firstPeriodStart,
firstPeriodStart + PERIOD_LENGTH
),
0
);
}
function testGetTotalSupplyTwabBetween_snapStart() public {
uint32 firstPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(firstPeriodStart);
twabController.mint(alice, 1000e18);
vm.warp(firstPeriodStart + PERIOD_LENGTH / 2);
twabController.mint(alice, 1000e18);
vm.warp(firstPeriodStart + PERIOD_LENGTH);
assertEq(
twabController.getTotalSupplyTwabBetween(
address(this),
firstPeriodStart - PERIOD_LENGTH / 2,
firstPeriodStart + PERIOD_LENGTH
),
1500e18
);
}
function testGetTotalSupplyTwabBetween_snapEnd() public {
uint32 firstPeriodStart = PERIOD_OFFSET + PERIOD_LENGTH;
vm.warp(firstPeriodStart);
twabController.mint(alice, 1000e18);
vm.warp(firstPeriodStart + PERIOD_LENGTH / 2);
twabController.mint(alice, 1000e18);
vm.warp(firstPeriodStart + PERIOD_LENGTH);
assertEq(
twabController.getTotalSupplyTwabBetween(
address(this),
firstPeriodStart,
firstPeriodStart + 1
),
1500e18
);
}
function testGetTotalSupplyAt_init() public {
vm.warp(PERIOD_OFFSET + PERIOD_LENGTH);
assertEq(twabController.getTotalSupplyAt(mockVault, uint32(PERIOD_OFFSET)), 0);
}
function testGetTotalSupplyAt_nonZero() public {
uint96 _mintAmount = 1000e18;
vm.startPrank(mockVault);
vm.warp(PERIOD_OFFSET);
twabController.mint(alice, _mintAmount);
vm.warp(PERIOD_OFFSET + PERIOD_LENGTH);
assertEq(twabController.getTotalSupplyAt(mockVault, uint32(PERIOD_OFFSET)), _mintAmount);
}
function testTotalSupply() public {
assertEq(twabController.totalSupply(mockVault), 0);
uint96 _mintAmount = 1000e18;
vm.startPrank(mockVault);
twabController.mint(alice, _mintAmount);
assertEq(twabController.totalSupply(mockVault), _mintAmount);
}
function testTotalSupplyDelegateBalance() public {
assertEq(twabController.totalSupply(mockVault), 0);
uint96 _mintAmount = 1000e18;
vm.startPrank(mockVault);
twabController.mint(alice, _mintAmount); //delegates by default
twabController.sponsor(alice);
assertEq(twabController.totalSupplyDelegateBalance(mockVault), 0);
}
function testSponsorship() external {
uint256 aliceTwab;
uint256 totalSupplyTwab;
uint256 bobTwab;
uint96 amount = 100e18;
uint32 t0 = PERIOD_OFFSET;
uint32 t05 = t0 + PERIOD_LENGTH / 2;
uint32 t1 = t0 + PERIOD_LENGTH;
uint32 t2 = t1 + PERIOD_LENGTH;
uint32 t3 = t2 + PERIOD_LENGTH;
vm.startPrank(mockVault);
vm.warp(t05);
twabController.mint(alice, amount);
vm.warp(t1);
twabController.sponsor(alice);
vm.warp(t2);
twabController.transfer(alice, bob, amount / 2);
vm.warp(t3);
// Alice's TWAB is the same as total supply TWAB
aliceTwab = twabController.getTwabBetween(mockVault, alice, t0, t1);
totalSupplyTwab = twabController.getTotalSupplyTwabBetween(mockVault, t0, t1);
assertEq(aliceTwab, amount / 2, "for the first period alice has half");
assertEq(aliceTwab, totalSupplyTwab, "alice has total supply");
// Alice now has zero, due to sponsorship
aliceTwab = twabController.getTwabBetween(mockVault, alice, t1, t2);
totalSupplyTwab = twabController.getTotalSupplyTwabBetween(mockVault, t1, t2);
assertEq(aliceTwab, 0);
assertEq(totalSupplyTwab, 0);
// Alice's TWAB is still 0 due to sponsorship
// Bob's TWAB is 1/2 amount, the same as total supply TWAB
aliceTwab = twabController.getTwabBetween(mockVault, alice, t2, t3);
bobTwab = twabController.getTwabBetween(mockVault, bob, t2, t3);
totalSupplyTwab = twabController.getTotalSupplyTwabBetween(mockVault, t2, t3);
assertEq(aliceTwab, 0);
assertEq(bobTwab, amount / 2);
assertEq(totalSupplyTwab, amount / 2);
}
function testSponsorshipDelegation() external {
assertEq(twabController.totalSupply(mockVault), 0);
assertEq(twabController.totalSupplyDelegateBalance(mockVault), 0);
uint96 _amount = 1000e18;
vm.startPrank(mockVault);
twabController.mint(alice, _amount);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), _amount);
assertEq(twabController.totalSupply(mockVault), _amount);
assertEq(twabController.totalSupplyDelegateBalance(mockVault), _amount);
twabController.sponsor(alice);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
address _sponsorshipAddress = SPONSORSHIP_ADDRESS;
assertEq(twabController.balanceOf(mockVault, _sponsorshipAddress), 0);
assertEq(twabController.delegateBalanceOf(mockVault, _sponsorshipAddress), 0);
assertEq(twabController.totalSupply(mockVault), _amount);
assertEq(twabController.totalSupplyDelegateBalance(mockVault), 0);
changePrank(alice);
twabController.delegate(mockVault, bob);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
assertEq(twabController.balanceOf(mockVault, bob), 0);
assertEq(twabController.delegateBalanceOf(mockVault, bob), _amount);
assertEq(twabController.totalSupply(mockVault), _amount);
assertEq(twabController.totalSupplyDelegateBalance(mockVault), _amount);
vm.stopPrank();
}
function testSimpleSponsorshipDelegation() external {
assertEq(twabController.totalSupply(mockVault), 0);
assertEq(twabController.totalSupplyDelegateBalance(mockVault), 0);
uint96 _amount = 10;
vm.startPrank(mockVault);
twabController.mint(alice, _amount);
twabController.mint(bob, _amount);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.balanceOf(mockVault, bob), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, bob), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, SPONSORSHIP_ADDRESS), 0);
twabController.sponsor(bob);
vm.stopPrank();
vm.prank(alice);
twabController.delegate(mockVault, bob);
// Balances stay the same
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.balanceOf(mockVault, bob), _amount);
// Delegate balances have changed
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
assertEq(twabController.delegateBalanceOf(mockVault, bob), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, SPONSORSHIP_ADDRESS), 0);
}
function testMint_afterTimerange() public {
vm.startPrank(mockVault);
vm.warp(type(uint48).max);
twabController.mint(alice, 1000e18);
vm.warp(type(uint64).max);
assertEq(twabController.balanceOf(mockVault, alice), 1000e18);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 1000e18);
assertEq(twabController.getBalanceAt(mockVault, alice, type(uint48).max), 0);
assertEq(twabController.getTotalSupplyAt(mockVault, type(uint48).max), 0);
twabController.burn(alice, 1000e18);
assertEq(twabController.balanceOf(mockVault, alice), 0);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
vm.stopPrank();
}
function testMint() external {
uint96 _amount = 1000e18;
vm.expectEmit(true, true, false, true);
emit IncreasedBalance(mockVault, alice, _amount, _amount);
vm.expectEmit(true, false, false, true);
emit ObservationRecorded(
mockVault,
alice,
_amount,
_amount,
true,
ObservationLib.Observation({
cumulativeBalance: 0,
balance: _amount,
timestamp: uint32(block.timestamp) - PERIOD_OFFSET
})
);
vm.expectEmit(true, false, false, true);
emit IncreasedTotalSupply(mockVault, _amount, _amount);
vm.expectEmit(true, false, false, true);
emit TotalSupplyObservationRecorded(
mockVault,
_amount,
_amount,
true,
ObservationLib.Observation({
cumulativeBalance: 0,
balance: _amount,
timestamp: uint32(block.timestamp) - PERIOD_OFFSET
})
);
vm.startPrank(mockVault);
twabController.mint(alice, _amount);
TwabLib.Account memory account = twabController.getAccount(mockVault, alice);
TwabLib.AccountDetails memory accountDetails = account.details;
assertEq(accountDetails.balance, _amount);
assertEq(accountDetails.delegateBalance, _amount);
vm.stopPrank();
}
function testBurn() external {
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
vm.expectEmit(true, true, false, true);
emit DecreasedBalance(mockVault, alice, _amount, _amount);
vm.expectEmit(true, false, false, true);
emit ObservationRecorded(
mockVault,
alice,
0,
0,
false,
ObservationLib.Observation({
cumulativeBalance: 0,
balance: 0,
timestamp: uint32(block.timestamp) - PERIOD_OFFSET
})
);
vm.expectEmit(true, false, false, true);
emit DecreasedTotalSupply(mockVault, _amount, _amount);
vm.expectEmit(true, false, false, true);
emit TotalSupplyObservationRecorded(
mockVault,
0,
0,
false,
ObservationLib.Observation({
cumulativeBalance: 0,
balance: 0,
timestamp: uint32(block.timestamp) - PERIOD_OFFSET
})
);
twabController.burn(alice, _amount);
TwabLib.Account memory account = twabController.getAccount(mockVault, alice);
TwabLib.AccountDetails memory accountDetails = account.details;
assertEq(accountDetails.balance, 0, "account details balance matches");
assertEq(accountDetails.delegateBalance, 0, "account details delegate balance matches");
vm.stopPrank();
}
function testMint_max() public {
vm.warp(100 days);
vm.startPrank(mockVault);
twabController.mint(alice, type(uint96).max);
uint256 currentTime = uint256(type(uint32).max) + 100 days;
console2.log("currentTime", currentTime);
// 4303607295
vm.warp(currentTime);
twabController.burn(alice, 100);
// get the twab up until the very end (excluding last period so we don't have an endtime beyond the records)
assertEq(
twabController.getTwabBetween(
mockVault,
alice,
100 days,
uint256(type(uint32).max) + PERIOD_OFFSET - PERIOD_LENGTH
),
type(uint96).max
);
vm.stopPrank();
}
function testGetTwabBetween_max() public {
vm.warp(100 days);
vm.startPrank(mockVault);
twabController.mint(alice, type(uint96).max);
uint256 currentTime = uint256(type(uint32).max) + 100 days;
console2.log("currentTime", currentTime);
// 4303607295
vm.warp(currentTime);
twabController.burn(alice, 100);
// get the twab up until the very end (excluding last period so we don't have an endtime beyond the records)
assertEq(
twabController.getTwabBetween(
mockVault,
alice,
100 days,
uint256(type(uint32).max) + PERIOD_OFFSET - PERIOD_LENGTH
),
type(uint96).max
);
vm.stopPrank();
}
function testIsNewEvent() external {
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
vm.expectEmit(true, true, false, true);
emit IncreasedBalance(mockVault, alice, _amount, _amount);
vm.expectEmit(true, false, false, true);
emit ObservationRecorded(
mockVault,
alice,
_amount,
_amount,
true,
ObservationLib.Observation({
cumulativeBalance: 0,
balance: _amount,
timestamp: uint32(block.timestamp) - PERIOD_OFFSET
})
);
vm.expectEmit(true, false, false, true);
emit IncreasedTotalSupply(mockVault, _amount, _amount);
vm.expectEmit(true, false, false, true);
emit TotalSupplyObservationRecorded(
mockVault,
_amount,
_amount,
true,
ObservationLib.Observation({
cumulativeBalance: 0,
balance: _amount,
timestamp: uint32(block.timestamp) - PERIOD_OFFSET
})
);
twabController.mint(alice, _amount);
vm.expectEmit(true, true, false, true);
emit DecreasedBalance(mockVault, alice, _amount, _amount);
vm.expectEmit(true, false, false, true);
emit ObservationRecorded(
mockVault,
alice,
0,
0,
false,
ObservationLib.Observation({
cumulativeBalance: 0,
balance: 0,
timestamp: uint32(block.timestamp) - PERIOD_OFFSET
})
);
vm.expectEmit(true, false, false, true);
emit DecreasedTotalSupply(mockVault, _amount, _amount);
vm.expectEmit(true, false, false, true);
emit TotalSupplyObservationRecorded(
mockVault,
0,
0,
false,
ObservationLib.Observation({
cumulativeBalance: 0,
balance: 0,
timestamp: uint32(block.timestamp) - PERIOD_OFFSET
})
);
twabController.burn(alice, _amount);
vm.stopPrank();
}
function testTransfer() external {
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), _amount);
changePrank(alice);
twabController.delegate(mockVault, bob);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
assertEq(twabController.balanceOf(mockVault, bob), 0);
assertEq(twabController.delegateBalanceOf(mockVault, bob), _amount);
changePrank(mockVault);
twabController.transfer(alice, charlie, _amount);
assertEq(twabController.balanceOf(mockVault, alice), 0);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
assertEq(twabController.balanceOf(mockVault, bob), 0);
assertEq(twabController.delegateBalanceOf(mockVault, bob), 0);
assertEq(twabController.balanceOf(mockVault, charlie), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, charlie), _amount);
vm.stopPrank();
}
function testTransfer_rejectSponsorshipAddress() public {
twabController.mint(alice, 100e18);
address sponsorship = SPONSORSHIP_ADDRESS;
vm.expectRevert(abi.encodeWithSelector(CannotTransferToSponsorshipAddress.selector));
twabController.transfer(alice, sponsorship, 100e18);
}
/* ============ delegate ============ */
function testDelegate() external {
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
assertEq(twabController.delegateOf(mockVault, alice), alice);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, bob), 0);
assertEq(twabController.balanceOf(mockVault, bob), 0);
assertEq(twabController.delegateBalanceOf(mockVault, charlie), 0);
assertEq(twabController.balanceOf(mockVault, charlie), 0);
changePrank(alice);
twabController.delegate(mockVault, bob);
assertEq(twabController.delegateOf(mockVault, alice), bob);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
assertEq(twabController.balanceOf(mockVault, bob), 0);
assertEq(twabController.delegateBalanceOf(mockVault, bob), _amount);
assertEq(twabController.balanceOf(mockVault, charlie), 0);
assertEq(twabController.delegateBalanceOf(mockVault, charlie), 0);
twabController.delegate(mockVault, charlie);
assertEq(twabController.delegateOf(mockVault, alice), charlie);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
assertEq(twabController.balanceOf(mockVault, bob), 0);
assertEq(twabController.delegateBalanceOf(mockVault, bob), 0);
assertEq(twabController.balanceOf(mockVault, charlie), 0);
assertEq(twabController.delegateBalanceOf(mockVault, charlie), _amount);
changePrank(mockVault);
twabController.mint(alice, _amount);
uint96 _totalAmount = _amount * 2;
assertEq(twabController.balanceOf(mockVault, alice), _totalAmount);
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
assertEq(twabController.balanceOf(mockVault, charlie), 0);
assertEq(twabController.delegateBalanceOf(mockVault, charlie), _totalAmount);
vm.stopPrank();
}
function testDelegateOf_default() public {
assertEq(twabController.delegateOf(mockVault, alice), alice);
}
function testDelegateOf_sponsorship() public {
vm.startPrank(mockVault);
twabController.sponsor(alice);
assertEq(twabController.delegateOf(mockVault, alice), SPONSORSHIP_ADDRESS);
}
function testDelegateOf_addressZero() public {
vm.startPrank(alice);
twabController.delegate(mockVault, address(0));
assertEq(twabController.delegateOf(mockVault, alice), SPONSORSHIP_ADDRESS);
}
function testDelegateOf_address() public {
address bob = makeAddr("bob");
vm.startPrank(alice);
twabController.delegate(mockVault, bob);
assertEq(twabController.delegateOf(mockVault, alice), bob);
}
function testMint_lastObservation() public {
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
uint lastAt = twabController.lastObservationAt();
console2.log("LAST AT", lastAt);
vm.warp(lastAt);
twabController.mint(alice, _amount);
vm.warp(lastAt + PERIOD_LENGTH);
assertEq(twabController.getBalanceAt(mockVault, alice, lastAt), _amount);
vm.stopPrank();
}
function testMint_toZero() public {
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
vm.expectRevert(abi.encodeWithSelector(TransferToZeroAddress.selector));
twabController.mint(address(0), _amount);
}
function testTransfer_toZero() public {
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
vm.expectRevert(abi.encodeWithSelector(TransferToZeroAddress.selector));
twabController.transfer(alice, address(0), _amount);
}
function testDelegate_toSelf() public {
vm.startPrank(mockVault);
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
assertEq(twabController.delegateOf(mockVault, alice), alice);
assertEq(twabController.delegateBalanceOf(mockVault, alice), _amount);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
}
function testDelegate_interpretBurnAddressAsSponsorship() external {
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
vm.startPrank(alice);
twabController.delegate(address(this), address(0));
assertEq(twabController.totalSupplyDelegateBalance(address(this)), 0);
assertEq(twabController.delegateBalanceOf(address(this), bob), 0);
assertEq(twabController.delegateBalanceOf(address(this), alice), 0);
twabController.delegate(address(this), bob);