Skip to content

DeFi-Circuit-Breaker/v1-core

 
 

Repository files navigation

DeFi Circuit Breaker: Token Rate-limits for your DeFi Protocol

Features

  • Protocol Agnositic approach to ERC20/Native rate-limiting
  • Performant Codebase
  • Multiple tokens supports with custom withdrawal rate limits for each token.
  • Records inflows/outflows of token and maintains a Historical running total of the protocol's token liquidity.
  • Enforces withdrawal limits and periods to prevent total fund drainage (hack mitigation).
  • Allows the contract owner to register tokens, override limits, and transfer admin privileges.

Integration

There are easy points of integration that you must do to have your protocol circuitBreaker properly set up.

  1. Instantiate Circuit Breaker contract with appropriate constructor params
  2. Register circuit breaker params for each desired token
  3. Add protected contracts to circuit breaker
  4. Enjoy enhanced safety

Example CircuitBreaker Integration

contract MockDeFiProtocol is ProtectedContract {
    using SafeERC20 for IERC20;

    constructor(address _circuitBreaker) ProtectedContract(_circuitBreaker) {}

    /*
     * @notice Use cbInflowSafeTransferFrom to safe transfer tokens and record inflow to circuit-breaker
     */
    function deposit(address _token, uint256 _amount) external {
        cbInflowSafeTransferFrom(_token, msg.sender, address(this), _amount);

        // Your logic here
    }

    /*
     * @notice Withdrawal hook for circuit breaker to safe transfer tokens and enforcement
     */
    function withdrawal(address _token, uint256 _amount) external {
        //  Your logic here

        cbOutflowSafeTransfer(_token, _amount, msg.sender, false);
    }
}

Example smart contract consumer

contract MockDeFiConsumer {
    ICircuitBreaker circuitBreaker;
    IMockDeFi defi;

    error RateLimited();

    function onTokenOutflow(address token, uint256 amount) external {
        defi.onTokenOutflow(address token, uint256 amount);
        if(circuitBreaker.isRateLimited()){
            revert RateLimited()
        }
        # If there is no revertOnRateLimit flag in the defi protocol
        # You need to revert in your smart contract to have consistent accounting
    }
}

Testing

forge test

DeFi CircuitBreaker Rate limit

Contributors

License

Copyright and related rights waived via CC0.

About

A circuit breaker for solidity defi protocols

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Solidity 61.6%
  • JavaScript 32.8%
  • Ruby 5.3%
  • Shell 0.2%
  • Makefile 0.1%
  • Python 0.0%