Contract request: Circuit Breaker

In their best practices for software engineering, ConsenSys has a few very good examples for implementing a “circuit breaker” functionality:

bool private stopped = false;
address private owner;

modifier isAdmin() {
    require(msg.sender == owner);
    _;
}

function toggleContractActive() isAdmin public {
    // You can add an additional modifier that restricts stopping a contract to be based on another action, such as a vote of users
    stopped = !stopped;
}

modifier stopInEmergency { if (!stopped) _; }
modifier onlyInEmergency { if (stopped) _; }

function deposit() stopInEmergency public {
    // some code
}

function withdraw() onlyInEmergency public {
    // some code
}

I think this is something the community would smile upon having. I’m going to implement it myself in the meantime and I’ll update this thread with what I end up using.

2 Likes

The API for Pausable is pretty much the same as what you’re proposing, could you take a look at it and see if it’d satisfy your needs?

I wanted to share a link to our docs but then realized that for whatever reason it’s not listed :sweat_smile:

3 Likes

Hi @PaulRBerg,

As per @nventuro, it is worth looking at Pausable.sol which uses PauserRole.sol

PauserRole is in the Role Based Access Control documentation

2 Likes

Oh yeah that’s exactly what I was looking for! There’s a bit of overlap between Ownable and Pausable, in that the “owner” becomes equivalent to “pausable”, but the benefits of using the code as it is are higher than the cost of modifying it.

Cheers :beers:

1 Like