Skip to content

Commit

Permalink
adding plugin example react app (#5)
Browse files Browse the repository at this point in the history
* added: example app

* updated: example and package deps
  • Loading branch information
Salmandabbakuti committed Feb 7, 2024
1 parent cdd06c7 commit 4f90cbb
Show file tree
Hide file tree
Showing 21 changed files with 477 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ on:
paths-ignore:
- "README.md"
- "LICENSE"
- "example/**"
pull_request:
branches: [main]
paths-ignore:
- "README.md"
- "LICENSE"
- "example/**"

jobs:
e2e-test:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dist/
yarn.lock
npm-debug.log
package-lock.json
.DS_Store

# Executables
*.swf
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.github/
src/
test/
example/

# Executables
*.swf
Expand Down
8 changes: 7 additions & 1 deletion 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.6-brightgreen)](https://www.npmjs.com/package/web3-plugin-superfluid)
[![npm version](https://img.shields.io/badge/npm-0.2.7-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 @@ -198,6 +198,12 @@ 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
Expand Down
21 changes: 21 additions & 0 deletions example/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Salman Dabbakuti

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# web3-plugin-superfluid-example

Superfluid Web3 Plugin example using Vite and React

## Usage

```bash
npm install

npm run dev
```

Currently, using below packages:

- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

- [web3](https://github.com/web3/web3.js): Ethereum JavaScript API

- [web3-plugin-superfluid](https://github.com/Salmandabbakuti/web3-plugin-superfluid/tree/main?tab=readme-ov-file#superfluid-web3-plugin): Web3js plugin for Superfluid Protocol
16 changes: 16 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Superfluid Web3Plugin Example React</title>
</head>

<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>

</html>
32 changes: 32 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "web3-plugin-superfluid-example",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --port 3000",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"antd": "^5.14.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"web3": "^4.4.0",
"web3-plugin-superfluid": "0.2.7"
},
"devDependencies": {
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@vitejs/plugin-react-swc": "^3.6.0",
"eslint": "^8.56.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vite-plugin-node-polyfills": "^0.19.0"
}
}
1 change: 1 addition & 0 deletions example/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions example/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#root {
max-width: 1280px;
margin: 0 auto;
}
235 changes: 235 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useState } from "react";
import { Button, message, Form, Input, Tabs } from "antd";
import { Web3 } from "web3";
import {
SuperfluidPlugin,
CFAV1Forwarder,
CFAV1
} from "web3-plugin-superfluid";
import "./App.css";

interface FlowInput {
token: string;
sender: string;
receiver: string;
}

interface CreateFlowInput extends FlowInput {
flowRate: string;
}

function App() {
const [account, setAccount] = useState<string>("");
const [cfav1Forwarder, setCfav1Forwarder] = useState<CFAV1Forwarder | null>(
null
);
const [cfav1, setCfav1] = useState<CFAV1 | null>(null);
const [flowInfo, setFlowInfo] = useState<any>(null);
const [createFlowInput, setCreateFlowInput] = useState<CreateFlowInput>({
token: "",
sender: "",
receiver: "",
flowRate: ""
});
const [getFlowInfoInput, setGetFlowInfoInput] = useState<FlowInput>({
token: "",
sender: "",
receiver: ""
});

const handleCreateFlowInputChange = (changedValues: any) => {
console.log(changedValues);
setCreateFlowInput({
...createFlowInput,
...changedValues
});
};

const handleGetFlowInfoInputChange = (changedValues: any) => {
console.log(changedValues);
setGetFlowInfoInput({
...getFlowInfoInput,
...changedValues
});
};

const handleConnectWallet = async () => {
if (!(window as any)?.ethereum)
return message.error("Please install MetaMask!");
try {
const web3 = new Web3((window as any).ethereum);
web3.registerPlugin(new SuperfluidPlugin());
// get chainid from metamask
const chainId = await web3.eth.getChainId();
const protocolAddresses = web3.superfluid.contractAddresses(
Number(chainId)
);
const cfav1Forwarder = web3.superfluid.cfav1Forwarder(
protocolAddresses.cfaV1Forwarder
);
const cfav1 = web3.superfluid.cfav1(protocolAddresses.cfaV1);
setCfav1Forwarder(cfav1Forwarder);
setCfav1(cfav1);
const [account1] = await (window as any).ethereum.request({
method: "eth_requestAccounts"
});
setAccount(account1);
message.success("Wallet Connected");
} catch (err: any) {
console.log("Connect Wallet Error", err);
message.error(err.message);
}
};

const getFlowInfo = async () => {
if (!cfav1) return message.error("Please connect wallet first!");
console.log(getFlowInfoInput);
const { token, sender, receiver } = getFlowInfoInput;
try {
const flowInfo = await cfav1.methods
.getFlow(token, sender, receiver)
.call();
console.log(flowInfo);
setFlowInfo(flowInfo);
} catch (err: any) {
console.log("Get Flow Info Error", err);
message.error(err.message);
}
};

const handleCreateFlow = async () => {
if (!cfav1Forwarder) return message.error("Please connect wallet first!");
const { token, sender, receiver, flowRate } = createFlowInput;
message.info("Creating Flow...");

try {
const tx = await cfav1Forwarder.methods
.createFlow(token, sender, receiver, flowRate, "0x")
.send({
from: account
});
console.log(tx);
message.success(`Flow Created, tx: ${tx.transactionHash}`);
} catch (err: any) {
console.log("Create Flow Error", err);
message.error(err.message);
}
};

return (
<>
<h2>Superfluid Web3 Plugin Example</h2>
<p>
This is an example of how to use the Superfluid Web3 Plugin to interact
with the Superfluid Protocol.
</p>
{/* add docs link here */}
<p>
<a href="https://github.com/Salmandabbakuti/web3-plugin-superfluid/tree/main?tab=readme-ov-file#superfluid-web3-plugin"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</a>
</p>
{account ? (
<>
<Button
type="primary"
onClick={() => {
setAccount("");
setCfav1Forwarder(null);
}}
>
{account.slice(0, 6)}...{account.slice(-4)}
</Button>
<Tabs
animated
defaultActiveKey="info"
items={[
{
key: "info",
label: "Flow Info",
children: (
<Form
labelCol={{ span: 10 }}
layout="vertical"
onValuesChange={handleGetFlowInfoInputChange}
onFinish={getFlowInfo}
>
<Form.Item name="token" label="Token">
<Input placeholder="Enter token address" required autoFocus />
</Form.Item>
<Form.Item name="sender" label="Sender">
<Input placeholder="Enter sender address" required />
</Form.Item>
<Form.Item name="receiver" label="Receiver">
<Input placeholder="Enter receiver address" required />
</Form.Item>
<Button type="primary" htmlType="submit">
Get Flow Info
</Button>

{flowInfo && (
<div
style={{
marginTop: 20,
backgroundColor: "#eee",
padding: 10,
textAlign: "center"
}}
>
<p>Flow Rate: {Number(flowInfo.flowRate)} wei/sec</p>
<p>
Flow Updated At:{" "}
{new Date(
Number(flowInfo.timestamp) * 1000
).toLocaleString()}
</p>
</div>
)}
</Form>
)
},
{
key: "create",
label: "Create Flow",
children: (
<Form
labelCol={{ span: 10 }}
layout="vertical"
onFinish={handleCreateFlow}
onValuesChange={handleCreateFlowInputChange}
>
<Form.Item name="token" label="Token">
<Input placeholder="Enter token address" autoFocus required />
</Form.Item>
<Form.Item name="sender" label="Sender">
<Input placeholder="Enter sender address" required />
</Form.Item>
<Form.Item name="receiver" label="Receiver">
<Input placeholder="Enter receiver address" required />
</Form.Item>
<Form.Item name="flowRate" label="Flow Rate">
<Input placeholder="Enter flowrate in wei/sec" required />
</Form.Item>
<Button type="primary" htmlType="submit">
Create Flow
</Button>
</Form>
)
}
]}
/>
</>
) : (
<Button type="primary" onClick={handleConnectWallet}>
Connect Wallet
</Button>
)}
</>
);
}

export default App;
1 change: 1 addition & 0 deletions example/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4f90cbb

Please sign in to comment.