unc_sdk/utils/
mod.rs

1//! Helper methods that often used in smart contracts.
2
3pub(crate) mod storage_key_impl;
4
5mod stable_map;
6pub(crate) use self::stable_map::StableMap;
7mod cache_entry;
8pub(crate) use cache_entry::{CacheEntry, EntryState};
9
10use crate::{env, PromiseResult, UncToken};
11
12/// Helper macro to log a message through [`env::log_str`].
13/// This macro can be used similar to the [`std::format`] macro.
14///
15/// This differs from [`std::format`] because instead of generating a string, it will log the utf8
16/// bytes as a log through [`env::log_str`].
17///
18/// The logged message will get persisted on chain.
19///
20/// # Example use
21///
22/// ```no_run
23/// use unc_sdk::log;
24///
25/// # fn main() {
26/// log!("test");
27/// let world: &str = "world";
28/// log!("{world}");
29/// log!("Hello {}", world);
30/// log!("x = {}, y = {y}", 10, y = 30);
31/// # }
32/// ```
33///
34/// [`env::log_str`]: crate::env::log_str
35#[macro_export]
36macro_rules! log {
37    ($($arg:tt)*) => {
38        $crate::env::log_str(::std::format!($($arg)*).as_str())
39    };
40}
41
42/// Helper macro to create assertions that will panic through the runtime host functions.
43///
44/// This macro can be used similarly to [`assert!`] but will reduce code size by not including
45/// file and rust specific data in the panic message.
46///
47/// # Examples
48///
49/// ```no_run
50/// use unc_sdk::require;
51///
52/// # fn main() {
53/// let a = 2;
54/// require!(a > 0);
55/// require!("test" != "other", "Some custom error message if false");
56/// # }
57/// ```
58#[macro_export]
59macro_rules! require {
60    ($cond:expr $(,)?) => {
61        if cfg!(debug_assertions) {
62            assert!($cond)
63        } else if !$cond {
64            $crate::env::panic_str("require! assertion failed");
65        }
66    };
67    ($cond:expr, $message:expr $(,)?) => {
68        if cfg!(debug_assertions) {
69            // Error message must be &str to match panic_str signature
70            let msg: &str = &$message;
71            assert!($cond, "{}", msg)
72        } else if !$cond {
73            $crate::env::panic_str(&$message)
74        }
75    };
76}
77
78/// Assert that predecessor_account_id == current_account_id, meaning contract called itself.
79pub fn assert_self() {
80    require!(env::predecessor_account_id() == env::current_account_id(), "Method is private");
81}
82
83/// Assert that 1 attoUNC was attached.
84pub fn assert_one_atto() {
85    require!(
86        env::attached_deposit() == UncToken::from_attounc(1),
87        "Requires attached deposit of exactly 1 attoUNC"
88    )
89}
90
91/// Returns true if promise was successful.
92/// Fails if called outside a callback that received 1 promise result.
93pub fn is_promise_success() -> bool {
94    require!(env::promise_results_count() == 1, "Contract expected a result on the callback");
95    env::promise_result_internal(0).is_ok()
96}
97
98/// Returns the result of the promise if successful. Otherwise returns None.
99/// Fails if called outside a callback that received 1 promise result.
100pub fn promise_result_as_success() -> Option<Vec<u8>> {
101    require!(env::promise_results_count() == 1, "Contract expected a result on the callback");
102    match env::promise_result(0) {
103        PromiseResult::Successful(result) => Some(result),
104        _ => None,
105    }
106}
107
108/// Deprecated helper function which used to generate code to initialize the [`GlobalAllocator`].
109/// This is now initialized by default. Disable `wee_alloc` feature to configure manually.
110///
111/// [`GlobalAllocator`]: std::alloc::GlobalAlloc
112#[deprecated(
113    since = "2.0.0",
114    note = "Allocator is already initialized with the default `wee_alloc` feature set. \
115            Please make sure you don't disable default features on the SDK or set the global \
116            allocator manually."
117)]
118#[macro_export]
119macro_rules! setup_alloc {
120    () => {};
121}
122
123#[cfg(test)]
124mod tests {
125    use crate::test_utils::get_logs;
126
127    #[test]
128    fn test_log_simple() {
129        log!("hello");
130
131        assert_eq!(get_logs(), vec!["hello".to_string()]);
132    }
133
134    #[test]
135    fn test_log_format() {
136        log!("hello {} ({})", "user_name", 25);
137
138        assert_eq!(get_logs(), vec!["hello user_name (25)".to_string()]);
139    }
140}