-
Notifications
You must be signed in to change notification settings - Fork 3
/
Space.t.sol
1482 lines (1228 loc) · 48.7 KB
/
Space.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: AGPL-3.0-only
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
// Testing utils
import {DSTest} from "@sense-finance/v1-core/src/tests/test-helpers/test.sol";
import {MockDividerSpace, MockAdapterSpace, ERC20Mintable} from "./utils/Mocks.sol";
import {Vm} from "forge-std/Vm.sol";
import {User} from "./utils/User.sol";
// External references
import {Vault, IVault, IWETH, IAuthorizer, IAsset, IProtocolFeesCollector} from "@balancer-labs/v2-vault/contracts/Vault.sol";
import {IPoolSwapStructs} from "@balancer-labs/v2-vault/contracts/interfaces/IPoolSwapStructs.sol";
import {Authentication} from "@balancer-labs/v2-solidity-utils/contracts/helpers/Authentication.sol";
import {IERC20} from "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/IERC20.sol";
import {Authorizer} from "@balancer-labs/v2-vault/contracts/Authorizer.sol";
import {FixedPoint} from "@balancer-labs/v2-solidity-utils/contracts/math/FixedPoint.sol";
import {IPriceOracle} from "../oracle/interfaces/IPriceOracle.sol";
// Internal references
import {SpaceFactory} from "../SpaceFactory.sol";
import {Space} from "../Space.sol";
import {Errors} from "../Errors.sol";
contract SpaceTest is DSTest {
using FixedPoint for uint256;
Vm internal constant vm = Vm(HEVM_ADDRESS);
IWETH internal constant weth =
IWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
uint256 public constant INTIAL_USER_BALANCE = 100e18;
uint256 public constant INIT_SCALE = 1.1e18;
Vault internal vault;
Space internal space;
SpaceFactory internal spaceFactory;
MockDividerSpace internal divider;
MockAdapterSpace internal adapter;
uint256 internal maturity;
ERC20Mintable internal pt;
ERC20Mintable internal target;
Authorizer internal authorizer;
User internal jim;
User internal ava;
User internal sid;
User internal sam;
uint256 internal ts;
uint256 internal g1;
uint256 internal g2;
function setUp() public {
// Init normalized starting conditions
vm.warp(0);
vm.roll(0);
// Create mocks
divider = new MockDividerSpace(18);
adapter = new MockAdapterSpace(18);
adapter.setScale(INIT_SCALE);
ts = FixedPoint.ONE.divDown(FixedPoint.ONE * 31622400 * 10); // 1 / 10 year in seconds
// 0.95 for selling Target
g1 = (FixedPoint.ONE * 950).divDown(FixedPoint.ONE * 1000);
// 1 / 0.95 for selling PT
g2 = (FixedPoint.ONE * 1000).divDown(FixedPoint.ONE * 950);
maturity = 15811200; // 6 months in seconds
divider.initSeries(maturity);
authorizer = new Authorizer(address(this));
vault = new Vault(authorizer, weth, 0, 0);
spaceFactory = new SpaceFactory(
vault,
address(divider),
ts,
g1,
g2,
true
);
space = Space(spaceFactory.create(address(adapter), maturity));
(address _pt, , , , , , , , ) = MockDividerSpace(divider).series(
address(adapter),
maturity
);
pt = ERC20Mintable(_pt);
target = ERC20Mintable(adapter.target());
// Mint this address PT and Target tokens
// Max approve the balancer vault to move this addresses tokens
pt.mint(address(this), INTIAL_USER_BALANCE);
target.mint(address(this), INTIAL_USER_BALANCE);
target.approve(address(vault), type(uint256).max);
pt.approve(address(vault), type(uint256).max);
jim = new User(vault, space, pt, target);
pt.mint(address(jim), INTIAL_USER_BALANCE);
target.mint(address(jim), INTIAL_USER_BALANCE);
ava = new User(vault, space, pt, target);
pt.mint(address(ava), INTIAL_USER_BALANCE);
target.mint(address(ava), INTIAL_USER_BALANCE);
sid = new User(vault, space, pt, target);
pt.mint(address(sid), INTIAL_USER_BALANCE);
target.mint(address(sid), INTIAL_USER_BALANCE);
sam = new User(vault, space, pt, target);
}
function testDeployPoolHasParams() public {
address pt = MockDividerSpace(divider).pt(address(adapter), maturity);
Space pool = new Space(
vault,
address(adapter),
maturity,
pt,
ts,
g1,
g2,
true
);
uint256 pti = pt < address(target) ? 0 : 1;
assertEq(pool.adapter(), address(adapter));
assertEq(pool.maturity(), maturity);
assertEq(pool.pti(), pti);
assertEq(pool.ts(), ts);
assertEq(pool.g1(), g1);
assertEq(pool.g2(), g2);
assertEq(pool.name(), "Sense Space 4th Oct 2021 cDAI Sense Principal Token, A2");
assertEq(pool.symbol(), "SPACE-sP-cDAI:04-10-2021:2");
}
function testJoinOnce() public {
jim.join();
// For the pool's first join –--
// It moved Target out of jim's account
assertEq(target.balanceOf(address(jim)), 99e18);
// and it minted jim's account BPT tokens equal to the value of underlying
// deposited (inital scale is 1e18, so it's one-to-one)
assertClose(
space.balanceOf(address(jim)),
uint256(1e18).mulDown(INIT_SCALE),
1e6
);
// but it did not move any PT
assertEq(pt.balanceOf(address(jim)), 100e18);
}
function testJoinMultiNoSwaps() public {
// Join once
jim.join();
// Join again after no swaps
jim.join();
// If the pool has been joined a second time and no swaps have occured –--
// It moved more Target out of jim's account
assertEq(target.balanceOf(address(jim)), 98e18);
// and it minted jim's account more BPT tokens
assertClose(
space.balanceOf(address(jim)),
uint256(2e18).mulDown(INIT_SCALE),
1e6
);
// but it still did not move any PT
assertEq(pt.balanceOf(address(jim)), 100e18);
}
function testSimpleSwapIn() public {
// Join once (first join is always Target-only)
jim.join();
// Can't swap any Target in b/c there aren't ever any PT to get out after the first join
try jim.swapIn(false, 1) {
fail();
} catch Error(string memory error) {
assertEq(error, Errors.SWAP_TOO_SMALL);
}
// Can successfully swap PT in
uint256 targetOt = jim.swapIn(true);
// Fixed amount in, variable amount out
// Calculated externally by solving the YS invariant
uint256 expectedTargetOut = 860452261775322692;
// Swapped one PT in
assertEq(pt.balanceOf(address(jim)), 99e18);
// Received less than one Target
assertEq(targetOt, expectedTargetOut);
(, uint256[] memory balances, ) = vault.getPoolTokens(
space.getPoolId()
);
(uint256 pti, uint256 targeti) = space.getIndices();
// Pool balances reflect the user's balances
assertEq(balances[pti], 1e18);
assertEq(balances[targeti], 1e18 - expectedTargetOut);
// Can not swap a full Target in b/c it pushes the rate below zero
try jim.swapIn(false) {
fail();
} catch Error(string memory error) {
assertEq(error, Errors.NEGATIVE_RATE);
}
// Can successfully swap a partial Target in
uint256 ptOut = jim.swapIn(false, 0.5e18);
uint256 expectedPTOut = 591079133821352896;
assertEq(
target.balanceOf(address(jim)),
99e18 + expectedTargetOut - 0.5e18
);
assertEq(ptOut, expectedPTOut);
}
function testSimpleSwapsOut() public {
jim.join();
// Can't swap any PT out b/c there aren't any PT to get out after the first join
try jim.swapOut(false, 1) {
fail();
} catch Error(string memory error) {
// This pushes the rate negtaive, though it would still fail on overflow if
// the negative rate check wasn't there
assertEq(error, Errors.NEGATIVE_RATE);
}
// Can successfully swap Target out
uint256 ptsIn = jim.swapOut(true, 0.1e18);
// Fixed amount out, variable amount in
// Calculated externally
uint256 expectedPTIn = 110582918254120990; // around 0.11612
// Received 0.1 Target
assertEq(target.balanceOf(address(jim)), 99e18 + 0.1e18);
assertEq(ptsIn, expectedPTIn);
}
function testExitOnce() public {
jim.join();
// Max exit
jim.exit(space.balanceOf(address(jim)));
// For the pool's first exit –--
// It moved PT back to jim's account
assertEq(pt.balanceOf(address(jim)), 100e18);
// And it took all of jim's account's BPT back
assertEq(space.balanceOf(address(jim)), 0);
// It moved almost all Target back to this account (locked MINIMUM_BPT permanently)
assertClose(target.balanceOf(address(jim)), 100e18, 1e6);
}
function testExitRounding() public {
vm.roll(0);
jim.join();
vm.roll(1);
// Complete exit leaving only min bpt in pool
jim.exit(space.balanceOf(address(jim)));
uint256 preSupply = space.totalSupply();
assertEq(preSupply, space.MINIMUM_BPT());
// Check Target reserves
(, uint256[] memory balances, ) = vault.getPoolTokens(
space.getPoolId()
);
assertEq(
balances[1 - space.pti()],
space.MINIMUM_BPT().divDown(INIT_SCALE)
);
vm.roll(2);
// Pre-swap join uses target in times init_scale to determine the bpt given out
uint256 TARGET_IN = 50e18;
sid.join(0, TARGET_IN);
uint256 joinedTargetInUnderlying = TARGET_IN.mulDown(INIT_SCALE);
uint256 postSupply = space.totalSupply();
assertEq(postSupply, preSupply + joinedTargetInUnderlying);
vm.roll(3);
sid.swapIn(true, 20e18);
}
function testGrowingTargetReservesWithStableBptSupply() public {
vm.roll(0);
adapter.setScale(1.1e18);
jim.join(0, 10e18);
vm.roll(1);
sid.swapIn(true, 0.05e18);
uint256 tOut;
for (uint256 i = 0; i < 20; i++) {
// 3 PT in
uint256 _tOut = sid.swapIn(true, 3e18);
// A PT to Target swap gives more Target as fees accrue after the
// PT reserves side has returned to 0
assertGt(_tOut, tOut);
tOut = _tOut;
// 3 PT out
sid.swapOut(false, 3e18);
}
vm.roll(2);
jim.exit(space.balanceOf(address(jim)));
// Jim ends up with more Target and PTs
assertGt(target.balanceOf(address(jim)), INTIAL_USER_BALANCE);
assertGt(pt.balanceOf(address(jim)), INTIAL_USER_BALANCE);
vm.roll(3);
}
function testJoinSwapExit() public {
jim.join();
// Swap out 0.1 Target
jim.swapOut(true, 0.1e18);
// Max exit
jim.exit(space.balanceOf(address(jim)));
// For the pool's first exit –--
// It moved PT back to jim's account (less rounding losses)
assertClose(pt.balanceOf(address(jim)), 100e18, 1e6);
// And it took all of jim's account's BPT back
assertEq(space.balanceOf(address(jim)), 0);
// It moved almost all Target back to this account (locked MINIMUM_BPT permanently)
assertClose(target.balanceOf(address(jim)), 100e18, 1e6);
}
function testMultiPartyJoinSwapExit() public {
// Jim tries to join 1 of each (should be Target-only)
jim.join();
// The pool moved one Target out of jim's account
assertEq(target.balanceOf(address(jim)), 99e18);
// Swap 0.8 PT in
sid.swapIn(true, 0.8e18);
// Ava tries to Join 0.8 of each (should take 0.8 PT and some amount of Target)
ava.join(0.8e18, 0.8e18);
assertGe(target.balanceOf(address(ava)), 99e18);
assertEq(pt.balanceOf(address(ava)), 99.2e18);
// Swap 0.2 PT in
sid.swapIn(true, 0.2e18);
// Ava tries to Join 1 of each (should take 1 PT and less Target than last time)
uint256 targetPreJoin = target.balanceOf(address(ava));
ava.join();
assertGe(target.balanceOf(address(ava)), 99e18);
// Should have joined less Target than last time
assertGt(
100e18 - targetPreJoin,
targetPreJoin - target.balanceOf(address(ava))
);
// Should have joined Target / PT at the ratio of the pool
assertEq(pt.balanceOf(address(ava)), 98.2e18);
(, uint256[] memory balances, ) = vault.getPoolTokens(
space.getPoolId()
);
(uint256 pti, uint256 targeti) = space.getIndices();
// All tokens are 18 decimals in `setUp`
uint256 targetPerPrincipal = (balances[targeti] * 1e18) / balances[pti];
// TargetPerPrincipal * 1 = Target amount in for 1 PT in
assertEq(
target.balanceOf(address(ava)),
targetPreJoin - targetPerPrincipal
);
// Jim and ava exit
jim.exit(space.balanceOf(address(jim)));
ava.exit(space.balanceOf(address(ava)));
// Can't swap after liquidity has been removed
try sid.swapIn(true, 1e12) {
fail();
} catch Error(string memory error) {
assertEq(error, "BAL#001");
}
try sid.swapOut(false, 1e12) {
fail();
} catch Error(string memory error) {
assertEq(error, "BAL#001");
}
// The first swap only took Target from Jim, so he'll have fewer Target but more PTs
assertClose(target.balanceOf(address(jim)), 99.2e18, 1e17);
assertClose(target.balanceOf(address(ava)), 99.9e18, 1e17);
assertClose(pt.balanceOf(address(jim)), 100.9e18, 1e12);
assertClose(pt.balanceOf(address(ava)), 100.1e18, 1e12);
}
function testMinBptOut() public {
uint256 minBpt = space.MINIMUM_BPT();
vm.expectRevert("SNS#108");
// Reverts if the minimum BPT isn't met by 1 token
jim.join(0, 1e18, INIT_SCALE.mulDown(1e18).sub(minBpt) + 1);
// Doesn't revert if the minimum BPT is exactly me
jim.join(0, 1e18, INIT_SCALE.mulDown(1e18).sub(minBpt));
// After a swap -----
sid.swapIn(true, 0.8e18);
// Calculate how many BPT Jim would mint from a specific join
uint256 preBpt = space.balanceOf(address(jim));
jim.join(1e18, 1e18);
uint256 newBpt = space.balanceOf(address(jim)) - preBpt;
jim.exit(newBpt);
vm.expectRevert("SNS#108");
jim.join(1e18, 1e18, newBpt + 2); // account for rounding error
jim.join(1e18, 1e18, newBpt);
}
function testSpaceFees() public {
// Target in
jim.join(0, 20e18);
// Init some PT in via swap
sid.swapIn(true, 4e18);
// Try as much of both in as possible
jim.join(20e18, 20e18);
// We can determine the implied price of PT in Target by making a very small swap
uint256 ptPrice = sid.swapIn(true, 0.0001e18).divDown(0.0001e18);
uint256 balance = 100e18;
uint256 startingPositionValue = balance + balance.mulDown(ptPrice);
// price execution is getting worse for pt out
uint256 targetInFor1PrincipalOut = 0;
for (uint256 i = 0; i < 20; i++) {
uint256 _targetInFor1PrincipalOut = ava.swapOut(false);
assertGt(_targetInFor1PrincipalOut, targetInFor1PrincipalOut);
targetInFor1PrincipalOut = _targetInFor1PrincipalOut;
// swap the pts back in
ava.swapIn(true, 1e18);
}
// price execution is getting worse for target out
uint256 ptInFor1TargetOut = 0;
for (uint256 i = 0; i < 20; i++) {
// price execution is getting worse
uint256 _ptInFor1TargetOut = ava.swapOut(true);
assertGt(_ptInFor1TargetOut, ptInFor1TargetOut);
ptInFor1TargetOut = _ptInFor1TargetOut;
// swap the target back in
ava.swapIn(false, 1e18);
}
// price execution is getting worse for pt in
uint256 targetOutFor1PrincipalIn = type(uint256).max;
for (uint256 i = 0; i < 20; i++) {
// price execution is getting worse
uint256 _targetOutFor1PrincipalIn = ava.swapIn(true);
assertLt(_targetOutFor1PrincipalIn, targetOutFor1PrincipalIn);
targetOutFor1PrincipalIn = _targetOutFor1PrincipalIn;
// swap the target back in
ava.swapIn(false, _targetOutFor1PrincipalIn);
}
// price execution is getting worse for target in
uint256 ptOutFor1TargetIn = type(uint256).max;
for (uint256 i = 0; i < 20; i++) {
// price execution is getting worse
uint256 _ptOutFor1TargetIn = ava.swapIn(false);
assertLt(_ptOutFor1TargetIn, ptOutFor1TargetIn);
ptOutFor1TargetIn = _ptOutFor1TargetIn;
// swap the pts back in
ava.swapIn(true, _ptOutFor1TargetIn);
}
jim.exit(space.balanceOf(address(jim)));
uint256 currentPositionValue = target.balanceOf(address(jim)) +
pt.balanceOf(address(jim)).mulDown(ptPrice);
assertGt(currentPositionValue, startingPositionValue);
}
function testApproachesOne() public {
// Target in
jim.join(0, 10e18);
// Init some PT in
sid.swapIn(true, 5.5e18);
// Try as much of both in as possible
jim.join(10e18, 10e18);
vm.warp(maturity - 1);
assertClose(sid.swapIn(true).mulDown(adapter.scale()), 1e18, 1e11);
assertClose(
sid.swapIn(false, uint256(1e18).divDown(adapter.scale())),
1e18,
1e11
);
}
function testConstantSumAfterMaturity() public {
// Target in
jim.join(0, 10e18);
// Init some PT in
sid.swapIn(true, 5.5e18);
// Try as much of both in as possible
jim.join(10e18, 10e18);
vm.warp(maturity + 1);
assertClose(sid.swapIn(true).mulDown(adapter.scale()), 1e18, 1e7);
assertClose(
sid.swapIn(false, uint256(1e18).divDown(adapter.scale())),
1e18,
1e7
);
}
function testCantJoinAfterMaturity() public {
vm.warp(maturity + 1);
try jim.join(0, 10e18) {
fail();
} catch Error(string memory error) {
assertEq(error, Errors.POOL_PAST_MATURITY);
}
}
function testProtocolFees() public {
IProtocolFeesCollector protocolFeesCollector = vault
.getProtocolFeesCollector();
// Grant protocolFeesCollector.setSwapFeePercentage role
bytes32 actionId = Authentication(address(protocolFeesCollector))
.getActionId(protocolFeesCollector.setSwapFeePercentage.selector);
authorizer.grantRole(actionId, address(this));
protocolFeesCollector.setSwapFeePercentage(0.1e18);
assertEq(space.balanceOf(address(protocolFeesCollector)), 0);
// Initialize liquidity
jim.join(0, 10e18);
jim.swapIn(true, 5.5e18);
jim.join(10e18, 10e18);
ava.join(10e18, 10e18);
uint256 NUM_WASH_TRADES = 6;
emit log_named_uint("PT", pt.balanceOf(address(ava)));
emit log_named_uint("target", target.balanceOf(address(ava)));
// Fee controller BPT before the swap run
uint256 feeControllerBPTPre = space.balanceOf(
address(protocolFeesCollector)
);
uint256 expectedFeesPaid = 0;
// Make some swaps
for (uint256 i = 0; i < NUM_WASH_TRADES; i++) {
// 5% of yield on each trade
uint256 targetIn = sid.swapOut(false);
uint256 idealYield = 1e18 - (targetIn * 0.95e18) / 1e18;
uint256 feeOnYield = (idealYield * 0.05e18) / 1e18;
expectedFeesPaid += feeOnYield;
uint256 targetOut = sid.swapIn(true);
idealYield = 1e18 - (targetOut * 0.95e18) / 1e18;
feeOnYield = (idealYield * 0.05e18) / 1e18;
expectedFeesPaid += feeOnYield;
}
// No additional BPT shares are minted for the controller until somebody joins or exits
assertEq(
space.balanceOf(address(protocolFeesCollector)),
feeControllerBPTPre
);
ava.exit(space.balanceOf(address(ava)));
uint256 feeControllerNewBPT = space.balanceOf(
address(protocolFeesCollector)
) - feeControllerBPTPre;
// Transfer fee controller's new BPT to sam, then redeem it
vm.prank(
address(protocolFeesCollector),
address(protocolFeesCollector)
);
space.transfer(address(sam), feeControllerNewBPT);
sam.exit(space.balanceOf(address(sam)));
emit log_named_uint("sam PTs", pt.balanceOf(address(sam)));
emit log_named_uint("sam target", target.balanceOf(address(sam)));
emit log_named_uint("expectedFeesPaid", expectedFeesPaid);
// Sid has his entire iniital PT balance back
assertEq(pt.balanceOf(address(sid)), 100e18);
// Sid has lost Target from trading fees
assertLt(target.balanceOf(address(sid)), 100e18);
emit log_named_uint("lost", 100e18 - target.balanceOf(address(sid)));
// assertEq(
// space.balanceOf(address(protocolFeesCollector)),
// 0
// );
// 7502641632334072
// 7488891101757368
// jim loss due to fees
// protocol controller gain due to fees (lines up with % of yield traded)
// fee controller should own x% of liquidity (based on x% of fees)
// TODO fees don't eat into non-trade invariant growth
// TODO fees are correctly proportioned to the fee set in the vault
// time goes by
}
function testTinySwaps() public {
jim.join(0, 10e18);
sid.swapIn(true, 5.5e18);
jim.join(10e18, 10e18);
// Swaps in can fail for being too small
try sid.swapIn(true, 1e6) {
fail();
} catch Error(string memory error) {
assertEq(error, Errors.SWAP_TOO_SMALL);
}
try sid.swapIn(false, 1e6) {
fail();
} catch Error(string memory error) {
assertEq(error, Errors.SWAP_TOO_SMALL);
}
// Swaps outs don't fail, but they ask for very high amounts in
// (rouding in favor of the LP has a big effect on small swaps)
assertGt(sid.swapOut(true, 1e6), 2e6);
assertGt(sid.swapOut(false, 1e6), 2e6);
}
function testJoinDifferentScaleValues() public {
// Jim join Target in
jim.join(0, 10e18);
// Sid inits PT
sid.swapIn(true, 5.5e18);
// Turn "auto growth" on for the adapter's scale
adapter.setScale(0);
uint256 initScale = adapter.scale();
// Determine how much Target Jim gets for one PT
uint256 targetOutForOnePrincipalInit = jim.swapIn(true);
// Swap that Target back in to restore the AMM state to before the prev swap
jim.swapIn(false, targetOutForOnePrincipalInit);
// Ava tries to join both in
ava.join();
// BPT from Ava's (1 PT, 1 Target) join
uint256 bptFromJoin = space.balanceOf(address(ava));
uint256 targetInFromJoin = INTIAL_USER_BALANCE -
target.balanceOf(address(ava));
uint256 ptInFromJoin = INTIAL_USER_BALANCE - pt.balanceOf(address(ava));
vm.warp(1 days);
uint256 scale1Week = adapter.scale();
ava.join();
// Ava's BPT out will exactly equal her first join
// Since the Target is worth more, she essentially got fewer BPT for the same amount of Underlying
assertClose(
bptFromJoin,
space.balanceOf(address(ava)) - bptFromJoin,
1e3
);
// Same amount of Target in (but it's worth more now)
assertClose(
targetInFromJoin * 2,
INTIAL_USER_BALANCE - target.balanceOf(address(ava)),
1e3
);
// Same amount of PT in
assertClose(
ptInFromJoin * 2,
INTIAL_USER_BALANCE - pt.balanceOf(address(ava)),
1e3
);
// Ava can exit her entire LP position just fine
ava.exit(space.balanceOf(address(ava)));
uint256 targetOutForOnePrincipal1Week = jim.swapIn(true);
// Gets fewer target out for one PT when Target is worth more
assertGt(targetOutForOnePrincipalInit, targetOutForOnePrincipal1Week);
// There is some change due to the YS invariant, but it's not much in 1 days time
// With the rate the Target is increasing in value, its growth should account for most of the change
// in swap rate
assertClose(
targetOutForOnePrincipalInit,
targetOutForOnePrincipal1Week.mulDown(
scale1Week.divDown(initScale)
),
1e15
);
}
function testDifferentDecimals() public {
// Setup ----
// Set PT/Yield to 8 decimals
MockDividerSpace divider = new MockDividerSpace(8);
divider.initSeries(maturity);
// Set Target to 9 decimals
MockAdapterSpace adapter = new MockAdapterSpace(9);
adapter.setScale(INIT_SCALE);
SpaceFactory spaceFactory = new SpaceFactory(
vault,
address(divider),
ts,
g1,
g2,
true
);
Space space = Space(spaceFactory.create(address(adapter), maturity));
(address _pt, , , , , , , , ) = MockDividerSpace(divider).series(
address(adapter),
maturity
);
ERC20Mintable pt = ERC20Mintable(_pt);
ERC20Mintable _target = ERC20Mintable(adapter.target());
User max = new User(vault, space, pt, _target);
_target.mint(address(max), 100e9);
pt.mint(address(max), 100e8);
User eve = new User(vault, space, pt, _target);
_target.mint(address(eve), 100e9);
pt.mint(address(eve), 100e8);
// Test ----
// Max joins 1 Target in
max.join(0, 1e9);
// The pool moved one Target out of max's account
assertEq(_target.balanceOf(address(max)), 99e9);
// Eve swaps 1 PT in
eve.swapIn(true, 1e8);
// Max tries to Join 1 of each (should take 1 PT and some amount of Target)
max.join(1e8, 1e9);
assertEq(pt.balanceOf(address(max)), 99e8);
// Compare Target pulled from max's account to the normal, 18 decimal case
jim.join(0, 1e18);
sid.swapIn(true, 1e18);
jim.join(1e18, 1e18);
// Determine Jim's Target balance in 9 decimals
uint256 jimTargetBalance = target.balanceOf(address(jim)) /
10**(18 - _target.decimals());
assertClose(_target.balanceOf(address(max)), jimTargetBalance, 1e6);
}
function testDifferentDecimalsMinReserves() public {
// Set PT/YT to 8 decimals
MockDividerSpace divider = new MockDividerSpace(8);
divider.initSeries(maturity);
// Set Target to 9 decima;s
MockAdapterSpace adapter = new MockAdapterSpace(9);
adapter.setScale(INIT_SCALE);
SpaceFactory spaceFactory = new SpaceFactory(
vault,
address(divider),
ts,
g1,
g2,
true
);
Space space = Space(spaceFactory.create(address(adapter), maturity));
(address _pt, , , , , , , , ) = MockDividerSpace(divider).series(
address(adapter),
maturity
);
ERC20Mintable pt = ERC20Mintable(_pt);
ERC20Mintable _target = ERC20Mintable(adapter.target());
User max = new User(vault, space, pt, _target);
_target.mint(address(max), 100e9);
pt.mint(address(max), 100e8);
User eve = new User(vault, space, pt, _target);
_target.mint(address(eve), 100e9);
pt.mint(address(eve), 100e8);
max.join(0, 5e9);
// Init the PT side of the pool
eve.swapIn(true, 1e8);
// Swaps work
eve.swapIn(true, 1e8);
eve.swapOut(false, 1e8);
emit log_named_uint("bpt", space.totalSupply());
// Exit everything
max.exit(space.balanceOf(address(max)));
(, uint256[] memory balances, ) = vault.getPoolTokens(
space.getPoolId()
);
// Reserves get stripped down to 1:1 due to downscaling
assertEq(balances[0], 1);
assertEq(balances[1], 1);
emit log_named_uint("bpt", space.totalSupply());
max.join(5e8, 5e9);
(, balances, ) = vault.getPoolTokens(space.getPoolId());
// Reserves are now equal, regardless of differences in decimals
assertEq(balances[0], 500000001);
assertEq(balances[1], 500000001);
emit log_named_uint("bpt", space.totalSupply());
// Swaps are borked
vm.expectRevert("BAL#001");
eve.swapIn(true, 1e8);
// BPT is just a multiple of the reserves on both sides, disconnected from the YS invariant
assertEq(space.totalSupply(), 500000001000000);
}
// companion test to testSmallDecimalsGuardInvalidState, the primary difference is that Sia does not join any liquidity
function testFailSmallDecimalsGuardInvalidState(
uint64 joinAmt,
uint64 swapInAmt1,
uint64 swapInAmt2
) public {
vm.assume(joinAmt / 2 > swapInAmt1);
vm.assume(swapInAmt1 / 2 > swapInAmt2);
// No tiny swaps
vm.assume(swapInAmt2 >= 1e7);
MockDividerSpace divider = new MockDividerSpace(8);
divider.initSeries(maturity);
MockAdapterSpace adapter = new MockAdapterSpace(8);
SpaceFactory spaceFactory = new SpaceFactory(
vault,
address(divider),
ts,
g1,
g2,
true
);
Space space = Space(spaceFactory.create(address(adapter), maturity));
(address _pt, , , , , , , , ) = MockDividerSpace(divider).series(
address(adapter),
maturity
);
ERC20Mintable pt = ERC20Mintable(_pt);
ERC20Mintable _target = ERC20Mintable(adapter.target());
User max = new User(vault, space, pt, _target);
_target.mint(address(max), uint256(joinAmt) * 2);
pt.mint(address(max), uint256(joinAmt) * 2);
User eve = new User(vault, space, pt, _target);
pt.mint(address(eve), swapInAmt1 + swapInAmt2);
max.join(0, joinAmt);
eve.swapIn(true, swapInAmt1);
max.exit(space.balanceOf(address(max)));
(, uint256[] memory balances, ) = vault.getPoolTokens(
space.getPoolId()
);
assertTrue(
!((balances[0] == 0 || balances[0] == 1) &&
(balances[1] == 0 || balances[1] == 1))
);
// Even though max re-joins all of his liquidity again...
max.join(joinAmt, joinAmt);
// ...eve's swapIn fails
eve.swapIn(true, swapInAmt2);
}
// companion test to testFailSmallDecimalNoLockedLiquidity, the primary difference is that Sia keeps a tiny amount of liquidity locked
function testSmallDecimalsGuardInvalidState(
uint64 joinAmt,
uint64 swapInAmt1,
uint64 swapInAmt2
) public {
vm.assume(joinAmt / 2 > swapInAmt1);
vm.assume(swapInAmt1 / 2 > swapInAmt2);
// No tiny swaps
vm.assume(swapInAmt2 >= 1e7);
MockDividerSpace divider = new MockDividerSpace(8);
divider.initSeries(maturity);
MockAdapterSpace adapter = new MockAdapterSpace(8);
SpaceFactory spaceFactory = new SpaceFactory(
vault,
address(divider),
ts,
g1,
g2,
true
);
Space space = Space(spaceFactory.create(address(adapter), maturity));
(address _pt, , , , , , , , ) = MockDividerSpace(divider).series(
address(adapter),
maturity
);
ERC20Mintable _target = ERC20Mintable(adapter.target());
User max = new User(vault, space, ERC20Mintable(_pt), _target);
_target.mint(address(max), uint256(joinAmt) * 2);
ERC20Mintable(_pt).mint(address(max), uint256(joinAmt) * 2);
User eve = new User(vault, space, ERC20Mintable(_pt), _target);
ERC20Mintable(_pt).mint(address(eve), swapInAmt1 + swapInAmt2);
User sia = new User(vault, space, ERC20Mintable(_pt), _target);
_target.mint(address(sia), 1e8);
// Sia keeps a little seed liquidity locked in the pool
sia.join(0, 1e8);
max.join(0, joinAmt);
// Init PTs
eve.swapIn(true, swapInAmt1);
// Exit everything besides Sia's seed liquidity
max.exit(space.balanceOf(address(max)));
(, uint256[] memory balances, ) = vault.getPoolTokens(
space.getPoolId()
);
assertTrue(
!((balances[0] == 0 || balances[0] == 1) &&
(balances[1] == 0 || balances[1] == 1))
);
// Re-join all of Max's liquidity
max.join(joinAmt, joinAmt);
// Swap in succeeds
eve.swapIn(true, swapInAmt2);
}
function testNonMonotonicScale() public {
adapter.setScale(1e18);
jim.join(0, 10e18);
sid.swapIn(true, 5.5e18);
jim.join(10e18, 10e18);
adapter.setScale(1.5e18);
jim.join(10e18, 10e18);
uint256 targetOut1 = sid.swapIn(true, 5.5e18);
adapter.setScale(1e18);
jim.join(10e18, 10e18);
uint256 targetOut2 = sid.swapIn(true, 5.5e18);
// Set scale to below the initial scale
adapter.setScale(0.5e18);
jim.join(10e18, 10e18);
uint256 targetOut3 = sid.swapIn(true, 5.5e18);
// Receive more and more Target out as the Scale value decreases
assertGt(targetOut3, targetOut2);
assertGt(targetOut2, targetOut1);
}