-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathMultiFlowTesterApp.t.sol
More file actions
336 lines (307 loc) · 10.5 KB
/
MultiFlowTesterApp.t.sol
File metadata and controls
336 lines (307 loc) · 10.5 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
// SPDX-License-Identifier: AGPLv3
pragma solidity ^0.8.23;
import {
ISuperfluid,
ISuperToken,
SuperAppBase,
SuperAppDefinitions,
IConstantFlowAgreementV1
} from "../apps/SuperAppBase.sol";
/**
* @title Multi Flow tester App (MFA)
* @author Superfluid
* @dev A super app that can split incoming flows to multiple outgoing flows.
* This is used for testing CFA callbacks logic.
*/
contract MultiFlowTesterApp is SuperAppBase {
struct ReceiverData {
address to;
uint256 proportion;
}
struct Configuration {
uint8 ratioPct;
ReceiverData[] receivers;
}
IConstantFlowAgreementV1 private _cfa;
ISuperfluid private _host;
constructor(IConstantFlowAgreementV1 cfa, ISuperfluid superfluid) {
assert(address(cfa) != address(0));
assert(address(superfluid) != address(0));
_cfa = cfa;
_host = superfluid;
uint256 configWord =
SuperAppDefinitions.APP_LEVEL_FINAL |
SuperAppDefinitions.BEFORE_AGREEMENT_CREATED_NOOP |
SuperAppDefinitions.BEFORE_AGREEMENT_TERMINATED_NOOP;
_host.registerApp(configWord);
}
function _parseUserData(
bytes memory userData
)
private pure
returns (address sender, Configuration memory configuration)
{
// parse user data
uint8 ratioPct;
address[] memory receivers;
uint256[] memory proportions;
(sender, ratioPct, receivers, proportions) = abi.decode(
userData, (address, uint8, address[], uint256[]));
assert(receivers.length == proportions.length);
configuration.ratioPct = ratioPct;
configuration.receivers = new ReceiverData[](receivers.length);
for (uint256 i = 0; i < receivers.length; ++i) {
assert(proportions[i] != 0);
configuration.receivers[i] = ReceiverData(receivers[i], proportions[i]);
}
}
function _sumProportions(ReceiverData[] memory receivers) internal pure returns(uint256 sum) {
for (uint256 i = 0; i < receivers.length; ++i) {
sum += receivers[i].proportion;
}
}
function _updateMultiFlow(
Configuration memory configuration,
ISuperToken superToken,
bytes4 selector,
int96 flowRate,
uint256 appCreditGranted,
bytes calldata ctx
)
private
returns (bytes memory newCtx)
{
uint256 sum = _sumProportions(configuration.receivers);
newCtx = ctx;
// in case of mfa, we underutlize the app credit for simplicity
uint256 flowRateDeposit = _cfa.getDepositRequiredForFlowRate(superToken, flowRate);
int96 safeFlowRate = _cfa.getMaximumFlowRateFromDeposit(superToken, flowRateDeposit - 1);
appCreditGranted = _cfa.getDepositRequiredForFlowRate(superToken, safeFlowRate);
// scale the flow rate and app credit numbers
appCreditGranted = appCreditGranted * configuration.ratioPct / 100;
// NOTE casting to int96 is okay here because ratioPct is uint8
flowRate = flowRate * int96(uint96(configuration.ratioPct)) / 100;
for (uint256 i = 0; i < configuration.receivers.length; ++i) {
ReceiverData memory receiverData = configuration.receivers[i];
uint256 targetCredit = appCreditGranted * receiverData.proportion / sum;
int96 targetFlowRate = _cfa.getMaximumFlowRateFromDeposit(
superToken,
targetCredit
);
flowRate -= targetFlowRate;
bytes memory callData = abi.encodeWithSelector(
selector,
superToken,
receiverData.to,
targetFlowRate,
new bytes(0)
);
(newCtx, ) = _host.callAgreementWithContext(
_cfa,
callData,
new bytes(0), // user data
newCtx
);
}
assert(flowRate >= 0);
}
// this function is for testing purpose
function createFlow(
ISuperToken superToken,
address receiver,
int96 flowRate,
bytes calldata ctx
)
external
returns (bytes memory newCtx)
{
bytes memory callData = abi.encodeCall(
_cfa.createFlow,
(
superToken,
receiver,
flowRate,
new bytes(0)
)
);
(newCtx, ) = _host.callAgreementWithContext(
_cfa,
callData,
new bytes(0), // user data
ctx
);
}
struct StackVars {
ISuperfluid.Context context;
address mfaSender;
Configuration configuration;
address flowSender;
address flowReceiver;
}
function afterAgreementCreated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata agreementData,
bytes calldata /*cbdata*/,
bytes calldata ctx
)
external override
onlyHost
returns(bytes memory newCtx)
{
assert(agreementClass == address(_cfa));
StackVars memory vars;
vars.context = _host.decodeCtx(ctx);
// parse user data
(vars.mfaSender, vars.configuration) = _parseUserData(vars.context.userData);
// validate the context
{
(vars.flowSender, vars.flowReceiver) = abi.decode(agreementData, (address, address));
assert(vars.flowSender == vars.context.msgSender);
assert(vars.flowReceiver == address(this));
assert(vars.context.appCreditGranted > 0);
}
int96 flowRate;
(,flowRate,,) = _cfa.getFlowByID(superToken, agreementId);
newCtx = _updateMultiFlow(
vars.configuration,
superToken,
_cfa.createFlow.selector,
flowRate,
vars.context.appCreditGranted,
ctx);
}
function beforeAgreementUpdated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata /*agreementData*/,
bytes calldata /*ctx*/
)
external view override
onlyHost
returns (bytes memory cbdata)
{
assert(agreementClass == address(_cfa));
(, int256 oldFlowRate, ,) = _cfa.getFlowByID(superToken, agreementId);
return abi.encode(oldFlowRate);
}
function afterAgreementUpdated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata agreementData,
bytes calldata /* cbdata */,
bytes calldata ctx
)
external override
onlyHost
returns (bytes memory newCtx)
{
assert(agreementClass == address(_cfa));
StackVars memory vars;
vars.context = _host.decodeCtx(ctx);
// parse user data
(vars.mfaSender, vars.configuration) = _parseUserData(vars.context.userData);
// validate the context
{
(vars.flowSender, vars.flowReceiver) = abi.decode(agreementData, (address, address));
assert(vars.flowSender == vars.context.msgSender);
assert(vars.flowReceiver == address(this));
assert(vars.context.appCreditGranted > 0);
}
int96 flowRate;
(,flowRate,,) = _cfa.getFlowByID(superToken, agreementId);
newCtx = _updateMultiFlow(
vars.configuration,
superToken,
_cfa.updateFlow.selector,
flowRate,
vars.context.appCreditGranted,
ctx);
}
function afterAgreementTerminated(
ISuperToken superToken,
address agreementClass,
bytes32 /*agreementId*/,
bytes calldata agreementData,
bytes calldata /*cbdata*/,
bytes calldata ctx
)
external override
onlyHost
returns (bytes memory newCtx)
{
assert(agreementClass == address(_cfa));
StackVars memory vars;
vars.context = _host.decodeCtx(ctx);
// parse user data
(vars.mfaSender, vars.configuration) = _parseUserData(vars.context.userData);
// validate the context
(vars.flowSender, vars.flowReceiver) = abi.decode(agreementData, (address, address));
assert(vars.flowSender == address(this) || vars.flowReceiver == address(this));
bytes memory callData;
newCtx = ctx;
if (vars.flowReceiver == address(this)) {
for (uint256 i = 0; i < vars.configuration.receivers.length; ++i) {
callData = abi.encodeCall(
_cfa.deleteFlow,
(
superToken,
address(this),
vars.configuration.receivers[i].to,
new bytes(0) //placeholder ctx
)
);
(newCtx, ) = _host.callAgreementWithContext(
_cfa,
callData,
new bytes(0), // user data
newCtx
);
}
} else /* if (vars.flowSender == address(this)) */ {
for (uint256 i = 0; i < vars.configuration.receivers.length; ++i) {
// skip current closed flow
if (vars.configuration.receivers[i].to == vars.flowReceiver) continue;
// close the rest of the mfa receiver flows
callData = abi.encodeCall(
_cfa.deleteFlow,
(
superToken,
address(this),
vars.configuration.receivers[i].to,
new bytes(0) //placeholder ctx
)
);
(newCtx, ) = _host.callAgreementWithContext(
_cfa,
callData,
new bytes(0), // user data
newCtx
);
}
// close the mfa sender flow
callData = abi.encodeCall(
_cfa.deleteFlow,
(
superToken,
vars.mfaSender,
address(this),
new bytes(0) //placeholder ctx
)
);
(newCtx, ) = _host.callAgreementWithContext(
_cfa,
callData,
new bytes(0), // user data
newCtx
);
}
}
modifier onlyHost() {
assert(msg.sender == address(_host));
_;
}
}