🦾
Super Apps
Manage Flows and IDAs using a smart contract
A Super App can "manage" agreements and respond to changes. This is where you can write your own custom logic/behavior. In this section we will deploy a new Super App using Remix.
So what will our app do? Our app will accept incoming CFA flows, and use some internal logic to determine where to redirect it. The reciever of all the incoming flows will be decided by whoever owns a ERC721 Non-fungible Token (NFT).
If the NFT is transferred to a new owner, all token flows are redirected to the new owner.
We will call the app Holy Grail, since whoever is in possession of the NFT is blessed with great power.
A chalice is handed to another person (https://unsplash.com/@reskp)

Video

If you'd prefer to listen and code along, I've made a video version of this tutorial section:
​

Prerequisites

Before starting this tutorial you should:

Setup

Click the link to load the contracts in Remix:
Remix - Ethereum IDE
Once it loads, you should see TradeableCashflow.sol and RedirectAll.sol in the editor window.
In the file explorer, click to expand gist "9799..." to view both contracts. TradeableCashflow.sol is our main contract, which inherits RedirectAll from the local file, and the ERC721 contract from the Open Zeppelin Github repo.
The two main concepts of TradeableCashflow.sol are:
  1. 1.
    Create a new ERC721 contract, and mint a single NFT to be the Holy Grail
  2. 2.
    Whenever the NFT is transferred, the _beforeTokenTransfer() hook calls _changeReceiver() which will redirect the incoming flows.

Calling Agreements

In order for our app to redirect flows, it must be able to call the Superfluid Agreements.
Let's look at what _changeReceiver() does in the contract RedirectAll.sol. The first step is to delete the current flow from the Super App to the current reciever.
1
// @dev Change the Receiver of the total flow
2
function _changeReceiver( address newReceiver ) internal {
3
// @dev delete flow to old receiver
4
_host.callAgreement(
5
_cfa,
6
abi.encodeWithSelector(
7
_cfa.deleteFlow.selector,
8
_acceptedToken,
9
address(this),
10
_receiver,
11
new bytes(0)
12
),
13
"0x"
14
);
Copied!
This is the first time we are introducing examples of how to interact with the Framework contracts directly. Don't worry about understanding every part right now, you can just skim to get an idea.
Here's how the same code would look if we were using the@superfluid-finance/js-sdk to perform the same action:
1
await flow({recipient: currentRecipient, flowRate: "0" });
Copied!
In the next part of _changeReveiver(), the Super App starts a new flow to the new reciever, or new NFT owner:
1
// @dev create flow to new receiver
2
_host.callAgreement(
3
_cfa,
4
abi.encodeWithSelector(
5
_cfa.createFlow.selector,
6
_acceptedToken,
7
newReceiver,
8
_cfa.getNetFlow(_acceptedToken, address(this)),
9
new bytes(0)
10
),
11
"0x"
12
);
13
// @dev set global receiver to new receiver
14
_receiver = newReceiver;
15
​
16
emit ReceiverChanged(_receiver);
17
}
Copied!
Again for comparison, here is how it would look if we used the superfluid-finance/js-sdk instead:
1
// Get the new incoming flow rate
2
const newFlowRate = await sf.cfa.getNetFlow({
3
superToken: _acceptedToken,
4
account: myContract.address
5
})
6
7
// Start a flow to the new recipient
8
await flow({recipient: newRecipient, flowRate: newFlowRate });
9
​
10
// Reassign the recipient to the newRecipient
11
currentRecipient = newRecipient
12
​
Copied!
This is how we can call an Agreement to start a flow in a contract. But what about reacting to incoming flows automatically? To respond to changes in an Agreement, we need to use Super App callbacks.

Callbacks

Super Apps can respond to changes in Agreements using callbacks. This can be done without needing to call a function in your contract.
When anyone creates a new flow to the Holy Grail contract, we need to "catch" this transaction, and automatically redirect the flow to the receiver.
Around line 170 in RedirectAll.sol we can start to see all the callbacks. The fist one is afterAgreementCreated(). This function is called any time a new agreement is created which involves the SuperApp.
1
function afterAgreementCreated(
2
ISuperToken _superToken,
3
address _agreementClass,
4
bytes32, // _agreementId,
5
bytes calldata /*_agreementData*/,
6
bytes calldata ,// _cbdata,
7
bytes calldata _ctx
8
)
9
external override
10
onlyExpected(_superToken, _agreementClass)
11
onlyHost
12
returns (bytes memory newCtx)
13
{
14
return _updateOutflow(_ctx);
15
}
Copied!
Using the modifier onlyExpected allows use to only execute this when the agreement is a Constant Flow Agreement, and the token address matches the _superToken .
Assuming all the checks pass, then we can trigger _updateOutFlow which combines the new incoming flow and creates the new flow
Now that you understand the two main components of a Super App, its time to deploy it!

Deployment

Switch back to TradeableCashflow.sol in the editor window.
Now in the Compiler tab, make sure you're using compiler version 0.7.0 and hit "compile".
Once compilation is complete, switch to the "Deploy and Run Transactions" tab. Select Environment β€œInjected Web3”, and in your wallet switch to Goerli test network.
Next select the TradeableCashflow contract to deploy, and use the following parameters to deploy:
Parameter
Value
OWNER
(Use your address)
_NAME
Holy Grail
_SYMBOL
GRAIL
​
Host
​
0x22ff293e14F1EC3A09B137e9e06084AFd63adDF9
CFA
0xEd6BcbF6907D4feEEe8a8875543249bEa9D308E8
ACCEPTEDTOKEN
0xf2d68898557ccb2cf4c10c3ef2b034b2a69dad00
This will deploy the Holy Grail contract, mint a single NFT, and transfer it to the owner. Once the transaction is confirmed, copy the address of the deployed contract.
​

Usage

Now comes the magic part. Since we used callbacks in our Super App, we can create a new flow from the Superfluid Dashboard. Our Super App will react to this automatically.
Using the address you copied in the previous step, start a new flow using fDAIx (shown as DAIx in the Dashboard) to your Super App.
After the transaction is confirmed, you will see both an incoming and outgoing flow from the same address. This is because you are the owner of the NFT, so you're effectively streaming to yourself via the Super App.
Now let's transfer the Holy Grail NFT to someone else using transferFrom(). Since there is only a single NFT, the tokenId will be always be 1.
Upon transfer, all incoming flows will be re-directed to the new NFT holder. You can verify this by logging into the Superfluid Dashboard using the receiver's account.
Excellent! Now you know how a Super App works. In the next section we will discuss deploying the Superfluid Framework so you can easily test your own Super App using a local blockchain.
Last modified 19d ago