Overview
Getting started with Foundry on ZKsync requires a few key adjustments to accommodate the differences between Ethereum and ZKsync environments.
This guide will walk you through the essential steps for setting up your project, running tests, and configuring Foundry for ZKsync compatibility.
ZKsync Context
A forge test
begins its execution in the standard EVM context, which is why compiling
solc
artifacts is necessary. However, during test execution, the context can switch over to ZKsync in several ways.
When switching to the ZKsync context, the following operations are performed:
- All
persisted_accounts
storages are migrated to ZKsync storage. - Any EVM bytecode deployed under the migrated accounts is replaced by its
zksolc
variant. - Solidity globals such as
block.number
andaddress.balance
will return ZKsync values even when the test begins in the EVM context. - The original EVM context (including the block environment) is preserved in case the test needs to switch back from ZKsync.
Switching to ZKsync
You can switch to the ZKsync context in the following ways:
CLI Flags
The --zksync
flag compiles the sources for zksolc
and automatically switches the
test execution to the ZKsync context. This flag is a shorthand for enabling two specific flags:
--zk-startup
: Switches to ZKsync on test startup.--zk-compile
: Compiles the sources usingzksolc
.
In the following sections, we will cover key areas for working with Foundry ZKsync:
- Compiling: Learn the adjustments needed for
zksolc
to compile projects in ZKsync environments. - Deploying: Understand the process of deploying contracts on ZKsync.
- Testing: Explore how to use
forge test
with the--zksync
context, including test configurations and strategies to ensure correct contract behavior in ZKsync’s execution environment.