Skip to content

Commit 72aa77d

Browse files
committed
add flowrate argument to onFlowCreated and onFlowUpdated
1 parent 00fbe2c commit 72aa77d

File tree

5 files changed

+43
-31
lines changed

5 files changed

+43
-31
lines changed

packages/ethereum-contracts/contracts/apps/CFASuperAppBase.sol

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ abstract contract CFASuperAppBase is ISuperApp {
134134
function onFlowCreated(
135135
ISuperToken /*superToken*/,
136136
address /*sender*/,
137+
int96 /*flowRate*/,
137138
bytes calldata ctx
138139
) internal virtual returns (bytes memory /*newCtx*/) {
139140
return ctx;
@@ -144,6 +145,7 @@ abstract contract CFASuperAppBase is ISuperApp {
144145
function onFlowUpdated(
145146
ISuperToken /*superToken*/,
146147
address /*sender*/,
148+
int96 /*flowRate*/,
147149
int96 /*previousFlowRate*/,
148150
uint256 /*lastUpdated*/,
149151
bytes calldata ctx
@@ -221,11 +223,13 @@ abstract contract CFASuperAppBase is ISuperApp {
221223
if (!isAcceptedSuperToken(superToken)) revert NotAcceptedSuperToken();
222224

223225
(address sender, ) = abi.decode(agreementData, (address, address));
226+
int96 flowRate = superToken.getCFAFlowRate(sender, address(this));
224227

225228
return
226229
onFlowCreated(
227230
superToken,
228231
sender,
232+
flowRate,
229233
ctx // userData can be acquired with `host.decodeCtx(ctx).userData`
230234
);
231235
}
@@ -264,17 +268,28 @@ abstract contract CFASuperAppBase is ISuperApp {
264268
if (!_isAcceptedAgreement(agreementClass)) return ctx;
265269
if (!isAcceptedSuperToken(superToken)) revert NotAcceptedSuperToken();
266270

271+
return _afterAgreementUpdatedHelper(superToken, agreementData, cbdata, ctx);
272+
}
273+
274+
// workaround to stack-too-deep compiler error
275+
function _afterAgreementUpdatedHelper(
276+
ISuperToken superToken,
277+
bytes calldata agreementData,
278+
bytes calldata cbdata,
279+
bytes calldata ctx
280+
) private returns (bytes memory) {
267281
(address sender, ) = abi.decode(agreementData, (address, address));
268282
(int96 previousFlowRate, uint256 lastUpdated) = abi.decode(cbdata, (int96, uint256));
283+
int96 flowRate = superToken.getCFAFlowRate(sender, address(this));
269284

270-
return
271-
onFlowUpdated(
272-
superToken,
273-
sender,
274-
previousFlowRate,
275-
lastUpdated,
276-
ctx // userData can be acquired with `host.decodeCtx(ctx).userData`
277-
);
285+
return onFlowUpdated(
286+
superToken,
287+
sender,
288+
flowRate,
289+
previousFlowRate,
290+
lastUpdated,
291+
ctx // userData can be acquired with `host.decodeCtx(ctx).userData`
292+
);
278293
}
279294

280295
// DELETED callbacks

packages/ethereum-contracts/test/foundry/apps/CFASuperAppBaseTester.t.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ contract CFASuperAppBaseTester is CFASuperAppBase {
4646

4747
// CREATE
4848

49-
function onFlowCreated(ISuperToken, /*superToken*/ address sender, bytes calldata ctx)
49+
function onFlowCreated(ISuperToken, /*superToken*/ address sender, int96 /*flowRate*/, bytes calldata ctx)
5050
internal
5151
override
5252
returns (bytes memory)
@@ -60,6 +60,7 @@ contract CFASuperAppBaseTester is CFASuperAppBase {
6060
function onFlowUpdated(
6161
ISuperToken, /*superToken*/
6262
address sender,
63+
int96 /*flowRate*/,
6364
int96 previousFlowRate,
6465
uint256 lastUpdated,
6566
bytes calldata ctx

packages/ethereum-contracts/test/foundry/apps/CrossStreamSuperApp.t.sol

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,24 @@ contract CrossStreamSuperApp is CFASuperAppBase {
2828
flowRecipient = z_;
2929
}
3030

31-
function onFlowCreated(ISuperToken superToken, address sender, bytes calldata ctx)
31+
function onFlowCreated(ISuperToken superToken, address sender, int96 flowRate, bytes calldata ctx)
3232
internal
3333
override
3434
returns (bytes memory newCtx)
3535
{
3636
newCtx = ctx;
3737

38-
// get incoming stream
39-
int96 inFlowRate = superToken.getFlowRate(sender, address(this));
40-
4138
if (prevSender == address(0)) {
4239
// first flow to super app creates a flow
43-
newCtx = superToken.createFlowWithCtx(flowRecipient, inFlowRate, newCtx);
40+
newCtx = superToken.createFlowWithCtx(flowRecipient, flowRate, newCtx);
4441
} else {
4542
// subsequent flows to super app updates and deletes the flow
46-
newCtx = superToken.updateFlowWithCtx(flowRecipient, inFlowRate, newCtx);
43+
newCtx = superToken.updateFlowWithCtx(flowRecipient, flowRate, newCtx);
4744
newCtx = superToken.deleteFlowWithCtx(prevSender, address(this), newCtx);
4845
}
4946

5047
prevSender = sender;
51-
prevFlowRate = inFlowRate;
48+
prevFlowRate = flowRate;
5249
}
5350
}
5451

packages/ethereum-contracts/test/foundry/apps/SuperAppTester/FlowSplitter.sol

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,51 +68,49 @@ contract FlowSplitter is CFASuperAppBase {
6868
// ---------------------------------------------------------------------------------------------
6969
// CALLBACK LOGIC
7070

71-
function onFlowCreated(ISuperToken superToken, address sender, bytes calldata ctx)
71+
function onFlowCreated(ISuperToken superToken, address, /*sender*/ int96 flowRate, bytes calldata ctx)
7272
internal
7373
override
7474
returns (bytes memory newCtx)
7575
{
7676
newCtx = ctx;
7777

78-
// get inflow rate from sender
79-
int96 inflowRate = superToken.getFlowRate(sender, address(this));
80-
8178
// if there's no outflow already, create outflows
8279
if (superToken.getFlowRate(address(this), mainReceiver) == 0) {
8380
newCtx =
84-
superToken.createFlowWithCtx(mainReceiver, (inflowRate * (1000 - sideReceiverPortion)) / 1000, newCtx);
81+
superToken.createFlowWithCtx(mainReceiver, (flowRate * (1000 - sideReceiverPortion)) / 1000, newCtx);
8582

86-
newCtx = superToken.createFlowWithCtx(sideReceiver, (inflowRate * sideReceiverPortion) / 1000, newCtx);
83+
newCtx = superToken.createFlowWithCtx(sideReceiver, (flowRate * sideReceiverPortion) / 1000, newCtx);
8784
}
8885
// otherwise, there's already outflows which should be increased
8986
else {
9087
newCtx = superToken.updateFlowWithCtx(
9188
mainReceiver,
9289
acceptedSuperToken.getFlowRate(address(this), mainReceiver)
93-
+ (inflowRate * (1000 - sideReceiverPortion)) / 1000,
90+
+ (flowRate * (1000 - sideReceiverPortion)) / 1000,
9491
newCtx
9592
);
9693

9794
newCtx = superToken.updateFlowWithCtx(
9895
sideReceiver,
99-
acceptedSuperToken.getFlowRate(address(this), sideReceiver) + (inflowRate * sideReceiverPortion) / 1000,
96+
acceptedSuperToken.getFlowRate(address(this), sideReceiver) + (flowRate * sideReceiverPortion) / 1000,
10097
newCtx
10198
);
10299
}
103100
}
104101

105102
function onFlowUpdated(
106103
ISuperToken superToken,
107-
address sender,
104+
address, /*sender*/
105+
int96 flowRate,
108106
int96 previousFlowRate,
109107
uint256, /*lastUpdated*/
110108
bytes calldata ctx
111109
) internal override returns (bytes memory newCtx) {
112110
newCtx = ctx;
113111

114112
// get inflow rate change from sender
115-
int96 inflowChange = superToken.getFlowRate(sender, address(this)) - previousFlowRate;
113+
int96 inflowChange = flowRate - previousFlowRate;
116114

117115
// update outflows
118116
newCtx = superToken.updateFlowWithCtx(

packages/ethereum-contracts/test/foundry/apps/SuperTokenV1Library.t.sol

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,19 +538,21 @@ contract SuperAppMock is CFASuperAppBase {
538538
function onFlowCreated(
539539
ISuperToken superToken,
540540
address sender,
541+
int96 flowRate,
541542
bytes calldata ctx
542543
) internal virtual override returns (bytes memory /*newCtx*/) {
543-
return _mirrorOrMatchIncomingFlow(superToken, sender, ctx);
544+
return _mirrorOrMatchIncomingFlow(superToken, sender, flowRate, ctx);
544545
}
545546

546547
function onFlowUpdated(
547548
ISuperToken superToken,
548549
address sender,
550+
int96 flowRate,
549551
int96 /*previousFlowRate*/,
550552
uint256 /*lastUpdated*/,
551553
bytes calldata ctx
552554
) internal virtual override returns (bytes memory /*newCtx*/) {
553-
return _mirrorOrMatchIncomingFlow(superToken, sender, ctx);
555+
return _mirrorOrMatchIncomingFlow(superToken, sender, flowRate, ctx);
554556
}
555557

556558
function onInFlowDeleted(
@@ -560,7 +562,7 @@ contract SuperAppMock is CFASuperAppBase {
560562
uint256 /*lastUpdated*/,
561563
bytes calldata ctx
562564
) internal virtual override returns (bytes memory /*newCtx*/) {
563-
return _mirrorOrMatchIncomingFlow(superToken, sender, ctx);
565+
return _mirrorOrMatchIncomingFlow(superToken, sender, 0, ctx);
564566
}
565567

566568
// outflow was deleted by the sender we mirror to, we make it "sticky" by simply restoring it.
@@ -574,10 +576,9 @@ contract SuperAppMock is CFASuperAppBase {
574576
return superToken.flowWithCtx(receiver, previousFlowRate, ctx);
575577
}
576578

577-
function _mirrorOrMatchIncomingFlow(ISuperToken superToken, address senderAndReceiver, bytes memory ctx)
579+
function _mirrorOrMatchIncomingFlow(ISuperToken superToken, address senderAndReceiver, int96 flowRate, bytes memory ctx)
578580
internal returns (bytes memory newCtx)
579581
{
580-
int96 flowRate = superToken.getFlowRate(senderAndReceiver, address(this));
581582
if (aclFlowSender == address(0)) {
582583
return superToken.flowWithCtx(senderAndReceiver, flowRate, ctx);
583584
} else {

0 commit comments

Comments
 (0)