Skip to content

Commit

Permalink
added: get contract addresses plugin method, docs, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Salmandabbakuti committed Dec 22, 2023
1 parent 9c3a2f8 commit cdd06c7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 40 deletions.
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Superfluid Web3 Plugin

[![npm version](https://img.shields.io/badge/npm-0.2.5-brightgreen)](https://www.npmjs.com/package/web3-plugin-superfluid)
[![npm version](https://img.shields.io/badge/npm-0.2.6-brightgreen)](https://www.npmjs.com/package/web3-plugin-superfluid)

The Superfluid Web3.js Plugin extends the capabilities of the Web3.js library to interact seamlessly with the [Superfluid Protocol](https://superfluid.finance). This plugin provides convenient methods for interacting with the Superfluid protocol contracts.

Expand Down Expand Up @@ -28,20 +28,17 @@ npm install web3-plugin-superfluid web3@latest --save
import { Web3 } from "web3";
import { SuperfluidPlugin } from "web3-plugin-superfluid";

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

// Contract Addresses vary from network to network
const cfav1ForwarderAddress = "0x...";
const cfav1Address = "0x...";
const hostAddress = "0x...";
const idav1Address = "0x...";
// Getting contract addresses of the network
const addresses = web3.superfluid.contractAddresses(80001); // chainId of the network

// Contract Instances
const cfav1Forwarder = web3.superfluid.cfav1Forwarder(cfav1ForwarderAddress);
const cfav1 = web3.superfluid.cfav1(cfav1Address);
const idav1 = web3.superfluid.idav1(idav1Address);
const host = web3.superfluid.host(hostAddress);
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";
Expand Down Expand Up @@ -79,14 +76,14 @@ const [account] = await window.ethereum.request({

web3.registerPlugin(new SuperfluidPlugin());

const cfav1ForwarderAddress = "0x...";
const cfav1Address = "0x...";
const hostAddress = "0x...";
const idav1Address = "0x...";
const cfav1Forwarder = web3.superfluid.cfav1Forwarder(cfav1ForwarderAddress);
const host = web3.superfluid.host(hostAddress);
const cfav1 = web3.superfluid.cfav1(cfav1Address);
const idav1 = web3.superfluid.idav1(idav1Address);
// 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):
Expand Down Expand Up @@ -201,6 +198,11 @@ npm publish

## Change Log

#### 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
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web3-plugin-superfluid",
"version": "0.2.5",
"version": "0.2.6",
"description": "Superfluid Web3.js Plugin",
"author": "Salman Dabbakuti",
"license": "MIT",
Expand All @@ -22,14 +22,16 @@
"peerDependencies": {
"web3": ">= 4.0.3"
},
"dependencies": {
"@superfluid-finance/metadata": "^1.1.21"
},
"devDependencies": {
"@types/jest": "^29.5.10",
"@types/node": "^20.9.4",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.5",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"ts-jest": "^29.1.1",
"typescript": "^5.3.2",
"web3": "^4.2.2"
"typescript": "^5.3.3",
"web3": "^4.3.0"
},
"repository": {
"type": "git",
Expand Down
30 changes: 30 additions & 0 deletions src/superfluid-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Contract, Web3PluginBase, validator } from "web3";
import sfMeta from "@superfluid-finance/metadata";
import cfav1ForwarderAbi from "./abis/cfav1Forwarder";
import hostAbi from "./abis/host";
import cfav1Abi from "./abis/cfav1";
Expand All @@ -8,6 +9,13 @@ export type CFAV1Forwarder = Contract<typeof cfav1ForwarderAbi>;
export type Host = Contract<typeof hostAbi>;
export type CFAV1 = Contract<typeof cfav1Abi>;
export type IDAV1 = Contract<typeof idav1Abi>;
export type ProtocolContractAddresses = {
cfaV1: string;
cfaV1Forwarder: string;
idaV1: string;
host: string;
[key: string]: any;
};

export class SuperfluidPlugin extends Web3PluginBase {
public pluginNamespace = "superfluid";
Expand Down Expand Up @@ -91,6 +99,28 @@ export class SuperfluidPlugin extends Web3PluginBase {
hostContract.link(this);
return hostContract;
}

/**
* This method returns Superfluid protocol contract addresses of given chainId
* @param chainId - ChainId of the network to get contract addresses of
* @throws Error if chainId is not supported
* @returns Protocol contract addresses object
* @example
* ```ts
* const web3 = new Web3("https://rpc-mumbai.maticvigil.com");
* web3.registerPlugin(new SuperfluidPlugin());
* const addresses = web3.superfluid.contractAddresses(80001);
* ```
*/

public contractAddresses(chainId: number): ProtocolContractAddresses {
const networkMetadata = sfMeta.networks.find(
(network) => network.chainId === chainId
);
if (!networkMetadata)
throw new Error("Superfluid Plugin: Unsupported ChainId");
return networkMetadata.contractsV1;
}
}

// Module Augmentation
Expand Down
50 changes: 35 additions & 15 deletions test/superfluid-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { Web3, Web3Eth, Web3Context } from "web3";
import { SuperfluidPlugin, CFAV1Forwarder, CFAV1, IDAV1, Host } from "../src";

// Test data: Mumbai testnet
// Test data: Mumbai testnet: 80001
// https://console.superfluid.finance/mumbai/protocol
const chainId = 80001;
const rpcUrl = "https://rpc-mumbai.maticvigil.com";
const cfav1ForwarderAddress = "0xcfA132E353cB4E398080B9700609bb008eceB125";
const cfav1Address = "0x49e565Ed1bdc17F3d220f72DF0857C26FA83F873";
const idav1Address = "0x804348D4960a61f2d5F9ce9103027A3E849E09b8";
const hostAddress = "0xEB796bdb90fFA0f28255275e16936D25d3418603";
const token = "0x5d8b4c2554aeb7e86f387b4d6c00ac33499ed01f"; //fDAIx on Mumbai
const sender = "0xc7203561EF179333005a9b81215092413aB86aE9";
const receiver = "0x7348943C8d263ea253c0541656c36b88becD77B9";
Expand All @@ -23,6 +20,7 @@ describe("SuperfluidPlugin Tests", () => {
expect(web3.superfluid.cfav1).toBeInstanceOf(Function);
expect(web3.superfluid.idav1).toBeInstanceOf(Function);
expect(web3.superfluid.host).toBeInstanceOf(Function);
expect(web3.superfluid.contractAddresses).toBeInstanceOf(Function);
});

it("should register Superfluid plugin to Web3Context", () => {
Expand All @@ -35,6 +33,7 @@ describe("SuperfluidPlugin Tests", () => {
expect(web3Context.superfluid.cfav1).toBeInstanceOf(Function);
expect(web3Context.superfluid.idav1).toBeInstanceOf(Function);
expect(web3Context.superfluid.host).toBeInstanceOf(Function);
expect(web3Context.superfluid.contractAddresses).toBeInstanceOf(Function);
});

it("should register Superfluid plugin to Web3Eth", () => {
Expand All @@ -47,6 +46,9 @@ describe("SuperfluidPlugin Tests", () => {
expect(web3EthContext.superfluid.cfav1).toBeInstanceOf(Function);
expect(web3EthContext.superfluid.idav1).toBeInstanceOf(Function);
expect(web3EthContext.superfluid.host).toBeInstanceOf(Function);
expect(web3EthContext.superfluid.contractAddresses).toBeInstanceOf(
Function
);
});

it("should throw error if address passed to plugin functions is not valid", () => {
Expand Down Expand Up @@ -81,36 +83,52 @@ describe("SuperfluidPlugin Method Tests", () => {
beforeAll(() => {
web3 = new Web3(rpcUrl);
web3.registerPlugin(new SuperfluidPlugin());
cfav1Forwarder = web3.superfluid.cfav1Forwarder(cfav1ForwarderAddress);
cfav1 = web3.superfluid.cfav1(cfav1Address);
host = web3.superfluid.host(hostAddress);
idav1 = web3.superfluid.idav1(idav1Address);
const addresses = web3.superfluid.contractAddresses(chainId);
cfav1Forwarder = web3.superfluid.cfav1Forwarder(addresses.cfaV1Forwarder);
cfav1 = web3.superfluid.cfav1(addresses.cfaV1);
host = web3.superfluid.host(addresses.host);
idav1 = web3.superfluid.idav1(addresses.idaV1);
});

it("should get flow info with forwarder", async () => {
it("contractAddresses: should throw error if chainId is not supported", () => {
expect(() => {
web3.superfluid.contractAddresses(123);
}).toThrow("Superfluid Plugin: Unsupported ChainId");
});

it("contractAddresses: should get contract addresses for given chainId", () => {
const addresses = web3.superfluid.contractAddresses(chainId);
expect(addresses).toBeInstanceOf(Object);
expect(addresses).toHaveProperty("cfaV1Forwarder");
expect(addresses).toHaveProperty("cfaV1");
expect(addresses).toHaveProperty("idaV1");
expect(addresses).toHaveProperty("host");
});

it("cfav1Forwarder: should get flow info", async () => {
const { lastUpdated, flowrate } = await cfav1Forwarder.methods
.getFlowInfo(token, sender, receiver)
.call();
expect(typeof lastUpdated).toBe("bigint");
expect(typeof flowrate).toBe("bigint");
});

it("should get flowrate with forwarder", async () => {
it("cfav1Forwarder: should get flowrate", async () => {
const flowrate = await cfav1Forwarder.methods
.getFlowrate(token, sender, receiver)
.call();
expect(typeof flowrate).toBe("bigint");
});

it("should get flow info with cfav1", async () => {
it("cfav1: should get flow info", async () => {
const { timestamp, flowRate } = await cfav1.methods
.getFlow(token, sender, receiver)
.call();
expect(typeof timestamp).toBe("bigint");
expect(typeof flowRate).toBe("bigint");
});

it("should get token IDA subscription details with idav1", async () => {
it("idav1: should get token IDA subscription details", async () => {
// Test data: Mumbai testnet
const token = "0x5d8b4c2554aeb7e86f387b4d6c00ac33499ed01f"; //fDAIx on Mumbai
const publisher = "0x018762341dda10cfaa2fc19966d2279dc70b6784";
Expand All @@ -125,7 +143,7 @@ describe("SuperfluidPlugin Method Tests", () => {
expect(typeof approved).toBe("boolean");
});

it("should get token IDA index details with idav1", async () => {
it("idav1: should get token IDA index details", async () => {
// Test data: Mumbai testnet
const token = "0x5d8b4c2554aeb7e86f387b4d6c00ac33499ed01f"; //fDAIx on Mumbai
const publisher = "0x018762341dda10cfaa2fc19966d2279dc70b6784";
Expand All @@ -139,7 +157,9 @@ describe("SuperfluidPlugin Method Tests", () => {
expect(typeof totalUnitsPending).toBe("bigint");
});

it("should check if address is trusted forwarder using host", async () => {
it("host: should check if address is trusted forwarder", async () => {
// Test data: Mumbai testnet
const cfav1ForwarderAddress = "0xcfA132E353cB4E398080B9700609bb008eceB125";
const result = await host.methods
.isTrustedForwarder(cfav1ForwarderAddress)
.call();
Expand Down

0 comments on commit cdd06c7

Please sign in to comment.