solidityethereumsecuritybeginner

Pausable Emergency Stop

An emergency stop / circuit breaker pattern that lets authorized roles pause contract operations.

All snippets

Code

solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract PausableToken is Pausable, Ownable {
    mapping(address => uint256) public balances;

    function deposit() external payable whenNotPaused {
        balances[msg.sender] += msg.value;
    }

    function withdraw(uint256 amount) external whenNotPaused {
        require(balances[msg.sender] >= amount, "Insufficient");
        balances[msg.sender] -= amount;
        payable(msg.sender).transfer(amount);
    }

    function pause() external onlyOwner { _pause(); }
    function unpause() external onlyOwner { _unpause(); }
}

Explanation

The Pausable contract provides whenNotPaused and whenPaused modifiers. In an emergency, the owner can pause the contract to prevent withdrawals while a fix is deployed. This is a critical security pattern for DeFi.

pausablecircuit-breakeropenzeppelinsecurity