Skip to main content

gmsol_sdk/js/
glv.rs

1use std::sync::Arc;
2
3use gmsol_programs::gmsol_store::accounts::Glv;
4use wasm_bindgen::prelude::*;
5
6use crate::{
7    glv::GlvModel,
8    utils::zero_copy::{
9        try_deserialize_zero_copy_from_base64_with_options, try_deserialize_zero_copy_with_options,
10    },
11};
12
13/// Wrapper of [`Glv`].
14#[wasm_bindgen(js_name = Glv)]
15#[derive(Clone)]
16pub struct JsGlv {
17    glv: Arc<Glv>,
18}
19
20#[wasm_bindgen(js_class = Glv)]
21impl JsGlv {
22    /// Create from base64 encoded account data with options.
23    pub fn decode_from_base64_with_options(
24        data: &str,
25        no_discriminator: Option<bool>,
26    ) -> crate::Result<Self> {
27        let glv = try_deserialize_zero_copy_from_base64_with_options(
28            data,
29            no_discriminator.unwrap_or(false),
30        )?;
31
32        Ok(Self {
33            glv: Arc::new(glv.0),
34        })
35    }
36
37    /// Create from base64 encoded account data with options.
38    pub fn decode_with_options(data: &[u8], no_discriminator: Option<bool>) -> crate::Result<Self> {
39        let glv = try_deserialize_zero_copy_with_options(data, no_discriminator.unwrap_or(false))?;
40
41        Ok(Self {
42            glv: Arc::new(glv.0),
43        })
44    }
45
46    /// Convert into [`JsGlvModel`].
47    pub fn to_model(&self, supply: u128) -> crate::Result<JsGlvModel> {
48        Ok(JsGlvModel {
49            model: GlvModel::new(self.glv.clone(), supply.try_into()?),
50        })
51    }
52
53    /// Returns GLV token address.
54    pub fn glv_token_address(&self) -> String {
55        self.glv.glv_token.to_string()
56    }
57
58    /// Returns long token address.
59    pub fn long_token_address(&self) -> String {
60        self.glv.long_token.to_string()
61    }
62
63    /// Returns short token address.
64    pub fn short_token_address(&self) -> String {
65        self.glv.short_token.to_string()
66    }
67
68    /// Create a clone of this market.
69    #[wasm_bindgen(js_name = clone)]
70    pub fn js_clone(&self) -> Self {
71        self.clone()
72    }
73}
74
75/// Wrapper of [`GlvModel`].
76#[wasm_bindgen(js_name = GlvModel)]
77#[derive(Clone)]
78pub struct JsGlvModel {
79    pub(super) model: GlvModel,
80}
81
82#[wasm_bindgen(js_class = GlvModel)]
83impl JsGlvModel {
84    /// Returns current supply.
85    pub fn supply(&self) -> u128 {
86        self.model.supply().into()
87    }
88
89    /// Returns GLV token address.
90    pub fn glv_token_address(&self) -> String {
91        self.model.glv_token.to_string()
92    }
93
94    /// Returns long token address.
95    pub fn long_token_address(&self) -> String {
96        self.model.long_token.to_string()
97    }
98
99    /// Returns short token address.
100    pub fn short_token_address(&self) -> String {
101        self.model.short_token.to_string()
102    }
103
104    /// Create a clone of this market model.
105    #[wasm_bindgen(js_name = clone)]
106    pub fn js_clone(&self) -> Self {
107        self.clone()
108    }
109}
110
111impl From<GlvModel> for JsGlvModel {
112    fn from(model: GlvModel) -> Self {
113        Self { model }
114    }
115}