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#[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 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 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 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 pub fn glv_token_address(&self) -> String {
55 self.glv.glv_token.to_string()
56 }
57
58 pub fn long_token_address(&self) -> String {
60 self.glv.long_token.to_string()
61 }
62
63 pub fn short_token_address(&self) -> String {
65 self.glv.short_token.to_string()
66 }
67
68 #[wasm_bindgen(js_name = clone)]
70 pub fn js_clone(&self) -> Self {
71 self.clone()
72 }
73}
74
75#[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 pub fn supply(&self) -> u128 {
86 self.model.supply().into()
87 }
88
89 pub fn glv_token_address(&self) -> String {
91 self.model.glv_token.to_string()
92 }
93
94 pub fn long_token_address(&self) -> String {
96 self.model.long_token.to_string()
97 }
98
99 pub fn short_token_address(&self) -> String {
101 self.model.short_token.to_string()
102 }
103
104 #[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}