To secure your code, you must stop thinking like a builder and start thinking like a breaker. Here is how to write Solidity like an auditor.
1. The Adversarial Mental Model
An auditor assumes every external actor is malicious. When writing a function, don’t ask “How will my users use this?” Instead, ask:
-
- “If I call this function 10,000 times in a single block, what happens?”
-
- “What happens if a contract calls this instead of an EOA (Externally Owned Account)?”
-
- “Can I front-run this transaction to profit from the state change?”
msg.sender to behave nicely. If a state change can be abused, it will be.
2. Master the Checks-Effects-Interactions (CEI) Pattern
Reentrancy remains one of the most common vulnerabilities in Solidity. Auditors look for this pattern immediately. To write secure code, you must strictly follow CEI:
-
- Checks: Validate all inputs and conditions (e.g.,
requirestatements).
- Checks: Validate all inputs and conditions (e.g.,
-
- Effects: Update the contract’s internal state (e.g., mapping balances).
-
- Interactions: Finally, perform external calls (e.g.,
transferor calling other contracts).
- Interactions: Finally, perform external calls (e.g.,
// BAD: Interaction before Effect
function withdraw(uint _amount) public {
require(balances[msg.sender] >= _amount);
(bool success, ) = msg.sender.call{value: _amount}(""); // Interaction
require(success);
balances[msg.sender] -= _amount; // Effect (Too late!)
}
// GOOD: Effect before Interaction
function withdraw(uint _amount) public {
require(balances[msg.sender] >= _amount); // Check
balances[msg.sender] -= _amount; // Effect
(bool success, ) = msg.sender.call{value: _amount}(""); // Interaction
require(success);
}
3. Be Explicit with Visibility and Permissions
Auditors despise ambiguity. Every function and variable should have an explicitly defined visibility. Since Solidity 0.5.0, this is required, but the “Auditor Way” takes it further by practicing the Principle of Least Privilege.
-
- Use
externalinstead ofpublicif the function is only called from outside the contract (it’s also more gas-efficient).
- Use
-
- Use
privateorinternalfor any state variables that don’t need to be read directly by other contracts.
- Use
-
- Always use established Access Control libraries like OpenZeppelin’s
AccessControlorOwnable.
- Always use established Access Control libraries like OpenZeppelin’s
4. Guard Against Arithmetic Overflows
While Solidity 0.8.x has built-in overflow/underflow checks, auditors still look for unchecked blocks. Use unchecked only when you are 100% mathematically certain an overflow cannot occur, typically to save gas in loops. If an auditor sees unchecked, it’s the first place they hunt for bugs.
5. The Power of Invariant Testing
Auditors use “Invariants”—properties of a contract that must always be true. For example: “The total supply of tokens must always equal the sum of all individual balances.”
Before launching, write invariant tests using tools like Foundry or Echidna. If your code can reach a state where an invariant is broken, you have a critical vulnerability.
6. Documentation: The Auditor’s Roadmap
Code that is hard to read is hard to audit. Use NatSpec (Ethereum Natural Language Specification Format) to document your functions clearly. If an auditor can’t understand what a function is supposed to do, they can’t tell if it’s doing it correctly.
/// @notice Withdraws collateral from the vault
/// @dev Implements CEI pattern to prevent reentrancy
/// @param amount The amount of tokens to withdraw
function withdraw(uint amount) external { ... }
Conclusion: Security is a Process
Writing like an auditor means slowing down. It means valuing correctness over features and security over gas savings. Before you deploy to Mainnet, run your code through static analysis tools like Slither, perform a “rubber duck” audit where you explain every line to a colleague, and always, always have an external pair of eyes look at your logic.
Remember: In Solidity, you are writing the vault and the money simultaneously. Build it to be a fortress.