Skip to content

Salmandabbakuti/web3-plugin-superfluid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Superfluid Web3 Plugin

npm version

The Superfluid Web3.js Plugin extends the capabilities of the Web3.js library to interact seamlessly with the Superfluid Protocol. This plugin provides convenient methods for interacting with the Superfluid protocol contracts.

Supported Features:

Installation

Note: Make sure you are using web3 version 4.0.3 or higher in your project.

npm install web3-plugin-superfluid web3@latest --save

Usage

Basic Usage

import { Web3 } from "web3";
import { SuperfluidPlugin } from "web3-plugin-superfluid";

const web3 = new Web3("https://rpc-mumbai.maticvigil.com"); // Any RPC node you wanted to connect with
web3.registerPlugin(new SuperfluidPlugin());

// Getting contract addresses of the network
const addresses = web3.superfluid.contractAddresses(80001); // chainId of the network

// Contract Instances
const cfav1Forwarder = web3.superfluid.cfav1Forwarder(addresses.cfaV1Forwarder);
const cfav1 = web3.superfluid.cfav1(addresses.cfaV1);
const host = web3.superfluid.host(addresses.host);
const idav1 = web3.superfluid.idav1(addresses.idaV1);

const token = "0x5d8b4c2554aeb7e86f387b4d6c00ac33499ed01f"; //fDAIx on Mumbai
const sender = "0xc7203561EF179333005a9b81215092413aB86aE9";
const receiver = "0x7348943C8d263ea253c0541656c36b88becD77B9";

// Getting FlowRate with cfav1Forwarder
const flowRate = await cfav1Forwarder.methods
  .getFlowRate(token, sender, receiver)
  .call();

console.log("FlowRate: ", flowRate.toString());

// Getting Flow Info with cfav1
const flow = await cfav1.methods.getFlow(token, sender, receiver).call();

console.log("Flow Info: ", flow);

Connecting Accounts to Web3

import { Web3 } from "web3";
import { SuperfluidPlugin } from "web3-plugin-superfluid";

// With any RPC node and private key
const web3 = new Web3("https://rpc-mumbai.maticvigil.com/");
const wallet = web3.eth.accounts.wallet.add("0x..."); // Private Key
const { address: account } = wallet[0];

// or with browser wallets
const web3 = new Web3(window.ethereum);
const [account] = await window.ethereum.request({
  method: "eth_requestAccounts"
});

web3.registerPlugin(new SuperfluidPlugin());

// Getting contract addresses of the network
const addresses = web3.superfluid.contractAddresses(80001); // chainId of the network

// Contract Instances
const cfav1Forwarder = web3.superfluid.cfav1Forwarder(addresses.cfaV1Forwarder);
const cfav1 = web3.superfluid.cfav1(addresses.cfaV1);
const host = web3.superfluid.host(addresses.host);
const idav1 = web3.superfluid.idav1(addresses.idaV1);

Money Streaming(CFA):

Creating flow:

// using cfav1forwarder
const tx = await cfav1Forwarder.methods
  .createFlow(token, sender, receiver, flowRate, "0x")
  .send({ from: account });

// using host
const callData = cfav1.methods
  .createFlow(token, receiver, flowRate, "0x")
  .encodeABI();

const tx = await host.methods
  .callAgreement(cfav1Address, callData, "0x")
  .send({ from: account });

Updating flow:

// using cfav1forwarder
const tx = await cfav1Forwarder.methods
  .updateFlow(token, sender, receiver, flowRate, "0x")
  .send({ from: account });

// using host
const callData = cfav1.methods
  .updateFlow(token, receiver, flowRate, "0x")
  .encodeABI();

const tx = await host.methods
  .callAgreement(cfav1Address, callData, "0x")
  .send({ from: account });

Deleting flow:

// using cfav1forwarder
const tx = await cfav1Forwarder.methods
  .deleteFlow(token, sender, receiver, "0x")
  .send({ from: account });

// using host
const callData = cfav1.methods
  .deleteFlow(token, sender, receiver, "0x")
  .encodeABI();

const tx = await host.methods
  .callAgreement(cfav1Address, callData, "0x")
  .send({ from: account });

Retrieving flow:

// with cfav1forwarder
const flow = await cfav1Forwarder.methods
  .getFlowInfo(token, sender, receiver)
  .call();

// with cfav1
const flow = await cfav1.methods.getFlow(token, sender, receiver).call();

Refer Superfluid CFA docs for more on respective contract methods.

Distributions(IDA):

Creating Index:

const callData = idav1.methods.createIndex(token, indexId, "0x").encodeABI();

const tx = await host.methods
  .callAgreement(idav1Address, callData, "0x")
  .send({ from: account });

Getting Subscription:

const { units, exist, approved } = await idav1.methods
  .getSubscription(token, publisher, indexId, subscriber)
  .call();

Getting Index:

const { exist, totalUnitsApproved, totalUnitsPending } = await idav1.methods
  .getIndex(token, publisher, indexId)
  .call();

Refer Superfluid IDA docs for more on respective contract methods.

Publishing

To publish a new version of the package to npm, run the following command:

npm run build

npm publish

Change Log

0.2.7

  • Added example to show how to use the plugin in a react app
  • Few tsdoc improvements
  • Updated dependencies

0.2.6

  • Added handy contractAddresses method to get contract addresses of the network
  • Updated tests and README.md examples with contractAddresses method

0.2.5

  • Updated README.md examples and TSDoc

0.2.3

  • Using jest for tests instead of mocha
  • Added tests for invalid address to cfav1Forwarder, cfav1, idav1, host

0.2.1

  • Updated README.md examples and docs

0.2.0

  • Added support for IDAv1 contract
  • IDAv1 examples, tests, and docs

0.1.0

  • Added support for CFAv1 contract
  • Added examples on encoding calldata with cfav1 contract for creating, updating, deleting in host's callAgreement method

0.0.9

  • Added examples for creating, updating, deleting, and retrieving flows using CFAv1Forwarder and Host contracts
  • Added examples for connecting accounts to web3 using private key and metamask

0.0.8

  • cfav1Forwarder, host contract type exports, function natspec comments
  • Tests Cleanup and refactoring

0.0.7

  • Fixed Types not being interpreted correctly, Plugin named export

0.0.1

  • Added support for CFAv1Forwarder and Host contracts

Resources

Safety

This is experimental software and subject to change over time.

This package is not audited and has not been tested for security. Use at your own risk. I do not give any warranties and will not be liable for any loss incurred through any use of this codebase.

License

This project is licensed under the MIT License - see the LICENSE file for details