From 7ba8b09dbb00464faad21d825d3f72ce6ac9f44d Mon Sep 17 00:00:00 2001 From: Salman Dabbakuti Date: Wed, 3 Jan 2024 10:05:45 +0530 Subject: [PATCH] added: example app --- .github/workflows/e2e.yml | 2 + .gitignore | 1 + .npmignore | 1 + README.md | 5 + example/LICENSE | 21 ++++ example/README.md | 19 +++ example/index.html | 16 +++ example/package.json | 32 +++++ example/public/vite.svg | 1 + example/src/App.css | 4 + example/src/App.tsx | 235 +++++++++++++++++++++++++++++++++++ example/src/assets/react.svg | 1 + example/src/index.css | 73 +++++++++++ example/src/main.tsx | 10 ++ example/tsconfig.json | 25 ++++ example/tsconfig.node.json | 10 ++ example/vite-env.d.ts | 1 + example/vite.config.ts | 8 ++ package.json | 2 +- src/superfluid-plugin.ts | 5 +- tsconfig.json | 4 +- 21 files changed, 470 insertions(+), 6 deletions(-) create mode 100644 example/LICENSE create mode 100644 example/README.md create mode 100644 example/index.html create mode 100644 example/package.json create mode 100644 example/public/vite.svg create mode 100644 example/src/App.css create mode 100644 example/src/App.tsx create mode 100644 example/src/assets/react.svg create mode 100644 example/src/index.css create mode 100644 example/src/main.tsx create mode 100644 example/tsconfig.json create mode 100644 example/tsconfig.node.json create mode 100644 example/vite-env.d.ts create mode 100644 example/vite.config.ts diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 0653f9b..2abc6d6 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -9,11 +9,13 @@ on: paths-ignore: - "README.md" - "LICENSE" + - "example/**" pull_request: branches: [main] paths-ignore: - "README.md" - "LICENSE" + - "example/**" jobs: e2e-test: diff --git a/.gitignore b/.gitignore index f224b9a..895a9c5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ dist/ yarn.lock npm-debug.log package-lock.json +.DS_Store # Executables *.swf diff --git a/.npmignore b/.npmignore index d539512..3382348 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,7 @@ .github/ src/ test/ +example/ # Executables *.swf diff --git a/README.md b/README.md index 5cc0372..0988336 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,11 @@ npm publish ## Change Log +#### 0.2.7 + +- Added example to show how to use the plugin in a react app +- Few tsdoc improvements + #### 0.2.6 - Added handy `contractAddresses` method to get contract addresses of the network diff --git a/example/LICENSE b/example/LICENSE new file mode 100644 index 0000000..3771861 --- /dev/null +++ b/example/LICENSE @@ -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. diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..2ca1e90 --- /dev/null +++ b/example/README.md @@ -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 diff --git a/example/index.html b/example/index.html new file mode 100644 index 0000000..659d8fe --- /dev/null +++ b/example/index.html @@ -0,0 +1,16 @@ + + + + + + + + Superfluid Web3Plugin Example React + + + +
+ + + + \ No newline at end of file diff --git a/example/package.json b/example/package.json new file mode 100644 index 0000000..761f293 --- /dev/null +++ b/example/package.json @@ -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.12.6", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "web3": "^4.3.0", + "web3-plugin-superfluid": "^0.2.6" + }, + "devDependencies": { + "@types/react": "^18.2.43", + "@types/react-dom": "^18.2.17", + "@typescript-eslint/eslint-plugin": "^6.14.0", + "@typescript-eslint/parser": "^6.14.0", + "@vitejs/plugin-react-swc": "^3.5.0", + "eslint": "^8.55.0", + "eslint-plugin-react-hooks": "^4.6.0", + "eslint-plugin-react-refresh": "^0.4.5", + "typescript": "^5.2.2", + "vite": "^5.0.8", + "vite-plugin-node-polyfills": "^0.19.0" + } +} diff --git a/example/public/vite.svg b/example/public/vite.svg new file mode 100644 index 0000000..e7b8dfb --- /dev/null +++ b/example/public/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/example/src/App.css b/example/src/App.css new file mode 100644 index 0000000..0167bd3 --- /dev/null +++ b/example/src/App.css @@ -0,0 +1,4 @@ +#root { + max-width: 1280px; + margin: 0 auto; +} \ No newline at end of file diff --git a/example/src/App.tsx b/example/src/App.tsx new file mode 100644 index 0000000..5f10a4f --- /dev/null +++ b/example/src/App.tsx @@ -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(""); + const [cfav1Forwarder, setCfav1Forwarder] = useState( + null + ); + const [cfav1, setCfav1] = useState(null); + const [flowInfo, setFlowInfo] = useState(null); + const [createFlowInput, setCreateFlowInput] = useState({ + token: "", + sender: "", + receiver: "", + flowRate: "" + }); + const [getFlowInfoInput, setGetFlowInfoInput] = useState({ + 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 ( + <> +

Superfluid Web3 Plugin Example

+

+ This is an example of how to use the Superfluid Web3 Plugin to interact + with the Superfluid Protocol. +

+ {/* add docs link here */} +

+ + Documentation + +

+ {account ? ( + <> + + + + + + + + + + + + + + {flowInfo && ( +
+

Flow Rate: {Number(flowInfo.flowRate)} wei/sec

+

+ Flow Updated At:{" "} + {new Date( + Number(flowInfo.timestamp) * 1000 + ).toLocaleString()} +

+
+ )} + + ) + }, + { + key: "create", + label: "Create Flow", + children: ( +
+ + + + + + + + + + + + + +
+ ) + } + ]} + /> + + ) : ( + + )} + + ); +} + +export default App; diff --git a/example/src/assets/react.svg b/example/src/assets/react.svg new file mode 100644 index 0000000..6c87de9 --- /dev/null +++ b/example/src/assets/react.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/example/src/index.css b/example/src/index.css new file mode 100644 index 0000000..adc0a52 --- /dev/null +++ b/example/src/index.css @@ -0,0 +1,73 @@ +:root { + font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; + + color-scheme: light dark; + color: #000; + background-color: #fff; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} + +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} + +button:hover { + border-color: #646cff; +} + +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + + a:hover { + color: #747bff; + } + + button { + background-color: #f9f9f9; + } +} \ No newline at end of file diff --git a/example/src/main.tsx b/example/src/main.tsx new file mode 100644 index 0000000..3d7150d --- /dev/null +++ b/example/src/main.tsx @@ -0,0 +1,10 @@ +import React from 'react' +import ReactDOM from 'react-dom/client' +import App from './App.tsx' +import './index.css' + +ReactDOM.createRoot(document.getElementById('root')!).render( + + + , +) diff --git a/example/tsconfig.json b/example/tsconfig.json new file mode 100644 index 0000000..a7fc6fb --- /dev/null +++ b/example/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "target": "ES2020", + "useDefineForClassFields": true, + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "module": "ESNext", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react-jsx", + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + }, + "include": ["src"], + "references": [{ "path": "./tsconfig.node.json" }] +} diff --git a/example/tsconfig.node.json b/example/tsconfig.node.json new file mode 100644 index 0000000..42872c5 --- /dev/null +++ b/example/tsconfig.node.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "composite": true, + "skipLibCheck": true, + "module": "ESNext", + "moduleResolution": "bundler", + "allowSyntheticDefaultImports": true + }, + "include": ["vite.config.ts"] +} diff --git a/example/vite-env.d.ts b/example/vite-env.d.ts new file mode 100644 index 0000000..11f02fe --- /dev/null +++ b/example/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/example/vite.config.ts b/example/vite.config.ts new file mode 100644 index 0000000..ad5a39e --- /dev/null +++ b/example/vite.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from "vite"; +import react from "@vitejs/plugin-react-swc"; +import { nodePolyfills } from "vite-plugin-node-polyfills"; + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [react(), nodePolyfills()] +}); diff --git a/package.json b/package.json index 469a3d4..47e8dcb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "web3-plugin-superfluid", - "version": "0.2.6", + "version": "0.2.7-beta.1", "description": "Superfluid Web3.js Plugin", "author": "Salman Dabbakuti", "license": "MIT", diff --git a/src/superfluid-plugin.ts b/src/superfluid-plugin.ts index 83baf89..27f3df7 100644 --- a/src/superfluid-plugin.ts +++ b/src/superfluid-plugin.ts @@ -9,13 +9,13 @@ export type CFAV1Forwarder = Contract; export type Host = Contract; export type CFAV1 = Contract; export type IDAV1 = Contract; -export type ProtocolContractAddresses = { +export interface ProtocolContractAddresses { cfaV1: string; cfaV1Forwarder: string; idaV1: string; host: string; [key: string]: any; -}; +} export class SuperfluidPlugin extends Web3PluginBase { public pluginNamespace = "superfluid"; @@ -112,7 +112,6 @@ export class SuperfluidPlugin extends Web3PluginBase { * const addresses = web3.superfluid.contractAddresses(80001); * ``` */ - public contractAddresses(chainId: number): ProtocolContractAddresses { const networkMetadata = sfMeta.networks.find( (network) => network.chainId === chainId diff --git a/tsconfig.json b/tsconfig.json index ca1516d..c7305a0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { // see https://www.typescriptlang.org/tsconfig to better understand tsconfigs "include": ["src"], - "exclude": ["./test/", "./node_modules/", "./dist/"], + "exclude": ["./test/", "./example", "./node_modules/", "./dist/"], "compilerOptions": { "target": "es2020", "module": "commonjs", @@ -9,7 +9,7 @@ // output .d.ts declaration files for consumers "declaration": true, // output .js.map sourcemap files for consumers - "sourceMap": true, + "sourceMap": false, // match output dir to input dir. e.g. dist/index instead of dist/src/index "rootDir": "src", "outDir": "dist",