Skip to main content

Cheats (AnvilCheats)

Each anvil.cheats starts a chain: steps are validated when added and run in order, once, when awaited. A failing step rejects with CHEAT_FAILED.

const chain = anvil.cheats.setBalanceEther(ALICE, 1.5).mine(2);
expect(chain.plan).toEqual([`setBalanceEther(${ALICE}, 1.5)`, "mine(2)"]);

await chain.run(); // same as `await chain`; a chain runs once
expect(await anvil.getBalance(ALICE)).toBe(parseEther("1.5"));

Tested in examples/test/e2e/cheats.e2e.test.ts:18

Stepanvil RPCArguments
setBalance(address, wei)anvil_setBalance
setBalanceEther(address, ether)anvil_setBalance"1.5" or 100
addBalance(address, wei)anvil_addBalance
dealErc20(token, address, amount)anvil_dealERC20token units
setErc20Allowance(token, owner, spender, amount)anvil_setERC20Allowance
setCode(address, bytecode)anvil_setCoderuntime code; "0x" removes it
setNonce(address, nonce)anvil_setNonce
setStorageAt(address, slot, value)anvil_setStorageAtslot number or hex; bigint value encoded to 32 bytes
impersonate(address) / stopImpersonating(address)anvil_impersonateAccount / anvil_stopImpersonatingAccount
setAutoImpersonate(enabled)anvil_autoImpersonateAccount
mine(blocks?, interval?)anvil_minedefault 1 block
setAutomine(enabled)evm_setAutomine
setIntervalMining(duration)evm_setIntervalMining0 stops it
increaseTime(duration)evm_increaseTime
setNextBlockTimestamp(timestamp)evm_setNextBlockTimestamp
setTime(timestamp)evm_setTime
setBlockTimestampInterval(duration) / removeBlockTimestampInterval()anvil_setBlockTimestampInterval / anvil_removeBlockTimestampInterval
setCoinbase(address)anvil_setCoinbase
setMinGasPrice(wei)anvil_setMinGasPricepre-London only
setNextBlockBaseFeePerGas(wei)anvil_setNextBlockBaseFeePerGas
setBlockGasLimit(gas)evm_setBlockGasLimit
dropTransaction(hash) / dropAllTransactions()anvil_dropTransaction / anvil_dropAllTransactions
removePoolTransactions(address)anvil_removePoolTransactions
setChainId(chainId)anvil_setChainIdthe container follows
setRpcUrl(url)anvil_setRpcUrlfork RPC URL
setLoggingEnabled(enabled)anvil_setLoggingEnabled
reset(options?)anvil_resetfixed version; clears snapshots
rollback(depth?)anvil_rollbackdefault 1
loadState(state)anvil_loadState
step(fn, label?)–any (client) => Promise between cheats
ExecutionDescription
planlabels of the queued steps, e.g. ["mine(2)"]
run()runs the steps; later calls return the same promise
then() / catch() / finally()a chain is a full promise (PromiseLike<void>)

Durations: seconds or { weeks, days, hours, minutes, seconds }; timestamps: seconds or a Date.

Task: Script state changes.