Back to Hub
CREATED BY CHAINLINK LABS

Circuit Breaker

Monitor on-chain events for anomalies, automatically pause contracts when thresholds are breached using a dual-trigger circuit breaker pattern.

Get the template
cre init --template=circuit-breaker-ts
View on GitHub

What This Template Does

This template demonstrates a dual-trigger circuit breaker pattern that monitors on-chain events for anomalies and automatically pauses contracts when thresholds are breached.

Two triggers work together:

  • LogTrigger โ€” Fires when ProtocolWithBreaker emits a PriceUpdated event. The workflow decodes the event data (newPrice, oldPrice, timestamp), reads on-chain state, calculates price deviation in basis points, and trips the breaker if deviation exceeds the threshold.
  • Cron โ€” Fires every 10 minutes for periodic health checks. Reads protocol state and logs current status for monitoring.

Both triggers converge on decision logic: if an anomaly is detected, a DON-signed report is sent via KeystoneForwarder to pause the protocol on-chain.

Key Technologies:

  • CRE (Chainlink Runtime Environment) โ€” Orchestrates dual-trigger workflows with DON consensus
  • LogTrigger + Cron โ€” Real-time event monitoring with async health checks
  • On-chain circuit breaker โ€” One-way pause by default (only CRE can unpause)

How It Works


Getting Started

1 Install dependencies
cd my-workflow && bun install && cd .. && cd contracts && bun install && cd ..
2 Run tests
cd my-workflow && bun test
3 Simulate the workflow

Simulate the Cron health check:

cre workflow simulate my-workflow --target staging-settings

Simulate the LogTrigger with an anomalous price event:

cre workflow simulate my-workflow --target staging-settings \
  --non-interactive \
  --trigger-index 0 \
  --evm-tx-hash 0x98dfb56db57cb0689e5e4092949d41431a283f52128066bc941b0c219d3f6203 \
  --evm-event-index 0
4 Deploy your own contract

Deploy ProtocolWithBreaker with constructor arguments: forwarder address, initial price, and deviation threshold in basis points (1000 = 10%):

forge create src/ProtocolWithBreaker.sol:ProtocolWithBreaker \
  --broadcast \
  --private-key <KEY> \
  --rpc-url <RPC> \
  --constructor-args 0x15fc6ae953e024d975e77382eeec56a9101f9f88 1000000000000000000 1000

Customization

  • Change threshold โ€” Update _deviationThresholdBps in the constructor args
  • Change schedule โ€” Edit schedule in config.staging.json
  • Add webhook alerts โ€” Use HTTPClient POST to send notifications when the breaker trips
  • Monitor different events โ€” Change the event listener to watch different contract events
  • Implement unpause logic โ€” Create a separate workflow to unpause the protocol

For full details, see the TypeScript README on GitHub.

Get the latest Chainlink content straight to your inbox.