Skip to main content

On-chain

Building on top of the Superfluid protocol involves interacting with Super Tokens and Distributions, which are essential concepts within the Superfluid ecosystem. Distributions, sometimes known as the General Distribution Agreement (GDA), allow for scalable and efficient one-to-many token distributions at a fixed gas cost.

Importance of Distributions

Distributions are at the core of Superfluid's functionality, enabling a multitude of applications such as subscription services, salary payments, and rewards distribution. They are designed to be flexible, allowing for both instant and streaming distributions.

  • Instant Distributions: Allow for a one-time distribution of a specified amount of tokens to all members of a pool instantly.
  • Streaming Distributions: Enable a continuous flow of tokens to pool members over time.

Key Functions of SuperTokenV1Library.sol

SuperTokenV1Library.sol provides several functions to interact with pools and distributions:

getFlowDistributionFlowRate

function getFlowDistributionFlowRate(ISuperToken token, address from, ISuperfluidPool to) internal view returns (int96)

Retrieves the flow rate of a distribution from a sender to a pool.

estimateFlowDistributionActualFlowRate

function estimateFlowDistributionActualFlowRate(ISuperToken token, address from, ISuperfluidPool to, int96 requestedFlowRate) internal view returns (int96 actualFlowRate, int96 totalDistributionFlowRate)

Estimates the actual flow rate that will be distributed based on the requested flow rate.

estimateDistributionActualAmount

function estimateDistributionActualAmount(ISuperToken token, address from, ISuperfluidPool to, uint256 requestedAmount) internal view returns (uint256 actualAmount)

Estimates the actual distribution amount that will be allocated to the pool based on the requested amount.

isMemberConnected

function isMemberConnected(ISuperToken token, address pool, address member) internal view returns (bool)

Checks whether a member is connected to a pool and eligible to receive distributions.

Working with Streaming Distributions

To interact with streaming distributions, you can use functions such as:

distributeFlow

function distributeFlow(ISuperToken token, address from, ISuperfluidPool pool, int96 requestedFlowRate) internal returns (bool)

Initiates a distribution flow from a sender to a pool with the specified flow rate.

Important

Keep in mind that the total amount of units in the pool needs to be significantly lower than the total flow rate or the total tokens distributed of the pool. To understand more why this is the case, please refer to the Member Units section.

Contract Example

Here's a simple contract example using some of the functions from SuperTokenV1Library.sol to interact with distributions:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@superfluid-finance/ethereum-contracts/contracts/superfluid/SuperTokenV1Library.sol";

contract MyDistributionsContract {
using SuperTokenV1Library for ISuperToken;

ISuperToken private _superToken;

constructor(ISuperToken superToken) {
_superToken = superToken;
}

function distributeInstantly(ISuperfluidPool pool, uint256 amount) public {
_superToken.estimateDistributionActualAmount(address(this), pool, amount);
// Additional logic for distribution
}

function startStreamingDistribution(ISuperfluidPool pool, int96 flowRate) public {
_superToken.distributeFlow(address(this), pool, flowRate);
// Additional logic for streaming distribution
}
}

This contract demonstrates how to estimate the amount for an instant distribution and start a streaming distribution using the SuperTokenV1Library.

tip

When using the SuperTokenV1Library.sol, you don't need to include the SuperToken as a parameter in your method call. Simply call SuperToken.[Method] and the library will handle the rest.

Further Reading

For more detailed information on the implementation and usage of SuperTokenV1Library.sol, refer to the SuperTokenV1Library Technical reference.