(Mastery Level β€” Security β€’ zk β€’ Cross-chain β€’ Deployment)


βš”οΈ Day 8 – Smart Contract Security & Auditing

β€œCode without security is like a vault without a lock.”


🎯 Objective


🧨 Top Vulnerabilities in Smart Contracts

1. Reentrancy Attack

Occurs when a contract calls another contract before updating its state β€” allowing the attacker to re-enter the function multiple times.

// ❌ Vulnerable
function withdraw(uint _amount) public {
    require(balances[msg.sender] >= _amount, "Not enough balance");
    (bool sent, ) = msg.sender.call{value: _amount}("");
    require(sent, "Failed");
    balances[msg.sender] -= _amount;
}

// βœ… Safe
function withdraw(uint _amount) public nonReentrant {
    require(balances[msg.sender] >= _amount, "Not enough balance");
    balances[msg.sender] -= _amount;
    (bool sent, ) = msg.sender.call{value: _amount}("");
    require(sent, "Failed");
}

Use ReentrancyGuard from OpenZeppelin:

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract SafeVault is ReentrancyGuard { ... }


2. Integer Overflow / Underflow

Before Solidity 0.8, arithmetic overflow/underflow caused bugs.

In 0.8+, checks are automatic β€” but still use unchecked carefully for optimization.