zksync-cli contract

Interact with contracts using the zksync-cli contract command.

The zksync-cli contract command comes with actions to read, write and encode smart contracts on ZKsync. These commands automate tasks such as method verification, ABI handling, output decoding, and proxy contract processing.

After running a 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.

Contract Read

The zksync-cli contract read command executes read-only methods on contracts, like checking ERC-20 balances or contract states.

Read Options

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

  • --chain <chain-name>: Select the chain to use (e.g., zksync-mainnet, zksync-sepolia).
  • --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)

Basic read example

zksync-cli contract read
  1. You will be prompted to select a chain, contract address, and method.
    ? Chain to use: ZKsync Sepolia Testnet
    ? Contract address: 0x45E6dC995113fd3d1A3b1964493105B9AA9a9A42
    
  2. Next you need to select a method (function) to call.
    • In case your contract is verified it will automatically identify the ABI:
      ? 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.
      ? Enter method to call: balanceOf(address)
      
    • Alternatively, you can specify the ABI file manually using the --abi option. See example
  3. You will be prompted to enter arguments for the method, one by one.
    ? 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.
     Method response (raw): 0x000000000000000000000000000000000000000000010508e606548a9e5d2000
    
  4. 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.
    ? Output types: uint256
     Decoded method response: 1232701801010000000000000
    

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.

zksync-cli contract read \
  --from "0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044"

Contract Write

The 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.

Write Options

You do not need to specify options below, 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)

Basic write example

zksync-cli contract write
  1. You will be prompted to select a chain, contract address, and method.
    
    ? Chain to use: ZKsync Sepolia Testnet
    ? Contract address: 0x45E6dC995113fd3d1A3b1964493105B9AA9a9A42
    
  2. Select a method (function) to call.
    • In case your contract is verified it will automatically identify the ABI:
      ? 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).
      ? Enter method to call: transfer(address,uint256)
      
    • Alternatively, you can specify the ABI file manually using the --abi option. See example
  3. You will be prompted to enter arguments for the method, one by one.
    ? Provide method arguments:
    ? [1/2] to (address): 0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044
    ? [2/2] amount (uint256): 1
    
  4. provide private key of the wallet to use to sign the transaction.
    ? Private key of the wallet to sign transaction: *****
    

When submitted a contract call will be made and you'll see the transaction hash.

 Transaction submitted. Transaction hash: 0xa83ad7e8932e18cdc57d3892040505a50d560a56fa507cabcd4180e9e5898bec

Encode

The zksync-cli contract encode command will get calldata (e.g. 0x1234) from contract method signature and arguments.

Encode options

  • --method <contractMethod(arguments)>: Contract method to interact with
  • --args, --arguments <arguments...>: Arguments
  • --abi <path/to/abi>: Contract artifact or ABI file location.

Basic encode example

zksync-cli contract encode
  1. Select a method (function) to encode.
    • Enter method signature manually, for example transfer(address,uint256).
      ? Enter method to call: transfer(address,uint256)
      
    • Alternatively, you can specify the ABI file using the --abi option. See example
    ? Contract method to call
      ────────── Provided contract ──────────
     approve(address spender, uint256 amount) returns (bool)
      transfer(address to, uint256 amount) returns (bool)
      ───────────────────────────────────────
      Type method manually
    
  2. Enter the arguments for the method, one by one.
    ? Provide method arguments:
    ? [1/2] to (address): 0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044
    ? [2/2] amount (uint256): 1
    

When finished you will see the encoded data.

 Encoded data: 0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f20776f726c64210000000000000000000000000000000000000000

Using a 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.

zksync-cli contract read \
  --abi "./Greeter.json"

You will be prompted to select a method (function):

  ────────── Provided contract ──────────
 greet() view returns (string)
  ───────────────────────────────────────
  Type method manually

Response will be decoded automatically according to the ABI file.


Made with ❤️ by the ZKsync Community