Overview
Circuit breakers are useful for pausing dApps and processes when adverse events are detected onchain. These circuit breakers can prevent loss of assets during volatile market events, unstable network conditions, or if systems that your dApp relies on become compromised.
The example circuit breaker contract is a highly configurable proof of concept that can be used with Data Feeds. It is capable of emitting events or calling custom functions based on predefined conditions, and it comes with an interactive UI that allows users to easily configure and manage the contract.
Objective
The Circuit Breaker repository contains an example implementation of a circuit breaker that can be used with any OCR price feed. You can monitor a given contract, specify the price when the circuit breaker will be triggered based on predefined conditions, and specify the underlying logic of what happens when the circuit breaker is triggered.
Before you begin
You can run this example on Linux, MacOS, or the Windows Subsystem for Linux.
Before you start this tutorial, install the required tools:
- Install git
- Run
git --versionto check the installation. You should see an output similar togit version x.x.x.
- Run
- Install Nodejs 16.0.0 or higher
- Run
node --versionto check the installation. You should see an output similar tov16.x.x.
- Run
- Install and configure a cryptocurrency wallet like MetaMask.
- Add the Avalanche Fuji testnet and LINK to your wallet:
Avalanche Fuji testnet
- Add testnet funds to your wallet. You will need both ERC-677 LINK and the gas currency for the chain where your contract is deployed. For this example, use Avalanche Fuji and testnet LINK. You can get both at faucets.chain.link.
Steps to implement
1 Setup the example
To set up this example, clone the source and install the required packages.
-
Clone the smartcontractkit/quickstarts-circuitbreaker repository and change directories:
git clone https://github.com/smartcontractkit/quickstarts-circuitbreaker.git && \ cd quickstarts-circuitbreaker -
Install the
chainlink/contractsnpm package:npm install @chainlink/contracts@0.6.1 --saveFor this tutorial, disregard the resulting
npmwarnings.
2 Deploy contracts
-
Open the
ExampleImplementation.solcontract in Remix: -
On the Solidity compiler tab, click Compile
ExampleImplementation.sol. -
On the Deploy and run transactions tab, select Injected Provider - MetaMask for the Environment field. Make sure the
ExampleImplementation.solcontract is selected in the Contract field. -
Scroll down to the Deploy section:

Click the dropdown carat to expand the Deploy section:

-
Enter the following input to the constructor:
Item Value _MAXBALANCE2000000000000_FEEDADDRESS0x31CF013A08c6Ac228C94551d535d5BAfE19c602a _EMERGENCYPOSSIBLEtrue- The
maxBalancein sats. For example,2000000000000for a max balance of $20,000. - The proxy contract address of the price feed. For example, the address for the BTC/USD price feed on Fuji is 0x31CF013A08c6Ac228C94551d535d5BAfE19c602a. Find other price feed addresses here.
- The
isEmergencyPossibleflag: set this totrueif you want the circuit breaking condition to be checked. You can turn off this check by setting this value tofalse.
- The
-
Click Deploy and confirm the transaction in your wallet.
Next, deploy the CircuitBreaker.sol contract:
-
Open the
CircuitBreaker.solcontract in Remix: -
On the Solidity compiler tab, click Compile
CircuitBreaker.sol. -
On the Deploy and run transactions tab, select Injected Provider - MetaMask for the Environment field. Make sure the
CircuitBreaker.solcontract is selected in the Contract field. -
There are no constructor inputs for this contract. Click Deploy and confirm the transaction in your wallet.
3 Register and fund an upkeep
Registering an upkeep on Chainlink Automation creates a smart contract that will run your circuit breaker contract.
-
Click Register new Upkeep.
-
Select the Custom logic trigger.
-
For Target contract address, input the address of your deployed
CircuitBreaker.solcontract. You can find this in your wallet's transaction history or copy it from Remix:

There may be a warning that says "Unable to verify if this is an Automation compatible contract." The CircuitBreaker.sol contract does implement the AutomationCompatibleInterface so you can disregard this warning. Click Next.
-
For Upkeep details, add the following:
- A name for your upkeep
- A starting balance of 2 testnet LINK
- The admin address and gas limit are prefilled for you.
- Although the Check data field is marked optional, it is required for this tutorial. You'll fill this in during the next step.
-
For your upkeep's Check data field, you need to ABI encode the address of your deployed
ExampleImplementation.solcontract.-
Navigate to the HashEx Online ABI Encoder.
-
Click Add argument and select Address as the argument type from the dropdown menu. 1. Enter the address of your deployed
ExampleImplementation.solcontract.
-
From the Encoded data field, copy the encoded address.
-
Navigate back to the Chainlink Automation app. In the Check Data field, enter
0xand paste in the ABI encoded address of theExampleImplementation.solcontract.
-
-
Click Register upkeep and confirm the transaction in your wallet.
4 Check emergency action
Now the Chainlink Automation network will watch your contract for these trigger parameters. If the price from the feed you provided is above the maxBalance threshold that was specified, the executeEmergencyAction() function will trigger. As defined in ExampleImplementation.sol, the function increments a counter:
/**
* executes emergency action
*/
function executeEmergencyAction() external {
counter += 1;
circuitbrokenflag = true;
emit emergencyActionPerformed(counter, feedAddress);
}
- Navigate to your upkeep in the Chainlink Automation app. If the
executeEmergencyAction()function is triggered, you will see "Perform upkeep" logs listed in the History section. - Navigate to Remix and expand the details for your deployed
ExampleImplementation.solcontract. Click the counter button to view the value of thecountervariable. - If the counter was incremented once, you should see
0: uint256: 1as the output:
5 Update the example implementation contract
If you want to update your ExampleImplementation.sol contract, you can redeploy it with updated values and then use the same Automation upkeep. For example, to update the maxBalance:
-
Redeploy the
ExampleImplementation.solcontract with your updatedmaxBalancevalue. -
ABI encode the address of your newly deployed
ExampleImplementation.solcontract. -
In the Chainlink Automation app, within the Actions menu for your existing upkeep, click Edit check data:

-
Enter
0x, paste the new ABI-encoded contract address, and click Change check data. MetaMask opens and prompts you to confirm the transaction.
You can use the same process to make other changes to your example implementation contract, like changing the logic in executeEmergencyAction() or changing the price feed that you're monitoring.
Cleanup
If you want to experiment further with this tutorial, you can pause the Automation upkeep so that it stops running while you work on updating your example implementation contract. Otherwise, you can cancel the upkeep to reclaim any unused testnet LINK back to your wallet. Both options are located in the Chainlink Automation app within the Actions menu on your upkeep's details page.