Dev.to Security 🔐 Cybersecurity 👁 0 📖 1 min read

5 Critical Smart Contract Checks Before Mainnet Deployment (Solidity & Foundry)

During high-volatility market swings—like the recent geopolitical shocks pushing $BTC and $ETH through heavy liquidation cascades—DeFi protocols face extreme on-chain stress. Even minor logic assumptions in Solidity cont

During high-volatility market swings—like the recent geopolitical shocks pushing $BTC and $ETH through heavy liquidation cascades—DeFi protocols face extreme on-chain stress. Even minor logic assumptions in Solidity contracts can lead to critical fund loss.

Before pushing any smart contract to mainnet or submitting code for an external audit, run through these 5 essential security checks:

1. ERC-4626 First-Depositor Share Inflation

If your vault calculates shares using raw totalAssets() / totalSupply(), an attacker can deposit 1 wei, donate tokens directly to the vault, and manipulate the share price to round subsequent deposits down to zero.

  • Fix: Implement OpenZeppelin's virtual shares/assets offset or mint dead shares on initialization.

2. Read-Only Reentrancy on View Functions

Adding nonReentrant to deposit() and withdraw() is not enough if an external protocol reads your vault's share price (getPricePerFullShare()) mid-execution while balances are temporarily out of sync.

  • Fix: Ensure state updates happen strictly before external calls (Checks-Effects-Interactions) or lock view getters during active state transitions.

3. Stale Chainlink Oracle Data & L2 Sequencer Uptime

Calling latestRoundData() without validation exposes your lending or perp protocol to stale prices during network congestion.

  • Fix: Always verify updatedAt > 0, enforce a strict heartbeat threshold (block.timestamp - updatedAt <= MAX_DELAY), and check the L2 Sequencer Uptime Feed on Arbitrum/Optimism/Base.

4. Non-Standard ERC-20 Edge Cases

Not every token behaves like standard OpenZeppelin ERC-20:

  • Fee-on-transfer tokens: Always measure balanceAfter - balanceBefore instead of trusting the input amount.
  • Missing return booleans (e.g., USDT): Always use SafeERC20 (safeTransfer, safeTransferFrom).

5. Stateful Invariant Fuzzing Over Unit Tests

Unit tests only check paths you already thought of. Stateful invariant testing in Foundry throws thousands of randomized multi-actor sequences at your protocol to verify that core solvency rules never break.

Need a Complete Pre-Audit Workflow?

To streamline pre-deployment reviews, I compiled a 45-Point Smart Contract Pre-Audit Checklist alongside a ready-to-use Foundry Vault Invariant & Solvency Test Template (VaultInvariant.t.sol) using ghost variables.

You can grab the full kit here:
👉 ZeroCentPlus Smart Contract Pre-Audit & Foundry Security Kit ($29)

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.