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
#[allow(hidden_glob_reexports)]
pub(crate) mod deploy;
pub mod rpcs;
#[allow(hidden_glob_reexports)]
pub(crate) mod transaction;
pub use deploy::*;
pub use transaction::*;

pub(crate) mod deploy_utils;
pub(crate) use deploy_utils::*;

pub(crate) mod transaction_utils;
pub(crate) use transaction_utils::*;

pub mod watcher;

pub(crate) mod contract;
pub use contract::*;

use wasm_bindgen::prelude::*;

use crate::types::verbosity::Verbosity;

#[wasm_bindgen]
pub struct SDK {
    rpc_address: Option<String>,
    verbosity: Option<Verbosity>,
}

impl Default for SDK {
    fn default() -> Self {
        Self::new(None, None)
    }
}

#[wasm_bindgen]
impl SDK {
    #[wasm_bindgen(constructor)]
    pub fn new(rpc_address: Option<String>, verbosity: Option<Verbosity>) -> Self {
        SDK {
            rpc_address,
            verbosity,
        }
    }

    #[wasm_bindgen(js_name = "getRPCAddress")]
    pub fn get_rpc_address(&self, rpc_address: Option<String>) -> String {
        rpc_address
            .as_ref()
            .cloned()
            .or_else(|| self.rpc_address.as_ref().map(String::to_owned))
            .unwrap_or_default()
    }

    #[wasm_bindgen(js_name = "setRPCAddress")]
    pub fn set_rpc_address(&mut self, rpc_address: Option<String>) -> Result<(), String> {
        self.rpc_address = rpc_address;
        Ok(())
    }

    #[wasm_bindgen(js_name = "getVerbosity")]
    pub fn get_verbosity(&self, verbosity: Option<Verbosity>) -> Verbosity {
        verbosity.unwrap_or(self.verbosity.unwrap_or(Verbosity::Low))
    }

    #[wasm_bindgen(js_name = "setVerbosity")]
    pub fn set_verbosity(&mut self, verbosity: Option<Verbosity>) -> Result<(), String> {
        self.verbosity = verbosity;
        Ok(())
    }
}