-
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.
Merge branch '4.x' into junaid/6201multiprovider
- Loading branch information
Showing
4 changed files
with
166 additions
and
2 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,28 @@ | ||
--- | ||
sidebar_position: 16 | ||
sidebar_label: '📚 Resources' | ||
--- | ||
# Resources | ||
|
||
## [Web3.js v4 course](https://www.youtube.com/watch?v=3ZO_t-Kyr1g&list=PLPn3rQCo3XrP4LbQcOyyHQR8McV7w3HZT) | ||
|
||
This comprehensive 14-part video course from ChainSafe equips you with the skills to conquer the blockchain using web3.js v4. Unlock the potential of web3.js v4 and build cutting-edge dApps. This course caters to all skill levels. | ||
|
||
[![Web3.js v4 course](https://img.youtube.com/vi/3ZO_t-Kyr1g/0.jpg)](https://www.youtube.com/watch?v=3ZO_t-Kyr1g&list=PLPn3rQCo3XrP4LbQcOyyHQR8McV7w3HZT) | ||
|
||
|
||
## [Web3.js series](https://www.youtube.com/watch?v=BQ_bDH91S4k&list=PLPn3rQCo3XrNf__8irs4-MjMt4fJqW2I_) | ||
|
||
This series of 3 videos takes you on a journey through web3.js. Whether you're a complete beginner or want to refine your skills, these videos have something for you: | ||
|
||
1. Getting Started: Kick off your web3 adventure by learning the ropes of web3.js. Master the basics, from installation to making your first call to the blockchain. | ||
|
||
2. Essential Tools: Unleash the power of web3.js utilities! From generating random bytes to hashing and checksumming addresses, you'll gain mastery of essential tools for Ethereum development. | ||
|
||
3. Sending Transactions: Dive deep into wallets and accounts. Learn how to sign and send transactions to the network, empowering you to interact with the blockchain directly. | ||
|
||
[![Web3.js series](https://img.youtube.com/vi/BQ_bDH91S4k/0.jpg)](https://www.youtube.com/watch?v=BQ_bDH91S4k&list=PLPn3rQCo3XrNf__8irs4-MjMt4fJqW2I_) | ||
|
||
## Hackathons | ||
|
||
You'll find the latest hackathons opportunities by following [web3js](https://twitter.com/web3_js) on X. |
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 | ||
::: |
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
c7960e1
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
9495
ops/sec (±4.04%
)9301
ops/sec (±4.81%
)0.98
processingContractDeploy
40906
ops/sec (±6.52%
)39129
ops/sec (±7.62%
)0.96
processingContractMethodSend
19275
ops/sec (±7.99%
)19443
ops/sec (±5.19%
)1.01
processingContractMethodCall
39033
ops/sec (±5.93%
)38971
ops/sec (±6.34%
)1.00
abiEncode
45635
ops/sec (±6.90%
)44252
ops/sec (±6.92%
)0.97
abiDecode
31170
ops/sec (±7.96%
)30419
ops/sec (±8.89%
)0.98
sign
1660
ops/sec (±3.29%
)1656
ops/sec (±4.08%
)1.00
verify
382
ops/sec (±0.41%
)373
ops/sec (±0.78%
)0.98
This comment was automatically generated by workflow using github-action-benchmark.