Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 36 additions & 68 deletions docs/guides/flow/examples/04-stream-management.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,21 @@ please note the following:

Declare the Solidity version used to compile the contract:

```solidity
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.22;
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L1-L2`}
</CodeBlock>

Import the relevant symbols from `@sablier/flow` and `@prb/math`:

```solidity
import { ud21x18 } from "@prb/math/src/UD21x18.sol";
import { ISablierFlow } from "@sablier/flow/src/interfaces/ISablierFlow.sol";
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L4-L5`}
</CodeBlock>

Declare the contract and add the Flow address as a constant:

```solidity
contract FlowStreamManager {
ISablierFlow public constant FLOW = ISablierFlow(0x7a86d3e6894f9c5B5f25FFBDAaE658CFc7569623);
}
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L7-L9`}
</CodeBlock>

<HardcodedDemonstration protocol="Flow" />

Expand All @@ -62,15 +58,9 @@ There are three deposit functions:
2. [`depositAndPause`](/reference/flow/contracts/contract.SablierFlow#depositandpause): deposits an amount of tokens and
then pauses the stream.

```solidity
function deposit(uint256 streamId, uint256 amount) external {
FLOW.deposit(streamId, amount);
}

function depositAndPause(uint256 streamId) external {
FLOW.depositAndPause(streamId, 3.14159e18);
}
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L15-L21`}
</CodeBlock>

## Withdraw

Expand All @@ -84,35 +74,31 @@ There are two withdrawal functions:
2. [`withdrawMax`](/reference/flow/contracts/contract.SablierFlow#withdrawmax): withdraws the entire withdrawable amount
of tokens.

```solidity
function withdraw(uint256 streamId) external {
FLOW.withdraw({ streamId: streamId, to: address(0xCAFE), amount: 2.71828e18 });
}
:::note

function withdrawMax(uint256 streamId) external {
FLOW.withdrawMax({ streamId: streamId, to: address(0xCAFE) });
}
```
The `withdraw` functions requires a fee. Make sure to send the correct amount in `msg.value`, as shown below.

:::

<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L51-L59`}
</CodeBlock>

## Adjust Rate per Second

Adjusting the rate per second means changing the amount of tokens that is streamed each second.

```solidity
function adjustRatePerSecond(uint256 streamId) external {
FLOW.adjustRatePerSecond({ streamId: streamId, newRatePerSecond: ud21x18(0.0001e18) });
}
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L11-L13`}
</CodeBlock>

## Pause

Pausing a stream means setting the rate per second to zero, which means no more streaming.

```solidity
function pause(uint256 streamId) external {
FLOW.pause(streamId);
}
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L23-L25`}
</CodeBlock>

## Restart

Expand All @@ -122,15 +108,9 @@ There are two restart functions:
2. [`restartAndDeposit`](/reference/flow/contracts/contract.SablierFlow#restartanddeposit): restarts a stream followed
by depositing an amount of tokens into it.

```solidity
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 });
}
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L39-L45`}
</CodeBlock>

## Refund

Expand All @@ -143,35 +123,23 @@ There are three refund functions:
3. [`refundMax`](/reference/flow/contracts/contract.SablierFlow#refundmax): refunds the entire refundable amount of
tokens.

```solidity
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);
}
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L27-L37`}
</CodeBlock>

## 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.

```solidity
function void(uint256 streamId) external {
FLOW.void(streamId);
}
```
<CodeBlock language="solidity" metastring={`reference title=""`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol#L47-L49`}
</CodeBlock>

## Full code

Below you can see the complete `FlowStreamManager` contract:

<CodeBlock language="solidity" showLineNumbers metastring={`reference title="Flow Stream Manager"`}>
<CodeBlock language="solidity" showLineNumbers metastring={`reference title="Full code"`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/flow/FlowStreamManager.sol`}
</CodeBlock>
7 changes: 7 additions & 0 deletions docs/guides/flow/examples/05-batchable.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ Try to implement the following ideas using `batch` function:
- Void and Withdraw Max
- Multiple Withdraw Max

:::note

If you include any `withdraw` function in your batch, keep in mind that it requires a fee. Make sure to send the correct
amount in `msg.value`, as shown below.

:::

Below, you will find the full code for it.

## Other ideas
Expand Down
12 changes: 9 additions & 3 deletions docs/guides/lockup/examples/stream-management/02-withdraw.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ To call any of these functions, you need to have created a stream. If you don't
[previous guide](/guides/lockup/examples/create-stream/lockup-linear) and create a stream with a brief duration,
assigning the `StreamManagement` contract as the recipient. Then, you can use the `withdraw` function like this:

:::note

The `withdraw` functions requires a fee. Make sure to send the correct amount in `msg.value`, as shown below.

:::

<CodeBlock language="solidity" showLineNumbers metastring={`reference title="Stream Management: Withdraw"`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/lockup/StreamManagement.sol#L20-L22`}
{`https://github.com/sablier-labs/evm-examples/blob/main/lockup/StreamManagement.sol#L20-L23`}
</CodeBlock>

In this example, the withdrawal address and withdrawal amount are hard-coded for demonstration purposes. However, in a
Expand All @@ -50,7 +56,7 @@ In addition to the `withdraw` function, there is the `withdrawMax` function, whi
withdrawable amount of tokens at the time of invocation:

<CodeBlock language="solidity" showLineNumbers metastring={`reference title="Stream Management: Withdraw Max"`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/lockup/StreamManagement.sol#L25-L27`}
{`https://github.com/sablier-labs/evm-examples/blob/main/lockup/StreamManagement.sol#L26-L29`}
</CodeBlock>

What `withdrawMax` does is call the
Expand All @@ -63,5 +69,5 @@ same time, transfer the NFT to another address.
Lastly, there is the `withdrawMultiple` function, with which you can use to withdraw from multiple streams at once:

<CodeBlock language="solidity" showLineNumbers metastring={`reference title="Stream Management: Withdraw Multiple"`}>
{`https://github.com/sablier-labs/evm-examples/blob/main/lockup/StreamManagement.sol#L30-L32`}
{`https://github.com/sablier-labs/evm-examples/blob/main/lockup/StreamManagement.sol#L32-L44`}
</CodeBlock>
1 change: 1 addition & 0 deletions docs/reference/flow/contracts/contract.SablierFlow.md
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ Emits a {Transfer}, {WithdrawFromFlowStream} and {MetadataUpdate} event. Notes:
- `to` must not be the zero address.
- `to` must be the recipient if `msg.sender` is not the stream's recipient or an approved third party.
- `amount` must be greater than zero and must not exceed the withdrawable amount.
- `msg.value` must be greater than or equal to the minimum fee in wei for the stream's sender.

```solidity
function withdraw(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ Emits a {Transfer}, {WithdrawFromFlowStream} and {MetadataUpdate} event. Notes:
- `to` must not be the zero address.
- `to` must be the recipient if `msg.sender` is not the stream's recipient or an approved third party.
- `amount` must be greater than zero and must not exceed the withdrawable amount.
- `msg.value` must be greater than or equal to the minimum fee in wei for the stream's sender.

```solidity
function withdraw(uint256 streamId, address to, uint128 amount) external payable;
Expand Down