1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Utilities

use wasm_bindgen::prelude::*;

use crate::error_conversion::to_js;

/// helper methods to get the fee address for various networks
#[wasm_bindgen]
pub struct MinerAddress {}

#[wasm_bindgen]
impl MinerAddress {
    /// address to use in mainnet for the fee
    pub fn mainnet_fee_address() -> String {
        ergo_lib::constants::MINERS_FEE_MAINNET_ADDRESS.to_string()
    }
    /// address to use in testnet for the fee
    pub fn testnet_fee_address() -> String {
        ergo_lib::constants::MINERS_FEE_TESTNET_ADDRESS.to_string()
    }
}

/// Wrapper for i64 for JS/TS
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct I64(i64);

#[wasm_bindgen]
impl I64 {
    /// Create from a standard rust string representation
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(string: &str) -> Result<I64, JsValue> {
        string.parse::<i64>().map_err(to_js).map(I64)
    }

    /// String representation of the value for use from environments that don't support i64
    pub fn to_str(&self) -> String {
        format!("{}", self.0)
    }

    /// Get the value as JS number (64-bit float)
    pub fn as_num(&self) -> js_sys::Number {
        js_sys::Number::from(self.0 as f64)
    }

    /// Addition with overflow check
    pub fn checked_add(&self, other: &I64) -> Result<I64, JsValue> {
        match self.0.checked_add(other.0) {
            Some(value) => Ok(I64(value)),
            None => Err(JsValue::from_str("overflow")),
        }
    }
}

impl From<i64> for I64 {
    fn from(v: i64) -> Self {
        I64(v)
    }
}

impl From<I64> for i64 {
    fn from(v: I64) -> Self {
        v.0
    }
}

#[allow(dead_code, missing_docs)]
pub fn set_panic_hook() {
    // When the `console_error_panic_hook` feature is enabled, we can call the
    // `set_panic_hook` function at least once during initialization, and then
    // we will get better error messages if our code ever panics.
    //
    // For more details see
    // https://github.com/rustwasm/console_error_panic_hook#readme
    #[cfg(feature = "console_error_panic_hook")]
    console_error_panic_hook::set_once();
}