From 3ac184a69f6a3223880a1cd6d69befdfbc75d245 Mon Sep 17 00:00:00 2001 From: Dan Forbes Date: Thu, 29 Aug 2024 08:16:09 -0400 Subject: [PATCH] fix(web3js): fix RPC URL and link --- content/tutorials/guide-web3js/10.index.md | 29 +++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/content/tutorials/guide-web3js/10.index.md b/content/tutorials/guide-web3js/10.index.md index 03e1b867..c5325925 100644 --- a/content/tutorials/guide-web3js/10.index.md +++ b/content/tutorials/guide-web3js/10.index.md @@ -53,7 +53,7 @@ cd my-project ### Initialization -Before using the ZKsync plugin for Web3.js, you need to [initialize Web3 with a provider](https://docs.web3js.org/#initialize-web3-with-a-provider) +Before using the ZKsync plugin for Web3.js, you need to [initialize Web3 with a provider](https://docs.web3js.org/guides/getting_started/quickstart#initialize-web3-with-a-provider) and [register the plugin](https://docs.web3js.org/guides/web3_plugin_guide/plugin_users#registering-the-plugin). #### Example @@ -62,10 +62,10 @@ and [register the plugin](https://docs.web3js.org/guides/web3_plugin_guide/plugi import { Web3 } from "web3"; import { ZKsyncPlugin } from "web3-plugin-zksync"; -const zksyncRpcUrl: string = "https://sepolia.era.zksync.dev"; +const web3: Web3 = new Web3("https://rpc.sepolia.org"); +const zksyncRpcUrl: string = "https://sepolia.era.zksync.dev"; console.log(`📞 Connecting to ZKsync Era [${zksyncRpcUrl}]`); -const web3: Web3 = new Web3(zksyncRpcUrl); web3.registerPlugin(new ZKsyncPlugin(zksyncRpcUrl)); ``` @@ -75,13 +75,16 @@ This examples uses the %%zk_testnet_name%%. ### Ethereum JSON-RPC API -Use the Web3.js `eth` package to fetch data from the ZKsync [Ethereum JSON-RPC API](https://docs.zksync.io/build/api-reference/ethereum-rpc). +ZKsync Era implements the [Ethereum JSON-RPC API](https://docs.zksync.io/build/api-reference/ethereum-rpc), +so you can use the Web3.js `eth` package to fetch data from the Layer 1 _and_ Layer 2 networks. #### Fetch the Latest Block Number ```javascript -const blockNumber = await web3.eth.getBlockNumber(); -console.log(`Current block number: ${blockNumber}`); +const l1BlockNumber = await web3.eth.getBlockNumber(); +console.log(`Current L1 block number: ${l1BlockNumber}`); +const l2BlockNumber = await web3.ZKsync.L2.eth.getBlockNumber(); +console.log(`Current L2 block number: ${l2BlockNumber}`); ``` ### ZKsync L2-Specific JSON-RPC API @@ -104,18 +107,20 @@ console.log(`Main contract: ${mainContract}`); import { Web3 } from "web3"; import { ZKsyncPlugin } from "web3-plugin-zksync"; -const zksyncRpcUrl: string = "https://sepolia.era.zksync.dev"; +const web3: Web3 = new Web3("https://rpc.sepolia.org"); +const zksyncRpcUrl: string = "https://sepolia.era.zksync.dev"; console.log(`📞 Connecting to ZKsync Era [${zksyncRpcUrl}]`); -const web3: Web3 = new Web3(zksyncRpcUrl); web3.registerPlugin(new ZKsyncPlugin(zksyncRpcUrl)); async function main() { -const blockNumber = await web3.eth.getBlockNumber(); -console.log(`Current block number: ${blockNumber}`); + const l1BlockNumber = await web3.eth.getBlockNumber(); + console.log(`Current L1 block number: ${l1BlockNumber}`); + const l2BlockNumber = await web3.ZKsync.L2.eth.getBlockNumber(); + console.log(`Current L2 block number: ${l2BlockNumber}`); -const mainContract = await web3.ZKsync.rpc.getMainContract(); -console.log(`Main contract: ${mainContract}`); + const mainContract = await web3.ZKsync.rpc.getMainContract(); + console.log(`Main contract: ${mainContract}`); } main().catch(console.error);