Cheat Codes
Introduction
To help with testing, the sandbox is shipped with a set of cheatcodes.
Cheatcodes allow you to change the time of the Aztec block, load certain state or more easily manipulate Ethereum instead of having to write dedicated RPC calls to anvil or hardhat.
If you aren't familiar with Anvil (Foundry), we recommend reading up on that since Aztec Sandbox uses Anvil as the local Ethereum instance.
Aims
The guide will cover how to manipulate the state of the:
- Ethereum blockchain;
- Aztec network.
Dependencies
For this guide, the following Aztec packages are used:
- @aztec/aztec.js
Initialization
import { createPXEClient, CheatCodes } from "@aztec/aztec.js";
const pxeRpcUrl = "http://localhost:8080";
const ethRpcUrl = "http://localhost:8545";
const pxe = createPXEClient(pxeRpcUrl);
const cc = await CheatCodes.create(ethRpcUrl, pxe);
There are two properties of the CheatCodes class - eth
and aztec
for cheatcodes relating to the Ethereum blockchain (L1) and the Aztec network (L2) respectively.
Ethereum related cheatcodes
These are cheatcodes exposed from anvil/hardhat conveniently wrapped for ease of use in the Sandbox.
Interface
// Fetch current block number of Ethereum
public async blockNumber(): Promise<number>
// Fetch chain ID of the local Ethereum instance
public async chainId(): Promise<number>
// Fetch current timestamp on Ethereum
public async timestamp(): Promise<number>
// Mine a given number of blocks on Ethereum. Mines 1 block by default
public async mine(numberOfBlocks = 1): Promise<void>
// Set the timestamp for the next block on Ethereum.
public async setNextBlockTimestamp(timestamp: number): Promise<void>
// Dumps the current Ethereum chain state to a given file.
public async dumpChainState(fileName: string): Promise<void>
// Loads the Ethereum chain state from a file. You may use `dumpChainState()` to save the state of the Ethereum chain to a file and later load it.
public async loadChainState(fileName: string): Promise<void>
// Load the value at a storage slot of a contract address on Ethereum
public async load(contract: EthAddress, slot: bigint): Promise<bigint>
// Set the value at a storage slot of a contract address on Ethereum (e.g. modify a storage variable on your portal contract or even the rollup contract).
public async store(contract: EthAddress, slot: bigint, value: bigint): Promise<void>
// Computes the slot value for a given map and key on Ethereum. A convenient wrapper to find the appropriate storage slot to load or overwrite the state.
public keccak256(baseSlot: bigint, key: bigint): bigint
// Let you send transactions on Ethereum impersonating an externally owned or contract, without knowing the private key.
public async startImpersonating(who: EthAddress): Promise<void>
// Stop impersonating an account on Ethereum that you are currently impersonating.
public async stopImpersonating(who: EthAddress): Promise<void>
// Set the bytecode for a Ethereum contract
public async etch(contract: EthAddress, bytecode: `0x${string}`): Promise<void>
// Get the bytecode for a Ethereum contract
public async getBytecode(contract: EthAddress): Promise<`0x${string}`>
blockNumber
Function Signature
public async blockNumber(): Promise<number>
Description
Fetches the current Ethereum block number.
Example
const blockNumber = await cc.eth.blockNumber();
chainId
Function Signature
public async chainId(): Promise<number>
Description
Fetches the Ethereum chain ID
Example
const chainId = await cc.eth.chainId();
timestamp
Function Signature
public async timestamp(): Promise<number>
Description
Fetches the current Ethereum timestamp.
Example
const timestamp = await cc.eth.timestamp();
mine
Function Signature
public async mine(numberOfBlocks = 1): Promise<void>
Description
Mines the specified number of blocks on Ethereum (default 1).
Example
const blockNum = await cc.eth.blockNumber();
await cc.eth.mine(10); // mines 10 blocks
const newBlockNum = await cc.eth.blockNumber(); // = blockNum + 10.
setNextBlockTimestamp
Function Signature
public async setNextBlockTimestamp(timestamp: number): Promise<void>
Description
Sets the timestamp (unix format in seconds) for the next mined block on Ethereum. Time can only be set in the future. If you set the timestamp to a time in the past, this method will throw an error.
Example
// // Set next block timestamp to 16 Aug 2023 10:54:30 GMT
await cc.eth.setNextBlockTimestamp(1692183270);
// next transaction you will do will have the timestamp as 1692183270
dumpChainState
Function Signature
public async dumpChainState(fileName: string): Promise<void>
Description
Dumps the current Ethereum chain state to a file.
Stores a hex string representing the complete state of the chain in a file with the provided path. Can be re-imported into a fresh/restarted instance of Anvil to reattain the same state.
When combined with loadChainState()
cheatcode, it can be let you easily import the current state of mainnet into the Anvil instance of the sandbox.