Smart Contract Pause System for Anomaly Detection

Manual reaction to anomalies in DeFi protocols is too slow, and every second of delay can cost millions. We develop a smart contract pause system that instantly stops fund leakage during suspicious activity without blocking legitimate operations. Our team delivers the project turnkey—from audit and detector configuration to implementation and ongoing support, ensuring reliable protection for your protocol.

Blockchain Development Services

Frequently Asked Questions

Latest works

  • Development of a web application for FEEDME
    Development of a web application for FEEDME
    1335
  • Development of an online store for the company FURNORO
    Development of an online store for the company FURNORO
    1293
  • B2B Advance company logo design
    B2B Advance company logo design
    738
  • Development of a web application for Enviok
    Development of a web application for Enviok
    1031
  • AIDER company logo development
    AIDER company logo development
    978
  • CRM development for Chasseurs
    CRM development for Chasseurs
    1087

After high-profile DeFi protocol exploits, it became clear: manual reaction to anomalies is too slow. Automatic smart contract pausing is the only way to stop a leak within seconds. But it must not block legitimate operations. Our engineers, with 5+ years of DeFi experience and 15+ delivered projects, offer a system that combines on-chain detectors and off-chain monitoring with over 99.9% accuracy. The average saving from preventing a single exploit exceeds $1 million — that's recent statistics.

How the smart contract pause system works on anomalies

The goal is to build a system that pauses the contract on real anomalies with minimal false positives, and does not itself become an attack vector. We use a combination of on-chain detectors and off-chain monitoring via OpenZeppelin Defender and Forta Network, ensuring reliability and transparency. The cost of error is high: exploit damage can reach $100 million.

How to set up automatic smart contract pausing

OpenZeppelin Pausable is the standard starting point. The source code is in OpenZeppelin Contracts.

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

contract ProtectedVault is Pausable, AccessControl {
    bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
    bytes32 public constant GUARDIAN_ROLE = keccak256("GUARDIAN_ROLE");

    function pause() external onlyRole(GUARDIAN_ROLE) {
        _pause();
    }

    function unpause() external onlyRole(DEFAULT_ADMIN_ROLE) {
        _unpause();
    }

    function deposit(uint256 amount) external whenNotPaused {
        // ...
    }

    function withdraw(uint256 amount) external whenNotPaused {
        // ...
    }
}

Key point: different roles for pause and unpause. An automated guardian could be compromised or err, but only humans via multisig/governance can unpause. That asymmetry is intentional.

What anomalies we detect

TVL anomaly and large transactions — developing the pause system

If more than X% of TVL leaves the contract within N blocks — that's a signal. We also monitor individual transactions exceeding 5% of TVL. Instead of automatic pause on these, we emit events for off-chain analysis.

contract AnomalyDetector {
    uint256 public constant MAX_TVL_DROP_BPS = 1000; // 10% per period
    uint256 public constant MONITORING_WINDOW = 100; // blocks

    function checkTVLAnomaly(uint256 currentTVL) internal {
        // ...
    }

    modifier checkWithdrawAnomaly(uint256 amount) {
        uint256 tvl = totalAssets();
        if (tvl > 0 && (amount * 10000 / tvl) > 500) { // 5% TVL
            emit LargeWithdrawal(msg.sender, amount, tvl);
        }
        _;
    }
}

Reentrancy detection on-chain

Complementing the standard nonReentrant — when a reentrancy attempt is detected, the contract pauses, not just reverts.

uint256 private _callDepth;

modifier noDeepCalls() {
    _callDepth++;
    if (_callDepth > 1) {
        _triggerPause("Reentrancy detected");
        revert("Reentrancy");
    }
    _;
    _callDepth--;
}

Why off-chain monitoring is more efficient

On-chain detectors are limited: they only see what happens in the current transaction. A more powerful pattern is off-chain monitoring + privileged pause transaction. We use OpenZeppelin Defender and Forta Network — this reduces reaction time by 5x compared to on-chain only.

Criterion On-chain Off-chain
Reaction speed ~1 block (12-15 s) 2-5 s (relayer)
Accuracy Medium (false positives 5-10%) High (<0.5%)
Cost Low (gas) Medium (Defender subscription)
Flexibility Hard to update Easy (update Autotask)

OpenZeppelin Defender

OZ Defender Sentinel + Autotask is the standard stack:

const { DefenderRelayProvider, DefenderRelaySigner } = require('@openzeppelin/defender-relay-client/lib/ethers');
exports.handler = async function(credentials) {
  const provider = new DefenderRelayProvider(credentials);
  const signer = new DefenderRelaySigner(provider, credentials, { speed: 'fast' });
  const contract = new ethers.Contract(VAULT_ADDRESS, VAULT_ABI, signer);
  const tvl = await contract.totalAssets();
  const threshold = await contract.pauseThreshold();
  if (tvl < threshold) {
    const tx = await contract.pause();
    await tx.wait();
  }
};

Forta Network

Forta is a decentralized detection bot network. Alerts are integrated into Defender via webhook. For a precise assessment of your scenario, get a consultation from our engineers.

How circuit breaker works

A more flexible pattern: not a full pause, but a circuit breaker — temporary operation limits when an anomaly occurs. The concept is borrowed from Circuit breaker design pattern.

contract CircuitBreaker {
    enum Status { Normal, Restricted, Paused }
    Status public status;
    uint256 public dailyWithdrawLimit;
    uint256 public dailyWithdrawn;
    uint256 public lastResetDay;

    function withdraw(uint256 amount) external {
        require(status != Status.Paused, "Paused");
        if (status == Status.Restricted) {
            require(amount <= restrictedWithdrawLimit, "Exceeds restricted limit");
        }
        // ...
    }
}

Advantage: when the daily limit is exceeded, the protocol does not pause — it simply rejects transactions that exceed the limit. Users can continue operating within normal volume. MakerDAO, Compound, Aave use similar mechanisms.

Step-by-step implementation guide

  1. Import Pausable and AccessControl into your contract, set up guardian and admin roles.
  2. Add on-chain detectors: TVL drop, large transactions, reentrancy detection.
  3. Connect off-chain monitoring: configure Defender Sentinel on LargeWithdrawal and TVL drop events, create an Autotask for automatic pause.
  4. Integrate Forta Network: set up detection bots and webhook to call pause on anomalies.
  5. Test scenarios: normal operation, false positives, attacks. Use formal verification and fuzzing.

What's included in the work

Component Description Timeline
Basic Pausable with AccessControl Role model, separation of pause/unpause 1 week
On-chain anomaly detectors TVL, large transactions, reentrancy 2 weeks
Off-chain monitoring (Defender/Forta) Setting up Sentinels, Autotasks, bots 1-2 weeks
Circuit breaker Limit restrictions instead of full pause 1-2 weeks
Pause protection mechanisms Time-limited, multisig, emergency unpause 1 week
Testing and audit Formal verification, fuzzing (Echidna) 2-3 weeks

Development timelines: basic system (Pausable + Defender monitoring) — 2-3 weeks. Full system with circuit breaker, Forta, and governance — 5-7 weeks. We guarantee transparency at every stage.

To protect your DeFi protocol, get a consultation — we'll evaluate your project for free. Contact us. Order development of a pause system for your contract.