Skip to main content

solana_sdk_wasm_js/
lib.rs

1//! solana-program Javascript interface
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![cfg(target_arch = "wasm32")]
4
5use {
6    log::Level,
7    wasm_bindgen::prelude::{wasm_bindgen, JsValue},
8};
9
10pub mod address;
11pub mod hash;
12pub mod instruction;
13pub mod keypair;
14pub mod message;
15pub mod transaction;
16
17/// Initialize Javascript logging and panic handler
18#[wasm_bindgen]
19pub fn solana_program_init() {
20    use std::sync::Once;
21    static INIT: Once = Once::new();
22
23    INIT.call_once(|| {
24        std::panic::set_hook(Box::new(console_error_panic_hook::hook));
25        console_log::init_with_level(Level::Info).unwrap();
26    });
27}
28
29pub fn display_to_jsvalue<T: std::fmt::Display>(display: T) -> JsValue {
30    display.to_string().into()
31}
32
33/// Simple macro for implementing conversion functions between wrapper types and
34/// wrapped types.
35mod conversion {
36    macro_rules! impl_inner_conversion {
37        ($Wrapper:ty, $Inner:ty) => {
38            impl From<$Inner> for $Wrapper {
39                fn from(inner: $Inner) -> Self {
40                    Self { inner }
41                }
42            }
43            impl std::ops::Deref for $Wrapper {
44                type Target = $Inner;
45                fn deref(&self) -> &Self::Target {
46                    &self.inner
47                }
48            }
49        };
50    }
51    pub(crate) use impl_inner_conversion;
52}