Schedule
← All posts
Development

Ship Smart Contracts That Don't Get Drained: A Practical Testing & Security Checklist

May 20, 2026 · 2 min read

Ship Smart Contracts That Don't Get Drained: A Practical Testing & Security Checklist

Smart contracts are public, immutable, and hold money — the worst possible combination for untested code. Here's the testing and security workflow I run before anything touches mainnet.

A web bug means a hotfix. A smart-contract bug can mean a drained treasury with no undo. Contracts are public, immutable and hold value — so the bar isn't "it works," it's "it can't be abused." This is the workflow I run on every contract.

Tests come before the frontend

I write the test suite before wiring any UI. Tests are the executable spec: a new developer should understand the whole system from the assertions alone, and every security assumption should have a test that proves it holds.

The bugs that actually drain contracts

Most exploits aren't exotic. They're a handful of recurring classes:

  • Reentrancy — external calls before state updates. Follow checks-effects-interactions, and use a nonReentrant guard on anything moving funds.
  • Broken access control — a privileged function missing onlyOwner or a role check. The most common and most embarrassing.
  • Arithmetic & rounding — division-before-multiplication, unchecked math, fee rounding that leaks value over time.
  • Oracle manipulation — trusting a spot price that a flash loan can move within a single block.

The testing setup

  • Unit tests for every function and every revert path — including the ones you want to fail.
  • Fuzz tests (Foundry) that throw thousands of random inputs at invariants: "total supply always equals the sum of balances," "an amount can never be retired twice."
  • Fork tests against a mainnet fork when you integrate with real protocols, so you test the actual environment, not a mock of it.
function test_cannotRetireTwice() public {
    credit.retire(id, 10, "buyer");
    vm.expectRevert(); // no balance left to burn
    credit.retire(id, 10, "buyer");
}

Getting audit-ready

An audit is not where you find your bugs — it's where you confirm you didn't miss any. Before one, I lock compiler versions, remove dead code, document every privileged action and external dependency, and make sure coverage is meaningful (not just high). A clean, well-tested codebase makes the audit cheaper and faster.

The pre-mainnet checklist

  • Every state-changing function has success and revert tests
  • Reentrancy guards on all value transfers
  • Access control on every privileged function
  • Invariants covered by fuzz tests
  • No unbounded loops that can hit the gas limit
  • Deploy script is one command and reproducible
  • External calls handled defensively (return values checked)

None of this is glamorous. But it's the difference between a contract that ships and a headline you don't want to be in.