Skip to content

Commit

Permalink
kaspaToUnit and sompiToUnitString
Browse files Browse the repository at this point in the history
  • Loading branch information
KaffinPX committed Sep 28, 2024
1 parent 89dc469 commit 5b4e972
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
15 changes: 9 additions & 6 deletions wallet/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pub fn try_kaspa_str_to_sompi<S: Into<String>>(s: S) -> Result<Option<u64>> {
Ok(Some(str_to_sompi(amount)?))
}

pub fn try_kaspa_str_to_sompi_with_decimals<S: Into<String>>(s: S, decimals: Option<u32>) -> Result<Option<u64>> {
pub fn try_kaspa_str_to_unit<S: Into<String>>(s: S, decimals: u32) -> Result<Option<u64>> {
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: Into<String>>(s: S) -> Result<Option<i64>> {
Expand All @@ -46,8 +46,7 @@ pub fn sompi_to_kaspa(sompi: u64) -> f64 {
}

#[inline]
pub fn sompi_to_kaspa_with_decimals(sompi: u64, decimals: Option<u32>) -> 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
Expand All @@ -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)))
Expand Down Expand Up @@ -127,8 +131,7 @@ fn str_to_sompi(amount: &str) -> Result<u64> {
Ok(integer + decimal)
}

fn str_to_sompi_with_decimals(amount: &str, decimals: Option<u32>) -> Result<u64> {
let decimals = decimals.unwrap_or(8);
fn str_to_unit(amount: &str, decimals: u32) -> Result<u64> {
let sompi_per_unit = 10u64.pow(decimals);

// Check if the amount contains a decimal point, if doesn't return value directly.
Expand Down
24 changes: 22 additions & 2 deletions wallet/core/src/wasm/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>) -> Option<BigInt> {
crate::utils::try_kaspa_str_to_sompi_with_decimals(kaspa, decimals).ok().flatten().map(Into::into)
pub fn kaspa_to_sompi(kaspa: String) -> Option<BigInt> {
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<BigInt> {
crate::utils::try_kaspa_str_to_unit(kaspa, decimals).ok().flatten().map(Into::into)
}

///
Expand All @@ -31,6 +40,17 @@ pub fn sompi_to_kaspa_string(sompi: ISompiToKaspa) -> Result<String> {
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<String> {
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,
Expand Down

0 comments on commit 5b4e972

Please sign in to comment.