Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
fix: update cli docs (#808)
Browse files Browse the repository at this point in the history
Co-authored-by: Dennis <[email protected]>
  • Loading branch information
JackHamer09 and idea404 authored Dec 5, 2023
1 parent f76b8ad commit 85d4271
Show file tree
Hide file tree
Showing 2 changed files with 300 additions and 24 deletions.
57 changes: 33 additions & 24 deletions docs/tools/zksync-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,47 @@ Ensure you have the following installed:

You can run commands without installation: `npx zksync-cli`. For example: `npx zksync-cli dev start`.

## Highlight: Start Developing Locally, Fast
### Local development commands

Use `npx zksync-cli dev` for an easy way to work with zkSync on your computer. It helps you set up and manage local zkSync and Ethereum nodes, Block Explorer, Wallet, and Bridge without hassle.

### Quick ‘dev’ Commands
- `npx zksync-cli dev start` - start local development environment (will ask to configure if starting for the first time)
- `npx zksync-cli dev clean` - clean data for configured modules
- `npx zksync-cli dev config` - select modules to run in local development environment

- `npx zksync-cli dev start`: Begin your local development environment (first-time users will be prompted to configure)
- `npx zksync-cli dev clean`: Clear data for configured modules
- `npx zksync-cli dev config`: Choose modules to run in local development environment
Run `npx zksync-cli dev` to see the full list of commands.

Run `npx zksync-cli help dev` for more commands and details.
### Create Project commands

### Other Essential Commands
- `npx zksync-cli create`: Create a project using updated templates. See [Quick Scaffolding with Templates](#quick-scaffolding-with-templates) for more information.

- **Basic Operations**:
- `npx zksync-cli create`: Creates a project in a specified folder. [More info](#quick-scaffolding-with-templates).
- `npx zksync-cli bridge deposit`: Moves funds from Ethereum (L1) to zkSync (L2).
- `npx zksync-cli bridge withdraw` and `npx zksync-cli bridge withdraw-finalize`: Manage funds withdrawal from zkSync (L2) to Ethereum (L1).
- **Help & Info**:
- `npx zksync-cli help`: General help.
- `npx zksync-cli help {command}`: Detailed command usage info.
- `npx zksync-cli --version`: CLI version info.
### Contract interaction commands

::: info Deposit and withdraw times
The easiest way to interact with zkSync contracts.

- Operations like deposit and withdraw can take several minutes.
- [Mainnet withdrawals have a 24h delay](../../reference/troubleshooting/withdrawal-delay.md).
:::
- `npx zksync-cli contract read`: run read-only contract methods
- `npx zksync-cli contract write`: send transactions to the contract

More commands and updates are coming! If you have suggestions, [open an issue on GitHub](https://github.com/matter-labs/zksync-cli/issues/new).
See full documentation and advanced examples [here](./examples/contract-interaction.md).

### Wallet commands

- `npx zksync-cli wallet transfer`: send ETH on L2 to another account
- `npx zksync-cli wallet balance`: displays ETH balance of the specified address

### Bridge commands

To simplify and streamline the templates section, you can consider the following edits:
- `npx zksync-cli bridge deposit`: deposits funds from Ethereum (L1) to zkSync (L2)
- `npx zksync-cli bridge withdraw`: withdraws funds from zkSync (L2) to Ethereum (L1)
- `npx zksync-cli bridge withdraw-finalize`: finalizes withdrawal of funds from zkSync (L2) to Ethereum (L1)

### Other commands

- `npx zksync-cli help`: Provides information about all supported commands
- `npx zksync-cli <command> --help`: Provides detailed information about how to use a specific command. Replace \<command\> with the name of the command you want help with (e.g., `create`, `dev config`, `bridge withdraw-finalize`)
- `npx zksync-cli --version`: Returns the current version

More commands and updates are coming! If you have suggestions, [open an issue on GitHub](https://github.com/matter-labs/zksync-cli/issues/new).

---

Expand All @@ -82,11 +91,11 @@ Jumpstart your projects with `npx zksync-cli create` using our updated templates
- **Features**: Includes examples for wallet and contract interactions using either viem or Ethers.
- **Resources**: [Scripting Templates on GitHub](https://github.com/matter-labs/zksync-scripting-templates#readme).

### 🔗 Supported Chains
### 🔗 Use custom Chain or RPC URLs

zkSync CLI supports Era Testnet and Era Mainnet by default. Use other networks by overriding L1 and L2 RPC URLs: `npx zksync-cli bridge deposit --rpc=http://... --l1-rpc=http://...`
By default zkSync CLI supports zkSync Goerli Testnet, zkSync Mainnet as well as local development networks. However, you can specify custom RPC URLs using `--rpc` and `--l1-rpc` options.

For using [local setup (dockerized testing node)](../testing/dockerized-testing.md) with default RPC URLs, select `Local Dockerized node` in CLI or use `--chain local-dockerized`.
Example: `npx zksync-cli bridge deposit --rpc=http://... --l1-rpc=http://...`

## Troubleshooting & Feedback

Expand Down
267 changes: 267 additions & 0 deletions docs/tools/zksync-cli/examples/contract-interaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
# Contract interaction

The [zksync-cli](../README.md) tool, now enhanced with `contract read` and `contract write` commands, offers efficient ways for developers to interact with smart contracts on zkSync. These commands automate tasks such as method verification, ABI handling, output decoding, and proxy contract processing.

### Table of contents

- [Contract Read - Running read-only methods](#contract-read)
- [Contract Write - Executing write operations](#contract-write)
- [Examples](#examples)
- [Basic read example](#basic-read-example)
- [Basic write example](#basic-write-example)
- [Using local ABI file](#using-local-abi-file)
- [Running read on behalf of another address](#running-read-on-behalf-of-another-address)
- [Write operation with value transfer](#write-operation-with-value-transfer)

<br />

---

<br />

## Contract Read

The `npx zksync-cli contract read` command executes read-only methods on contracts, like checking ERC-20 balances or contract states. [See basic example](#basic-read-example)

### Read Options

You do not need to specify options bellow, you will be prompted to enter them if they are not specified.

- `--chain <chain-name>`: Select the chain to use
- `--rpc <url>`: Provide RPC URL instead of selecting a chain
- `--contract <address>`: Specify contract's address
- `--method <method-signature>`: Defines the contract method to interact with
- `--arguments <method-arguments...>`: Pass arguments to the contract method
- `--data <0x-transaction-data>`: Instead of specifying the method and arguments, you can pass the raw transaction data
- `--outputTypes <output-types...>`: Specifies output types for decoding
- `--from <address>`: Call method on behalf of specified address
- `--abi <path>`: Path to local ABI file or contract artifact
- `--decode-skip`: Skips prompting for output types and decoding the response
- `--show-info`: Displays transaction request information (e.g. encoded transaction data)

<br />

---

<br />

## Contract Write

The `npx zksync-cli contract write` command performs write operations on smart contracts. It enables sending transactions that alter the state of a contract, such as transferring tokens or changing ownership. [See basic example](#basic-write-example)

### Write Options

You do not need to specify options bellow, you will be prompted to enter them if they are not specified.

- `--chain <chain-name>`: Select the chain to use
- `--rpc <url>`: Provide RPC URL instead of selecting a chain
- `--contract <address>`: Specify contract's address
- `--method <method-signature>`: Defines the contract method to interact with
- `--arguments <method-arguments...>`: Pass arguments to the contract method
- `--value <ether-amount>`: Ether amount to send with the transaction (e.g. 0.01)
- `--private-key <wallet-private-key>`: Private key of the wallet to use to sign the transaction
- `--data <0x-transaction-data>`: Instead of specifying the method and arguments, you can pass the raw transaction data
- `--abi <path>`: Path to local ABI file or contract artifact
- `--show-info`: Displays transaction request information (e.g. encoded transaction data)

<br />

---

<br />

## Examples

#### Basic read example

```bash
npx zksync-cli contract read
```

You will be prompted to select a chain, contract address, and method.

```bash
? Chain to use: zkSync Goerli Testnet
? Contract address: 0x3e7676937A7E96CFB7616f255b9AD9FF47363D4b
```

Next you need to select a **method (function) to call**.

- In case your contract is verified it will automatically identify the ABI:

```bash
? Contract method to call
────────── Provided contract ──────────
❯ balanceOf(address account) view returns (uint256)
decimals() pure returns (uint8)
name() pure returns (string)
symbol() pure returns (string)
totalSupply() view returns (uint256)
───────────────────────────────────────
Type method manually
```

- Otherwise you'll have to enter method signature manually, for example `balanceOf(address)`.
```bash
? Enter method to call: balanceOf(address)
```
- Alternatively, you can specify the ABI file manually using the `--abi` option. [See example](#using-local-abi-file)
After that, you will be prompted to enter **arguments** for the method, one by one.
```bash
? Provide method arguments:
? [1/1] account (address): 0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044
```
When submitted a contract call will be made and you'll see the response in it's original encoded form.
```bash
✔ Method response (raw): 0x000000000000000000000000000000000000000000010508e606548a9e5d2000
```
Finally, you will be asked the **method output** type to decode the response. You can skip this step by submitting empty response or completely skip it by passing `--decode-skip` option.
```bash
? Output types: uint256
✔ Decoded method response: 1232701801010000000000000
```
**Tip**: after running command with prompts you will see a full command with all the options that you can copy and use later to quickly run the same command again. For example:
```bash
npx zksync-cli contract read \
--chain "zksync-goerli" \
--contract "0x3e7676937A7E96CFB7616f255b9AD9FF47363D4b" \
--method "balanceOf(address)" \
--args "0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044" \
--output "uint256"
```
<br />
---
<br />
#### Basic write example
```bash
npx zksync-cli contract write
```
You will be prompted to select a chain, contract address, and method.
```bash
? Chain to use: zkSync Goerli Testnet
? Contract address: 0x3e7676937A7E96CFB7616f255b9AD9FF47363D4b
```
Next you need to select a **method (function) to call**.
- In case your contract is verified it will automatically identify the ABI:
```bash
? Contract method to call
────────── Provided contract ──────────
❯ approve(address spender, uint256 amount) returns (bool)
transfer(address to, uint256 amount) returns (bool)
───────────────────────────────────────
Type method manually
```
- Otherwise you'll have to enter method signature manually, for example `transfer(address,uint256)`.

```bash
? Enter method to call: transfer(address,uint256)
```

- Alternatively, you can specify the ABI file manually using the `--abi` option. [See example](#using-local-abi-file)

After that, you will be prompted to enter **arguments** for the method, one by one.

```bash
? Provide method arguments:
? [1/2] to (address): 0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044
? [2/2] amount (uint256): 1
```

Now you need to provide private key of the wallet to use to sign the transaction.

```bash
? Private key of the wallet to sign transaction: *****
```

When submitted a contract call will be made and you'll see the transaction hash
```bash
✔ Transaction submitted. Transaction hash: 0xa83ad7e8932e18cdc57d3892040505a50d560a56fa507cabcd4180e9e5898bec
```
**Tip**: after running command with prompts you will see a full command with all the options that you can copy and use later to quickly run the same command again. For example:
```bash
npx zksync-cli contract write \
--chain "zksync-goerli" \
--contract "0x3e7676937A7E96CFB7616f255b9AD9FF47363D4b" \
--method "transfer(address to, uint256 amount)" \
--args "0x3e7676937A7E96CFB7616f255b9AD9FF47363D4b" "1"
```
<br />
---
<br />
### Using local ABI file
You can specify a local ABI file using the `--abi` option. It should be a JSON file with either ABI data (array) or contract artifact which you get after compiling your contracts.
```bash
npx zksync-cli contract read \
--abi "./Greeter.json"
```
Now you will be able to select a method:
```bash
────────── Provided contract ──────────
❯ greet() view returns (string)
───────────────────────────────────────
Type method manually
```
Response will be decoded automatically as well according to the ABI file.
<br />
---
<br />
### Running read on behalf of another address
You can specify the `--from` option to run the method on behalf of another address. This is useful when you need to call a method that expects a specific address as `msg.sender`.
```bash
npx zksync-cli contract read \
--from "0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044"
```
<br />
---
<br />
### Write operation with value transfer
Here, the command sends a transaction that includes Ether transfer along with the method call.
```bash
npx zksync-cli contract write \
--value "0.1"
```

0 comments on commit 85d4271

Please sign in to comment.