Quickstart
Superfluid is a protocol revolving around a new token standard called Super Token. Super Tokens have special capabilities (super powers?). One of them allows to create second-by-second token transfers (called Money Streaming). In this guide, we'll walk through the process of setting up and deploying a basic contract for creating, updating and deleting Superfluid money streams. Let's go!
This is just an example to get you started with Superfluid. You can use this as a base to build more complex applications.
You DO NOT need to set up the FlowSender contract to create, update and delete streams. You can do this directly from the Dashboard or by interacting with the CFAv1Forwarder contract.
Prerequisites
- Solidity knowledge
- A development environment (Remix, Hardhat, or Foundry)
- A testnet wallet (e.g., MetaMask)
- Access to a testnet native coin (e.g. Polygon Mumbai Testnet Matic - get faucet here)
Contract Overview: FlowSender
In this guide we will describe the contract FlowSender
:
- This contract is designed to interact with the Superfluid protocol, specifically to send Super Tokens using Money Streaming.
- This contract enables the creation, modification, and deletion of continuous money streams.
- The contract interacts with an example super token (fake DAI Super Token - fDAIx) to create a stream from itself to a receiver.
- Each time a stream is created, a new receiver is added to the list of receivers, and the Superfluid Protocol keeps track of the balances and the receivers on the blockchain.
Visualization showing the user creating a flow through FlowSender and all of the streams that have been created
If you are new to blockchain development, you may not be familiar with the term "Tx". Tx is short for transaction, which is a record of an action on the blockchain. Transactions are used to transfer value, store data, and interact with smart contracts.
Contract and Key Functions
Click here to show FlowSender
contract
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { ISuperfluid, ISuperToken } from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/ISuperfluid.sol";
import { SuperTokenV1Library } from "@superfluid-finance/ethereum-contracts/contracts/apps/SuperTokenV1Library.sol";
interface IFakeDAI is IERC20 {
function mint(address account, uint256 amount) external;
}
contract FlowSender {
using SuperTokenV1Library for ISuperToken;
mapping (address => bool) public accountList;
ISuperToken public daix;
// fDAIx address on Polygon Mumbai = 0x5D8B4C2554aeB7e86F387B4d6c00Ac33499Ed01f
constructor(ISuperToken _daix) {
daix = _daix;
}
/// @dev Mints 10,000 fDAI to this contract and wraps it all into fDAIx
function gainDaiX() external {
// Get address of fDAI by getting underlying token address from DAIx token
IFakeDAI fdai = IFakeDAI( daix.getUnderlyingToken() );
// Mint 10,000 fDAI
fdai.mint(address(this), 10000e18);
// Approve fDAIx contract to spend fDAI
fdai.approve(address(daix), 20000e18);
// Wrap the fDAI into fDAIx
daix.upgrade(10000e18);
}
/// @dev controls a stream with the given flowrate between this contract and the desired receiver
function setStream(address receiver, int96 flowRate) external {
daix.flow(receiver, flowRate);
}
/// @dev get flow rate between this contract and the given receiver
function getFlowRate(address receiver) external view returns (int96 flowRate) {
return daix.getFlowRate(address(this), receiver);
}
}
- gainDaiX: Mints and wraps fDAI into fDAIx (Superfluid's wrapped token).
- setStream: Controls a stream between the contract and a receiver
- getFlowRate: Reads the current flow rate of a stream.
Environment Setup and Deployment
Deploy the FlowSender
contract to a testnet using your preferred development environment.
- Remix IDE
- Foundry
- Hardhat
Using Remix IDE
- Access Remix IDE: Visit Remix IDE.
- Create New File: In the 'File Explorers' tab, create a new
.sol
file. - Paste Contract Code: Copy the
FlowSender
contract code into the new file. - Compile the Contract: Go to the 'Solidity Compiler' tab and click 'Compile'.
- Deploy the Contract:
- Switch to the 'Deploy & Run Transactions' tab.
- Connect to MetaMask, selecting Mumbai Testnet.
- Click on 'Deploy' to deploy your contract.
For more information, check out the Remix IDE documentation.
Using Foundry
- Set Up Foundry: Initialize a new Foundry project with
forge init
. - Place Contract: Add the
FlowSender
contract to yoursrc
directory. - Write Deployment Script: Create a deployment script in the
script
directory. - Compile and Deploy: Use
forge build
to compile andforge deploy --network mumbai
to deploy.
Learn more about Foundry at the Foundry Book.
Using Hardhat
- Initialize Hardhat: Set up a new Hardhat project by running
npx hardhat init
in your terminal. - Install Dependencies: Install necessary dependencies like
@nomiclabs/hardhat-ethers
. - Add Contract: Place the
FlowSender
contract in thecontracts
directory. - Create Deployment Script: Write a script in the
scripts
folder to deploy the contract. - Deploy: Run the script using
npx hardhat run scripts/deploy.js --network mumbai
.
Explore more about Hardhat in the Hardhat documentation.
When deploying the contract, you will need to provide the address of the fDAIx token on the testnet you are using. You can find those addresses in the Superfluid Explorer.
Interacting with the Contract
Once you have deployed your contract, you can run your tests on your favourite framework. You can also test your smart contract live in the browser using the Superfluid Flow Sender Playground below. Modify yourAddress
putting the address to your deployed contract and follow the steps on the interface below.
function FlowSenderPlayground() { const yourAddress="0xe8f9eEDf7f171111169C71119cd625ee629E9831"; return ( <div> <FlowSenderComponent contractAddress={yourAddress} /> </div> ); }
Further Exploration
Understanding Super Tokens
Super Tokens are the core of Superfluid, offering real-time finance capabilities. Learn about how these upgraded ERC-20 tokens enable new financial primitives.
Exploring Money Streaming
Money Streaming is a novel concept introduced by Superfluid, allowing continuous and instant value transfer over time.
Diving into Distributions
Distributions are a key feature for scalable one-to-many payments in Superfluid. Learn how you can leverage distributions for various use cases.