Skip to content

Commit

Permalink
add signer example
Browse files Browse the repository at this point in the history
  • Loading branch information
avkos committed Feb 21, 2024
1 parent 350ec0d commit 8cd8a51
Showing 1 changed file with 85 additions and 8 deletions.
93 changes: 85 additions & 8 deletions docs/docs/guides/wagmi_usage/wagmi.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ If you're using [Wagmi](https://wagmi.sh/react/getting-started#use-wagmi) and wa
import {Web3} from 'web3'
import {useMemo} from 'react'
import type {Chain, Client, Transport} from 'viem'
import {type Config, useClient} from 'wagmi'
import {type Config, useClient, useConnectorClient} from 'wagmi'

export function clientToWeb3js(client?: Client<Transport, Chain>) {
if (!client) {
Expand All @@ -23,8 +23,7 @@ export function clientToWeb3js(client?: Client<Transport, Chain>) {
if (transport.type === 'fallback') {
return new Web3(transport.transports[0].value.url)
}

return new Web3(transport.url)
return new Web3(transport)
}

/** Action to convert a viem Client to a web3.js Instance. */
Expand All @@ -33,16 +32,94 @@ export function useWeb3js({chainId}: { chainId?: number } = {}) {
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 example:
Get block data example:

```typescript
import {useWeb3js} from '../web3/useWeb3js'
import {mainnet} from 'wagmi/chains' // for example for mainnet
// somewhere in your React render method
const web3js = useWeb3js({chainId: mainnet.id})
// ...
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

```


Expand Down

1 comment on commit 8cd8a51

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 8cd8a51 Previous: 6c075db Ratio
processingTx 9510 ops/sec (±3.93%) 9301 ops/sec (±4.81%) 0.98
processingContractDeploy 39427 ops/sec (±7.67%) 39129 ops/sec (±7.62%) 0.99
processingContractMethodSend 18586 ops/sec (±6.94%) 19443 ops/sec (±5.19%) 1.05
processingContractMethodCall 39669 ops/sec (±5.60%) 38971 ops/sec (±6.34%) 0.98
abiEncode 46139 ops/sec (±7.04%) 44252 ops/sec (±6.92%) 0.96
abiDecode 30910 ops/sec (±7.10%) 30419 ops/sec (±8.89%) 0.98
sign 1694 ops/sec (±0.60%) 1656 ops/sec (±4.08%) 0.98
verify 381 ops/sec (±0.39%) 373 ops/sec (±0.78%) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.