Skip to content

Instantly share code, notes, and snippets.

@donoso-eth
Last active June 22, 2022 15:58
Show Gist options
  • Save donoso-eth/e4fc0145ed75ed84309da1a467175931 to your computer and use it in GitHub Desktop.
Save donoso-eth/e4fc0145ed75ed84309da1a467175931 to your computer and use it in GitHub Desktop.
// ============= ============= Stop Stream Use Case Business Logic ============= ============= //
// #region Stop Stream Use Case Business Logic
/**************************************************************************
* Stop Stream Use Case Business Logic
*
* Step 1 : createStopStreamTask() Internal function call from the super app callback
* (after created Stream in which also a stream will be created to the receiver)
* - will create a gelato timed task that will lauchh with start-time at the desired duration of the stream
* - will store the taskId
*
* Step 2 : checkerStopStream() Function.
* - always return "canExec = true" as we are only waiting to the time to execute
* - returns the execPayload of stopStream()
*
* Step 3 : Executable Function: stopStream()
* - will stop the outcoming stream from super app to receiver
* - will stop the incoming stream from sender to super app
* - will cancel the task so it only be executed once (no need to stop the stream twice)
*************************************************************************/
function createStopStreamTask(
uint256 duration,
address sender,
address to
) internal {
bytes32 taskId = IOps(ops).createTimedTask(
uint128(block.timestamp + duration), //// timestamp at which the task should befirst executed (stream should stop)
600, /// Interval between executions, we will cancel after the first
address(this), /// Contract executing the task
this.stopStream.selector, /// Executable function's selector
address(this), /// Resolver contract, in our case will be the same
abi.encodeWithSelector(this.checkerStopStream.selector, sender, to),/// Checker Condition
ETH, /// feetoken
true /// we will use the treasury contract for funding
);
taskIdByUser[sender] = taskId;
createStreamFlag = true;
}
function checkerStopStream(address sender, address receiver)
external
pure
returns (bool canExec, bytes memory execPayload)
{
canExec = true;
execPayload = abi.encodeWithSelector(
this.stopStream.selector,
address(sender),
address(receiver)
);
}
function stopStream(address sender, address receiver) external onlyOps {
//// check if
/////// STOP IF EXISTS outcoming stream
(, int96 outFlowRate, , ) = cfa.getFlow(
superToken,
address(this),
receiver
);
if (outFlowRate > 0) {
host.callAgreement(
cfa,
abi.encodeWithSelector(
cfa.deleteFlow.selector,
superToken,
address(this),
receiver,
new bytes(0) // placeholder
),
"0x"
);
}
/////// STOP IF EXISTS incoming stream
(, int96 inFlowRate, , ) = cfa.getFlow(
superToken,
sender,
address(this)
);
if (inFlowRate > 0) {
host.callAgreement(
cfa,
abi.encodeWithSelector(
cfa.deleteFlow.selector,
superToken,
sender,
address(this),
new bytes(0) // placeholder
),
"0x"
);
}
bytes32 _taskId = taskIdByUser[sender];
cancelTaskbyId(_taskId,sender);
stopStreamFlag = true;
}
// #endregion Stop Stream Use Case Business Logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment