Skip to content

Instantly share code, notes, and snippets.

@donoso-eth
Last active June 22, 2022 15:58
Show Gist options
  • Save donoso-eth/5491f5f94409f9648fa196c3ed24ca3d to your computer and use it in GitHub Desktop.
Save donoso-eth/5491f5f94409f9648fa196c3ed24ca3d to your computer and use it in GitHub Desktop.
// ============= ============= Start and Stop Stream Bonus Track#1 ============= ============= //
// #region Plan Future Start and Future Stop
/**************************************************************************
* Start and Stop Stream Use Case Business Logic
* What makes this use case special is that we are concatenating two use cases
* Start Stream(a) and StopStream (b) being the Start Stream Step 3a, the Stop Steam Step 1b
*
* Step 1a : planStream() Public function called by the user
* - msg.sender will grant full stream permissions to contract
* - will create a gelato timed task that will lauchh with start-time at the desired stream start
* - will store the taskId
*
* Step 2a : checkerPlanStream() Function.
* - always return "canExec = true" as we are only waiting to the time to execute
* - returns the execPayload of startStream()
*
* Step 3a and 1b : Executable Function: startStream()
* - will create a stream from sender to receiver (contrat has acl permissions)
* - will cancel the task so it only run once
* - will create a timed task to stop the stream
* - will store the new taskId
*
* Step 2b: checkerStopPlanStream() function
* - always return "canExec = true" as we are only waiting to the time to execute
* - returns the execPayload of stopPlannedStream()
*
* Step 3: Executable Function: stopPlannedStream()
* - will stop the stream between sender and recevier
* - will cabcel the task so it only runs once
*
*************************************************************************/
function planStream(PlanStream memory config) external {
bytes32 taskId = IOps(ops).createTimedTask(
uint128(block.timestamp + config.plannedStart), //// timestamp at which the task should be first executed (stream should start)
600, /// Interval between executions, we will cancel after the first
address(this), /// Contract executing the task
this.startStream.selector, /// Executable function's selector
address(this), /// Resolver contract, in our case will be the same
abi.encodeWithSelector(
this.checkerPlanStream.selector,
config.stream
), /// Checker Condition
ETH, /// feetoken
true /// we will use the treasury contract for funding
);
taskIdByUser[config.stream.sender] = taskId;
}
function checkerPlanStream(StreamConfig memory stream)
external
pure
returns (bool canExec, bytes memory execPayload)
{
canExec = true;
execPayload = abi.encodeWithSelector(this.startStream.selector, stream);
}
function startStream(StreamConfig memory stream) external onlyOps {
// bytes memory userData = abi.encode(stream.duration,stream.receiver);
_cfaLib.createFlowByOperator(
stream.sender,
stream.receiver,
superToken,
stream.flowRate,
"0x"
);
//// cancelprevoius task
bytes32 oldTaskId = taskIdByUser[stream.sender];
cancelTaskbyId(oldTaskId, stream.sender);
//// create new timed at
bytes32 taskId = IOps(ops).createTimedTask(
uint128(block.timestamp + stream.duration),//// timestamp at which the task should be first executed (stream should stop)
600, /// Interval between executions, we will cancel after the first
address(this), /// /// Contract executing the task
this.stopPlannedStream.selector, /// Executable function's selector
address(this), /// Resolver contract, in our case will be the same
abi.encodeWithSelector(
this.checkerStopPlanStream.selector,
stream.sender,
stream.receiver
), /// Checker Condition
ETH, /// feetoken
true /// we will use the treasury contract for funding
);
taskIdByUser[stream.sender] = taskId;
}
function checkerStopPlanStream(address sender, address receiver)
external
pure
returns (bool canExec, bytes memory execPayload)
{
canExec = true;
execPayload = abi.encodeWithSelector(
this.stopPlannedStream.selector,
address(sender),
address(receiver)
);
}
function stopPlannedStream(address sender, address receiver)
external
onlyOps
{
/////// STOP IF EXISTS incoming stream
(, int96 inFlowRate, , ) = cfa.getFlow(superToken, sender, receiver);
if (inFlowRate > 0) {
host.callAgreement(
cfa,
abi.encodeWithSelector(
cfa.deleteFlow.selector,
superToken,
sender,
receiver,
new bytes(0) // placeholder
),
"0x"
);
}
bytes32 _taskId = taskIdByUser[sender];
cancelTaskbyId(_taskId,sender);
}
// #endregion Plan Future Start and Future Stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment