use std::sync::Arc;
use gmsol_programs::gmsol_store::accounts::Glv;
use wasm_bindgen::prelude::*;
use crate::{
glv::GlvModel,
utils::zero_copy::{
try_deserialize_zero_copy_from_base64_with_options, try_deserialize_zero_copy_with_options,
},
};
#[wasm_bindgen(js_name = Glv)]
#[derive(Clone)]
pub struct JsGlv {
glv: Arc<Glv>,
}
#[wasm_bindgen(js_class = Glv)]
impl JsGlv {
pub fn decode_from_base64_with_options(
data: &str,
no_discriminator: Option<bool>,
) -> crate::Result<Self> {
let glv = try_deserialize_zero_copy_from_base64_with_options(
data,
no_discriminator.unwrap_or(false),
)?;
Ok(Self {
glv: Arc::new(glv.0),
})
}
pub fn decode_with_options(data: &[u8], no_discriminator: Option<bool>) -> crate::Result<Self> {
let glv = try_deserialize_zero_copy_with_options(data, no_discriminator.unwrap_or(false))?;
Ok(Self {
glv: Arc::new(glv.0),
})
}
pub fn to_model(&self, supply: u128) -> crate::Result<JsGlvModel> {
Ok(JsGlvModel {
model: GlvModel::new(self.glv.clone(), supply.try_into()?),
})
}
pub fn glv_token_address(&self) -> String {
self.glv.glv_token.to_string()
}
pub fn long_token_address(&self) -> String {
self.glv.long_token.to_string()
}
pub fn short_token_address(&self) -> String {
self.glv.short_token.to_string()
}
#[wasm_bindgen(js_name = clone)]
pub fn js_clone(&self) -> Self {
self.clone()
}
}
#[wasm_bindgen(js_name = GlvModel)]
#[derive(Clone)]
pub struct JsGlvModel {
pub(super) model: GlvModel,
}
#[wasm_bindgen(js_class = GlvModel)]
impl JsGlvModel {
pub fn supply(&self) -> u128 {
self.model.supply().into()
}
pub fn glv_token_address(&self) -> String {
self.model.glv_token.to_string()
}
pub fn long_token_address(&self) -> String {
self.model.long_token.to_string()
}
pub fn short_token_address(&self) -> String {
self.model.short_token.to_string()
}
#[wasm_bindgen(js_name = clone)]
pub fn js_clone(&self) -> Self {
self.clone()
}
}
impl From<GlvModel> for JsGlvModel {
fn from(model: GlvModel) -> Self {
Self { model }
}
}