generated from web3/web3.js-plugin-template
-
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.
- Loading branch information
Showing
20 changed files
with
319 additions
and
263 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
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 |
---|---|---|
|
@@ -21,10 +21,11 @@ | |
], | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
"lint": "cd packages/web3-plugin-wallet-rpc && yarn lint", | ||
"build": "cd packages/web3-plugin-wallet-rpc && yarn build", | ||
"build:docs": "cd packages/web3-plugin-wallet-rpc && yarn build:docs", | ||
"test": "cd packages/web3-plugin-wallet-rpc && yarn test", | ||
"lint": "yarn lint:plugin && yarn lint:example", | ||
"lint:plugin": "cd packages/web3-plugin-wallet-rpc && yarn lint", | ||
"lint:example": "cd packages/example-react-app && yarn lint", | ||
"start:example": "cd packages/example-react-app && yarn start" | ||
} | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<!DOCTYPE html> | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
|
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
packages/example-react-app/src/components/AccountDetail.tsx
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,51 @@ | ||
import { | ||
type MutableRefObject, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
|
||
import { type IWeb3Context, Web3Context } from "../web3/Web3Context"; | ||
|
||
export function AccountDetail({ address }: { address: string }) { | ||
const web3Context: IWeb3Context = useContext(Web3Context); | ||
|
||
const [balance, setBalance] = useState<number>(NaN); | ||
const subscriptionId: MutableRefObject<string | undefined> = | ||
useRef(undefined); | ||
|
||
async function updateBalance(): Promise<void> { | ||
const newBalance = await web3Context.web3.eth.getBalance(address); | ||
|
||
setBalance(parseFloat(web3Context.web3.utils.fromWei(newBalance, "ether"))); | ||
} | ||
|
||
useEffect(() => { | ||
async function subscribeToNewBlockHeaders() { | ||
const newBlockSubscription = | ||
await web3Context.web3.eth.subscribe("newBlockHeaders"); | ||
|
||
subscriptionId.current = newBlockSubscription.id; | ||
|
||
newBlockSubscription.on("data", () => { | ||
void updateBalance(); | ||
}); | ||
} | ||
|
||
void subscribeToNewBlockHeaders(); | ||
|
||
return () => { | ||
void web3Context.web3.eth.subscriptionManager.unsubscribe( | ||
({ id }) => subscriptionId.current === id, | ||
); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<div>{address}</div> | ||
<div>Balance in native token: {`${balance}`}</div> | ||
</> | ||
); | ||
} |
Oops, something went wrong.