-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web3.js Adapter for Wagmi: Documenation (#6813)
* add wagmi documentation * add link to repo * add signer example * fixes
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
label: '🔄 Wagmi usage' | ||
collapsible: true | ||
collapsed: true | ||
link: null | ||
position: 11 |
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,131 @@ | ||
--- | ||
sidebar_position: 1 | ||
sidebar_label: 'Wagmi Web3js Adapter' | ||
title: 'Wagmi Web3js Adapter' | ||
--- | ||
|
||
|
||
### Reference Implementation | ||
If you're using [Wagmi](https://wagmi.sh/react/getting-started#use-wagmi) and want to add web3.js, use this code in your project. This snippet will help you to convert a `Viem` client to a `web3.js` instance for signing transactions and interacting with the blockchain: | ||
|
||
|
||
```typescript | ||
import {Web3} from 'web3' | ||
import {useMemo} from 'react' | ||
import type {Chain, Client, Transport} from 'viem' | ||
import {type Config, useClient, useConnectorClient} from 'wagmi' | ||
|
||
export function clientToWeb3js(client?: Client<Transport, Chain>) { | ||
if (!client) { | ||
return new Web3() | ||
} | ||
|
||
const {transport} = client | ||
|
||
if (transport.type === 'fallback') { | ||
return new Web3(transport.transports[0].value.url) | ||
} | ||
return new Web3(transport) | ||
} | ||
|
||
/** Action to convert a viem Client to a web3.js Instance. */ | ||
export function useWeb3js({chainId}: { chainId?: number } = {}) { | ||
const client = useClient<Config>({chainId}) | ||
return useMemo(() => clientToWeb3js(client), [client]) | ||
} | ||
|
||
/** Action to convert a viem ConnectorClient to a web3.js Instance. */ | ||
export function useWeb3jsSigner({chainId}: { chainId?: number } = {}) { | ||
const {data: client} = useConnectorClient<Config>({chainId}) | ||
return useMemo(() => clientToWeb3js(client), [client]) | ||
} | ||
``` | ||
|
||
### Usage examples | ||
Get block data example: | ||
|
||
```typescript | ||
import {useWeb3js} from '../web3/useWeb3js' | ||
import {mainnet} from 'wagmi/chains' | ||
import {useEffect, useState} from "react"; | ||
|
||
type Block = { | ||
hash: string | ||
extraData: string | ||
miner: string | ||
|
||
} | ||
|
||
function Block() { | ||
const web3js = useWeb3js({chainId: mainnet.id}) | ||
const [block, setBlock] = useState<Block>() | ||
|
||
useEffect(() => { | ||
web3js.eth.getBlock(19235006).then((b) => { | ||
setBlock(b as Block) | ||
}).catch(console.error) | ||
}, [setBlock]); | ||
|
||
|
||
if (!block) return (<div>Loading...</div>) | ||
|
||
return ( | ||
<> | ||
<div id='hash'>{block.hash}</div> | ||
<div id='extraData'>{block.extraData}</div> | ||
<div id='miner'>{block.miner}</div> | ||
|
||
</> | ||
) | ||
} | ||
|
||
export default Block | ||
|
||
``` | ||
|
||
Send transaction example: | ||
|
||
```typescript | ||
import {mainnet} from 'wagmi/chains' | ||
import {useAccount, useConnect} from "wagmi"; | ||
import {useWeb3jsSigner} from "../web3/useWeb3js"; | ||
import {useEffect} from "react"; | ||
|
||
function SendTransaction() { | ||
const account = useAccount() | ||
const {connectors, connect,} = useConnect() | ||
const web3js = useWeb3jsSigner({chainId: mainnet.id}) | ||
|
||
useEffect(() => { | ||
if (account && account.address) { | ||
web3js.eth.sendTransaction({ | ||
from: account.address, | ||
to: '0x', // some address | ||
value: '0x1' // set your value | ||
}).then(console.log).catch(console.error) | ||
} | ||
}, [account]) | ||
|
||
return ( | ||
<> | ||
{connectors.map((connector) => ( | ||
<button | ||
key={connector.uid} | ||
onClick={() => connect({connector})} | ||
type="button" | ||
> | ||
{connector.name} | ||
</button> | ||
))} | ||
</> | ||
) | ||
} | ||
|
||
export default SendTransaction | ||
|
||
``` | ||
|
||
|
||
:::tip | ||
[This repository](https://github.com/avkos/wagmi-web3js-example-app) contains an example Wagmi app that demonstrates how to interact with the Ethereum blockchain using the web3.js library | ||
::: |
cf4b93f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
processingTx
9648
ops/sec (±3.77%
)9301
ops/sec (±4.81%
)0.96
processingContractDeploy
39408
ops/sec (±7.81%
)39129
ops/sec (±7.62%
)0.99
processingContractMethodSend
20200
ops/sec (±4.62%
)19443
ops/sec (±5.19%
)0.96
processingContractMethodCall
39492
ops/sec (±6.11%
)38971
ops/sec (±6.34%
)0.99
abiEncode
43522
ops/sec (±8.95%
)44252
ops/sec (±6.92%
)1.02
abiDecode
31553
ops/sec (±7.34%
)30419
ops/sec (±8.89%
)0.96
sign
1549
ops/sec (±1.09%
)1656
ops/sec (±4.08%
)1.07
verify
350
ops/sec (±1.05%
)373
ops/sec (±0.78%
)1.07
This comment was automatically generated by workflow using github-action-benchmark.