--- eip: 7265 title: Circuit Breaker description: Interface for a Circuit Breaker that triggers a temporary halt on protocol-wide token outflows when a threshold is exceeded. author: Diyahir Campos (@diyahir), Blagoj Dimovski (@b-d1), Philippe Dumonet (@Philogy), Meir Bank (@meirbank) discussions-to: https://ethereum-magicians.org/t/eip-7265-circuit-breaker-standard/14909 status: Draft type: Standards Track category: ERC created: 2023-07-03 requires: 20 --- ## Abstract This standard outlines a smart contract interface for a Circuit Breaker that triggers a temporary halt on protocol-wide token outflows when a threshold is exceeded for a predefined metric. This circuit breaker does not assume the structure of the underlying protocol, and mainly serves as a pass-through vehicle for token outflows. In order to maintain correct internal accounting for integrated protocols, and to provide maximum flexibility for developers, developers can specify if the circuit breaker contract should delay settlement and temporarily custody outflows during the cooldown period, or revert on attempted outflows. ## Motivation Security breaches have become pervasive in DeFi. Over $6 Billion dollars worth of assets were stolen in exploits. When something goes wrong, protocols are rapidly drained of the majority of TVL often in a matter of seconds. Although many protocols have smart contracts which are fully upgradeable by governance, in practice there is usually no time to upgrade or patch vulnerabilities before large amounts of TVL are drained. Despite this, it is practically feasible to set a circuit breaker to be tripped in cases where an unusually large amount of tokens are withdrawn from a protocol in a short period of time. This will allow teams to take the necessary steps to fix their protocols, and save a significant portion of locked funds from being stolen. This will not prevent hacks, rather it will extend the actionable period that protocols have to address the situation. ## Specification The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 and RFC 8174. ```solidity // SPDX-License-Identifier: CC0-1.0 pragma solidity 0.8.19; /// @title Circuit Breaker /// @dev See https://eips.ethereum.org/EIPS/eip-[EIP NUMBER] interface IERC7265 { /// @dev MUST be emitted in `registerAsset` when an asset is registered /// @param asset MUST be the address of the asset for which to set rate limit parameters. /// For any EIP-20 token, MUST be an EIP-20 token contract. /// For the native asset (ETH on mainnet), MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param metricThreshold The threshold metric which defines when a rate limit is triggered /// @param minAmountToLimit The minimum amount of nominal asset liquidity at which point rate limits can be triggered event AssetRegistered(address indexed asset, uint256 metricThreshold, uint256 minAmountToLimit); /// @dev MUST be emitted in `onTokenInflow` and `onNativeAssetInflow` during asset inflow into a protected contract /// @param token MUST be the address of the asset flowing in. /// For any EIP-20 token, MUST be an EIP-20 token contract. /// For the native asset (ETH on mainnet), MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param amount MUST equal the amount of asset transferred into the protected contract event AssetInflow(address indexed token, uint256 indexed amount); /// @dev MUST be emitted in `onTokenOutflow` and `onNativeAssetOutflow` when a rate limit is triggered /// @param asset MUST be the address of the asset triggering the rate limit. /// For any EIP-20 token, MUST be an EIP-20 token contract. /// For the native asset (ETH on mainnet), MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param timestamp MUST equal the block.timestamp at the time of rate limit breach event AssetRateLimitBreached(address indexed asset, uint256 timestamp); /// @dev MUST be emitted in `onTokenOutflow` and `onNativeAssetOutflow` when an asset is successfully withdrawn /// @param asset MUST be the address of the asset withdrawn. /// For any EIP-20 token, MUST be an EIP-20 token contract. /// For the native asset (ETH on mainnet), MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param recipient MUST be the address of the recipient withdrawing the assets /// @param amount MUST be the amount of assets being withdrawn event AssetWithdraw(address indexed asset, address indexed recipient, uint256 amount); /// @dev MUST be emitted in `claimLockedFunds` when a recipient claims locked funds /// @param asset MUST be the address of the asset claimed. /// For any EIP-20 token, MUST be an EIP-20 token contract. /// For the native asset (ETH on mainnet), MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param recipient MUST be the address of the recipient claiming the assets event LockedFundsClaimed(address indexed asset, address indexed recipient); /// @dev MUST be emitted in `setAdmin` when a new admin is set /// @param newAdmin MUST be the new admin address event AdminSet(address indexed newAdmin); /// @dev MUST be emitted in `startGracePeriod` when a new grace period is successfully started /// @param gracePeriodEnd MUST be the end timestamp of the new grace period event GracePeriodStarted(uint256 gracePeriodEnd); /// @notice Register rate limit parameters for a given asset /// @dev Each asset that will be rate limited MUST be registered using this function, including the native asset (ETH on mainnet). /// If an asset is not registered, it will not be subject to rate limiting or circuit breaking and unlimited immediate withdrawals MUST be allowed. /// MUST revert if the caller is not the current admin. /// MUST revert if the asset has already been registered. /// @param _asset The address of the asset for which to set rate limit parameters. /// To set the rate limit parameters for any EIP-20 token, MUST be an EIP-20 token contract. /// To set rate limit parameters For the native asset, MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param _metricThreshold The threshold metric which defines when a rate limit is triggered. /// This is intentionally left open to allow for various implementations, including percentage-based (see reference implementation), nominal, and more. /// MUST be greater than 0. /// @param _minAmountToLimit The minimum amount of nominal asset liquidity at which point rate limits can be triggered. /// This limits potential false positives triggered either by minor assets with low liquidity or by low liquidity during early stages of protocol launch. /// Below this amount, withdrawals of this asset MUST NOT trigger a rate limit. /// However, if a rate limit is triggered, assets below the minimum trigger amount to limit MUST still be locked. function registerAsset( address _asset, uint256 _metricThreshold, uint256 _minAmountToLimit ) external; /// @notice Modify rate limit parameters for a given asset /// @dev MAY be used only after registering an asset. /// MUST revert if asset is not previously registered with the `registerAsset` method. /// MUST revert if the caller is not the current admin. /// @param _asset The address of the asset contract for which to set rate limit parameters. /// To update the rate limit parameters for any EIP-20 token, MUST be an EIP-20 token contract. /// To update the rate limit parameters For the native asset (ETH on mainnet), MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param _metricThreshold The threshold metric which defines when a rate limit is triggered. /// This is left open to allow for various implementations, including percentage-based (see reference implementation), nominal, and more. /// MUST be greater than 0. /// @param _minAmountToLimit The minimum amount of nominal asset liquidity at which point rate limits can be triggered. /// This limits potential false positives caused both by minor assets with low liquidity and by low liquidity during early stages of protocol launch. /// Below this amount, withdrawals of this asset MUST NOT trigger a rate limit. /// However, if a rate limit is triggered, assets below the minimum amount to limit MUST still be locked. function updateAssetParams( address _asset, uint256 _metricThreshold, uint256 _minAmountToLimit ) external; /// @notice Record EIP-20 token inflow into a protected contract /// @dev This method MUST be called from all protected contract methods where an EIP-20 token is transferred in from a user. /// MUST revert if caller is not a protected contract. /// MUST revert if circuit breaker is not operational. /// @param _token MUST be an EIP-20 token contract /// @param _amount MUST equal the amount of token transferred into the protected contract function onTokenInflow(address _token, uint256 _amount) external; /// @notice Record EIP-20 token outflow from a protected contract and transfer tokens to recipient if rate limit is not triggered /// @dev This method MUST be called from all protected contract methods where an EIP-20 token is transferred out to a user. /// Before calling this method, the protected contract MUST transfer the EIP-20 tokens to the circuit breaker contract. /// For an example, see ProtectedContract.sol in the reference implementation. /// MUST revert if caller is not a protected contract. /// MUST revert if circuit breaker is not operational. /// If the token is not registered, this method MUST NOT revert and MUST transfer the tokens to the recipient. /// If a rate limit is not triggered or the circuit breaker is in grace period, this method MUST NOT revert and MUST transfer the tokens to the recipient. /// If a rate limit is triggered and the circuit breaker is not in grace period and `_revertOnRateLimit` is TRUE, this method MUST revert. /// If a rate limit is triggered and the circuit breaker is not in grace period and `_revertOnRateLimit` is FALSE and caller is a protected contract, this method MUST NOT revert. /// If a rate limit is triggered and the circuit breaker is not in grace period, this method MUST record the locked funds in the internal accounting of the circuit breaker implementation. /// @param _token MUST be an EIP-20 token contract /// @param _amount MUST equal the amount of tokens transferred out of the protected contract /// @param _recipient MUST be the address of the recipient of the transferred tokens from the protected contract /// @param _revertOnRateLimit MUST be TRUE to revert if a rate limit is triggered or FALSE to return without reverting if a rate limit is triggered (delayed settlement) function onTokenOutflow( address _token, uint256 _amount, address _recipient, bool _revertOnRateLimit ) external; /// @notice Record native asset (ETH on mainnet) inflow into a protected contract /// @dev This method MUST be called from all protected contract methods where native asset is transferred in from a user. /// MUST revert if caller is not a protected contract. /// MUST revert if circuit breaker is not operational. /// @param _amount MUST equal the amount of native asset transferred into the protected contract function onNativeAssetInflow(uint256 _amount) external; /// @notice Record native asset (ETH on mainnet) outflow from a protected contract and transfer native asset to recipient if rate limit is not triggered /// @dev This method MUST be called from all protected contract methods where native asset is transferred out to a user. /// When calling this method, the protected contract MUST send the native asset to the circuit breaker contract in the same call. /// For an example, see ProtectedContract.sol in the reference implementation. /// MUST revert if caller is not a protected contract. /// MUST revert if circuit breaker is not operational. /// If native asset is not registered, this method MUST NOT revert and MUST transfer the native asset to the recipient. /// If a rate limit is not triggered or the circuit breaker is in grace period, this method MUST NOT revert and MUST transfer the native asset to the recipient. /// If a rate limit is triggered and the circuit breaker is not in grace period and `_revertOnRateLimit` is TRUE, this method MUST revert. /// If a rate limit is triggered and the circuit breaker is not in grace period and `_revertOnRateLimit` is FALSE and caller is a protected contract, this method MUST NOT revert. /// If a rate limit is triggered and the circuit breaker is not in grace period, this method MUST record the locked funds in the internal accounting of the circuit breaker implementation. /// @param _recipient MUST be the address of the recipient of the transferred native asset from the protected contract /// @param _revertOnRateLimit MUST be TRUE to revert if a rate limit is triggered or FALSE to return without reverting if a rate limit is triggered (delayed settlement) function onNativeAssetOutflow(address _recipient, bool _revertOnRateLimit) external payable; /// @notice Allow users to claim locked funds when rate limit is resolved /// @dev When a asset is transferred out during a rate limit period, the settlement may be delayed and the asset custodied in the circuit breaker. /// This method allows users to claim funds that were delayed in settlement after the rate limit is resolved or a grace period is activated. /// MUST revert if the recipient does not have locked funds for a given asset. /// MUST revert if circuit breaker is rate limited or is not operational. /// MUST transfer tokens or native asset (ETH on mainnet) to the recipient on successful call. /// MUST update internal accounting of circuit breaker implementation to reflect withdrawn balance on successful call. /// @param _asset To claim locked EIP-20 tokens, this MUST be an EIP-20 token contract. /// To claim native asset, this MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param _recipient MUST be the address of the recipient of the locked funds from the circuit breaker function claimLockedFunds(address _asset, address _recipient) external; /// @notice Set the admin of the contract to govern the circuit breaker /// @dev The admin SHOULD represent the governance contract of the protected protocol. /// The admin has authority to: withdraw locked funds, set grace periods, register asset parameters, update asset parameters, /// set new admin, override rate limit, add protected contracts, remove protected contracts. /// MUST revert if the caller is not the current admin. /// MUST revert if `_newAdmin` is address(0). /// MUST update the circuit breaker admin to the new admin in the stored state of the implementation on successful call. /// @param _newAdmin MUST be the address of the new admin function setAdmin(address _newAdmin) external; /// @notice Override a rate limit /// @dev This method MAY be called when the protocol admin (typically governance) is certain that a rate limit is the result of a false positive. /// MUST revert if caller is not the current admin. /// MUST allow the grace period to extend for the full withdrawal period to not trigger the rate limit again if the rate limit is removed just before the withdrawal period ends. /// MUST revert if the circuit breaker is not currently rate limited. function overrideRateLimit() external; /// @notice Override an expired rate limit /// @dev This method MAY be called by anyone once the cooldown period is complete. /// MUST revert if the cooldown period is not complete. /// MUST revert if the circuit breaker is not currently rate limited. function overrideExpiredRateLimit() external; /// @notice Add new protected contracts /// @dev MUST be used to add protected contracts. Protected contracts MUST be part of your protocol. /// Protected contracts have the authority to trigger rate limits and withdraw assets. /// MUST revert if caller is not the current admin. /// MUST store protected contracts in the stored state of the circuit breaker implementation. /// @param _ProtectedContracts an array of addresses of protected contracts to add function addProtectedContracts(address[] calldata _ProtectedContracts) external; /// @notice Remove protected contracts /// @dev MAY be used to remove protected contracts. Protected contracts MUST be part of your protocol. /// Protected contracts have the authority to trigger rate limits and withdraw assets. /// MUST revert if caller is not the current admin. /// MUST remove protected contracts from stored state in the circuit breaker implementation. /// @param _ProtectedContracts an array of addresses of protected contracts to remove function removeProtectedContracts(address[] calldata _ProtectedContracts) external; /// @notice Set a custom grace period /// @dev MAY be called by admin to set a custom grace period during which rate limits will not be active. /// MUST revert if caller is not the current admin. /// MUST start grace period until end timestamp. /// @param _gracePeriodEndTimestamp The ending timestamp of the grace period /// MUST be greater than the current block.timestamp function startGracePeriod(uint256 _gracePeriodEndTimestamp) external; /// @notice Lock the circuit breaker /// @dev MAY be called by admin to lock the circuit breaker /// While the protocol is not operational: inflows, outflows, and claiming locked funds MUST revert function markAsNotOperational() external; /// @notice Migrates locked funds after exploit for recovery /// @dev MUST revert if the protocol is operational. /// MUST revert if caller is not the current admin. /// MUST transfer assets to recovery recipient on successful call. /// @param _assets Array of assets to recover. /// For any EIP-20 token, MUST be an EIP-20 token contract. /// For the native asset (ETH on mainnet), MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @param _recoveryRecipient The address of the recipient to receive recovered funds. Often this will be a multisig wallet. function migrateFundsAfterExploit( address[] calldata _assets, address _recoveryRecipient ) external; /// @notice View funds locked for a given recipient and asset /// @param recipient The address of the recipient /// @param asset To view the balance of locked EIP-20 tokens, this MUST be an EIP-20 token contract. /// To claim native ETH, this MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @return amount The locked balance for the given recipient and asset function lockedFunds(address recipient, address asset) external view returns (uint256 amount); /// @notice Check if a given address is a protected contract /// @param account The address of the contract to check /// @return protectionActive MUST be TRUE if the contract is protected, FALSE if it is not protected function isProtectedContract(address account) external view returns (bool protectionActive); /// @notice View the admin of the circuit breaker /// @dev This SHOULD be the governance contract for your protocol /// @return admin The admin of the circuit breaker function admin() external view returns (address); /// @notice Check is the circuit breaker is rate limited /// @return isRateLimited MUST be TRUE if the circuit breaker is rate limited, FALSE if it is not rate limited function isRateLimited() external view returns (bool); /// @notice View the rate limit cooldown period /// @dev The duration of a rate limit once triggered /// @return rateLimitCooldownPeriod The rate limit cooldown period function rateLimitCooldownPeriod() external view returns (uint256); /// @notice View the last rate limit timestamp /// @return lastRateLimitTimestamp MUST return the last rate limit timestamp function lastRateLimitTimestamp() external view returns (uint256); /// @notice View the grace period end timestamp /// @return gracePeriodEndTimestamp MUST return the grace period end timestamp function gracePeriodEndTimestamp() external view returns (uint256); /// @notice Check if a rate limit is currently triggered for a given asset /// @param _asset To check if triggered for EIP-20 tokens, this MUST be an EIP-20 token contract. /// To check if triggered For the native asset (ETH on mainnet), this MUST be address 0x0000000000000000000000000000000000000001 equivalent to address(1). /// @return isRateLimitTriggered MUST return TRUE if a rate limit is currently triggered for given asset, FALSE if not function isRateLimitTriggered(address _asset) external view returns (bool); /// @notice Check if the circuit breaker is currently in grace period /// @return isInGracePeriod MUST return TRUE if the circuit breaker is currently in grace period, FALSE otherwise function isInGracePeriod() external view returns (bool); /// @notice Check if the circuit breaker is operational /// @return isOperational MUST return TRUE if the circuit breaker is operational (not exploited), FALSE otherwise function isOperational() external view returns (bool); } ``` ## Rationale ### Tracking token inflows to support percentage based rate limits Circuit breakers implementations may support percentage based rate limiting. Percentage-based rate limiting is the recommended approach for protected protocols to use. Compared to having fixed notional token rate limits, this avoids the possibility that a team member will incorrectly update a token notional limit as TVL grows. In practice rate limits are always relative to total liquidity, so being able to know the total liquidity in the protected protocol is essential. In order to support percentage-based rate limiting, protocols integrating the standard must record notional token inflows using the applicable hook function. ### Choosing between delayed settlement or reverting on rate limit Protocols can decide if withdrawal attempts should revert or custody tokens and delay settlement in the event of a circuit breaker trigger. It is possible that protocols can also expose this option to users and integration developers. Delayed settlement is beneficial for cases where token outflow or token withdrawal logic is mixed with other types of logic, for instance a withdrawal function that also closes a financial position. Reverting on such a function during a false positive rate limit would render users unable to close their positions and could potentially lead to liquidations or other negative externalities. On the other hand, third party integrations could assume that if a token withdrawal function is executed successfully that the full amount will be credited to them immediately, and therefore delayed settlement could create internal accounting errors. In those cases, reverting is preferred. ### Mandatory inclusion of post-hack recovery methods While it would theoretically be possible to recover locked funds by upgrading the circuit breaker contract, the ability for the admin (representing protocol governance) to recover locked funds in the circuit breaker is mandatory in this standard to ensure that all implementations have this ability and funds are never locked in a circuit breaker due to the absence of upgradeability. ### Registering token parameters By registering rate limit parameters for each token protected, protocols can set up different parameters for different tokens. Protocols can also opt to rate limit certain tokens, and not rate limit certain tokens. Tokens that are not registered will never be locked in the circuit breaker. The rationale for this is to give protocol governors the maximum flexibility in setting rate limits for their circuit breaker. ### Protecting numerous protocol contracts The Circuit Breaker is designed to protect numerous contracts under the realm of a single protocol. This is important for protocols which have assets spread among many contracts, to use separate Circuit Breakers would not be effective. ### Extendability The Circuit Breaker is designed as a minimum base standard. We expect many extensions to be built on the standard, including tokenized lock positions, compatibility with other standards, third party integrations and others. ### Minimum amount rate limited When token balances are low during initial bootstrapping stages of a protocol lifecycle, there may be large fluctuations in total tokens locked. Governors can specify the minimum amount of total liquidity required for an asset to trigger a rate limit, and until that limit is reached, the asset will not trigger new rate limits. However, if a rate limit is triggered, the asset will still be locked. ## Backwards Compatibility No backward compatibility issues found. ## Test Cases Test cases can be found [here](../assets/eip-7265/test/core/user/CircuitBreakerUserOps.t.sol), [here](../assets/eip-7265/test/core/admin/CircuitBreakerEmergencyOps.t.sol), and [here](../assets/eip-7265/test/core/admin/CircuitBreakerAdminOps.t.sol). ## Reference Implementation Reference Implementation can be found [here](../assets/eip-7265/src/core/CircuitBreaker.sol). ## Security Considerations ### Protocols Compatibility When exploring the Circuit Breaker, it is important to carefully assess and understand which protocols are suitable for its usage. Considerations should include the protocol's architecture, upgradability, and contract interactions to ensure seamless integration and compatibility. - DeFi in Nature - Upgradeable - Able to delegate CB admin responsibilities to DAO, Multisig, or other party - Token notional drawdown events should not be common events (Lotteries) ### Integration, Testing, Setup Proper Circuit Breaker Integration: The Circuit Breaker should be properly integrated into the protocol recording all asset inflows and outflows. We’ve made this easy by inheriting the ‘ProtectedContract’ contract. Thorough testing and verification should be performed to ensure seamless interactions with the protocol's critical functions and to identify any potential conflicts or vulnerabilities. Admin Address Selection: The selection of the admin address responsible for triggering the Circuit Breaker is critical, as it holds protocol-critical implications. It is crucial to exercise caution and implement appropriate measures to ensure the admin address remains secure and inaccessible to unauthorized parties. CB Token Parameters: Selecting the appropriate parameters for the Circuit Breaker token is vital to avoid false positives and maximize returns in the event of a hack. ### Social Consensus Definition of 'Hack': Hacks are inherently social constructs, and their identification relies on contextual observation. While a hack does not perform actions beyond what the protocol permits, it often involves ad-hoc classification based on factors such as flashloans, bug exploits, and similar occurrences. Although hacks can be challenging to precisely define, there is a collective understanding within the community to recognize and discern them when encountered. Remediation Consensus: Reaching a consensus on remediation is vital in order to identify the most suitable course of action when the Circuit Breaker is triggered. The determination of the pro-rata distribution of remaining funds is dependent on understanding the liabilities of the protocol and requires a collective agreement within the community. It is imperative to thoroughly address these security considerations during the design, implementation, and deployment of the Circuit Breaker. By doing so, you can help minimize risks, enhance the overall security posture, and ensure a resilient response to potential exploits. ## Copyright Copyright and related rights waived via [CC0](../LICENSE.md).