pub(crate) mod storage_key_impl;
mod stable_map;
pub(crate) use self::stable_map::StableMap;
mod cache_entry;
pub(crate) use cache_entry::{CacheEntry, EntryState};
use crate::{env, PromiseResult, UncToken};
#[macro_export]
macro_rules! log {
($($arg:tt)*) => {
$crate::env::log_str(::std::format!($($arg)*).as_str())
};
}
#[macro_export]
macro_rules! require {
($cond:expr $(,)?) => {
if cfg!(debug_assertions) {
assert!($cond)
} else if !$cond {
$crate::env::panic_str("require! assertion failed");
}
};
($cond:expr, $message:expr $(,)?) => {
if cfg!(debug_assertions) {
let msg: &str = &$message;
assert!($cond, "{}", msg)
} else if !$cond {
$crate::env::panic_str(&$message)
}
};
}
pub fn assert_self() {
require!(env::predecessor_account_id() == env::current_account_id(), "Method is private");
}
pub fn assert_one_atto() {
require!(
env::attached_deposit() == UncToken::from_attounc(1),
"Requires attached deposit of exactly 1 attoUNC"
)
}
pub fn is_promise_success() -> bool {
require!(env::promise_results_count() == 1, "Contract expected a result on the callback");
env::promise_result_internal(0).is_ok()
}
pub fn promise_result_as_success() -> Option<Vec<u8>> {
require!(env::promise_results_count() == 1, "Contract expected a result on the callback");
match env::promise_result(0) {
PromiseResult::Successful(result) => Some(result),
_ => None,
}
}
#[deprecated(
since = "2.0.0",
note = "Allocator is already initialized with the default `wee_alloc` feature set. \
Please make sure you don't disable default features on the SDK or set the global \
allocator manually."
)]
#[macro_export]
macro_rules! setup_alloc {
() => {};
}
#[cfg(test)]
mod tests {
use crate::test_utils::get_logs;
#[test]
fn test_log_simple() {
log!("hello");
assert_eq!(get_logs(), vec!["hello".to_string()]);
}
#[test]
fn test_log_format() {
log!("hello {} ({})", "user_name", 25);
assert_eq!(get_logs(), vec!["hello user_name (25)".to_string()]);
}
}