In Web3, mistakes are immutable and vulnerabilities are catastrophic. Before handing your code over to an external auditing firm—or worse, deploying straight to mainnet—run your codebase through this comprehensive Solidity pre-launch hygiene checklist.
Why Pre-Audit Hygiene Matters
An audit is not a substitute for standard quality assurance. When auditors spend their allocated hours pointing out missing NatSpec tags, floating pragmas, or basic formatting issues, they have less time to uncover deep, protocol-breaking economic and access-control vulnerabilities.
Cleaning your codebase beforehand increases the return on investment (ROI) of your professional audit and dramatically reduces the risk of post-launch exploits.
1. Compiler & Configuration Hygiene
-
- Lock Your Pragma: Avoid using floating pragmas like
pragma solidity ^0.8.20;in production. Lock the exact version (e.g.,pragma solidity 0.8.24;) to ensure the deployed bytecode behaves identically to your tested environment.
- Lock Your Pragma: Avoid using floating pragmas like
-
- Awareness of EVM Compatibility: If compiling to an L2 (such as Arbitrum, Optimism, or zkSync), verify that your compiler target matches the chain. For example, recent opcodes like
PUSH0(introduced in the Shanghai hard fork) may not be supported across all target networks.
- Awareness of EVM Compatibility: If compiling to an L2 (such as Arbitrum, Optimism, or zkSync), verify that your compiler target matches the chain. For example, recent opcodes like
-
- Audit Third-Party Dependencies: Use verified, stable releases of audited libraries like OpenZeppelin. Avoid copying and pasting snippets from forums or using unvetted GitHub submodules.
2. Architectural & Access Control Patterns
-
- Use Safe Ownership Transfers: Prefer OpenZeppelin’s
Ownable2Stepover basicOwnable. A two-step transfer mechanism prevents permanently bricking a contract due to an accidental address typo.
- Use Safe Ownership Transfers: Prefer OpenZeppelin’s
-
- Implement Granular Roles: For complex protocols, ditch monolithic administrative roles. Implement role-based access control (
AccessControl) to practice the principle of least privilege.
- Implement Granular Roles: For complex protocols, ditch monolithic administrative roles. Implement role-based access control (
-
- Emergency Stops: Ensure critical functions have circuit breakers (
Pausable), especially for asset withdrawals, minting, or external integrations.
- Emergency Stops: Ensure critical functions have circuit breakers (
3. Execution Flow & Logic Security
<h3>The Checks-Effects-Interactions (CEI) Pattern</h3>
<p>
While modern versions of Solidity (0.8.x+) protect against standard integer overflow/underflow, <strong>reentrancy</strong> remains a major threat. Always structure your functions to validate input first, update internal state second, and interact with external contracts last.
</p>
<pre><code>// GOOD: CEI Pattern enforced
function withdraw(uint256 amount) external {
// 1. Checks
require(balances[msg.sender] >= amount, “Insufficient balance”);
// 2. Effects
balances[msg.sender] -= amount;
// 3. Interactions
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Transfer failed");
}
<ul>
<li>
<strong>Guard Against Read-Only Reentrancy:</strong> Even if your balance-modifying functions are protected by <code>ReentrancyGuard</code>, ensure that view functions relied upon by external integrations (like pricing oracles) cannot be manipulated mid-execution.
</li>
<li>
<strong>Beware of Strict Equality:</strong> Avoid using <code>this.balance == target</code>. Attackers can forcefully send Ether via <code>selfdestruct</code> or coinbase rewards to permanently break strict balance checks.
</li>
<li>
<strong>Validate Return Values:</strong> Ensure all low-level calls (e.g., <code>address.call()</code>) and ERC-20 token interactions check boolean return values or utilize <code>SafeERC20</code>.
</li>
</ul>
4. Precision, Math & Gas Hygiene
-
- Multiplication Before Division: Integer division in Solidity truncates decimals. Always perform multiplications before divisions to preserve calculation precision (e.g.,
(x * y) / zinstead of(x / z) * y).
- Multiplication Before Division: Integer division in Solidity truncates decimals. Always perform multiplications before divisions to preserve calculation precision (e.g.,
-
- Custom Errors Over String Reverts: Replace long revert strings with custom errors (e.g.,
revert InsufficientBalance();). This reduces deployment bytecode size and saves gas on revert.
- Custom Errors Over String Reverts: Replace long revert strings with custom errors (e.g.,
-
- Variable Packing: Group
uint128,uint64,bool, andaddressvariables together within storage layouts to occupy the same 32-byte slot, lowering storage write costs.
- Variable Packing: Group
-
- Constants and Immutables: Mark variables that do not change as
constantorimmutableto read them directly from bytecode instead of storage.
- Constants and Immutables: Mark variables that do not change as
5. NatSpec & Off-Chain Observability
Auditors and block explorers rely heavily on clear contract interfaces. Code without documentation obscures the developer’s original intent.
-
- Full NatSpec Coverage: Document all public and external functions using
@notice,@dev,@param, and@return.
- Full NatSpec Coverage: Document all public and external functions using
-
- Emit Events for State Changes: Every operation modifying critical state (ownership changes, fee updates, large transfers) must emit an event. Index up to three relevant parameters for efficient off-chain querying.
6. Pre-Launch Testing & Automated Tooling
Never send code to an auditor without running a complete test suite and static analysis suite locally.
-
- Branch Coverage: Aim for >90% branch coverage via unit testing frameworks like Foundry or Hardhat.
-
- Fuzzing & Invariant Testing: Write property-based tests. Test invariants like: “Total user deposits must always equal contract balance.”
-
- Static Analysis: Run automated analysis tools like:
-
- Slither: Detects common vulnerabilities, reentrancy paths, and shadowing.
-
- Aderyn: A modern Rust-based static analyzer tailored for Solidity.
-
- Static Analysis: Run automated analysis tools like:
-
- Linters and Formatters: Keep code legible using
prettier-plugin-solidityandsolhintto ensure consistent formatting across the codebase.
- Linters and Formatters: Keep code legible using