-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathGeneralDistributionAgreement.t.sol
More file actions
1249 lines (1028 loc) · 53 KB
/
GeneralDistributionAgreement.t.sol
File metadata and controls
1249 lines (1028 loc) · 53 KB
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: AGPLv3
pragma solidity ^0.8.23;
import { EnumerableSet } from "@openzeppelin-v5/contracts/utils/structs/EnumerableSet.sol";
import { SafeCast } from "@openzeppelin-v5/contracts/utils/math/SafeCast.sol";
import { IAccessControl } from "@openzeppelin-v5/contracts/access/IAccessControl.sol";
import "@superfluid-finance/solidity-semantic-money/src/SemanticMoney.sol";
import "../../FoundrySuperfluidTester.t.sol";
import {
GeneralDistributionAgreementV1,
IGeneralDistributionAgreementV1
} from "../../../../contracts/agreements/gdav1/GeneralDistributionAgreementV1.sol";
import { SuperTokenV1Library } from "../../../../contracts/apps/SuperTokenV1Library.sol";
import { SuperfluidUpgradeableBeacon } from "../../../../contracts/upgradability/SuperfluidUpgradeableBeacon.sol";
import { ISuperToken, SuperToken } from "../../../../contracts/superfluid/SuperToken.sol";
import { ISuperfluidToken } from "../../../../contracts/interfaces/superfluid/ISuperfluidToken.sol";
import { ISuperfluidPool, SuperfluidPool } from "../../../../contracts/agreements/gdav1/SuperfluidPool.sol";
import { IPoolNFTBase } from "../../../../contracts/interfaces/agreements/gdav1/IPoolNFTBase.sol";
import { IPoolAdminNFT } from "../../../../contracts/interfaces/agreements/gdav1/IPoolAdminNFT.sol";
import { SuperfluidPoolStorageLayoutMock } from "./SuperfluidPoolUpgradabilityMock.t.sol";
/// @title GeneralDistributionAgreementV1 Integration Tests
/// @author Superfluid
/// @notice This is a contract that runs integrations tests for the GDAv1
/// It tests interactions between contracts and more complicated interactions
/// with a range of values when applicable and it aims to ensure that the
/// these interactions work as expected.
contract GeneralDistributionAgreementV1IntegrationTest is FoundrySuperfluidTester {
using SuperTokenV1Library for ISuperToken;
using EnumerableSet for EnumerableSet.AddressSet;
using SafeCast for uint256;
using SafeCast for int256;
struct UpdateMemberData {
address member;
uint64 newUnits;
}
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/// @dev The freePool uses `poolConfig` where both transfer and distributeFromAnyAddress is true
SuperfluidPool public freePool;
uint256 public liquidationPeriod;
constructor() FoundrySuperfluidTester(10) { }
function setUp() public override {
super.setUp();
vm.startPrank(alice);
freePool = SuperfluidPool(address(superToken.createPool(alice, poolConfig)));
_addAccount(address(freePool));
vm.stopPrank();
(liquidationPeriod,) = sf.governance.getPPPConfig(sf.host, superToken);
}
function _getMembers(uint8 length) internal view returns (address[] memory) {
if (length > TEST_ACCOUNTS.length - 2) revert("Too many members");
address[] memory members = new address[](length);
for (uint8 i = 0; i < length; ++i) {
// do not use Admin and Alice
members[i] = TEST_ACCOUNTS[i + 2];
}
return members;
}
/*//////////////////////////////////////////////////////////////////////////
GDA Integration Tests
//////////////////////////////////////////////////////////////////////////*/
function testInitializeGDA(SuperfluidUpgradeableBeacon beacon) public {
GeneralDistributionAgreementV1 gdaV1 = new GeneralDistributionAgreementV1(sf.host, beacon);
assertEq(address(gdaV1.superfluidPoolBeacon()), address(beacon), "GDAv1.t: Beacon address not equal");
}
function testRevertAppendIndexUpdateByPoolByNonPool(BasicParticle memory p, Time t) public {
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_ONLY_SUPER_TOKEN_POOL.selector);
sf.gda.appendIndexUpdateByPool(superToken, p, t);
}
function testRevertPoolSettleClaimByNonPool(address claimRecipient, int256 amount) public {
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_ONLY_SUPER_TOKEN_POOL.selector);
sf.gda.poolSettleClaim(superToken, claimRecipient, amount);
}
function testProxiableUUIDIsExpectedValue(PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
assertEq(
SuperfluidPool(address(pool)).proxiableUUID(),
keccak256("org.superfluid-finance.contracts.SuperfluidPool.implementation")
);
}
function testPositiveBalanceIsPatricianPeriodNow(address account) public view {
(bool isPatricianPeriod,) = sf.gda.isPatricianPeriodNow(superToken, account);
assertEq(isPatricianPeriod, true);
}
function testNegativeBalanceIsPatricianPeriodNowIsTrue(PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
uint256 balance = superToken.balanceOf(alice);
int96 flowRate = balance.toInt256().toInt96() / type(int32).max;
int96 requestedDistributionFlowRate = int96(flowRate);
_helperConnectPool(bob, superToken, pool);
_helperUpdateMemberUnits(pool, alice, bob, 1);
_helperDistributeFlow(superToken, alice, alice, pool, requestedDistributionFlowRate);
_helperWarpToCritical(superToken, alice, 1);
(bool isPatricianPeriod,) = sf.gda.isPatricianPeriodNow(superToken, alice);
assertEq(isPatricianPeriod, true);
}
function testNegativeBalanceIsPatricianPeriodNowIsFalse(PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
uint256 balance = superToken.balanceOf(alice);
int96 flowRate = balance.toInt256().toInt96() / type(int32).max;
int96 requestedDistributionFlowRate = int96(flowRate);
_helperConnectPool(bob, superToken, pool);
_helperUpdateMemberUnits(pool, alice, bob, 1);
(int96 actualDistributionFlowRate,) =
sf.gda.estimateFlowDistributionActualFlowRate(superToken, alice, pool, requestedDistributionFlowRate);
_helperDistributeFlow(superToken, alice, alice, pool, requestedDistributionFlowRate);
if (actualDistributionFlowRate > 0) {
_helperWarpToInsolvency(superToken, alice, liquidationPeriod, 1);
}
(bool isPatricianPeriod,) = sf.gda.isPatricianPeriodNow(superToken, alice);
assertEq(isPatricianPeriod, false);
}
function testNegativeBalanceIsPatricianPeriodNowIsFalseWithZeroDeposit(PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
uint256 aliceBalance = superToken.balanceOf(alice);
int96 flowRate = aliceBalance.toInt256().toInt96() / type(int32).max;
int96 requestedDistributionFlowRate = int96(flowRate);
vm.startPrank(sf.governance.owner());
sf.governance.setRewardAddress(sf.host, ISuperfluidToken(address(0)), alice);
vm.stopPrank();
_helperConnectPool(bob, superToken, pool);
_helperUpdateMemberUnits(pool, alice, bob, 1);
(int256 aliceRTB, uint256 deposit,,) = superToken.realtimeBalanceOfNow(alice);
_helperDistributeFlow(superToken, alice, alice, pool, requestedDistributionFlowRate);
int96 fr = sf.gda.getFlowRate(superToken, alice, pool);
vm.warp(block.timestamp + (INIT_SUPER_TOKEN_BALANCE / uint256(uint96(fr))) + 1);
(aliceRTB, deposit,,) = superToken.realtimeBalanceOfNow(alice);
_helperDistributeFlow(superToken, bob, alice, pool, 0);
(bool isPatricianPeriod,) = sf.gda.isPatricianPeriodNow(superToken, alice);
assertEq(isPatricianPeriod, false, "false patrician period");
}
function testCreatePool(bool useForwarder, PoolConfig memory config) public {
_helperCreatePool(superToken, alice, alice, useForwarder, config);
}
function testCreatePoolWithCustomERC20Metadata(PoolConfig memory config, uint8 decimals) public {
vm.assume(decimals < 32);
_helperCreatePoolWithCustomERC20Metadata(
superToken, alice, alice, config, PoolERC20Metadata("My SuperToken", "MYST", decimals)
);
}
function testRevertConnectPoolByNonHost(address notHost, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
vm.assume(notHost != address(sf.host));
vm.startPrank(notHost);
vm.expectRevert("unauthorized host");
sf.gda.connectPool(pool, "0x");
vm.stopPrank();
}
function testRevertNonHostDisconnectPool(address notHost, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
vm.assume(notHost != address(sf.host));
vm.startPrank(notHost);
vm.expectRevert("unauthorized host");
sf.gda.disconnectPool(pool, "0x");
vm.stopPrank();
}
function testConnectPool(address caller, bool useForwarder, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, caller, alice, useForwarder, config);
_helperConnectPool(caller, superToken, pool, useForwarder);
}
function testDisconnectPool(address caller, bool useForwarder, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, caller, alice, useForwarder, config);
_helperConnectPool(caller, superToken, pool, useForwarder);
_helperDisconnectPool(caller, superToken, pool, useForwarder);
}
function testRevertDistributeFlowToNonPool(int96 requestedFlowRate) public {
vm.assume(requestedFlowRate >= 0);
vm.assume(requestedFlowRate < int96(type(int64).max));
vm.startPrank(alice);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_ONLY_SUPER_TOKEN_POOL.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distributeFlow, (superToken, alice, ISuperfluidPool(bob), requestedFlowRate, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeFlowToPoolOfWrongToken(int96 requestedFlowRate) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, poolConfig);
vm.assume(requestedFlowRate >= 0);
vm.assume(requestedFlowRate < int96(type(int64).max));
ISuperToken badToken = sfDeployer.deployNativeAssetSuperToken("Super Bad", "BADx");
vm.startPrank(alice);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_ONLY_SUPER_TOKEN_POOL.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distributeFlow, (badToken, alice, pool, requestedFlowRate, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeFromAnyAddressWhenNotAllowed(bool useForwarder) public {
PoolConfig memory config = PoolConfig({ transferabilityForUnitsOwner: true, distributionFromAnyAddress: false });
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, useForwarder, config);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_DISTRIBUTE_FROM_ANY_ADDRESS_NOT_ALLOWED.selector);
vm.startPrank(bob);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distribute, (superToken, bob, pool, 1, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeFlowFromAnyAddressWhenNotAllowed(bool useForwarder) public {
PoolConfig memory config = PoolConfig({ transferabilityForUnitsOwner: true, distributionFromAnyAddress: false });
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, useForwarder, config);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_DISTRIBUTE_FROM_ANY_ADDRESS_NOT_ALLOWED.selector);
vm.startPrank(bob);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distributeFlow, (superToken, bob, pool, 1, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertIfNotAdminUpdatesMemberUnitsViaGDA(bool useForwarder, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, useForwarder, config);
vm.startPrank(bob);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_NOT_POOL_ADMIN.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.updateMemberUnits, (pool, bob, 69, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertIfNotAdminOrGDAChangesMemberUnitsViaPool() public {
vm.startPrank(bob);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_NOT_POOL_ADMIN_OR_GDA.selector);
freePool.updateMemberUnits(bob, 69);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_NOT_POOL_ADMIN_OR_GDA.selector);
freePool.increaseMemberUnits(bob, 69);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_NOT_POOL_ADMIN_OR_GDA.selector);
freePool.decreaseMemberUnits(bob, 69);
vm.stopPrank();
}
function testRevertDistributeFlowWithNegativeFlowRate(int96 requestedFlowRate, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
vm.assume(requestedFlowRate < 0);
vm.startPrank(alice);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_NO_NEGATIVE_FLOW_RATE.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distributeFlow, (superToken, alice, pool, requestedFlowRate, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeToNonPool(uint256 requestedAmount) public {
vm.assume(requestedAmount < uint256(type(uint128).max));
vm.startPrank(alice);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_ONLY_SUPER_TOKEN_POOL.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distribute, (superToken, alice, ISuperfluidPool(bob), requestedAmount, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeToPoolOfWrongToken(uint256 requestedAmount) public {
vm.assume(requestedAmount < uint256(type(uint128).max));
ISuperToken badToken = sfDeployer.deployNativeAssetSuperToken("Super Bad", "BADx");
vm.startPrank(alice);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_ONLY_SUPER_TOKEN_POOL.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distribute, (badToken, alice, ISuperfluidPool(bob), requestedAmount, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeForOthers(address signer, uint256 requestedAmount) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, poolConfig);
vm.assume(requestedAmount < uint256(type(uint128).max));
vm.assume(signer != alice);
vm.startPrank(signer);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_DISTRIBUTE_FOR_OTHERS_NOT_ALLOWED.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distribute, (superToken, alice, pool, requestedAmount, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeFlowForOthers(address signer, int32 requestedFlowRate) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, poolConfig);
vm.assume(requestedFlowRate > 0);
vm.assume(signer != alice);
vm.startPrank(signer);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_DISTRIBUTE_FOR_OTHERS_NOT_ALLOWED.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distributeFlow, (superToken, alice, pool, requestedFlowRate, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeFlowInsufficientBalance(PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
uint256 balance = superToken.balanceOf(alice);
balance /= 4 hours;
int96 tooBigFlowRate = int96(int256(balance)) + 1;
_helperConnectPool(bob, superToken, pool);
_helperUpdateMemberUnits(pool, alice, bob, 1);
vm.startPrank(alice);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_INSUFFICIENT_BALANCE.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distributeFlow, (superToken, alice, pool, tooBigFlowRate, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertLiquidateNonCriticalDistributor(int32 flowRate, int96 units, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
vm.assume(flowRate > 0);
_helperConnectPool(bob, superToken, pool);
_helperUpdateMemberUnits(pool, alice, bob, uint96(units));
_helperDistributeFlow(superToken, alice, alice, pool, flowRate);
vm.startPrank(bob);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_NON_CRITICAL_SENDER.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distributeFlow, (superToken, alice, pool, 0, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertDistributeInsufficientBalance(PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
uint256 balance = superToken.balanceOf(alice);
_helperConnectPool(bob, superToken, pool);
vm.startPrank(alice);
sf.gdaV1Forwarder.updateMemberUnits(pool, bob, 1, new bytes(0));
vm.stopPrank();
vm.startPrank(alice);
vm.expectRevert(IGeneralDistributionAgreementV1.GDA_INSUFFICIENT_BALANCE.selector);
sf.host.callAgreement(
sf.gda,
abi.encodeCall(sf.gda.distribute, (superToken, alice, pool, balance + 1, new bytes(0))),
new bytes(0)
);
vm.stopPrank();
}
function testRevertPoolOperatorConnectMember(
address notOperator,
address member,
bool doConnect,
uint32 time,
PoolConfig memory config
) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
vm.assume(notOperator != address(sf.gda));
vm.startPrank(notOperator);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_NOT_GDA.selector);
SuperfluidPool(address(pool)).operatorConnectMember(member, doConnect, time);
vm.stopPrank();
}
function testRevertPoolUpdateMemberThatIsPool(uint128 units, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
vm.assume(units < uint128(type(int128).max));
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_NO_POOL_MEMBERS.selector);
vm.startPrank(alice);
pool.updateMemberUnits(address(pool), units);
vm.stopPrank();
}
function testSuperfluidPoolStorageLayout() public {
SuperfluidPoolStorageLayoutMock mock = new SuperfluidPoolStorageLayoutMock(sf.gda);
mock.validateStorageLayout();
}
function testDistributeFlowUsesMinDepositWhenFlowDepositIsLess(
uint64 distributionFlowRate,
uint32 minDepositFlowRate,
address member,
FoundrySuperfluidTester._StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
vm.assume(distributionFlowRate < minDepositFlowRate);
vm.assume(distributionFlowRate > 0);
vm.assume(member != address(pool));
vm.assume(member != address(freePool)); // yes, this also happened
vm.assume(member != address(0));
_addAccount(member);
vm.startPrank(address(sf.governance.owner()));
uint256 minimumDeposit = 4 hours * uint256(minDepositFlowRate);
sf.governance.setSuperTokenMinimumDeposit(sf.host, superToken, minimumDeposit);
vm.stopPrank();
_helperConnectPool(member, superToken, pool);
_helperUpdateMemberUnits(pool, alice, member, 1, useBools_);
_helperDistributeFlow(superToken, alice, alice, pool, int96(int64(distributionFlowRate)));
(, uint256 buffer,,) = superToken.realtimeBalanceOfNow(alice);
assertEq(buffer, minimumDeposit, "GDAv1.t: Min buffer should be used");
}
function testDistributeFlowIgnoresMinDepositWhenFlowDepositIsGreater(
int32 distributionFlowRate,
uint32 minDepositFlowRate,
address member,
FoundrySuperfluidTester._StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
vm.assume(uint32(distributionFlowRate) >= minDepositFlowRate);
vm.assume(distributionFlowRate > 0);
vm.assume(member != address(0));
vm.assume(member != address(freePool));
_addAccount(member);
vm.startPrank(address(sf.governance.owner()));
uint256 minimumDeposit = 4 hours * uint256(minDepositFlowRate);
sf.governance.setSuperTokenMinimumDeposit(sf.host, superToken, minimumDeposit);
vm.stopPrank();
_helperConnectPool(member, superToken, pool);
_helperUpdateMemberUnits(pool, alice, member, 1, useBools_);
_helperDistributeFlow(superToken, alice, alice, pool, int96(distributionFlowRate));
(, uint256 buffer,,) = superToken.realtimeBalanceOfNow(alice);
assertTrue(buffer >= minimumDeposit, "GDAv1.t: Buffer should be >= minDeposit");
}
function testDistributeFlowToConnectedMemberSendingToCFA(
int32 flowRate,
uint64 units,
FoundrySuperfluidTester._StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
vm.assume(flowRate > 0);
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
int96 requestedDistributionFlowRate = int96(flowRate);
uint128 memberUnits = uint128(units);
_helperUpdateMemberUnits(pool, alice, bob, memberUnits, useBools_);
_helperDistributeFlow(superToken, alice, alice, pool, requestedDistributionFlowRate);
_helperConnectPool(bob, superToken, pool);
// bob sends a flow of 1 to alice
vm.startPrank(bob);
superToken.createFlow(alice, requestedDistributionFlowRate * 10);
vm.stopPrank();
int96 aliceGDANetFlowRate = sf.gda.getNetFlow(superToken, alice);
int96 bobGDANetFlowRate = sf.gda.getNetFlow(superToken, bob);
int96 aliceCFANetFlowRate = sf.cfa.getNetFlow(superToken, alice);
int96 bobCFANetFlowRate = sf.cfa.getNetFlow(superToken, bob);
assertEq(
aliceGDANetFlowRate + bobGDANetFlowRate + aliceCFANetFlowRate + bobCFANetFlowRate,
0,
"alice and bob GDA net flow rates !="
);
}
function testDistributeToEmptyPool(uint64 distributionAmount, bool useForwarder, PoolConfig memory config) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, useForwarder, config);
_helperDistributeViaGDA(superToken, alice, alice, pool, distributionAmount, useForwarder);
}
function testDistributeFlowToEmptyPool(int32 flowRate, bool useForwarder, PoolConfig memory config) public {
vm.assume(flowRate >= 0);
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, useForwarder, config);
_helperDistributeFlow(superToken, alice, alice, pool, flowRate, useForwarder);
assertEq(sf.gda.getFlowRate(superToken, alice, pool), 0, "GDAv1.t: distributionFlowRate should be 0");
}
function testDistributeFlowCriticalLiquidation(
uint64 units,
_StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
uint256 balance = superToken.balanceOf(alice);
int96 flowRate = balance.toInt256().toInt96() / type(int32).max;
int96 requestedDistributionFlowRate = int96(flowRate);
uint128 memberUnits = uint128(units);
_helperConnectPool(bob, superToken, pool);
_helperUpdateMemberUnits(pool, alice, bob, memberUnits, useBools_);
(int96 actualDistributionFlowRate,) =
sf.gda.estimateFlowDistributionActualFlowRate(superToken, alice, pool, requestedDistributionFlowRate);
_helperDistributeFlow(superToken, alice, alice, pool, requestedDistributionFlowRate);
if (actualDistributionFlowRate > 0) {
_helperWarpToCritical(superToken, alice, 1);
_helperDistributeFlow(superToken, bob, alice, pool, 0);
}
}
function testDistributeFlowInsolventLiquidation(
uint64 units,
_StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
uint256 balance = superToken.balanceOf(alice);
int96 flowRate = balance.toInt256().toInt96() / type(int32).max;
int96 requestedDistributionFlowRate = int96(flowRate);
uint128 memberUnits = uint128(units);
_helperConnectPool(bob, superToken, pool);
_helperUpdateMemberUnits(pool, alice, bob, memberUnits, useBools_);
_helperDistributeFlow(superToken, alice, alice, pool, requestedDistributionFlowRate);
(int96 actualDistributionFlowRate,) =
sf.gda.estimateFlowDistributionActualFlowRate(superToken, alice, pool, requestedDistributionFlowRate);
_helperDistributeFlow(superToken, alice, alice, pool, requestedDistributionFlowRate);
if (actualDistributionFlowRate > 0) {
_helperWarpToInsolvency(superToken, alice, liquidationPeriod, 1);
_helperDistributeFlow(superToken, bob, alice, pool, 0);
}
}
function testDistributeToDisconnectedMembers(
uint64[5] memory memberUnits,
uint256 distributionAmount,
_StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
address distributor = alice;
uint256 distributorBalance = superToken.balanceOf(distributor);
vm.assume(distributionAmount < distributorBalance);
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
address[] memory members = _getMembers(5);
for (uint256 i = 0; i < members.length; ++i) {
if (sf.gda.isPool(superToken, members[i]) || members[i] == address(0)) continue;
_helperUpdateMemberUnits(pool, alice, members[i], memberUnits[i], useBools_);
}
uint256 actualAmount = sf.gda.estimateDistributionActualAmount(superToken, alice, pool, distributionAmount);
_helperDistributeViaGDA(superToken, alice, alice, pool, distributionAmount, useBools_.useForwarder);
uint128 perUnitDistributionAmount = uint128(actualAmount / pool.getTotalUnits());
for (uint256 i = 0; i < members.length; ++i) {
if (sf.gda.isPool(superToken, members[i]) || members[i] == address(0)) continue;
uint128 memberIUnits = pool.getUnits(members[i]);
assertEq(perUnitDistributionAmount * memberIUnits, pool.getTotalAmountReceivedByMember(members[i]));
}
}
function testDistributeToConnectedMembers(
uint64[5] memory memberUnits,
uint256 distributionAmount,
_StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
address distributor = alice;
uint256 distributorBalance = superToken.balanceOf(distributor);
vm.assume(distributionAmount < distributorBalance);
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
address[] memory members = _getMembers(5);
for (uint256 i = 0; i < members.length; ++i) {
if (sf.gda.isPool(superToken, members[i]) || members[i] == address(0)) continue;
_helperConnectPool(members[i], superToken, pool, useBools_.useForwarder);
_helperUpdateMemberUnits(pool, alice, members[i], memberUnits[i], useBools_);
_addAccount(members[i]);
}
uint256 actualAmount = sf.gda.estimateDistributionActualAmount(superToken, alice, pool, distributionAmount);
_helperDistributeViaGDA(superToken, alice, alice, pool, distributionAmount, useBools_.useForwarder);
uint128 perUnitDistributionAmount = uint128(actualAmount / pool.getTotalUnits());
for (uint256 i = 0; i < members.length; ++i) {
if (sf.gda.isPool(superToken, members[i]) || members[i] == address(0)) continue;
uint128 memberIUnits = pool.getUnits(members[i]);
assertEq(perUnitDistributionAmount * memberIUnits, pool.getTotalAmountReceivedByMember(members[i]));
}
}
function testDistributeFlowToConnectedMembers(
uint64[5] memory memberUnits,
int32 flowRate,
_StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
vm.assume(flowRate > 0);
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
address[] memory members = _getMembers(5);
for (uint256 i = 0; i < members.length; ++i) {
if (sf.gda.isPool(superToken, members[i]) || members[i] == address(0)) continue;
_helperConnectPool(members[i], superToken, pool, useBools_.useForwarder);
_helperUpdateMemberUnits(pool, alice, members[i], memberUnits[i], useBools_);
_addAccount(members[i]);
}
_helperDistributeFlow(superToken, alice, alice, pool, 100, useBools_.useForwarder);
int96 poolAdjustmentFlowRate = useBools_.useForwarder
? sf.gdaV1Forwarder.getPoolAdjustmentFlowRate(address(pool))
: sf.gda.getPoolAdjustmentFlowRate(address(pool));
assertEq(poolAdjustmentFlowRate, 0, "GDAv1.t: Pool adjustment rate is non-zero");
}
function testDistributeFlowToUnconnectedMembers(
uint64[5] memory memberUnits,
int32 flowRate,
uint16 warpTime,
_StackVars_UseBools memory useBools_,
PoolConfig memory config
) public {
vm.assume(flowRate > 0);
ISuperfluidPool pool;
{
pool = _helperCreatePool(superToken, alice, alice, false, config);
}
address[] memory members = _getMembers(5);
for (uint256 i = 0; i < members.length; ++i) {
if (sf.gda.isPool(superToken, members[i]) || members[i] == address(0)) continue;
_helperUpdateMemberUnits(pool, alice, members[i], memberUnits[i], useBools_);
}
int96 actualDistributionFlowRate;
{
int96 requestedFlowRate = flowRate;
_helperDistributeFlow(superToken, alice, alice, pool, requestedFlowRate, useBools_.useForwarder);
(actualDistributionFlowRate,) =
sf.gda.estimateFlowDistributionActualFlowRate(superToken, alice, pool, requestedFlowRate);
vm.warp(block.timestamp + warpTime);
}
uint128 totalUnits = pool.getTotalUnits();
for (uint256 i; i < members.length; ++i) {
if (members[i] != address(0)) {
// @note we test realtimeBalanceOfNow here as well
(int256 memberRTB,,) = sf.gda.realtimeBalanceOf(superToken, members[i], block.timestamp);
(int256 rtbNow,,,) = sf.gda.realtimeBalanceOfNow(superToken, members[i]);
assertEq(memberRTB, rtbNow, "testDistributeFlowToUnconnectedMembers: rtb != rtbNow");
assertEq(
pool.getTotalDisconnectedFlowRate(),
actualDistributionFlowRate,
"testDistributeFlowToUnconnectedMembers: pendingDistributionFlowRate != actualDistributionFlowRate"
);
(int256 memberClaimable,) = pool.getClaimableNow(members[i]);
assertEq(
memberClaimable,
totalUnits > 0
? (actualDistributionFlowRate * int96(int256(uint256(warpTime)))) * int96(uint96(memberUnits[i]))
/ uint256(totalUnits).toInt256()
: int256(0),
"testDistributeFlowToUnconnectedMembers: memberClaimable != (actualDistributionFlowRate * warpTime) / totalUnits"
);
assertEq(memberRTB, 0, "testDistributeFlowToUnconnectedMembers: memberRTB != 0");
vm.startPrank(members[i]);
if (useBools_.useGDA) {
if (useBools_.useForwarder) {
sf.gdaV1Forwarder.claimAll(pool, members[i], new bytes(0));
} else {
superToken.claimAll(pool, members[i]);
}
} else {
pool.claimAll();
}
vm.stopPrank();
(memberRTB,,) = sf.gda.realtimeBalanceOf(superToken, members[i], block.timestamp);
assertEq(
memberRTB, memberClaimable, "testDistributeFlowToUnconnectedMembers: memberRTB != memberClaimable"
);
}
}
}
// Pool ERC20 functions
function testApproveOnly(address owner, address spender, uint256 amount, PoolConfig memory config) public {
vm.assume(owner != address(0));
vm.assume(spender != address(0));
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
_helperSuperfluidPoolApprove(pool, owner, spender, amount);
}
function testIncreaseAllowance(address owner, address spender, uint256 addedValue, PoolConfig memory config)
public
{
vm.assume(owner != address(0));
vm.assume(spender != address(0));
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
_helperSuperfluidPoolIncreaseAllowance(pool, owner, spender, addedValue);
}
function testDecreaseAllowance(
address owner,
address spender,
uint256 addedValue,
uint256 subtractedValue,
PoolConfig memory config
) public {
vm.assume(owner != address(0));
vm.assume(spender != address(0));
vm.assume(addedValue >= subtractedValue);
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, false, config);
_helperSuperfluidPoolIncreaseAllowance(pool, owner, spender, addedValue);
_helperSuperfluidPoolDecreaseAllowance(pool, owner, spender, subtractedValue);
}
function testRevertIfUnitsTransferReceiverIsPool(address from, int96 unitsAmount, int128 transferAmount)
public
{
// @note we use int96 because overflow will happen otherwise
vm.assume(unitsAmount >= 0);
vm.assume(transferAmount > 0);
vm.assume(from != address(0));
vm.assume(transferAmount <= unitsAmount);
_helperUpdateMemberUnits(freePool, alice, from, uint128(int128(unitsAmount)));
vm.startPrank(from);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_NO_POOL_MEMBERS.selector);
freePool.transfer(address(freePool), uint256(uint128(transferAmount)));
vm.stopPrank();
}
function testRevertIfTransferNotAllowed(bool useForwarder) public {
PoolConfig memory config = PoolConfig({ transferabilityForUnitsOwner: false, distributionFromAnyAddress: true });
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, useForwarder, config);
_helperUpdateMemberUnits(pool, alice, bob, 1000);
vm.startPrank(bob);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_TRANSFER_UNITS_NOT_ALLOWED.selector);
pool.transfer(alice, 1000);
vm.stopPrank();
}
function testRevertIfTransferFromNotAllowed(bool useForwarder) public {
PoolConfig memory config = PoolConfig({ transferabilityForUnitsOwner: false, distributionFromAnyAddress: true });
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, useForwarder, config);
_helperUpdateMemberUnits(freePool, alice, bob, 1000);
vm.startPrank(bob);
pool.approve(carol, 1000);
vm.stopPrank();
vm.startPrank(carol);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_TRANSFER_UNITS_NOT_ALLOWED.selector);
pool.transferFrom(bob, carol, 1000);
vm.stopPrank();
}
function testBasicTransfer(
FoundrySuperfluidTester._StackVars_UseBools memory useBools_,
uint8 a, uint8 b, // One must use small sized data type to find equality cases
uint128 unitsAmount,
uint128 transferAmount
) public {
address from = address(uint160(a));
address to = address(uint160(b));
transferAmount = uint96(bound(transferAmount, 0, unitsAmount));
vm.assume(from != address(0));
_helperUpdateMemberUnits(freePool, alice, from, unitsAmount, useBools_);
if (from == to) {
vm.startPrank(from);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_SELF_TRANSFER_NOT_ALLOWED.selector);
freePool.transfer(to, transferAmount);
vm.stopPrank();
} else if (to == address(0)) {
vm.startPrank(from);
vm.expectRevert(ISuperfluidPool.SUPERFLUID_POOL_NO_ZERO_ADDRESS.selector);
freePool.transfer(to, transferAmount);
vm.stopPrank();
} else {
_helperSuperfluidPoolUnitsTransfer(freePool, from, to, uint256(uint128(transferAmount)));
}
}
function testIncreaseDecreaseMemberUnits(
address member,
uint120 increaseAmount,
uint120 decreaseAmount
) public {
vm.assume(increaseAmount >= decreaseAmount);
vm.assume(member != address(0));
vm.assume(member != address(freePool));
vm.startPrank(alice);
freePool.increaseMemberUnits(member, increaseAmount);
assertEq(freePool.getUnits(member), increaseAmount);
freePool.decreaseMemberUnits(member, decreaseAmount);
assertEq(freePool.getUnits(member), increaseAmount - decreaseAmount);
// explicitly test for overflow and underflow behaviour
freePool.updateMemberUnits(member, 10);
vm.expectRevert(stdError.arithmeticError);
freePool.increaseMemberUnits(member, type(uint128).max);
vm.expectRevert(stdError.arithmeticError);
freePool.decreaseMemberUnits(member, 11);
vm.stopPrank();
}
function testApproveAndTransferFrom(
address owner,
address spender,
int128 transferAmount,
FoundrySuperfluidTester._StackVars_UseBools memory useBools_
) public {
vm.assume(transferAmount > 0);
vm.assume(spender != address(0));
vm.assume(owner != address(0));
vm.assume(spender != owner);
_helperUpdateMemberUnits(freePool, alice, owner, uint128(int128(transferAmount)), useBools_);
_helperSuperfluidPoolApprove(freePool, owner, spender, uint256(uint128(transferAmount)));
_helperSuperfluidPoolUnitsTransferFrom(freePool, spender, owner, spender, uint256(uint128(transferAmount)));
}
function testIncreaseAllowanceAndTransferFrom(
address owner,
address spender,
int128 transferAmount,
FoundrySuperfluidTester._StackVars_UseBools memory useBools_
) public {
vm.assume(transferAmount > 0);
vm.assume(spender != address(0));
vm.assume(owner != address(0));
vm.assume(spender != owner);
_helperUpdateMemberUnits(freePool, alice, owner, uint128(int128(transferAmount)), useBools_);
_helperSuperfluidPoolIncreaseAllowance(freePool, owner, spender, uint256(uint128(transferAmount)));
_helperSuperfluidPoolUnitsTransferFrom(freePool, spender, owner, spender, uint256(uint128(transferAmount)));
}
function testGetClaimable(
address member,
uint128 units,
uint64 distributionAmount,
bool useForwarder,
PoolConfig memory config
) public {
vm.assume(member != address(0));
vm.assume(member != address(freePool));
vm.assume(member != alice); // alice is the test distributor
vm.assume(units > 0);
vm.assume(distributionAmount > 0);
vm.assume(units < distributionAmount);
vm.assume(distributionAmount < type(uint128).max);
// Create a pool for testing
ISuperfluidPool pool = _helperCreatePool(superToken, alice, alice, useForwarder, config);
_addAccount(member);
// Step 1: Assign units to disconnected member
_helperUpdateMemberUnits(pool, alice, member, units, _StackVars_UseBools({useForwarder: useForwarder, useGDA: false}));
(int256 balanceBefore,,,) = superToken.realtimeBalanceOfNow(member);
(int256 claimableBefore,) = pool.getClaimableNow(member);
// Distribute
_helperDistributeViaGDA(superToken, alice, alice, pool, distributionAmount, useForwarder);
// Check disconnected member: expect balance unchanged, claimable increased
(int256 balanceAfter1,,,) = superToken.realtimeBalanceOfNow(member);
(int256 claimableAfter1,) = pool.getClaimableNow(member);
if (!sf.gda.isMemberConnected(pool, member)) {
assertEq(balanceAfter1, balanceBefore, "Disconnected member balance should not change");
assertTrue(claimableAfter1 > claimableBefore, "Disconnected member claimable amount should increase");
}
// Step 2: Connect member and distribute again
_helperConnectPool(member, superToken, pool, useForwarder);
_helperDistributeViaGDA(superToken, alice, alice, pool, distributionAmount, useForwarder);
(int256 balanceAfter2,,,) = superToken.realtimeBalanceOfNow(member);
(int256 claimableAfter2,) = pool.getClaimableNow(member);
// Check connected member: balance increased, claimable remains 0
assertTrue(balanceAfter2 > balanceAfter1, "Connected member balance should increase");
assertEq(claimableAfter2, 0, "Connected member claimable amount should be 0");
}
function testAutoConnect(address member, uint128 units, uint64 distributionAmount) public {
vm.assume(member != address(0));
vm.assume(member != address(freePool));
vm.assume(member != alice); // alice is the test distributor
vm.assume(units > 0);
vm.assume(units < distributionAmount);
uint256 expectedAmount = (distributionAmount / units) * units;