Getting Started

Learn how to setup and use Foundry with your Validium project.


Basic Usage

Testing

Use forge test --zksync to run tests written for your smart contracts.

For an overview of how to write tests using foundry-zksync please refer to Foundry testing here.

Deploying Smart Contracts with forge

Compilation with forge build --zksync

forge build --zksync is used for compiling smart contracts into Validium VM bytecode. The compiled files are stored in a structured directory at <PROJECT-ROOT>/zkout/.

Command:

forge build [OPTIONS] --zksync

Options:

To get the list of all build options run:

forge build --help

Besides standard options from original Foundry, there are Validium specific set of options:

  • --zk-startup[=<ENABLE_ZKVM_AT_STARTUP>] Enable zkEVM at startup.

  • --zk-compile[=<COMPILE_FOR_ZKVM>] Compile for zkEVM.

  • --zk-solc-path <ZK_SOLC_PATH> Solc compiler path to use when compiling with zksolc.

  • --zk-enable-eravm-extensions[=<ENABLE_ERAVM_EXTENSIONS>] Enable the system contract compilation mode.

  • --zk-force-evmla[=<FORCE_EVMLA>] Forcibly switch to the EVM legacy assembly pipeline.

  • --zk-llvm-options <LLVM_OPTIONS> ZkSolc extra LLVM options.

  • --zk-fallback-oz[=<FALLBACK_OZ>] Try to recompile with -Oz if the bytecode is too large.

  • --zk-detect-missing-libraries Detect missing libraries, instead of erroring.

  • -O, --zk-optimizer-mode <LEVEL> Set the LLVM optimization parameter -O[0 | 1 | 2 | 3 | s | z]. Use 3 for best performance and z for minimal size.

  • --zk-optimizer Enables optimizations.

  • --zk-avoid-contracts <AVOID_CONTRACTS> Contracts to avoid compiling on Validium.

Examples:

Compile with default settings or specify zksolc version:

Deployment with forge create --zksync

Command:

The following commands make use of Foundry keystore instead of private keys. Learn how to create a foundry keystore.

forge create --zksync deploys smart contracts to Validium.

Usage:

Options: To get the list of all create options run:

Examples: Deploy Greeter.sol to ZKsync Sepolia Testnet:

Click to view the `Greeter.sol` contract

Deploying Factory Contracts

To deploy contracts like GreeterFactory.sol, use the --zk-enable-eravm-extensions flag.

Click to view the `GreeterFactory.sol` contract

Compile GreeterFactory.sol:

Deploy GreeterFactory.sol:

Deploy Greeter.sol via GreeterFactory.sol:

Interact with Greeter.sol

Output:

To decode the output to a readable string:

Output:

Basic Validium Chain Interactions with cast

Introduction

This guide introduces you to fundamental interactions within the ZKsync chain using cast, a component of the foundry-zksync toolkit. Learn how to query chain IDs, retrieve client versions, check L2 ETH balances, obtain gas prices, and more.

Command:

Chain ID Retrieval

  • Local Node: Retrieve the Chain ID for a local Validium node with:

    Expected Output: 270, indicating the Chain ID of your local Validium node.

  • Validium Sepolia Testnet: For the ZKsync Sepolia Testnet, use:

    Expected Output: 300, the Chain ID for the Validium Sepolia Testnet.

Client Version Information

Knowing the client version is vital for compatibility checks and debugging:

Expected Output: ZKsync/v2.0, denoting the client version.

L2 Balance Check

Verify the Layer 2 (L2) balance of an account:

Expected Output: A numerical value, e.g., 774909739323110932, representing the account's L2 balance.

Current Gas Price

Fetch the current gas price on the network for transaction cost estimations:

Expected Output: A value such as 100000000, indicating the current gas price.

Latest Block Details

Gain insights into the latest block on the Validium chain:

Expected Output: Detailed information about the latest block, including base fee per gas, gas limit, block hash, and more.

Sending Transactions

Initiate transactions, such as contract function calls, using cast:

Example:

This command calls the setGreeting function of a contract, updating the greeting to "Hello, Validium!".

Support

If you're having an issues creating Foundry Validium project, please check out list of issues in foundry-zksync github issues page or reach out to us by creating a GitHub discussion.

Last updated