A test Solidity smart contract circuit breaker implementation in production.
The CircuitBreaker is an Ethereum-based smart contract designed to provide an on-chain circuit breaker mechanism. It aims to enhance the security of decentralized applications (dApps) by automatically pausing the contract in case of potential exploits. This contract is particularly useful for DeFi projects and other high-value smart contract systems where quick response to anomalies is required.
-
Automatic Pausing: The contract automatically pauses itself if potential exploit conditions are detected.
-
Customizable Thresholds: Easily adjust transaction limits and value thresholds.
-
Developer Alerts: Emits events for potential exploits and allows custom alerts to be sent.
-
Manual Controls: Includes functions for manual pausing and unpausing by the contract owner.
-
Integratable: Designed to be easily integrated with existing smart contract functions.
The contract monitors for three main types of potential exploits:
-
Transaction Frequency: Detects if too many transactions occur within a single block.
-
Large Transactions: Identifies individual transactions that exceed a maximum value threshold.
-
High Block Value: Monitors the total value of transactions within a block, pausing if it exceeds a set limit.
-
CircuitBreaker
: Main contract inheriting fromPausable
andOwnable
. -
Key Functions:
-
checkAndUpdateCircuitBreaker
: Core function for exploit detection. -
resetCounters
: Resets transaction counters on new blocks. -
pause
andunpause
: Manual control functions (owner only). -
sendAlert
: Allows the owner to send custom alerts. -
someFunction
andtransfer
: Example functions demonstrating integration.
-
-
Node.js and npm installed.
-
Truffle or Hardhat for Ethereum development.
-
OpenZeppelin Contracts library.
- Clone the repository:
git clone https://github.com/arunimshukla/circuit-breaker.git
cd circuit-breaker
-
Install dependencies:
npm install @openzeppelin/contracts
-
Compile the contract:
truffle compile
or
npx hardhat compile
-
Deploy the contract:
truffle migrate
or
npx hardhat run scripts/deploy.js
-
After deployment, the contract owner can adjust the threshold constants if needed.
-
Integrate the
checkAndUpdateCircuitBreaker
function into your existing contract functions:function yourFunction() external payable whenNotPaused { checkAndUpdateCircuitBreaker(msg.value); // Your function logic here }
-
The contract will automatically pause if any exploit conditions are met.
-
Monitor emitted events (
ExploitDetected
andAlertSent
) for real-time notifications. -
The owner can manually pause/unpause the contract and send custom alerts as needed.
-
Regularly review and adjust the threshold values based on your project's needs and network conditions.
-
Ensure that only trusted addresses have owner privileges.
-
Consider implementing a time-delay mechanism for unpausing to allow for thorough checks before resuming operations.
-
Regularly audit the contract and any integrated systems for potential vulnerabilities.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE.md file for details.
This smart contract is provided as-is. While it includes security features, it should be thoroughly audited and tested before use in any production environment. The developer assume no liability for any losses or damages incurred through the use of this contract.