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