-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding plugin example react app (#5)
* added: example app * updated: example and package deps
- Loading branch information
1 parent
cdd06c7
commit 4f90cbb
Showing
21 changed files
with
477 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ dist/ | |
yarn.lock | ||
npm-debug.log | ||
package-lock.json | ||
.DS_Store | ||
|
||
# Executables | ||
*.swf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.github/ | ||
src/ | ||
test/ | ||
example/ | ||
|
||
# Executables | ||
*.swf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#root { | ||
max-width: 1280px; | ||
margin: 0 auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.