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
- Import Pausable and AccessControl into your contract, set up guardian and admin roles.
- Add on-chain detectors: TVL drop, large transactions, reentrancy detection.
- Connect off-chain monitoring: configure Defender Sentinel on LargeWithdrawal and TVL drop events, create an Autotask for automatic pause.
- Integrate Forta Network: set up detection bots and webhook to call pause on anomalies.
- 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.







