Macro elrond_wasm::require

source ·
macro_rules! require {
    ($expression:expr, $($msg_tokens:tt),+  $(,)?) => { ... };
}
Expand description

Allows us to write Solidity style require!(<condition>, <error_msg>) and avoid if statements.

The most common way to use it is to provide a string message with optional format arguments.

It is also possible to give the error as a variable of types such as &str, &[u8] or ManagedBuffer.

Examples:

fn only_accept_positive(&self, x: i32) {
    require!(x > 0, "only positive values accepted");
}

fn only_accept_negative(&self, x: i32) {
    require!(x < 0, "only negative values accepted, {} is not negative", x);
}

fn only_accept_zero(&self, x: i32, message: &ManagedBuffer<Self::Api>) {
    require!(x == 0, message,);
}