How do you test a smart contract before deploying it to mainnet?
What the interviewer wants to hear
- Unit tests for every function and revert path
- Fuzz and invariant tests
- Mainnet fork tests with real integrations
- Static analysis tools like Slither
- Testnet deployment, audits and bug bounties
- Deployment scripts and verification tested too
Model answer
I write unit tests in Foundry for every function, including all revert paths and access control, checking events and balances. Then fuzz tests that run functions with random inputs, and invariant tests that call random sequences of functions and check properties that must always hold, such as total shares matching total deposits or the protocol never owing more than it holds. For integrations with tokens, oracles or DEXs, I run fork tests against a mainnet snapshot so the behaviour is real, including odd tokens like fee-on-transfer ones. I run static analysis with Slither and check coverage. Deployment scripts are tested on a fork and a public testnet like Sepolia, and contracts are verified on the explorer. For anything holding real value, an external audit, sometimes formal verification of key properties, and a bug bounty come before launch, plus monitoring and a pause plan after.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Blockchain Developer questions
- What is a reentrancy attack and how do you prevent it?
- What techniques do you use to reduce gas costs in Solidity contracts?
- How do upgradeable smart contracts work, and what are the trade-offs?
- What is a blockchain oracle and how do you use price data safely?
- How do signature-based approvals like ERC-2612 permit and Permit2 work, and what are their risks?
- How do you build a reliable dApp frontend that reads and writes contracts with viem and wagmi?
- How do chain reorganizations affect a dApp or backend service, and how do you handle them?
- How do you design admin control for a protocol so it can be maintained without making users trust a single person?
- Walk me through how you deploy a set of contracts to mainnet safely.