Overview
Curated: · Written: · Reviewed:
Web3 Development Tooling & Testing
In one line
Treat a contract system as a state machine with financial consequences: a green test only counts if the source, compiler settings, lockfile, deployed artifact, pinned chain state and retained inputs all agree. Reach for this framing whenever you must show that a test predicts production behaviour.
Key definitions
- Invariant: a property that must hold after any sequence of valid calls. You state it first, then the test checks it.
- Handler contract: a Foundry contract whose bounded actions the fuzzer calls, with ghost variables for accounting the contract does not store.
- Pinned fork: a fork fixed to a block number and chain ID, served by an archive node.
- Metadata suffix: the trailing CBOR in runtime bytecode. It records compiler metadata, so two builds with identical code can still differ in hash.
- Domain separator: the EIP-712 hash of name, version, chainId and verifyingContract. It binds a signature to one chain and one contract.
Formulas and complexity
How it works
Source, compiler settings and lockfile produce an artifact with a fixed hash. The deploy plan records chain, deployer, nonce and predicted address before anything is broadcast. Post-deploy, the receipt must match the plan, and verification must reproduce the deployed runtime bytecode. Monitoring then reads canonical state at a pinned block.
When to use and when not to
| Situation | Choose | Why |
|---|---|---|
| Heavy fuzz and invariant work on contracts | Foundry | Tests are Solidity, built-in stateful fuzzing, fast shrinking |
| Indexer, keeper or relayer tests in TypeScript | Hardhat or viem layer | Same language as off-chain code, mature plugins |
| Property tests that must cross the contract boundary | Neither alone | Keep properties in Solidity, end-to-end wiring in TypeScript |
| Reproducing a production bug | Pinned fork at incident block | Real storage, oracle values and adversary contracts |
| Signing or deploying from a fresh counter | Do not | Reverted transactions spend their nonce |
Traps interviewers probe
- Bare expectRevert and vm.assume(amount > 0). The test passes for the wrong reason and hides the zero input that drains the vault. Fix: use
vm.expectRevert(Custom.selector)andboundin handlers, and assert the state afterwards. - Unit tests for a sequence bug. Forty passing single-call tests cannot find a deposit, donate, borrow, withdraw sequence. Fix: a stateful invariant campaign with a handler, plus a differential or invariant assertion on share price.
- Unpinned fork at latest. A suite that passed Monday fails Tuesday because a pool rebalanced. Fix: pin block number and chain ID, record both with the release, and re-pin when bytecode or layout changes.
- Re-deriving CREATE addresses after a revert. A reverted transaction consumes its nonce, so a fresh script lands at different addresses. Fix: resume from the recorded plan with the same nonce and calldata. Use CREATE2 when the address must survive a retry, and remember that its squatting risk is real.
- Caching the EIP-712 domain across chainChanged. The signature covers the cached chainId, so a switch to another chain produces a signature that the wrong deployment must reject. Fix: rebuild the domain from the live chainId after every provider event. The contract should recompute the separator from block.chainid and reject mismatches.
Interview tips
Open by naming the state machine and the property the test proves, then name the tool and the trade-off. Expect these follow-ups: walk me through your CI pipeline for a contract repo; a fuzz test found a conservation violation, what do you do in the next hour; how do you test an upgrade against live state; what does the runbook say when the third of five deploy transactions fails.
30-second outline: (1) The chain is source, compiler, artifact, deployment, verification and monitoring, and each link must be reproducible. (2) Foundry for invariant fuzzing, TypeScript for off-chain wiring, both pinned. (3) One incident: a fork test passed but mainnet failed, so the fork was unpinned and the oracle timing was not reproduced. (4) The fix: pin the block, reproduce the incident block, and add a regression seed. (5) State what the test does not prove: it covers the pinned block only, not mempool or MEV behaviour.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.