Skip to main content

equanetwork_math/
mod.rs

1#![allow(
2    unexpected_cfgs,
3    unused_attributes,
4    clippy::needless_range_loop,
5    // `U128` is `u128` without `wasm`; `.into()` keeps wasm builds working.
6    clippy::useless_conversion
7)]
8#![cfg_attr(target_os = "solana", no_std)]
9#![cfg_attr(feature = "wasm", allow(non_snake_case))]
10#![cfg_attr(target_os = "solana", feature(cfg_boolean_literals))]
11
12mod bitmap;
13mod consts;
14mod error;
15mod price;
16mod skew;
17mod swap;
18mod token;
19
20pub use bitmap::*;
21pub use consts::*;
22pub use error::*;
23pub use price::*;
24pub use skew::*;
25pub use swap::*;
26pub use token::*;
27
28#[cfg(feature = "wasm")]
29use serde::Serializer;
30
31#[cfg(feature = "wasm")]
32pub fn u64_serialize<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
33where
34    S: Serializer,
35{
36    serializer.serialize_u128(*value as u128)
37}
38
39#[cfg(not(feature = "wasm"))]
40pub type U128 = u128;
41
42#[cfg(feature = "wasm")]
43use core::fmt::{Debug, Formatter};
44
45#[cfg(feature = "wasm")]
46use ethnum::U256;
47
48#[cfg(feature = "wasm")]
49use wasm_bindgen::prelude::*;
50
51#[cfg(feature = "wasm")]
52#[wasm_bindgen]
53extern "C" {
54    #[wasm_bindgen(typescript_type = "bigint")]
55    #[derive(PartialEq)]
56    pub type U128;
57}
58
59#[cfg(feature = "wasm")]
60impl Debug for U128 {
61    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
62        write!(f, "{:?}", JsValue::from(self))
63    }
64}
65
66#[cfg(feature = "wasm")]
67impl From<U128> for u128 {
68    fn from(value: U128) -> u128 {
69        JsValue::from(value)
70            .try_into()
71            .expect("Number too large to fit in u128")
72    }
73}
74
75#[cfg(feature = "wasm")]
76impl From<U128> for U256 {
77    fn from(value: U128) -> U256 {
78        let u_128: u128 = value.into();
79        <U256>::from(u_128)
80    }
81}
82
83#[cfg(feature = "wasm")]
84impl From<u128> for U128 {
85    fn from(value: u128) -> U128 {
86        JsValue::from(value).unchecked_into()
87    }
88}
89
90#[cfg(feature = "wasm")]
91impl TryFrom<U256> for U128 {
92    type Error = core::num::TryFromIntError;
93
94    fn try_from(value: U256) -> Result<Self, Self::Error> {
95        let u_128: u128 = value.try_into()?;
96        Ok(U128::from(u_128))
97    }
98}