Skip to Content

State

Set code, nonce, and storage on any account on your Stagenet.

Use this to:

  • Replace a contract’s bytecode with a mock for testing.
  • Force-set a storage slot to put a contract into an exact state.
  • Bump an account’s nonce to match a production scenario.

How it works

Each method writes directly to your Stagenet’s chain state.

After a value is set, it behaves like normal EVM state: transactions can modify it, and reads return the latest value.

This matches the behaviour of hardhat_setStorageAt, anvil_setStorageAt, and Foundry’s vm.store.

setCode

Replace the code at an address with arbitrary bytecode. Pass "0x" to wipe the code.

const addr = "0x0000000000000000000000000000000000001234"; await stagenet.setCode(addr, "0x6042"); await stagenet.setCode(addr, "0x"); // empty
ParameterTypeDescription
addressstringA 0x-prefixed Ethereum address
bytecodestringThe bytecode as 0x-prefixed hex. Pass "0x" for empty code.

Returns { address }.

setNonce

Set the nonce on an account. Transaction-driven nonce increments after this proceed from the new value.

await stagenet.setNonce(alice, 42); await stagenet.setNonce(alice, "0x10"); // hex also accepted (= 16)
ParameterTypeDescription
addressstringA 0x-prefixed Ethereum address
noncestring | bigint | numberNon-negative integer

Returns { address, nonce }. nonce is the post-update value as a decimal string.

setStorageAt

Write a 32-byte value into a specific storage slot.

The slot accepts decimal slot numbers (5, "5", 5n) and 0x hex (≤32 bytes, left-padded) — slot keys are uint256, so this is unambiguous. The value must be an exactly 32-byte hex word (0x + 64 hex chars); short values like "0x42" are rejected — encode them explicitly.

The strict value rule matches the convention used by Hardhat, Anvil, and Foundry’s vm.store — the caller encodes their value explicitly so there is no ambiguity between uint-style (left-padded) and bytes-style (right-padded).

// Encode a number as a uint256-style 32-byte word const toUint = (n: bigint) => "0x" + n.toString(16).padStart(64, "0"); await stagenet.setStorageAt(token, 0, toUint(42n)); // declared slot zero

Or use a helper from viem for the value:

import { pad, toHex } from "viem"; await stagenet.setStorageAt(token, 0, pad(toHex(42n)));
ParameterTypeDescription
addressstringA 0x-prefixed Ethereum address
slotstring | number | bigintStorage slot key — decimal slot number or 0x hex (≤32 bytes). 10 is slot ten, not 0x10
valuestringValue to write — exactly 32 bytes (0x + 64 hex chars)

Returns { address, slot }. slot echoes back the 32-byte slot key that was written.

Last updated on