πŸ“˜
CFAv1 Library
The CFAv1 Library is a thin layer of abstraction on the Constant Flow Agreement V1. This guide covers how to import, initialize, and use the library, as well as documentation for each function.

The CFA Library

The Constant Flow Agreement library (CFAv1Library.sol) makes working with the CFA much simpler. Instead of following the host.callAgreement(...) pattern, you can make calls to the protocol in fewer lines of code. If you've already looked at the Superfluid core-sdk, then the syntax for creating, updating, and deleting flows will look similar to what you may have already seen. For a more advanced set of examples using the CFA Library, you can refer to this mock contract.

Initializing the Library

1
// initializing the CFA Library
2
pragma solidity ^0.8.0
3
​
4
import {
5
ISuperfluid
6
} from "@superfluid-finance/ethereum-contracts/contracts/interfaces/superfluid/ISuperfluid.sol"; //"@superfluid-finance/ethereum-monorepo/packages/ethereum-contracts/contracts/interfaces/superfluid/ISuperfluid.sol";
7
​
8
import {
9
IConstantFlowAgreementV1
10
} from "@superfluid-finance/ethereum-contracts/contracts/interfaces/agreements/IConstantFlowAgreementV1.sol";
11
​
12
import {
13
CFAv1Library
14
} from "@superfluid-finance/ethereum-contracts/contracts/apps/CFAv1Library.sol";
15
​
16
contract CFALibraryMock {
17
​
18
using CFAv1Library for CFAv1Library.InitData;
19
20
//initialize cfaV1 variable
21
CFAv1Library.InitData public cfaV1;
22
23
constructor(
24
ISuperfluid host
25
) {
26
27
//initialize InitData struct, and set equal to cfaV1
28
cfaV1 = CFAv1Library.InitData(
29
host,
30
//here, we are deriving the address of the CFA using the host contract
31
IConstantFlowAgreementV1(
32
address(host.getAgreementClass(
33
keccak256("org.superfluid-finance.agreements.ConstantFlowAgreement.v1")
34
))
35
)
36
);
37
}
38
39
//your contract code here...
40
}
Copied!

Using the CFA Library

After initializing the library, it gets very simple to create, update, and delete flows:
1
// CFA CRUD functionality
2
cfaV1.createFlow(receiver, token, flowRate);
3
cfaV1.updateFlow(receiver, token, flowRate);
4
cfaV1.deleteFlow(sender, receiver, token);
Copied!
If you'd like, you can also add user data as an optional parameter to each call to the CFA:
1
// with user data
2
cfaV1.createFlow(receiver, token, flowRate, userData);
3
cfaV1.updateFlow(receiver, token, flowRate, userData);
4
cfaV1.deleteFlow(sender, receiver, token, userData);
Copied!
However, it's worth noting that the parameters passed to each function in the library need to be of the same type as the parameters used in the vanilla solidity syntax seen in the beginning of this page:
receiver - the address of the receiver
token - the ISuperToken used in the flow
flowRate - an int96 variable which represents the total amount of the token you'd like to send per second, denominated in wei
userData - an optional bytes value which represents additional data you'd like to pass along with your function call. You can learn more about user data here.

Using the CFA Library inside of a Super App

If you need to perform operations with the Constant Flow Agreement inside of Super App callbacks, the syntax for using the library will look somewhat different. You'll need to use the withCtx versions of each function. For more on why this is, you can see this section on callAgreement vs callAgreementWithContext.
1
// with Ctx - to be used inside of super app callbacks
2
// NOTE: ctx is a bytes value
3
cfaV1.createFlowWithCtx(ctx, receiver,token, flowRate);
4
cfaV1.updateFlowWithCtx(ctx, receiver,token, flowRate);
5
cfaV1.deleteFlowWithCtx(ctx, sender, receiver,token);
6
​
7
//withCtx & userData - to be used inside of super app callbacks
8
cfaV1.createFlowWithCtx(ctx, receiver, token, flowRate, userData);
9
cfaV1.updateFlowWithCtx(ctx, receiver, token, flowRate, userData);
10
cfaV1.deleteFlowWithCtx(ctx, sender, receiver,token, userData);
Copied!
All other variable fields will are the same as detailed in the previous section, but ctx will be the ctx which is passed in to the Super App callback by the framework where these functions are being called. This ctx value is of type bytes. For example, in the afterAgreementCreated callback:
1
//in super app callback
2
function afterAgreementCreated(
3
ISuperToken superToken,
4
address agreementClass,
5
bytes32, // _agreementId,
6
bytes calldata /_agreementData/,
7
bytes calldata ,// _cbdata,
8
bytes calldata ctx
9
) external returns (bytes memory newCtx) {
10
11
//this function takes the callback's ctx value as the first param
12
//and returns an updated ctx value upon completion
13
return cfaV1.createFlowWithCtx(ctx, receiver,token, flowRate);
14
}
Copied!
The final parameter passed into the callback - ctx, will be passed as the ctx value to the withCtx functions within the library.
Each withCtx function in the library also returns a new ctx value as well, which you will need to return inside of the super app callback. Again, more information on this can be found in this section on callAgreement vs callAgreementWithContext.

​