Managing a Stream
This section will guide you through the different functions of Flow and how to interact with them. Before diving in, please note the following:
- We assume you are already familiar with creating Flow streams.
- We also assume that the stream management contract is authorized to invoke each respective function. To learn more about access control in Flow, see the Access Control guide.
The code in this guide is not production-ready, and is implemented in a simplistic manner for the purpose of learning.
Set up your contract
Declare the Solidity version used to compile the contract:
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.22;
Import the relevant symbols from @sablier/flow
and @prb/math
:
import { ud21x18 } from "@prb/math/src/UD21x18.sol";
import { ud60x18 } from "@prb/math/src/UD60x18.sol";
import { Broker, ISablierFlow } from "@sablier/flow/src/interfaces/ISablierFlow.sol";
Declare the contract and add the Flow address as a constant:
contract FlowStreamManager {
ISablierFlow public constant FLOW = ISablierFlow(0x5Ae8c13f6Ae094887322012425b34b0919097d8A);
}
In the code above, the contract addresses are hard-coded for demonstration purposes. However, in production, you would likely use input parameters to allow flexibility in changing the addresses.
Also, these addresses are deployed on Sepolia. If you need to work with a different chain, Flow addresses can be obtained from the Deployment Addresses page.
Deposit
Depositing into streams means adding tokens to the stream, which will then be distributed to the recipient based on the value of rate per second.
A deposit is also referred to as a top-up.
There are three deposit functions:
deposit
: deposits an amount of tokens.depositAndPause
: deposits an amount of tokens and then pauses the stream.depositViaBroker
: deposits an amount of tokens and transfers a fee to the broker specified.
function deposit(uint256 streamId, uint256 amount) external {
FLOW.deposit(streamId, amount);
}
function depositAndPause(uint256 streamId) external {
FLOW.depositAndPause(streamId, 3.14159e18);
}
function depositViaBroker(uint256 streamId) external {
Broker memory broker = Broker({ account: address(0xDEAD), fee: ud60x18(0.0001e18) });
FLOW.depositViaBroker({
streamId: streamId,
totalAmount: 3.14159e18,
sender: msg.sender,
recipient: address(0xCAFE),
broker: broker
});
}
Withdraw
The recipient of a stream can withdraw any amount, not exceeding the withdrawable amount. The recipient also has the option to withdraw the tokens to an alternate address of their choice.
There are two withdrawal functions:
withdraw
: withdraws an amount of tokens not exceeding the withdrawable amount.withdrawMax
: withdraws the entire withdrawable amount of tokens.
function withdraw(uint256 streamId) external {
FLOW.withdraw({ streamId: streamId, to: address(0xCAFE), amount: 2.71828e18 });
}
function withdrawMax(uint256 streamId) external {
FLOW.withdrawMax({ streamId: streamId, to: address(0xCAFE) });
}
Adjust Rate per Second
Adjusting the rate per second means changing the amount of tokens that is streamed each second.
function adjustRatePerSecond(uint256 streamId) external {
FLOW.adjustRatePerSecond({ streamId: streamId, newRatePerSecond: ud21x18(0.0001e18) });
}
Pause
Pausing a stream means setting the rate per second to zero, which means no more streaming.
function pause(uint256 streamId) external {
FLOW.pause(streamId);
}
Restart
There are two restart functions:
restart
: restarts a stream.restartAndDeposit
: restarts a stream followed by depositing an amount of tokens into it.
function restart(uint256 streamId) external {
FLOW.restart({ streamId: streamId, ratePerSecond: ud21x18(0.0001e18) });
}
function restartAndDeposit(uint256 streamId) external {
FLOW.restartAndDeposit({ streamId: streamId, ratePerSecond: ud21x18(0.0001e18), amount: 2.71828e18 });
}
Refund
There are three refund functions:
refund
: refunds an amount of tokens not exceeding the refundable amount.refundAndPause
: refunds an amount of tokens, and then pauses the stream.refundMax
: refunds the entire refundable amount of tokens.
function refund(uint256 streamId) external {
FLOW.refund({ streamId: streamId, amount: 1.61803e18 });
}
function refundAndPause(uint256 streamId) external {
FLOW.refundAndPause({ streamId: streamId, amount: 1.61803e18 });
}
function refundMax(uint256 streamId) external {
FLOW.refundMax(streamId);
}
Void
Voiding a stream means permanently stopping it from streaming any tokens. This is slightly different from pausing a stream because it also sets the stream's uncovered debt to zero.
function void(uint256 streamId) external {
FLOW.void(streamId);
}
Full code
Below you can see the complete FlowStreamManager
contract:
loading...