Skip to content

Commit

Permalink
fix: add cli to web3js (#53)
Browse files Browse the repository at this point in the history
<!--

Thank you for contributing to the ZKsync Code Community!

Before submitting the PR, please make sure you do the following:

- Read the [Contributing
Guide](https://github.com/ZKsync-Community-Hub/community-code/blob/main/CONTRIBUTING.md).
- Understand our [Code of
Conduct](https://github.com/ZKsync-Community-Hub/community-code/blob/main/CODE_OF_CONDUCT.md)

-->

# Description

<!-- Please describe what are the changes and what they are solving for
in this PR. -->

## Linked Issues

<!-- If you have any issues this PR is related to, link them here. -->
<!--
Check out
https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
on how to automate linking a GitHub Issue to a PR.
-->

## Additional context
  • Loading branch information
jennyg0 authored Aug 29, 2024
1 parent 65d1407 commit 1275f01
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 25 deletions.
87 changes: 69 additions & 18 deletions content/tutorials/guide-web3js/10.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,48 @@ It serves as an essential tool for connecting and crafting applications within t
and can be extended to support other networks through its [plugin system](https://docs.web3js.org/guides/web3_plugin_guide/).

You can use the [ZKsync plugin](https://github.com/web3/web3-plugin-zksync) for Web3.js
to interact with the [ZKsync JSON-RPC API](https://docs.zksync.io/build/api.html) smart contracts deployed on ZKsync.
to interact with the [ZKsync JSON-RPC API](https://docs.zksync.io/build/api.html) smart contracts deployed
on ZKsync. Whether you are setting up a project from scratch or integrating Web3.js into an existing project,
this guide has you covered.

## Installation
## Installation with existing project

Start by adding Web3.js and the ZKsync plugin for Web3.js to your project.
For existing projects, start by adding Web3.js and the ZKsync plugin for Web3.js.
Open your terminal and execute the following command:

```bash
npm install --save web3 web3-plugin-zksync
npm install web3 web3-plugin-zksync
```

This command installs the latest version of Web3.js and the ZKsync plugin for Web3.js and adds them to your project's dependencies.

## Initial Setup
## Creating a project with ZKsync CLI

This section provides a quick start for developers who prefer using ZKsync CLI to bootstrap their Web3.js
project from scratch. It’s ideal for those looking to create scripts that interact with ZKsync contracts in a new project.

**Step 1:** Install ZKsync CLI globally

```bash
npm install -g @matterlabs/zksync-cli
```

**Step 2:** Create a new ZKsync project by running

```bash
zksync-cli create --template node_web3js my-project
```

And then choose your preferred package manager in the list. This command will generate a new project with all
necessary configurations for ZKsync and install all necessary dependencies.

**Step 3:** Navigate to the newly created project directory:

```bash
cd my-project
```

**Step 4:** Let's walk through the code to understand each part before running the script.

### Initialization

Expand Down Expand Up @@ -52,13 +80,8 @@ Use the Web3.js `eth` package to fetch data from the ZKsync [Ethereum JSON-RPC A
#### Fetch the Latest Block Number

```javascript

async function main() {
const blockNumber = await web3.eth.getBlockNumber();
console.log(`Current block number: ${blockNumber}`);
}

main().catch(console.error);
const blockNumber = await web3.eth.getBlockNumber();
console.log(`Current block number: ${blockNumber}`);
```

### ZKsync L2-Specific JSON-RPC API
Expand All @@ -70,15 +93,43 @@ from the `zks_` namespace of the [JSON-RPC API](https://docs.zksync.io/build/api

<!-- /*spellchecker: disable*/ -->
```javascript
const mainContract = await web3.ZKsync.rpc.getMainContract();
console.log(`Main contract: ${mainContract}`);
```
<!-- /*spellchecker: enable*/ -->

**Step 5:** Write your code in `src/main.ts` file, then run the script using `npm run start`.

```javascript
import { Web3 } from "web3";
import { ZKsyncPlugin } from "web3-plugin-zksync";

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 mainContract = await web3.ZKsync.rpc.getMainContract();
console.log(`Main contract: ${mainContract}`);
}
const blockNumber = await web3.eth.getBlockNumber();
console.log(`Current block number: ${blockNumber}`);

const mainContract = await web3.ZKsync.rpc.getMainContract();
console.log(`Main contract: ${mainContract}`);
}

main().catch(console.error);
```
<!-- /*spellchecker: enable*/ -->

### Wallet Configuration
## Recap
In this tutorial, you’ve learned how to set up a Web3.js project with ZKsync, both by integrating it into an
existing project and by starting from scratch using ZKsync CLI. You’ve also explored how to interact with
ZKsync specific JSON-RPC methods, such as retrieving the current block number and fetching the main contract
address.

## Learn More

Refer to the Web3.js documentation for [details regarding wallet configuration](https://docs.web3js.org/#setting-up-a-wallet).
- To further enhance your skills, explore the examples provided in the ZKsync CLI scripting template found under `src/examples`.
These examples demonstrate additional scripts you can run with Web3.js to interact with ZKsync.
- Refer to the [ZKsync Web3.js documentation](https://sdk.zksync.io/js/web3js) for more details and
code samples to continue building with the Web3.js Plugin.
18 changes: 11 additions & 7 deletions content/tutorials/guide-web3js/_dir.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title: Using web3.js to interact with ZKsync
title: Using Web3.js to interact with ZKsync
featured: false
authors:
- name: ChainSafe
Expand All @@ -7,13 +7,17 @@ authors:
github_repo: https://github.com/ChainSafe
tags:
- guide
- web3.js
summary: This page will guide you through the steps to use web3.js to interact with ZKsync.
description: This guide outlines how to use the ZKsync web3.js plugin to interact with ZKsync.
- Web3.js
summary:
This guide will teach you how to set up and use Web3.js to interact with ZKsync, leveraging the ZKsync Web3.js plugin.
description:
Learn how to use the ZKsync Web3.js plugin to interact with ZKsync. This guide covers setting up a new Web3.js project
with ZKsync CLI, integrating ZKsync into an existing project, and sending RPC requests to ZKsync.
what_you_will_learn:
- How to install web3.js and the ZKsync plugin
- How to setup a Web3.js project using ZKsync CLI
- How to install Web3.js and the ZKsync plugin in an existing project
- How to send RPC requests to ZKsync
- How to use ZKsync-specific methods
updated: 2024-05-09
tools:
- web3.js
- Web3.js
- ZKsync CLI

0 comments on commit 1275f01

Please sign in to comment.