Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update the javascript sdk for zksync era documentation #52

Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
3125b78
feat: added dropdown to the pages
Ngozi-Txfusion Jun 18, 2024
c3ea76e
Revert "feat: added dropdown to the pages"
Ngozi-Txfusion Jun 19, 2024
8787a7f
chore: update the providers, troubleshooting
Ngozi-Txfusion Jun 20, 2024
1bdc222
Merge branch 'main' into update-the-javascript-sdk-for-zksync-era-doc…
Ngozi-Txfusion Jun 20, 2024
1104e4f
chore: update
Ngozi-Txfusion Jun 20, 2024
53d4e2a
chore: minor update
Ngozi-Txfusion Jun 20, 2024
667c972
chore: minor update
Ngozi-Txfusion Jun 20, 2024
a675a6d
chore: minor
Ngozi-Txfusion Jun 20, 2024
b3c576a
chore: minor update
Ngozi-Txfusion Jun 20, 2024
145f006
chore: minor update
Ngozi-Txfusion Jun 20, 2024
177f03d
chore: minor update
Ngozi-Txfusion Jun 20, 2024
ef54dfb
chore: minor update
Ngozi-Txfusion Jun 20, 2024
e3ec265
chore: minor update
Ngozi-Txfusion Jun 20, 2024
44b4ec4
chore: minor update
Ngozi-Txfusion Jun 20, 2024
c90e928
chore: minor update
Ngozi-Txfusion Jun 20, 2024
cb66c3c
chore: minor update
Ngozi-Txfusion Jun 20, 2024
9cb43a7
chore: minor update
Ngozi-Txfusion Jun 20, 2024
b00cefa
chore: minor changes
Ngozi-Txfusion Jun 20, 2024
0181d50
chore: minor update
Ngozi-Txfusion Jun 20, 2024
f76cd76
chore: minor update
Ngozi-Txfusion Jun 21, 2024
0e5d856
chore: minor update
Ngozi-Txfusion Jun 21, 2024
fbc4f23
chore: minor update
Ngozi-Txfusion Jun 21, 2024
1d4ff53
chore: minor update
Ngozi-Txfusion Jun 21, 2024
f9c01a2
chore: minor update
Ngozi-Txfusion Jun 21, 2024
5df389f
chore: minor update
Ngozi-Txfusion Jun 21, 2024
09e48fb
chore: minor update
Ngozi-Txfusion Jun 21, 2024
c26e3fe
chore: minor updates
Ngozi-Txfusion Jun 21, 2024
2c17333
chore: minor update
Ngozi-Txfusion Jun 21, 2024
78f8918
chore: minor update
Ngozi-Txfusion Jun 21, 2024
c98bb66
chore: minor update
Ngozi-Txfusion Jun 21, 2024
a556796
chore: minor update
Ngozi-Txfusion Jun 21, 2024
b3c9a83
chore: minor update
Ngozi-Txfusion Jun 21, 2024
abab488
chore: minor changes
Ngozi-Txfusion Jun 21, 2024
32641ab
chore: minor update
Ngozi-Txfusion Jun 21, 2024
3024dc7
Update content/sdk/10.js/00.ethers/00.guides/01.guides.md
Ngozi-Txfusion Jun 24, 2024
05d8f33
Update content/sdk/10.js/00.ethers/00.guides/01.guides.md
Ngozi-Txfusion Jun 24, 2024
e9eefd0
Update content/sdk/10.js/00.ethers/00.guides/01.guides.md
Ngozi-Txfusion Jun 24, 2024
a6e07bf
Update content/sdk/10.js/00.ethers/00.guides/01.guides.md
Ngozi-Txfusion Jun 24, 2024
cf93fb6
Update content/sdk/10.js/00.ethers/20.v6/11.migration.md
Ngozi-Txfusion Jun 24, 2024
709b5ff
Update content/sdk/10.js/00.ethers/00.guides/01.guides.md
Ngozi-Txfusion Jun 24, 2024
e29bc94
Update content/sdk/10.js/00.ethers/00.guides/01.guides.md
Ngozi-Txfusion Jun 24, 2024
64966d7
Update content/sdk/10.js/00.ethers/10.v5/00.getting-started.md
Ngozi-Txfusion Jun 24, 2024
ba67b85
Update content/sdk/10.js/00.ethers/10.v5/00.getting-started.md
Ngozi-Txfusion Jun 24, 2024
4ddbec0
Update content/sdk/10.js/00.ethers/10.v5/01.features.md
Ngozi-Txfusion Jun 24, 2024
e143a2c
Update content/sdk/10.js/00.ethers/10.v5/03.providers.md
Ngozi-Txfusion Jun 24, 2024
3e487ab
Update content/sdk/10.js/00.ethers/10.v5/03.providers.md
Ngozi-Txfusion Jun 24, 2024
97b98aa
Update content/sdk/10.js/00.ethers/10.v5/01.features.md
Ngozi-Txfusion Jun 24, 2024
70bed7b
Update content/sdk/10.js/00.ethers/10.v5/01.features.md
Ngozi-Txfusion Jun 24, 2024
1cb2515
Update content/sdk/10.js/00.ethers/10.v5/01.features.md
Ngozi-Txfusion Jun 24, 2024
7e0b643
Update content/sdk/10.js/00.ethers/10.v5/01.features.md
Ngozi-Txfusion Jun 24, 2024
a049f89
chore: minor update
Ngozi-Txfusion Jun 24, 2024
c0c30e7
chore: minor update
Ngozi-Txfusion Jun 24, 2024
744e08d
chore: minor update
Ngozi-Txfusion Jun 24, 2024
ec60c12
chore: minor update
Ngozi-Txfusion Jun 24, 2024
d813af6
chore: minor update
Ngozi-Txfusion Jun 24, 2024
632df24
chore: minor update
Ngozi-Txfusion Jun 24, 2024
c830128
chore: minor change
Ngozi-Txfusion Jun 24, 2024
0411077
chore: minor update
Ngozi-Txfusion Jun 25, 2024
ad5200d
chore: minor update
Ngozi-Txfusion Jun 25, 2024
361d18b
chore: minor changes
Ngozi-Txfusion Jun 25, 2024
e9fcc13
chore: minor update
Ngozi-Txfusion Jun 25, 2024
4aac757
chore: minor changes
Ngozi-Txfusion Jun 25, 2024
9127703
chore: edit types page
Ngozi-Txfusion Jun 25, 2024
b7c9c18
chore: minor update
Ngozi-Txfusion Jun 25, 2024
c41cf56
chore: minor update
Ngozi-Txfusion Jun 25, 2024
f55a436
chore: added the installation
Ngozi-Txfusion Jun 25, 2024
7349610
chore: wrong spelling
Ngozi-Txfusion Jun 25, 2024
ed47697
chore: minor update
Ngozi-Txfusion Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/sdk/00.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ can use ZKsync Era's scalable, secure, and low-cost transactions.
---
title: JavaScript
icon: i-heroicons-code-bracket
to: /sdk/js/ethers
to: /sdk/js/ethers/guides/overview
---
Integrate JS with ZKsync Era for secure transactions.
::
Expand Down
106 changes: 106 additions & 0 deletions content/sdk/10.js/00.ethers/00.guides/01.guides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: Quickstart
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved
description: Getting Started with ZKsync JavaScript SDK
tags: ["zksync", "javascript", "sdk", "tutorial", "guide"]
---

This tutorial will help you get started with using the ZKsync JavaScript SDK for various operations like wallet management,
transactions, and error handling.

## Prerequisites

Before begin, make sure you have the following prerequisites:

- Node.js or Yarn installed on your system
- An Ethereum wallet like MetaMask
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved

## Installation

First, let's install the ZKsync SDK. Open your terminal and run the following command:

::code-group

```bash [yarn]
yarn add zksync ethers
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved
```

```bash [npm]
npm install zksync ethers
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved
```

::

## Example

Create a new file named `quickstart.js` in your project directory and add the following code:

```javascript
const { Provider, Wallet } = require('zksync-ethers');
const { ethers } = require('ethers');

// Connect to the ZKsync network
const provider = new Provider('https://sepolia.era.zksync.dev');
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved

// Replace with your private key
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved
const PRIVATE_KEY = 'your-private-key';
const wallet = new Wallet(PRIVATE_KEY, provider);

const run = async () => {
// Check wallet address
console.log(`Wallet address: ${wallet.address}`);

// Check balance
const balance = await wallet.getBalance();
console.log(`Balance: ${ethers.formatEther(balance)} ETH`);

// Estimate gas
const gasEstimate = await wallet.estimateGas({
to: 'recipient-address', // Replace with actual recipient address
value: ethers.parseEther('0.01'),
});
console.log(`Estimated gas: ${gasEstimate.toString()}`);

// Send transaction
try {
const tx = await wallet.sendTransaction({
to: 'recipient-address', // Replace with actual recipient address
value: ethers.parseEther('0.01'),
});
console.log(`Transaction hash: ${tx.hash}`);
await tx.wait();
console.log('Transaction confirmed');
} catch (error) {
console.error('Transaction failed:', error);
}
};

run();
```

## Update Placeholders

Replace the placeholders in the script:

- `your-private-key`: 'Your actual private key.
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved
- `recipient-address`: The actual recipient address.

## Run the Script

Execute the script using Node.js:

```bash
node quickstart.js
```

## Expected Output
When you run the script, you should see output similar to the following:

```sh
Wallet address: 0xB3ae03260388FfDD09A2eec00161709d36FddF50
Balance: 1.009773569 ETH
Estimated gas: 261930
Transaction hash: 0xba0713f5e73ac5c787bf194d44c0ab8b380d4a59aa9552dac4dbb1d21cfd5087
Transaction confirmed
```

You have successfully set up and interacted with the ZKsync network using the JavaScript SDK!
123 changes: 123 additions & 0 deletions content/sdk/10.js/00.ethers/10.v5/00.getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,126 @@ ensuring smoother and more efficient workflows.
This version is now obsolete. For continued support and access to the latest features and improvements, please migrate to
[version 6](/sdk/js/ethers/v6/getting-started) for better performance and new features.
::

However, ZKsync still supports Ethers v5, and this is how to get started:

## Installation

Install the `zksync-ethers` library:

```bash
npm install zksync-ethers ethers
```

## Initialization

To initialize the provider to connect to the ZKsync Sepolia network:
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved

```typescript
import { Provider, types } from "zksync-ethers";

const provider = Provider.getDefaultProvider(types.Network.Sepolia);
```

## Network information

To fetch details about the connected network and the current block number:

```typescript
(async () => {
const network = await provider.getNetwork();
const blockNumber = await provider.getBlockNumber();
console.log(`Network: ${JSON.stringify(network)}`);
console.log(`Block number: ${blockNumber}`);
})();
```

## Transaction details

To fetch transaction details:

```typescript
(async () => {
const transactionHash = "0xaa065e5a57e1f8470a6f258e2b6eee87c547eab066b8620ce7f3fd51405665e1";
const result = await provider.getTransactionDetails(transactionHash);
console.log(`Transaction Details: ${JSON.stringify(result)}`);
})();
```

## Error handling

Wrap the main logic in a try-catch block to handle potential errors:

```typescript
(async () => {
try {
// Your code here
} catch (error) {
console.error(`Error fetching transaction details from ZKsync: ${error}`);
}
})();
```
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved

## Example code

Here is an example script demonstrating the usage of the SDK:

```typescript
import { Provider, types } from "zksync-ethers";

async function main() {
const provider = Provider.getDefaultProvider(types.Network.Sepolia);

const network = await provider.getNetwork();
const blockNumber = await provider.getBlockNumber();
console.log(`Network: ${JSON.stringify(network)}`);
console.log(`Block number: ${blockNumber}`);

const transactionHash = "0xaa065e5a57e1f8470a6f258e2b6eee87c547eab066b8620ce7f3fd51405665e1";

try {
const result = await provider.getTransactionDetails(transactionHash);
if (result) {
console.log(`Transaction Details from ZKsync: ${JSON.stringify(result)}`);
console.log(`ethCommitTxHash: ${result.ethCommitTxHash}`);
console.log(`ethExecuteTxHash: ${result.ethExecuteTxHash}`);
console.log(`ethProveTxHash: ${result.ethProveTxHash}`);
} else {
console.log(`Transaction with hash ${transactionHash} not found in ZKsync.`);
}
} catch (error) {
console.error(`Error fetching transaction details from ZKsync: ${error}`);
}
}

main()
.then(() => console.log("Script executed successfully"))
.catch((error) => console.error(`Error executing script: ${error}`));
```

## Run the script

Use the following command to run the script:

```bash
npx ts-node <file-name>
```

## Output

Example output when the script runs successfully:

```sh
Network: {"name":"unknown","chainId":"300"}
Block number: 2725282
Transaction Details from ZKsync: {"ethCommitTxHash":"0xfe921b3af6bf14d35d6c550f1a337f20a46997a36c24bae37c1b2d129ee3b4d6","ethExecuteTxHash":"0x08e42763d6ba1052d117174acbb708d9e015ae9246574cf9d9b06c001b31e750","ethProveTxHash":"0x49ab3b1a7cc72911492afe39f21d5f557abcb2769aa63841f658c719a7ec5ba2","fee":"0x1252b3c112d2e","gasPerPubdata":"0xc350","initiatorAddress":"0xb71ce978bf48e3e4669a7a0acb89850023fc3279","isL1Originated":false,"receivedAt":"2024-06-03T09:16:41.519Z","status":"verified"}
ethCommitTxHash: 0xfe921b3af6bf14d35d6c550f1a337f20a46997a36c24bae37c1b2d129ee3b4d6
ethExecuteTxHash: 0x08e42763d6ba1052d117174acbb708d9e015ae9246574cf9d9b06c001b31e750
ethProveTxHash: 0x49ab3b1a7cc72911492afe39f21d5f557abcb2769aa63841f658c719a7ec5ba2
Script executed successfully
```

::callout{icon="i-heroicons-light-bulb"}
If you encounter any error while using the v5, kindly visit the
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved
[troubleshooting Guide](/sdk/troubleshooting#javascript-sdk) for more information.
Ngozi-Txfusion marked this conversation as resolved.
Show resolved Hide resolved
::
Loading
Loading