1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
// Note: Simple macros cannot be placed in dharitri-wasm-derive,
// because Rust "cannot export macro_rules! macros from a `proc-macro` crate type currently".
/// Getting all imports needed for a smart contract.
#[macro_export]
macro_rules! imports {
() => {
use core::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div,
DivAssign, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub,
SubAssign,
};
use dharitri_wasm::{
api::{
BigIntApi, BlockchainApi, BlockchainApiImpl, CallValueApi, CallValueApiImpl,
CryptoApi, CryptoApiImpl, EllipticCurveApi, ErrorApi, ErrorApiImpl, LogApi,
LogApiImpl, ManagedTypeApi, PrintApi, PrintApiImpl, SendApi, SendApiImpl,
},
arrayvec::ArrayVec,
contract_base::{ContractBase, ProxyObjBase},
dct::*,
dharitri_codec::{DecodeError, NestedDecode, NestedEncode, TopDecode},
err_msg,
io::*,
non_zero_usize,
non_zero_util::*,
require, require_old, sc_error, sc_panic, sc_print,
storage::mappers::*,
types::{
SCResult::{Err, Ok},
*,
},
Box, Vec,
};
};
}
/// Imports required for deriving serialization and TypeAbi.
#[macro_export]
macro_rules! derive_imports {
() => {
use dharitri_wasm::{
derive::{ManagedVecItem, TypeAbi},
dharitri_codec,
dharitri_codec::dharitri_codec_derive::{
NestedDecode, NestedEncode, TopDecode, TopDecodeOrDefault, TopEncode,
TopEncodeOrDefault,
},
};
};
}
/// Compact way of returning a static error message.
#[macro_export]
macro_rules! sc_error {
($s:expr) => {
dharitri_wasm::types::SCResult::Err(dharitri_wasm::types::StaticSCError::from($s)).into()
};
}
/// Allows us to write Solidity style `require!(<condition>, <error_msg>)` and avoid if statements.
///
/// It can only be used in a function that returns `SCResult<_>` where _ can be any type.
///
/// Example:
///
/// ```rust
/// # use dharitri_wasm::require_old;
/// # use dharitri_wasm::types::{*, SCResult::Ok};
/// # pub trait ExampleContract: dharitri_wasm::contract_base::ContractBase
/// # {
/// fn only_accept_positive_old(&self, x: i32) -> SCResult<()> {
/// require_old!(x > 0, "only positive values accepted");
/// Ok(())
/// }
/// # }
/// ```
#[macro_export]
macro_rules! require_old {
($expression:expr, $error_msg:expr) => {
if (!($expression)) {
return dharitri_wasm::sc_error!($error_msg);
}
};
}
#[macro_export]
macro_rules! sc_panic {
($msg:tt, $($arg:expr),+) => {{
let mut ___buffer___ =
dharitri_wasm::types::ManagedBufferCachedBuilder::<Self::Api>::new_from_slice(&[]);
dharitri_wasm::derive::format_receiver_args!(___buffer___, $msg, $($arg),+);
dharitri_wasm::contract_base::ErrorHelper::<Self::Api>::signal_error_with_message(___buffer___.into_managed_buffer());
}};
($msg:tt) => {
dharitri_wasm::contract_base::ErrorHelper::<Self::Api>::signal_error_with_message($msg);
};
}
/// 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:
///
/// ```rust
/// # use dharitri_wasm::{types::ManagedBuffer, require};
/// # pub trait ExampleContract: dharitri_wasm::contract_base::ContractBase
/// # {
/// 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, {:x} is not negative", x);
/// }
///
/// fn only_accept_zero(&self, x: i32, message: &ManagedBuffer<Self::Api>) {
/// require!(x == 0, message);
/// }
/// # }
/// ```
#[macro_export]
macro_rules! require {
($expression:expr, $($msg_tokens:tt),+) => {
if (!($expression)) {
dharitri_wasm::sc_panic!($($msg_tokens),+);
}
};
}
#[macro_export]
macro_rules! sc_print {
($msg:tt, $($arg:expr),*) => {{
let mut ___buffer___ =
dharitri_wasm::types::ManagedBufferCachedBuilder::<Self::Api>::new_from_slice(&[]);
dharitri_wasm::derive::format_receiver_args!(___buffer___, $msg, $($arg),*);
<Self::Api as dharitri_wasm::api::PrintApi>::print_api_impl().print_managed_buffer(___buffer___.into_managed_buffer().get_raw_handle());
}};
}
/// Equivalent to the `?` operator for SCResult.
#[deprecated(
since = "0.16.0",
note = "The `?` operator can now be used on `SCResult`, please use it instead."
)]
#[macro_export]
macro_rules! sc_try {
($s:expr) => {
match $s {
dharitri_wasm::types::SCResult::Ok(t) => t,
dharitri_wasm::types::SCResult::Err(e) => {
return dharitri_wasm::types::SCResult::Err(e);
},
}
};
}
/// Very compact way of not allowing anyone but the owner to call a function.
///
/// It can only be used in a function that returns `SCResult<_>` where _ can be any type.
///
/// ```rust
/// # use dharitri_wasm::*;
/// # use dharitri_wasm::api::BlockchainApi;
/// # use dharitri_wasm::types::{*, SCResult::Ok};
/// # pub trait ExampleContract: dharitri_wasm::contract_base::ContractBase
/// # {
/// fn only_callable_by_owner(&self) -> SCResult<()> {
/// only_owner!(self, "Caller must be owner");
/// Ok(())
/// }
/// # }
/// ```
#[deprecated(
since = "0.26.0",
note = "Replace with the `#[only_owner]` attribute that can be placed on an endpoint. That one is more compact and shows up in the ABI."
)]
#[macro_export]
macro_rules! only_owner {
($trait_self: expr, $error_msg:expr) => {
if ($trait_self.blockchain().get_caller() != $trait_self.blockchain().get_owner_address()) {
return sc_error!($error_msg);
}
};
}
/// Converts usize to NonZeroUsize or returns SCError.
#[macro_export]
macro_rules! non_zero_usize {
($input: expr, $error_msg:expr) => {
if let Some(nz) = NonZeroUsize::new($input) {
nz
} else {
return sc_error!($error_msg);
}
};
}