Interact with contracts
You can interact with smart contracts on ZKsync using the ZKsync CLI. These commands automate tasks such as method verification, ABI handling, output decoding and proxy contract processing.
Read example
Run the following command without any options:
zksync-cli contract read
ZKsync CLI prompts with an interactive list of questions to define the options.
? Chain to use: ZKsync Sepolia Testnet
? Contract address: 0x927488F48ffbc32112F1fF721759649A89721F8F
Next, ZKsync CLI will prompt to select the contract method to call. If the smart contract is verified on ZKsync, the CLI returns a list of the smart contract's available methods.
? Contract method to call (Use arrow keys)
──────────────── Provided contract ────────────────
❯ allowance(address owner, address spender) view returns (uint256)
balanceOf(address account) view returns (uint256)
decimals() view returns (uint8)
name() view returns (string)
symbol() view returns (string)
totalSupply() view returns (uint256)
───────────────────────────────────────────────────
Type method manually
Otherwise the CLI will prompt you to enter the method signature manually.
? Enter method to call balanceOf(address account)
You can pass the ABI file of the smart contract using the --abi
option.
This decodes the contract methods and infer the output type of the selected method.
Once you select a method to call, the CLI prompts you to enter the method arguments, one by one:
? Provide method arguments:
? [1/1] account (address) 0xfAa77e3314c82cD5097B80Ab42C6A3749f33dEE6
If you selected a verified method from the list of the smart contract's methods, you will see a response in it's original encoded form along with the decoded method response.
✔ Method response (raw): 0x0000000000000000000000000000000000000000000000000000000000000000
✔ Decoded method response: 0
If you entered the smart contract's method manually, the CLI will prompt you for the method's output type to decode the response. You can skip this step by submitting an empty response.
✔ Method response (raw): 0x0000000000000000000000000000000000000000000000000000000000000000
Provide output types to decode the response (optional)
? Output types uint256
✔ Decoded method response: 0
To skip the decode method response step, you can use the --decode-skip
option.
For example, the constructed command for the above:
npx zksync-cli contract read --chain "zksync-sepolia" --contract "0x927488F48ffbc32112F1fF721759649A89721F8F" --method "balanceOf(address account)" --args "0xfAa77e3314c82cD5097B80Ab42C6A3749f33dEE6" --output "uint256"
Write example
Run the following command without any options:
zksync-cli contract write
ZKsync CLI prompts with an interactive list of questions to define the options.
? Chain to use: ZKsync Sepolia Testnet
? Contract address: 0x927488F48ffbc32112F1fF721759649A89721F8F
Next, ZKsync CLI prompts to select the contract method to call. If the smart contract is verified on ZKsync, the CLI returns a list of the smart contract's available methods.
? Contract method to call
──────────────── Provided contract ────────────────
approve(address spender, uint256 amount)
burn(uint256 amount)
burnFrom(address account, uint256 amount)
decreaseAllowance(address spender, uint256 subtractedValue)
increaseAllowance(address spender, uint256 addedValue)
mint(address _to, uint256 _amount)
❯ transfer(address to, uint256 amount)
transferFrom(address from, address to, uint256 amount)
───────────────────────────────────────────────────
Type method manually
Otherwise the CLI will prompt you to enter the method signature manually.
? Enter method to call transfer(address, uint256)
You can pass the ABI file of the smart contract using the --abi
option.
This will decode the contract methods and infer the output type of the selected method.
Once you select a method to call, the CLI prompts you to enter the method arguments, one by one:
? Provide method arguments:
? [1/2] to (address) 0xfAa77e3314c82cD5097B80Ab42C6A3749f33dEE6
? [2/2] amount (uint256) 1
Next, the CLI prompts for the private key of your wallet to sign the transaction.
? Private key of the wallet to sign transaction ******
When submitted a contract call is be made and a transaction hash will be returned.
✔ Transaction submitted. Transaction hash: 0x7f82dbf93281930ee01644d01a84b3f13df51d0082370298d353982f008f09e1
✔ Transaction processed successfully. Transaction link: https://sepolia.explorer.zksync.io/tx/0x7f82dbf93281930ee01644d01a84b3f13df51d0082370298d353982f008f09e1
The fully constructed command for the above actions:
npx zksync-cli contract write --chain "zksync-sepolia" --contract "0x927488F48ffbc32112F1fF721759649A89721F8F" --method "transfer(address to, uint256 amount)" --args "0xfAa77e3314c82cD5097B80Ab42C6A3749f33dEE6" "1"
Encode example
You can encode the contract method signature and arguments into raw calldata. This can be helpful for debugging or unit testing with lower-level libraries.
To encode a method with arguments, run the CLI command zksync-cli contract encode
.
The CLI then prompts for the method to encode.
? Enter method to encode transfer(address, uint256)
Next, the CLI will ask for the arguments to provide for the method.
? Provide method arguments:
? [1/2] address 0xfAa77e3314c82cD5097B80Ab42C6A3749f33dEE6
? [2/2] uint256 1
The ZKsync CLI then returns the encoded data for the method with the arguments.
✔ Encoded data: 0xa9059cbb000000000000000000000000faa77e3314c82cd5097b80ab42c6a3749f33dee60000000000000000000000000000000000000000000000000000000000000001
--abi
If you are interacting with a smart contract that is not verified publicly, you can specify a local ABI file with the --abi
option.
Your ABI data must be in a JSON format or a contract artifact from compilation.
zksync-cli contract read --abi "./Greeter.json"
Providing an ABI for a smart contract will allow the CLI to provide the decoded list of methods to interact with as well as decode the response.
--from
You can specify another address to execute the transaction with the --from
option.
This is useful when you need to call a method that expects a specific address as msg.sender
.
zksync-cli contract read --from "0xa1cf087DB965Ab02Fb3CFaCe1f5c63935815f044"
--value
To send ETH along with your method call using ZKsync CLI, you can pass a string value with the --value
option.
zksync-cli contract write --value "0.1"