-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathIDASuperAppTester.t.sol
More file actions
297 lines (275 loc) · 8.25 KB
/
IDASuperAppTester.t.sol
File metadata and controls
297 lines (275 loc) · 8.25 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
// SPDX-License-Identifier: AGPLv3
pragma solidity ^0.8.23;
import {
ISuperfluid,
ISuperToken,
ISuperApp
} from "../superfluid/Superfluid.sol";
import {
IInstantDistributionAgreementV1
} from "../interfaces/agreements/IInstantDistributionAgreementV1.sol";
contract IDASuperAppTester is ISuperApp {
ISuperfluid private _host;
IInstantDistributionAgreementV1 private _ida;
ISuperToken private _token;
uint32 private _indexId;
constructor(
ISuperfluid host,
uint256 configWord,
IInstantDistributionAgreementV1 ida,
ISuperToken token,
uint32 indexId)
{
_host = host;
_host.registerApp(configWord);
_ida = ida;
_token = token;
_indexId = indexId;
_host.callAgreement(
_ida,
abi.encodeCall(
_ida.createIndex,
(
_token,
_indexId,
new bytes(0) // placeholder ctx
)
),
new bytes(0) // user data
);
}
function updateSubscription(
address subscriber,
uint128 units
)
external
{
_host.callAgreement(
_ida,
abi.encodeCall(
_ida.updateSubscription,
(
_token,
_indexId,
subscriber,
units,
new bytes(0) // placeholder ctx
)
),
new bytes(0) // user data
);
}
function distribute(
uint128 amount
)
external
{
_host.callAgreement(
_ida,
abi.encodeCall(
_ida.distribute,
(
_token,
_indexId,
amount,
new bytes(0) // placeholder ctx
)
),
new bytes(0) // user data
);
}
function _expectCallback(
bytes calldata ctx,
bytes32 callbackType,
bytes calldata agreementData
)
private view
{
ISuperfluid.Context memory context = ISuperfluid(msg.sender).decodeCtx(ctx);
bytes32 expectedCallbackType;
bytes4 expectedSelector;
bytes memory expectedAgreementData;
(
expectedCallbackType,
expectedSelector,
expectedAgreementData
) = abi.decode(context.userData, (bytes32, bytes4, bytes));
require(expectedCallbackType == callbackType, "wrong callbackType");
require(expectedSelector == context.agreementSelector, "wrong agreementSelector");
require(keccak256(expectedAgreementData) == keccak256(agreementData), "wrong aAgreementData");
}
event SubscriptionDataBefore(
address publisher,
uint32 indexId,
bool approved,
uint128 units,
uint256 pendingDistribution);
event SubscriptionDataAfter(
address publisher,
uint32 indexId,
bool approved,
uint128 units,
uint256 pendingDistribution);
function _packSubscriptionData(bytes32 agreementId) private view returns (bytes memory){
address publisher;
uint32 indexId;
bool approved;
uint128 units;
uint256 pendingDistribution;
(
publisher,
indexId,
approved,
units,
pendingDistribution
) = _ida.getSubscriptionByID(_token, agreementId);
return abi.encode(publisher, indexId, approved, units, pendingDistribution);
}
bool private _forceGetSubscriptionByID = false;
function setForceGetSubscriptionByID() external {
_forceGetSubscriptionByID = true;
}
function _emitSubscriptionDataEvents(bytes memory cbdata, bytes32 agreementId, bool deleted)
private
{
address publisher;
uint32 indexId;
bool approved;
uint128 units;
uint256 pendingDistribution;
if (cbdata.length > 0) {
(
publisher,
indexId,
approved,
units,
pendingDistribution
) = abi.decode(cbdata, (address, uint32, bool, uint128, uint256));
emit SubscriptionDataBefore(publisher, indexId, approved, units, pendingDistribution);
}
if (!deleted) {
(
publisher,
indexId,
approved,
units,
pendingDistribution
) = _ida.getSubscriptionByID(_token, agreementId);
emit SubscriptionDataAfter(publisher, indexId, approved, units, pendingDistribution);
}
}
function beforeAgreementCreated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata agreementData,
bytes calldata ctx
)
external view
requireValidCtx(ctx)
onlyExpected(superToken, agreementClass)
virtual override
returns (bytes memory cbdata)
{
_expectCallback(ctx, keccak256("created"), agreementData);
if (_forceGetSubscriptionByID) {
_ida.getSubscriptionByID(_token, agreementId);
}
return new bytes(0);
}
function afterAgreementCreated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata agreementData,
bytes calldata cbdata,
bytes calldata ctx
)
external
requireValidCtx(ctx)
onlyExpected(superToken, agreementClass)
virtual override
returns (bytes memory newCtx)
{
_expectCallback(ctx, keccak256("created"), agreementData);
_emitSubscriptionDataEvents(cbdata, agreementId, false);
return ctx;
}
function beforeAgreementUpdated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata agreementData,
bytes calldata ctx
)
external view
requireValidCtx(ctx)
onlyExpected(superToken, agreementClass)
virtual override
returns (bytes memory /*cbdata*/)
{
_expectCallback(ctx, keccak256("updated"), agreementData);
return _packSubscriptionData(agreementId);
}
function afterAgreementUpdated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata agreementData,
bytes calldata cbdata,
bytes calldata ctx
)
external
requireValidCtx(ctx)
onlyExpected(superToken, agreementClass)
virtual override
returns (bytes memory newCtx)
{
_expectCallback(ctx, keccak256("updated"), agreementData);
_emitSubscriptionDataEvents(cbdata, agreementId, false);
return ctx;
}
function beforeAgreementTerminated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata agreementData,
bytes calldata ctx
)
external view
requireValidCtx(ctx)
onlyExpected(superToken, agreementClass)
virtual override
returns (bytes memory /*cbdata*/)
{
_expectCallback(ctx, keccak256("deleted"), agreementData);
return _packSubscriptionData(agreementId);
}
function afterAgreementTerminated(
ISuperToken superToken,
address agreementClass,
bytes32 agreementId,
bytes calldata agreementData,
bytes calldata cbdata,
bytes calldata ctx
)
external
requireValidCtx(ctx)
onlyExpected(superToken, agreementClass)
virtual override
returns (bytes memory newCtx)
{
_expectCallback(ctx, keccak256("deleted"), agreementData);
_emitSubscriptionDataEvents(cbdata, agreementId, true);
return ctx;
}
modifier requireValidCtx(bytes calldata ctx) {
require(ISuperfluid(msg.sender).isCtxValid(ctx), "IDASuperAppTester: ctx not valid before");
_;
}
modifier onlyExpected(ISuperToken superToken, address agreementClass) {
require(superToken == _token, "not accepted token");
require(agreementClass == address(_ida), "not ida");
_;
}
}