Skip to main content

Create a Batch of Dynamic Streams

In this guide, we will show you how you can use Solidity to batch create dynamic streams via the Batch Lockup contract.

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

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:

loading...

Now, import the relevant symbols from @sablier/lockup:

loading...

Create a contract called BatchLDStreamCreator, and declare a constant DAI of type IERC20, a constant LOCKUP of type ISablierLockup, and a constant BATCH_LOCKUP of type ISablierBatchLockup:

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, Lockup addresses can be obtained from the Lockup Deployments page.

Batch create functions

There are two batch create functions for the Dynamic streams:

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

Function definition

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

loading...

Batch size

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

loading...

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 tokens that the creator contract will be in possession of after they are transferred from the calling address (you):

loading...

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

Stream Parameters

Given that we declared a batchSize of two, we need to define two BatchLockup.CreateWithTimestampsLD structs:

loading...

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

loading...

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

loading...

Invoke the batch create function

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

loading...

Full code

Below you can see the full code. You can also access the code on GitHub through this link.

Batch Lockup Dynamic stream creator
loading...