Skip to content

@stdlib/stoppable

Provides traits that allow stopping a contract. Useful for emergency or maintenance modes. Requires an Ownable trait from @stdlib/ownable. This trait manages a single flag stopped in the contract, and handling the stopped state must be done in the contract itself.

To use this library, import @stdlib/stoppable:

import "@stdlib/stoppable"; // this would automatically import @stdlib/ownable too!

Traits

Stoppable

Trait Stoppable implements a receiver for the Message string “Stop” that can be sent by the owner. It implements the stopped() getter function that returns true if the contract is stopped (or false otherwise) and provides private (non-getter) functions requireNotStopped() and requireStopped().

Source code:

@interface("org.ton.stoppable")
trait Stoppable with Ownable {
/// Whether the contract is stopped.
stopped: Bool;
/// The owner of the contract.
owner: Address;
/// Requires the contract to be not stopped.
///
/// #### Error codes
///
/// * 133 — if the contract is stopped
///
fun requireNotStopped() {
throwUnless(TactExitCodeContractStopped, !self.stopped);
}
/// Requires the contract to be stopped.
fun requireStopped() {
require(self.stopped, "Contract not stopped");
}
/// Receiver for the message `"Stop"` that stops the contract.
///
/// Can only be called by the owner and if the contract is not stopped already.
receive("Stop") {
self.requireOwner();
self.requireNotStopped();
self.stopped = true;
self.reply("Stopped".asComment());
}
/// Returns `true` if the contract is stopped (or `false` otherwise).
get fun stopped(): Bool {
return self.stopped;
}
}

Usage example:

import "@stdlib/ownable";
import "@stdlib/stoppable";
contract MyContract with Stoppable {
owner: Address;
stopped: Bool;
init(owner: Address) {
self.owner = owner;
self.stopped = false;
}
}

Resumable

The Resumable trait extends the Stoppable trait and allows resuming contract execution.

Source code:

@interface("org.ton.resumable")
trait Resumable with Stoppable {
/// Whether the contract is stopped.
stopped: Bool;
/// The owner of the contract.
owner: Address;
/// Receiver for the message `"Resume"` that resumes the contract execution.
///
/// Can only be called by the owner and if the contract is stopped.
receive("Resume") {
self.requireOwner();
self.requireStopped();
self.stopped = false;
self.reply("Resumed".asComment());
}
}

Usage example:

import "@stdlib/ownable";
import "@stdlib/stoppable";
contract MyContract with Resumable {
owner: Address;
stopped: Bool;
init(owner: Address) {
self.owner = owner;
self.stopped = false;
}
}

Sources