we_cdk/
macros.rs

1mod asset;
2mod block;
3mod call_contract;
4mod converts;
5mod crypto;
6mod lease;
7mod memory;
8mod storage;
9mod tx;
10mod utils;
11
12#[doc(hidden)]
13#[macro_export]
14macro_rules! internal_data {
15    (this) => {{
16        (core::ptr::null(), 0)
17    }};
18    (system_token) => {{
19        (core::ptr::null(), 0)
20    }};
21}
22
23#[doc(hidden)]
24#[macro_export]
25macro_rules! error {
26    ($error:expr) => {
27        if $error != 0 {
28            return $error;
29        }
30    };
31}
32
33/// Verify inputs and conditions
34///
35/// # Result
36/// If the condition was not satisfied,
37/// the execution will be stopped with error code 300 (RuntimeError::Exception)
38///
39/// # Usage
40/// ```
41/// use we_cdk::*;
42///
43/// #[action]
44/// fn _constructor() {
45///     let balance = get_balance!(this);
46///     require!(balance > 42);
47/// }
48///
49/// #[action]
50/// fn require_with_message() {
51///     let balance = get_balance!(this);
52///     require!(balance > 1337, "Balance is less than 1337!");
53/// }
54/// ```
55#[macro_export]
56macro_rules! require {
57    ($condition:expr) => {
58        if !($condition) {
59            return 300;
60        }
61    };
62    ($condition:expr, $message:tt) => {
63        if !($condition) {
64            let error = wevm::v0::bindings::require($message.as_ptr(), $message.len());
65            if error != 0 {
66                return error;
67            } else {
68                return 300;
69            }
70        }
71    }
72}