PauseLib

Description:

Smart contract deployed on Ethereum with Factory features.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

{{
  "language": "Solidity",
  "sources": {
    "PauseLib.sol": {
      "content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

library PauseLib {

    event EmergencyPaused(address indexed caller, string reason);
    event EmergencyUnpaused(address indexed caller);
    event CircuitBreakerTriggered(string reason, uint256 timestamp);
    event AutoPauseTriggered(string condition, uint256 value, uint256 threshold);

    struct PauseState {
        bool isPaused;
        uint256 pausedAt;
        address pausedBy;
        string pauseReason;
        uint256 unpauseAfter;
    }

    struct CircuitBreaker {
        uint256 lastTriggerTime;
        uint256 triggerCount;
        uint256 windowStart;
        uint256 maxTriggers;
        uint256 windowDuration;
        bool isActive;
    }

    struct AutoPauseCondition {
        string name;
        uint256 threshold;
        uint256 currentValue;
        bool isEnabled;
        uint256 cooldownPeriod;
        uint256 lastTriggered;
    }

    function emergencyPause(
        PauseState storage pauseState,
        address caller,
        string memory reason
    ) internal {
        require(!pauseState.isPaused, "Already paused");

        pauseState.isPaused = true;
        pauseState.pausedAt = block.timestamp;
        pauseState.pausedBy = caller;
        pauseState.pauseReason = reason;
        pauseState.unpauseAfter = 0;

        emit EmergencyPaused(caller, reason);
    }

    function timedPause(
        PauseState storage pauseState,
        address caller,
        string memory reason,
        uint256 duration
    ) internal {
        require(!pauseState.isPaused, "Already paused");
        require(duration > 0 && duration <= 24 hours, "Invalid duration");

        pauseState.isPaused = true;
        pauseState.pausedAt = block.timestamp;
        pauseState.pausedBy = caller;
        pauseState.pauseReason = reason;
        pauseState.unpauseAfter = block.timestamp + duration;

        emit EmergencyPaused(caller, reason);
    }

    function emergencyUnpause(
        PauseState storage pauseState,
        address caller
    ) internal {
        require(pauseState.isPaused, "Not paused");

        require(
            pauseState.pausedBy == caller ||
            (pauseState.unpauseAfter > 0 && block.timestamp >= pauseState.unpauseAfter),
            "Cannot unpause"
        );

        pauseState.isPaused = false;
        pauseState.pausedAt = 0;
        pauseState.pausedBy = address(0);
        pauseState.pauseReason = "";
        pauseState.unpauseAfter = 0;

        emit EmergencyUnpaused(caller);
    }

    function initCircuitBreaker(
        CircuitBreaker storage breaker,
        uint256 maxTriggers,
        uint256 windowDuration
    ) internal {
        breaker.maxTriggers = maxTriggers;
        breaker.windowDuration = windowDuration;
        breaker.windowStart = block.timestamp;
        breaker.isActive = true;
    }

    function triggerCircuitBreaker(
        CircuitBreaker storage breaker,
        PauseState storage pauseState,
        string memory reason
    ) internal returns (bool shouldPause) {
        if (!breaker.isActive) return false;

        uint256 currentTime = block.timestamp;

        if (currentTime >= breaker.windowStart + breaker.windowDuration) {
            breaker.windowStart = currentTime;
            breaker.triggerCount = 0;
        }

        breaker.triggerCount++;
        breaker.lastTriggerTime = currentTime;

        emit CircuitBreakerTriggered(reason, currentTime);

        if (breaker.triggerCount >= breaker.maxTriggers) {
            emergencyPause(pauseState, address(this),
                string(abi.encodePacked("Circuit breaker: ", reason)));
            return true;
        }

        return false;
    }

    function checkAutoPause(
        AutoPauseCondition storage condition,
        uint256 currentValue,
        PauseState storage pauseState
    ) internal returns (bool triggered) {
        if (!condition.isEnabled) return false;

        uint256 currentTime = block.timestamp;

        if (currentTime < condition.lastTriggered + condition.cooldownPeriod) {
            return false;
        }

        condition.currentValue = currentValue;

        if (currentValue >= condition.threshold) {
            condition.lastTriggered = currentTime;

            string memory reason = string(abi.encodePacked(
                "Auto-pause: ", condition.name, " exceeded threshold"
            ));

            emergencyPause(pauseState, address(this), reason);

            emit AutoPauseTriggered(condition.name, currentValue, condition.threshold);

            return true;
        }

        return false;
    }

    function requireNotPaused(PauseState storage pauseState) internal view {

        if (pauseState.isPaused &&
            pauseState.unpauseAfter > 0 &&
            block.timestamp >= pauseState.unpauseAfter) {

            return;
        }

        require(!pauseState.isPaused,
            string(abi.encodePacked("Contract paused: ", pauseState.pauseReason)));
    }

    function canUnpause(
        PauseState storage pauseState,
        address caller
    ) internal view returns (bool) {
        if (!pauseState.isPaused) return false;

        if (pauseState.pausedBy == caller) return true;

        if (pauseState.unpauseAfter > 0 && block.timestamp >= pauseState.unpauseAfter) {
            return true;
        }

        return false;
    }

    function getPauseInfo(PauseState storage pauseState)
        internal
        view
        returns (
            bool isPaused,
            uint256 pausedAt,
            address pausedBy,
            string memory reason,
            uint256 timeUntilUnpause
        )
    {
        isPaused = pauseState.isPaused;
        pausedAt = pauseState.pausedAt;
        pausedBy = pauseState.pausedBy;
        reason = pauseState.pauseReason;

        if (pauseState.unpauseAfter > 0 && block.timestamp < pauseState.unpauseAfter) {
            timeUntilUnpause = pauseState.unpauseAfter - block.timestamp;
        } else {
            timeUntilUnpause = 0;
        }
    }

    function getCircuitBreakerStats(CircuitBreaker storage breaker)
        internal
        view
        returns (
            uint256 triggerCount,
            uint256 maxTriggers,
            uint256 windowTimeLeft,
            bool isActive
        )
    {
        triggerCount = breaker.triggerCount;
        maxTriggers = breaker.maxTriggers;
        isActive = breaker.isActive;

        uint256 windowEnd = breaker.windowStart + breaker.windowDuration;
        if (block.timestamp < windowEnd) {
            windowTimeLeft = windowEnd - block.timestamp;
        } else {
            windowTimeLeft = 0;
        }
    }

    function configureCircuitBreaker(
        CircuitBreaker storage breaker,
        uint256 maxTriggers,
        uint256 windowDuration,
        bool isActive
    ) internal {
        require(maxTriggers > 0, "Invalid max triggers");
        require(windowDuration > 0, "Invalid window duration");

        breaker.maxTriggers = maxTriggers;
        breaker.windowDuration = windowDuration;
        breaker.isActive = isActive;

        breaker.windowStart = block.timestamp;
        breaker.triggerCount = 0;
    }

    function configureAutoPause(
        AutoPauseCondition storage condition,
        string memory name,
        uint256 threshold,
        bool isEnabled,
        uint256 cooldownPeriod
    ) internal {
        condition.name = name;
        condition.threshold = threshold;
        condition.isEnabled = isEnabled;
        condition.cooldownPeriod = cooldownPeriod;
    }
}"
    }
  },
  "settings": {
    "optimizer": {
      "enabled": true,
      "runs": 200
    },
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    },
    "remappings": []
  }
}}

Tags:
Factory|addr:0xb510e9879ec292356a2876aecd819e2c2d8816de|verified:true|block:23526111|tx:0x61b84134685622f19f08fa5a38250db3cf9b17525a8185a4a776fd20f7acde4e|first_check:1759846519

Submitted on: 2025-10-07 16:15:19

Comments

Log in to comment.

No comments yet.