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:
loading...
Import the relevant symbols from @sablier/flow and @prb/math:
loading...
Declare the contract and add the Flow address as a constant:
loading...
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 Ethereum Sepolia. If you need to work with a different chain, Flow addresses can be obtained from the Flow Deployments 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.
loading...
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.
The withdraw functions requires a fee. Make sure to send the correct amount in msg.value, as shown below.
loading...
Adjust Rate per Second
Adjusting the rate per second means changing the amount of tokens that is streamed each second.
loading...
Pause
Pausing a stream means setting the rate per second to zero, which means no more streaming.
loading...
Restart
There are two restart functions:
restart: restarts a stream.restartAndDeposit: restarts a stream followed by depositing an amount of tokens into it.
loading...
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.
loading...
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.
loading...
Full code
Below you can see the complete FlowStreamManager contract:
loading...