From 5b4e97299c1d5b4a8871f611adfd3f44c929bd7d Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Sat, 28 Sep 2024 21:00:04 +0300 Subject: [PATCH] kaspaToUnit and sompiToUnitString --- wallet/core/src/utils.rs | 15 +++++++++------ wallet/core/src/wasm/utils.rs | 24 ++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/wallet/core/src/utils.rs b/wallet/core/src/utils.rs index 22d6017ca..e433bebf1 100644 --- a/wallet/core/src/utils.rs +++ b/wallet/core/src/utils.rs @@ -19,14 +19,14 @@ pub fn try_kaspa_str_to_sompi>(s: S) -> Result> { Ok(Some(str_to_sompi(amount)?)) } -pub fn try_kaspa_str_to_sompi_with_decimals>(s: S, decimals: Option) -> Result> { +pub fn try_kaspa_str_to_unit>(s: S, decimals: u32) -> Result> { let s: String = s.into(); let amount = s.trim(); if amount.is_empty() { return Ok(None); } - Ok(Some(str_to_sompi_with_decimals(amount, decimals)?)) + Ok(Some(str_to_unit(amount, decimals)?)) } pub fn try_kaspa_str_to_sompi_i64>(s: S) -> Result> { @@ -46,8 +46,7 @@ pub fn sompi_to_kaspa(sompi: u64) -> f64 { } #[inline] -pub fn sompi_to_kaspa_with_decimals(sompi: u64, decimals: Option) -> f64 { - let decimals = decimals.unwrap_or(8); +pub fn sompi_to_unit(sompi: u64, decimals: u32) -> f64 { let sompi_per_unit = 10u64.pow(decimals); sompi as f64 / sompi_per_unit as f64 @@ -63,6 +62,11 @@ pub fn sompi_to_kaspa_string(sompi: u64) -> String { sompi_to_kaspa(sompi).separated_string() } +#[inline] +pub fn sompi_to_unit_string(sompi: u64, decimals: u32) -> String { + sompi_to_unit(sompi, decimals).separated_string() +} + #[inline] pub fn sompi_to_kaspa_string_with_trailing_zeroes(sompi: u64) -> String { separated_float!(format!("{:.8}", sompi_to_kaspa(sompi))) @@ -127,8 +131,7 @@ fn str_to_sompi(amount: &str) -> Result { Ok(integer + decimal) } -fn str_to_sompi_with_decimals(amount: &str, decimals: Option) -> Result { - let decimals = decimals.unwrap_or(8); +fn str_to_unit(amount: &str, decimals: u32) -> Result { let sompi_per_unit = 10u64.pow(decimals); // Check if the amount contains a decimal point, if doesn't return value directly. diff --git a/wallet/core/src/wasm/utils.rs b/wallet/core/src/wasm/utils.rs index 6ec66e62e..c5cad8ac8 100644 --- a/wallet/core/src/wasm/utils.rs +++ b/wallet/core/src/wasm/utils.rs @@ -16,8 +16,17 @@ extern "C" { /// can be used to parse user input. /// @category Wallet SDK #[wasm_bindgen(js_name = "kaspaToSompi")] -pub fn kaspa_to_sompi(kaspa: String, decimals: Option) -> Option { - crate::utils::try_kaspa_str_to_sompi_with_decimals(kaspa, decimals).ok().flatten().map(Into::into) +pub fn kaspa_to_sompi(kaspa: String) -> Option { + crate::utils::try_kaspa_str_to_sompi(kaspa).ok().flatten().map(Into::into) +} + +/// Convert a Kaspa string to a specific unit represented by bigint. +/// This function provides correct precision handling and +/// can be used to parse user input. +/// @category Wallet SDK +#[wasm_bindgen(js_name = "kaspaToUnit")] +pub fn kaspa_to_unit(kaspa: String, decimals: u32) -> Option { + crate::utils::try_kaspa_str_to_unit(kaspa, decimals).ok().flatten().map(Into::into) } /// @@ -31,6 +40,17 @@ pub fn sompi_to_kaspa_string(sompi: ISompiToKaspa) -> Result { Ok(crate::utils::sompi_to_kaspa_string(sompi)) } +/// +/// Convert Sompi to a string representation of an unit in Kaspa. +/// +/// @category Wallet SDK +/// +#[wasm_bindgen(js_name = "sompiToUnitString")] +pub fn sompi_to_unit_string(sompi: ISompiToKaspa, decimals: u32) -> Result { + let sompi = sompi.try_as_u64()?; + Ok(crate::utils::sompi_to_unit_string(sompi, decimals)) +} + /// /// Format a Sompi amount to a string representation of the amount in Kaspa with a suffix /// based on the network type (e.g. `KAS` for mainnet, `TKAS` for testnet,