use std::collections::BTreeMap;
use serde::Serialize;
use serde_wasm_bindgen::Serializer;
use wasm_bindgen::prelude::*;
use crate::Error;
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Balance {
inner: lwk_common::SignedBalance,
}
impl From<lwk_common::SignedBalance> for Balance {
fn from(inner: lwk_common::SignedBalance) -> Self {
Self { inner }
}
}
impl From<lwk_common::Balance> for Balance {
fn from(balance: lwk_common::Balance) -> Self {
let signed_map: std::collections::BTreeMap<lwk_wollet::elements::AssetId, i64> = balance
.iter()
.map(|(asset_id, amount)| (*asset_id, *amount as i64))
.collect();
Self {
inner: lwk_common::SignedBalance::from(signed_map),
}
}
}
#[wasm_bindgen]
impl Balance {
#[wasm_bindgen(js_name = toJSON)]
pub fn to_json(&self) -> Result<JsValue, Error> {
let serializer = Serializer::new().serialize_maps_as_objects(true);
Ok(self
.inner
.as_ref()
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect::<BTreeMap<String, String>>()
.serialize(&serializer)?)
}
#[wasm_bindgen]
pub fn entries(&self) -> Result<JsValue, Error> {
let serializer = Serializer::new().serialize_large_number_types_as_bigints(true);
Ok(self.inner.serialize(&serializer)?)
}
#[wasm_bindgen(js_name = toString)]
pub fn to_string_js(&self) -> String {
serde_json::to_string(&self.inner).expect("contain simple types")
}
}