-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathSuperTokenV1Library.sol
More file actions
1946 lines (1830 loc) · 73.6 KB
/
SuperTokenV1Library.sol
File metadata and controls
1946 lines (1830 loc) · 73.6 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: MIT
pragma solidity >= 0.8.11;
import {
ISuperfluid,
ISuperToken,
IConstantFlowAgreementV1,
IGeneralDistributionAgreementV1,
ISuperfluidPool,
PoolConfig,
PoolERC20Metadata
} from "../interfaces/superfluid/ISuperfluid.sol";
/**
* @title Library for Token Centric Interface
* @author Superfluid
* @dev Set `using for ISuperToken` in including file, and call any of these functions on an instance
* of ISuperToken.
* The architecture of the Superfluid framework and its initial API were heavily influenced by the
* gas economics on Ethereum at the time, leading to compromises in terms of API ergonomics.
* This library mitigates that by providing a more convenient Solidity API for SuperTokens.
* While most methods are just wrappers around equivalent methods in a Superfluid agreement,
* some implement higher level abstractions.
* Note using the library in foundry tests can lead to counter-intuitive behaviour.
* E.g. "prank" won't set the expected msg.sender for calls done using the library.
* Also, reverts caused by the library itself won't be recognized by foundry, because
* it expects them to happen in the context of an external call.
* This is not specific to this library, but a general limitation of foundry when using libraries in tests.
*/
library SuperTokenV1Library {
/** AGREEMENT-ABSTRACTED FUNCTIONS ************************************* */
/**
* @dev creates a flow to an account or to pool members.
* If the receiver is an account, it uses the CFA, if it's a pool it uses the GDA.
* @param token Super token address
* @param receiverOrPool The receiver (account) or pool
* @param flowRate the flowRate to be set.
* @return newFlowRate The new flow rate after the operation.
* Note that all the specifics of the underlying agreement used still apply.
* E.g. if the GDA is used, the effective flowRate may differ from the selected one.
*/
function flowX(
ISuperToken token,
address receiverOrPool,
int96 flowRate
) internal returns(int96 newFlowRate) {
address sender = address(this);
(, IGeneralDistributionAgreementV1 gda) = _getAndCacheHostAndGDA(token);
if (gda.isPool(token, receiverOrPool)) {
return distributeFlow(
token,
sender,
ISuperfluidPool(receiverOrPool),
flowRate
);
} else {
flow(token, receiverOrPool, flowRate);
return flowRate;
}
}
/**
* @dev transfers `amount` to an account or distributes it to pool members.
* @param token Super token address
* @param receiverOrPool The receiver (account) or pool
* @param amount the amount to be transferred/distributed
* @return distributedAmount The amount actually transferred/distributed
* Note in case of distribution, the effective amount may be smaller than requested.
*/
function transferX(
ISuperToken token,
address receiverOrPool,
uint256 amount
) internal returns(uint256 distributedAmount) {
address sender = address(this);
(, IGeneralDistributionAgreementV1 gda) = _getAndCacheHostAndGDA(token);
if (gda.isPool(token, receiverOrPool)) {
return distribute(
token,
sender,
ISuperfluidPool(receiverOrPool),
amount
);
} else {
token.transfer(receiverOrPool, amount);
return amount;
}
}
/** AGREEMENT-ABSTRACTED VIEW FUNCTIONS ************************************* */
/**
* @dev get flow rate between two accounts for given token
* @param token The token used in flow
* @param sender The sender of the flow
* @param receiverOrPool The receiver or pool receiving or distributing the flow
* @return flowRate The flow rate
* Note: edge case: if a CFA stream is going to a pool, it will return 0.
*/
function getFlowRate(ISuperToken token, address sender, address receiverOrPool)
internal view returns(int96 flowRate)
{
if (_isPool(token, receiverOrPool)) {
(, IGeneralDistributionAgreementV1 gda) = _getHostAndGDA(token);
(, flowRate,) = gda.getFlow(token, sender, ISuperfluidPool(receiverOrPool));
} else {
(, IConstantFlowAgreementV1 cfa) = _getHostAndCFA(token);
(, flowRate, , ) = cfa.getFlow(token, sender, receiverOrPool);
}
}
/**
* @dev get flow info between an account and another account or pool for given token
* @param token The token used in flow
* @param sender The sender of the flow
* @param receiverOrPool The receiver or pool receiving or distributing the flow
* @return lastUpdated Timestamp of flow creation or last flowrate change
* @return flowRate The flow rate
* @return deposit The amount of deposit the flow
* @return owedDeposit The amount of owed deposit of the flow
* Note: edge case: a CFA stream going to a pool will not be "seen".
*/
function getFlowInfo(ISuperToken token, address sender, address receiverOrPool)
internal view
returns(uint256 lastUpdated, int96 flowRate, uint256 deposit, uint256 owedDeposit)
{
if (_isPool(token, receiverOrPool)) {
(, IGeneralDistributionAgreementV1 gda) = _getHostAndGDA(token);
(lastUpdated, flowRate, deposit) = gda.getFlow(token, sender, ISuperfluidPool(receiverOrPool));
} else {
(, IConstantFlowAgreementV1 cfa) = _getHostAndCFA(token);
(lastUpdated, flowRate, deposit, owedDeposit) = cfa.getFlow(token, sender, receiverOrPool);
}
}
/**
* @dev get net flow rate for given account for given token (CFA + GDA)
* @param token Super token address
* @param account Account to query
* @return flowRate The net flow rate of the account
*/
function getNetFlowRate(ISuperToken token, address account)
internal view returns (int96 flowRate)
{
(, IConstantFlowAgreementV1 cfa) = _getHostAndCFA(token);
(, IGeneralDistributionAgreementV1 gda) = _getHostAndGDA(token);
int96 cfaNetFlow = cfa.getNetFlow(token, account);
int96 gdaNetFlow = gda.getNetFlow(token, account);
return cfaNetFlow + gdaNetFlow;
}
/**
* @dev get the aggregated flow info of the account (CFA + GDA)
* @param token Super token address
* @param account Account to query
* @return lastUpdated Timestamp of the last change of the net flow
* @return flowRate The net flow rate of token for account
* @return deposit The sum of all deposits for account's flows
* @return owedDeposit The sum of all owed deposits for account's flows
*/
function getNetFlowInfo(ISuperToken token, address account)
internal
view
returns (uint256 lastUpdated, int96 flowRate, uint256 deposit, uint256 owedDeposit)
{
(, IConstantFlowAgreementV1 cfa) = _getHostAndCFA(token);
(, IGeneralDistributionAgreementV1 gda) = _getHostAndGDA(token);
{
(uint256 lastUpdatedCFA, int96 cfaNetFlowRate, uint256 cfaDeposit, uint256 cfaOwedDeposit) =
cfa.getAccountFlowInfo(token, account);
lastUpdated = lastUpdatedCFA;
flowRate += cfaNetFlowRate;
deposit += cfaDeposit;
owedDeposit += cfaOwedDeposit;
}
{
(uint256 lastUpdatedGDA, int96 gdaNetFlowRate, uint256 gdaDeposit) = gda.getAccountFlowInfo(token, account);
if (lastUpdatedGDA > lastUpdated) {
lastUpdated = lastUpdatedGDA;
}
flowRate += gdaNetFlowRate;
deposit += gdaDeposit;
}
}
/**
* @dev calculate buffer needed for a CFA flow with the given flowrate (for GDA, see 2nd notice below)
* @notice the returned amount is exact only for the scenario where no flow exists before.
* In order to get the buffer delta for a delta flowrate, you need to get the buffer amount
* for the new total flowrate and subtract the previous buffer.
* That's because there's not always linear proportionality between flowrate and buffer.
* @notice for GDA flows, the required buffer is typically slightly lower.
* That's due to an implementation detail (round-up "clipping" to 64 bit in the CFA).
* The return value of this method is thus to be considered not a precise value, but a
* lower bound for GDA flows.
* @param token The token used in flow
* @param flowRate The flowrate to calculate the needed buffer for
* @return bufferAmount The buffer amount based on flowRate, liquidationPeriod and minimum deposit
*/
function getBufferAmountByFlowRate(ISuperToken token, int96 flowRate) internal view
returns (uint256 bufferAmount)
{
(, IConstantFlowAgreementV1 cfa) = _getHostAndCFA(token);
return cfa.getDepositRequiredForFlowRate(token, flowRate);
}
/** CFA BASE FUNCTIONS ************************************* */
/**
* @dev Sets the given CFA flowrate between the caller and a given receiver.
* If there's no pre-existing flow and `flowRate` non-zero, a new flow is created.
* If there's an existing flow and `flowRate` non-zero, the flowRate of that flow is updated.
* If there's an existing flow and `flowRate` zero, the flow is deleted.
* If the existing and given flowRate are equal, no action is taken.
* On creation of a flow, a "buffer" amount is automatically detracted from the sender account's available balance.
* If the sender account is solvent when the flow is deleted, this buffer is redeemed to it.
* @param token Super token address
* @param receiver The receiver of the flow
* @param flowRate The wanted flowrate in wad/second. Only positive values are valid here.
* @return bool
*/
function flow(
ISuperToken token,
address receiver,
int96 flowRate
) internal returns (bool) {
return flow(token, receiver, flowRate, new bytes(0));
}
/**
* @dev Set CFA flowrate with userData
* @param token Super token address
* @param receiver The receiver of the flow
* @param flowRate The wanted flowrate in wad/second. Only positive values are valid here.
* @param userData The userdata passed along with call
* @return bool
*/
function flow(
ISuperToken token,
address receiver,
int96 flowRate,
bytes memory userData
) internal returns (bool) {
// note: from the lib's perspective, the caller is "this", NOT "msg.sender"
address sender = address(this);
int96 prevFlowRate = getCFAFlowRate(token, sender, receiver);
if (flowRate > 0) {
if (prevFlowRate == 0) {
return createFlow(token, receiver, flowRate, userData);
} else if (prevFlowRate != flowRate) {
return updateFlow(token, receiver, flowRate, userData);
} // else no change, do nothing
return true;
} else if (flowRate == 0) {
if (prevFlowRate > 0) {
return deleteFlow(token, sender, receiver, userData);
} // else no change, do nothing
return true;
} else {
// can't set negative flowrate
revert IConstantFlowAgreementV1.CFA_INVALID_FLOW_RATE();
}
}
/**
* @dev Create flow without userData
* @param token The token used in flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
*/
function createFlow(ISuperToken token, address receiver, int96 flowRate)
internal returns (bool)
{
return createFlow(token, receiver, flowRate, new bytes(0));
}
/**
* @dev Create flow with userData
* @param token The token used in flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
* @param userData The userdata passed along with call
*/
function createFlow(ISuperToken token, address receiver, int96 flowRate, bytes memory userData)
internal returns (bool)
{
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.createFlow,
(token, receiver, flowRate, new bytes(0))
),
userData // userData
);
return true;
}
/**
* @dev Update flow without userData
* @param token The token used in flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
*/
function updateFlow(ISuperToken token, address receiver, int96 flowRate)
internal returns (bool)
{
return updateFlow(token, receiver, flowRate, new bytes(0));
}
/**
* @dev Update flow with userData
* @param token The token used in flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
* @param userData The userdata passed along with call
*/
function updateFlow(ISuperToken token, address receiver, int96 flowRate, bytes memory userData)
internal returns (bool)
{
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.updateFlow,
(token, receiver, flowRate, new bytes(0))
),
userData
);
return true;
}
/**
* @dev Delete flow without userData
* @param token The token used in flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
*/
function deleteFlow(ISuperToken token, address sender, address receiver)
internal returns (bool)
{
return deleteFlow(token, sender, receiver, new bytes(0));
}
/**
* @dev Delete flow with userData
* @param token The token used in flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param userData The userdata passed along with call
*/
function deleteFlow(ISuperToken token, address sender, address receiver, bytes memory userData)
internal returns (bool)
{
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.deleteFlow,
(token, sender, receiver, new bytes(0))
),
userData
);
return true;
}
/** CFA ACL ************************************* */
/**
* @notice Like `flow`, but can be invoked by an account with flowOperator permissions
* on behalf of the sender account.
* @param token Super token address
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param flowRate The wanted flowRate in wad/second. Only positive values are valid here.
* @return bool
*/
function flowFrom(
ISuperToken token,
address sender,
address receiver,
int96 flowRate
) internal returns (bool) {
return flowFrom(token, sender, receiver, flowRate, new bytes(0));
}
/**
* @notice Like `flowFrom`, but takes userData
* @param token Super token address
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param flowRate The wanted flowRate in wad/second. Only positive values are valid here.
* @param userData The userdata passed along with call
* @return bool
*/
function flowFrom(
ISuperToken token,
address sender,
address receiver,
int96 flowRate,
bytes memory userData
) internal returns (bool) {
int96 prevFlowRate = getCFAFlowRate(token, sender, receiver);
if (flowRate > 0) {
if (prevFlowRate == 0) {
return createFlowFrom(token, sender, receiver, flowRate, userData);
} else if (prevFlowRate != flowRate) {
return updateFlowFrom(token, sender, receiver, flowRate, userData);
} // else no change, do nothing
return true;
} else if (flowRate == 0) {
if (prevFlowRate > 0) {
return deleteFlowFrom(token, sender, receiver, userData);
} // else no change, do nothing
return true;
} else {
revert IConstantFlowAgreementV1.CFA_INVALID_FLOW_RATE();
}
}
/**
* @dev Update permissions for flow operator
* @param token The token used in flow
* @param flowOperator The address given flow permissions
* @param allowCreate creation permissions
* @param allowUpdate update permissions
* @param allowDelete deletion permissions
* @param flowRateAllowance The allowance provided to flowOperator
*/
function setFlowPermissions(
ISuperToken token,
address flowOperator,
bool allowCreate,
bool allowUpdate,
bool allowDelete,
int96 flowRateAllowance
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
uint8 permissionsBitmask = (allowCreate ? 1 : 0)
| (allowUpdate ? 1 : 0) << 1
| (allowDelete ? 1 : 0) << 2;
host.callAgreement(
cfa,
abi.encodeCall(
cfa.updateFlowOperatorPermissions,
(token, flowOperator, permissionsBitmask, flowRateAllowance, new bytes(0))
),
new bytes(0)
);
return true;
}
/**
* @dev Update permissions for flow operator - give operator max permissions
* @param token The token used in flow
* @param flowOperator The address given flow permissions
*/
function setMaxFlowPermissions(
ISuperToken token,
address flowOperator
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.authorizeFlowOperatorWithFullControl,
(token, flowOperator, new bytes(0))
),
new bytes(0)
);
return true;
}
/**
* @dev Update permissions for flow operator - revoke all permission
* @param token The token used in flow
* @param flowOperator The address given flow permissions
*/
function revokeFlowPermissions(
ISuperToken token,
address flowOperator
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.revokeFlowOperatorWithFullControl,
(token, flowOperator, new bytes(0))
),
new bytes(0)
);
return true;
}
/**
* @dev Increases the flow rate allowance for flow operator
* @notice allowing userData to be a parameter here triggered stack too deep error
* @param token The token used in flow
* @param flowOperator The address whose flow rate allowance is increased
* @param addedFlowRateAllowance amount to increase allowance by
*/
function increaseFlowRateAllowance(ISuperToken token, address flowOperator, int96 addedFlowRateAllowance)
internal
returns (bool)
{
return increaseFlowRateAllowance(token, flowOperator, addedFlowRateAllowance, new bytes(0));
}
/**
* @dev Increases the flow rate allowance for flow operator
* @notice allowing userData to be a parameter here triggered stack too deep error
* @param token The token used in flow
* @param flowOperator The address whose flow rate allowance is increased
* @param addedFlowRateAllowance amount to increase allowance by
* @param userData The userdata passed along with call
*/
function increaseFlowRateAllowance(
ISuperToken token,
address flowOperator,
int96 addedFlowRateAllowance,
bytes memory userData
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(cfa.increaseFlowRateAllowance, (token, flowOperator, addedFlowRateAllowance, new bytes(0))),
userData
);
return true;
}
/**
* @dev Decreases the flow rate allowance for flow operator
* @notice allowing userData to be a parameter here triggered stack too deep error
* @param token The token used in flow
* @param flowOperator The address whose flow rate allowance is decreased
* @param subtractedFlowRateAllowance amount to decrease allowance by
*/
function decreaseFlowRateAllowance(ISuperToken token, address flowOperator, int96 subtractedFlowRateAllowance)
internal
returns (bool)
{
return decreaseFlowRateAllowance(token, flowOperator, subtractedFlowRateAllowance, new bytes(0));
}
/**
* @dev Decreases the flow rate allowance for flow operator
* @notice allowing userData to be a parameter here triggered stack too deep error
* @param token The token used in flow
* @param flowOperator The address whose flow rate allowance is decreased
* @param subtractedFlowRateAllowance amount to decrease allowance by
* @param userData The userdata passed along with call
*/
function decreaseFlowRateAllowance(
ISuperToken token,
address flowOperator,
int96 subtractedFlowRateAllowance,
bytes memory userData
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.decreaseFlowRateAllowance, (token, flowOperator, subtractedFlowRateAllowance, new bytes(0))
),
userData
);
return true;
}
/**
* @dev Increases the flow rate allowance for flow operator and adds the permissions
* @notice allowing userData to be a parameter here triggered stack too deep error
* @param token The token used in flow
* @param flowOperator The address whose flow rate allowance is increased
* @param permissionsToAdd The permissions to add for the flow operator
* @param addedFlowRateAllowance amount to increase allowance by
*/
function increaseFlowRateAllowanceWithPermissions(
ISuperToken token,
address flowOperator,
uint8 permissionsToAdd,
int96 addedFlowRateAllowance
) internal returns (bool) {
return
increaseFlowRateAllowanceWithPermissions(
token,
flowOperator,
permissionsToAdd,
addedFlowRateAllowance,
new bytes(0)
);
}
/**
* @dev Increases the flow rate allowance for flow operator and adds the permissions
* @notice allowing userData to be a parameter here triggered stack too deep error
* @param token The token used in flow
* @param flowOperator The address whose flow rate allowance is increased
* @param permissionsToAdd The permissions to add for the flow operator
* @param addedFlowRateAllowance amount to increase allowance by
* @param userData The userdata passed along with call
*/
function increaseFlowRateAllowanceWithPermissions(
ISuperToken token,
address flowOperator,
uint8 permissionsToAdd,
int96 addedFlowRateAllowance,
bytes memory userData
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.increaseFlowRateAllowanceWithPermissions,
(token, flowOperator, permissionsToAdd, addedFlowRateAllowance, new bytes(0))
),
userData
);
return true;
}
/**
* @dev Decreases the flow rate allowance for flow operator and removes the permissions
* @notice allowing userData to be a parameter here triggered stack too deep error
* @param token The token used in flow
* @param flowOperator The address whose flow rate allowance is subtracted
* @param permissionsToRemove The permissions to remove for the flow operator
* @param subtractedFlowRateAllowance amount to subtract allowance by
*/
function decreaseFlowRateAllowanceWithPermissions(
ISuperToken token,
address flowOperator,
uint8 permissionsToRemove,
int96 subtractedFlowRateAllowance
) internal returns (bool) {
return decreaseFlowRateAllowanceWithPermissions(
token, flowOperator, permissionsToRemove, subtractedFlowRateAllowance, new bytes(0)
);
}
/**
* @dev Decreases the flow rate allowance for flow operator and removes the permissions
* @notice allowing userData to be a parameter here triggered stack too deep error
* @param token The token used in flow
* @param flowOperator The address whose flow rate allowance is subtracted
* @param permissionsToRemove The permissions to remove for the flow operator
* @param subtractedFlowRateAllowance amount to subtract allowance by
* @param userData The userdata passed along with call
*/
function decreaseFlowRateAllowanceWithPermissions(
ISuperToken token,
address flowOperator,
uint8 permissionsToRemove,
int96 subtractedFlowRateAllowance,
bytes memory userData
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.decreaseFlowRateAllowanceWithPermissions,
(token, flowOperator, permissionsToRemove, subtractedFlowRateAllowance, new bytes(0))
),
userData
);
return true;
}
/**
* @dev Creates flow as an operator without userData
* @param token The token to flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
*/
function createFlowFrom(
ISuperToken token,
address sender,
address receiver,
int96 flowRate
) internal returns (bool) {
return createFlowFrom(token, sender, receiver, flowRate, new bytes(0));
}
/**
* @dev Creates flow as an operator with userData
* @param token The token to flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
* @param userData The user provided data
*/
function createFlowFrom(
ISuperToken token,
address sender,
address receiver,
int96 flowRate,
bytes memory userData
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.createFlowByOperator,
(token, sender, receiver, flowRate, new bytes(0))
),
userData
);
return true;
}
/**
* @dev Updates flow as an operator without userData
* @param token The token to flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
*/
function updateFlowFrom(
ISuperToken token,
address sender,
address receiver,
int96 flowRate
) internal returns (bool) {
return updateFlowFrom(token, sender, receiver, flowRate, new bytes(0));
}
/**
* @dev Updates flow as an operator with userData
* @param token The token to flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
* @param userData The user provided data
*/
function updateFlowFrom(
ISuperToken token,
address sender,
address receiver,
int96 flowRate,
bytes memory userData
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.updateFlowByOperator,
(token, sender, receiver, flowRate, new bytes(0))
),
userData
);
return true;
}
/**
* @dev Deletes flow as an operator without userData
* @param token The token to flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
*/
function deleteFlowFrom(
ISuperToken token,
address sender,
address receiver
) internal returns (bool) {
return deleteFlowFrom(token, sender, receiver, new bytes(0));
}
/**
* @dev Deletes flow as an operator with userData
* @param token The token to flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param userData The user provided data
*/
function deleteFlowFrom(
ISuperToken token,
address sender,
address receiver,
bytes memory userData
) internal returns (bool) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
host.callAgreement(
cfa,
abi.encodeCall(
cfa.deleteFlowByOperator,
(token, sender, receiver, new bytes(0))
),
userData
);
return true;
}
/** CFA With CTX FUNCTIONS ************************************* */
/**
* @dev Set CFA flowrate with context
* @param token Super token address
* @param receiver The receiver of the flow
* @param flowRate The wanted flowrate in wad/second. Only positive values are valid here.
* @param ctx Context bytes (see ISuperfluid.sol for Context struct)
* @return newCtx The updated context after the execution of the agreement function
*/
function flowWithCtx(
ISuperToken token,
address receiver,
int96 flowRate,
bytes memory ctx
) internal returns (bytes memory newCtx) {
// note: from the lib's perspective, the caller is "this", NOT "msg.sender"
address sender = address(this);
int96 prevFlowRate = getCFAFlowRate(token, sender, receiver);
if (flowRate > 0) {
if (prevFlowRate == 0) {
return createFlowWithCtx(token, receiver, flowRate, ctx);
} else if (prevFlowRate != flowRate) {
return updateFlowWithCtx(token, receiver, flowRate, ctx);
} // else no change, do nothing
return ctx;
} else if (flowRate == 0) {
if (prevFlowRate > 0) {
return deleteFlowWithCtx(token, sender, receiver, ctx);
} // else no change, do nothing
return ctx;
} else {
// can't set negative flowrate
revert IConstantFlowAgreementV1.CFA_INVALID_FLOW_RATE();
}
}
/**
* @notice Like `flowFrom`, with context
* @param token Super token address
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param flowRate The wanted flowRate in wad/second. Only positive values are valid here.
* @param ctx Context bytes (see ISuperfluid.sol for Context struct)
* @return newCtx The updated context after the execution of the agreement function
*/
function flowFromWithCtx(
ISuperToken token,
address sender,
address receiver,
int96 flowRate,
bytes memory ctx
) internal returns (bytes memory newCtx) {
int96 prevFlowRate = getCFAFlowRate(token, sender, receiver);
if (flowRate > 0) {
if (prevFlowRate == 0) {
return createFlowFromWithCtx(token, sender, receiver, flowRate, ctx);
} else if (prevFlowRate != flowRate) {
return updateFlowFromWithCtx(token, sender, receiver, flowRate, ctx);
} // else no change, do nothing
return ctx;
} else if (flowRate == 0) {
if (prevFlowRate > 0) {
return deleteFlowFromWithCtx(token, sender, receiver, ctx);
} // else no change, do nothing
return ctx;
} else {
revert IConstantFlowAgreementV1.CFA_INVALID_FLOW_RATE();
}
}
/**
* @dev Create flow with context
* @param token The token to flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
* @param ctx Context bytes (see ISuperfluid.sol for Context struct)
* @return newCtx The updated context after the execution of the agreement function
*/
function createFlowWithCtx(
ISuperToken token,
address receiver,
int96 flowRate,
bytes memory ctx
) internal returns (bytes memory newCtx) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
(newCtx, ) = host.callAgreementWithContext(
cfa,
abi.encodeCall(
cfa.createFlow,
(
token,
receiver,
flowRate,
new bytes(0) // placeholder
)
),
"0x",
ctx
);
}
/**
* @dev Create flow by operator with context
* @param token The token to flow
* @param sender The sender of the flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
* @param ctx Context bytes (see ISuperfluid.sol for Context struct)
* @return newCtx The updated context after the execution of the agreement function
*/
function createFlowFromWithCtx(
ISuperToken token,
address sender,
address receiver,
int96 flowRate,
bytes memory ctx
) internal returns (bytes memory newCtx) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
(newCtx, ) = host.callAgreementWithContext(
cfa,
abi.encodeCall(
cfa.createFlowByOperator,
(
token,
sender,
receiver,
flowRate,
new bytes(0) // placeholder
)
),
"0x",
ctx
);
}
/**
* @dev Update flow with context
* @param token The token to flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
* @param ctx Context bytes (see ISuperfluid.sol for Context struct)
* @return newCtx The updated context after the execution of the agreement function
*/
function updateFlowWithCtx(
ISuperToken token,
address receiver,
int96 flowRate,
bytes memory ctx
) internal returns (bytes memory newCtx) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
(newCtx, ) = host.callAgreementWithContext(
cfa,
abi.encodeCall(
cfa.updateFlow,
(
token,
receiver,
flowRate,
new bytes(0) // placeholder
)
),
"0x",
ctx
);
}
/**
* @dev Update flow by operator with context
* @param token The token to flow
* @param sender The receiver of the flow
* @param receiver The receiver of the flow
* @param flowRate The desired flowRate
* @param ctx Context bytes (see ISuperfluid.sol for Context struct)
* @return newCtx The updated context after the execution of the agreement function
*/
function updateFlowFromWithCtx(
ISuperToken token,
address sender,
address receiver,
int96 flowRate,
bytes memory ctx
) internal returns (bytes memory newCtx) {
(ISuperfluid host, IConstantFlowAgreementV1 cfa) = _getAndCacheHostAndCFA(token);
(newCtx, ) = host.callAgreementWithContext(
cfa,
abi.encodeCall(
cfa.updateFlowByOperator,
(
token,
sender,
receiver,
flowRate,