Skip to main content

Create a Batch of Lockup Linear Streams

In this guide, we will show you how to programmatically batch create linear streams via the Sablier's Batch contract.

This guide assumes that you have already gone through the Protocol Concepts section.

note

This guide interacts with the Periphery contract.

caution

The code in this guide is not production-ready, and is implemented in a simplistic manner for the purpose of learning.

Set up a contract

Declare the Solidity version used to compile the contract:

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.19;

Now, import the relevant symbols from @sablier/v2-core and @sablier/v2-periphery:

import { ISablierV2LockupLinear } from "@sablier/v2-core/src/interfaces/ISablierV2LockupLinear.sol";
import { Broker, LockupLinear } from "@sablier/v2-core/src/types/LockupLinear.sol";
import { ud60x18 } from "@sablier/v2-core/src/types/Math.sol";
import { IERC20 } from "@sablier/v2-core/src/types/Tokens.sol";
import { ISablierV2Batch } from "@sablier/v2-periphery/src/interfaces/ISablierV2Batch.sol";
import { Batch} from "@sablier/v2-periphery/src/types/DataTypes.sol";

Create a contract called BatchLockupLinearStreamCreator, and declare a constant DAI of type IERC20, a constant LOCKUP_LINEAR of type ISablierV2LockupLinear, and a constant BATCH of type ISablierV2Batch:

contract BatchLockupLinearStreamCreator {
IERC20 public constant DAI = IERC20(0x6B175474E89094C44Da98b954EedeAC495271d0F);
ISablierV2LockupLinear public constant LOCKUP_LINEAR =
ISablierV2LockupLinear(0xAFb979d9afAd1aD27C5eFf4E27226E3AB9e5dCC9);
ISablierV2Batch public constant BATCH = ISablierV2Batch(0xEa07DdBBeA804E7fe66b958329F8Fa5cDA95Bd55);
}

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 Mainnet. If you need to work with a different chain, the Sablier addresses can be obtained from the Deployment Addresses page.

Batch create functions

There are two batch create functions for the Lockup Linear contract:

Which one you choose depends upon your use case. In this guide, we will use createWithDurations.

Function definition

Define a function called batchCreateStreams that takes a parameter perStreamAmount and returns an array of ids for the created streams:

function batchCreateStreams(uint128 perStreamAmount) public returns (uint256[] memory streamIds) {
// ...
}

Batch size

Next, declare a batch size, which is needed to calculate the transfer amount:

// Create a batch of two streams
uint256 batchSize = 2;

// Calculate the combined amount of DAI assets to transfer to this contract
uint256 transferAmount = perStreamAmount * batchSize;

ERC-20 steps

To create a stream, the caller must approve the creator contract to pull the tokens from the calling address's account. Then, we have to also approve the Batch contract to pull the assets that the creator contract will be in possession of after they are transferred from the calling address (you):

// Transfer the provided amount of DAI tokens to this contract
DAI.transferFrom(msg.sender, address(this), transferAmount);

// Approve the Batch contract to spend DAI
DAI.approve(address(BATCH), transferAmount);

For more guidance on how to approve and transfer ERC-20 assets, see this article on the Ethereum website.

Stream Parameters

Given that we declared a batchSize of two, we need to define two Batch.CreateWithDurations structs:

// Declare the first stream in the batch
Batch.CreateWithDurations memory stream0;
stream0.sender = address(0xABCD); // The sender to stream the assets, he will be able to cancel the stream
stream0.recipient = address(0xCAFE); // The recipient of the streamed assets
stream0.totalAmount = perStreamAmount; // The total amount of each stream, inclusive of all fees
stream0.cancelable = true; // Whether the stream will be cancelable or not
stream0.durations = LockupLinear.Durations({
cliff: 4 weeks, // Assets will be unlocked only after 4 weeks
total: 52 weeks // Setting a total duration of ~1 year
});
stream0.broker = Broker(address(0), ud60x18(0)); // Optional parameter left undefined

To add some variety, we will change the parameters of the second stream:

// Declare the second stream in the batch
Batch.CreateWithDurations memory stream1;
stream1.sender = address(0xABCD); // The sender to stream the assets, he will be able to cancel the stream
stream1.recipient = address(0xBEEF); // The recipient of the streamed assets
stream1.totalAmount = perStreamAmount; // The total amount of each stream, inclusive of all fees
stream1.cancelable = false; // Whether the stream will be cancelable or not
stream1.durations = LockupLinear.Durations({
cliff: 1 weeks, // Assets will be unlocked only after 1 week
total: 26 weeks // Setting a total duration of ~6 months
});
stream1.broker = Broker(address(0), ud60x18(0)); // Optional parameter left undefined

Once both structs are declared, the batch array has to be filled:

// Fill the batch param
Batch.CreateWithDurations[] memory batch = new Batch.CreateWithDurations[](batchSize);
batch[0] = stream0;
batch[1] = stream1;

Invoke the batch create function

With all parameters set, we can now call the createWithDurations function, and assign the ids of the newly created streams to the array:

streamIds = BATCH.createWithDurations(LOCKUP_LINEAR, DAI, batch);

The complete Batch Lockup Linear stream creator contract

Below you can see the complete functioning code: a contract that batch creates Lockup Linear streams using Sablier's Batch that start at block.timestamp. You can access the code on GitHub through this link.

Batch Lockup Linear stream creator
loading...