templar_common/
lib.rs

1pub mod accumulator;
2pub mod asset;
3pub mod borrow;
4pub mod chunked_append_only_list;
5pub mod event;
6pub mod fee;
7pub mod governance;
8pub mod guard;
9pub mod incoming_deposit;
10pub mod interest_rate_strategy;
11pub mod market;
12pub mod number;
13pub mod oracle;
14pub mod price;
15pub mod registry;
16pub mod snapshot;
17pub mod supply;
18pub mod time;
19pub mod time_chunk;
20#[cfg(feature = "rpc")]
21pub mod utils;
22pub mod vault;
23pub mod versioned_state;
24pub mod withdrawal_queue;
25
26pub use primitive_types;
27pub use schemars;
28
29/// Panic helper that works in both WASM and native contexts.
30///
31/// In WASM contexts (contract compilation), uses `near_sdk::env::panic_str`.
32/// In native contexts (bots, tests), uses standard `panic!`.
33#[cfg(target_arch = "wasm32")]
34#[inline]
35pub fn panic_with_message(msg: &str) -> ! {
36    near_sdk::env::panic_str(msg);
37}
38
39/// Panic helper that works in both WASM and native contexts.
40///
41/// In WASM contexts (contract compilation), uses `near_sdk::env::panic_str`.
42/// In native contexts (bots, tests), uses standard `panic!`.
43#[cfg(not(target_arch = "wasm32"))]
44#[inline]
45pub fn panic_with_message(msg: &str) -> ! {
46    panic!("{}", msg);
47}
48
49/// Extension trait for `Option` and `Result` that panics with a custom message on failure.
50pub trait UnwrapReject<T> {
51    /// Unwraps the value with a default panic message.
52    fn unwrap_or_reject(self) -> T;
53    /// Unwraps the value with a custom panic message.
54    fn expect_or_reject(self, msg: &str) -> T;
55}
56
57impl<T> UnwrapReject<T> for Option<T> {
58    fn unwrap_or_reject(self) -> T {
59        self.expect_or_reject("called `Option::unwrap_or_reject()` on a `None` value")
60    }
61
62    fn expect_or_reject(self, msg: &str) -> T {
63        match self {
64            Some(value) => value,
65            None => panic_with_message(msg),
66        }
67    }
68}
69
70impl<T, E: std::fmt::Display> UnwrapReject<T> for Result<T, E> {
71    fn unwrap_or_reject(self) -> T {
72        self.expect_or_reject("called `Result::unwrap_or_reject()` on an `Err` value")
73    }
74
75    fn expect_or_reject(self, msg: &str) -> T {
76        match self {
77            Ok(value) => value,
78            Err(err) => panic_with_message(&format!("{msg}: {err}")),
79        }
80    }
81}
82
83/// Approximation of `1 / (1000 * 60 * 60 * 24 * 365.2425)`.
84///
85/// exact = 0.00000000003168873850681143096456210346297...
86/// this  = 0.00000000003168873850681143096456210346
87///
88/// error =~ 9.375e-27 %
89pub static YEAR_PER_MS: number::Decimal =
90    number::Decimal::from_repr([0x40FC_AB61_4AE4_B2B5, 0x22D7_9641, 0, 0, 0, 0, 0, 0]);
91
92pub mod contract {
93    pub fn list<T, U: FromIterator<T>>(
94        i: impl IntoIterator<Item = T>,
95        offset: Option<u32>,
96        count: Option<u32>,
97    ) -> U {
98        let offset = offset.map_or(0, |o| o as usize);
99        let count = count.map_or(usize::MAX, |c| c as usize);
100        i.into_iter().skip(offset).take(count).collect()
101    }
102
103    #[macro_export]
104    macro_rules! self_ext {
105        ($gas:expr) => {
106            Self::ext(::near_sdk::env::current_account_id()).with_static_gas($gas)
107        };
108    }
109}