Skip to content

Commit

Permalink
wallet - use precise decimal conversion when parsing strings to sompi
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Sep 14, 2023
1 parent 2083308 commit 1238659
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions wallet/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,14 @@ use kaspa_consensus_core::network::NetworkType;
use separator::Separatable;
use workflow_log::style;

pub fn try_map_kaspa_str_to_sompi(s: Option<&str>) -> Result<Option<u64>> {
if let Some(s) = s {
try_kaspa_str_to_sompi(s)
} else {
Ok(None)
}
}

pub fn try_map_kaspa_str_to_sompi_i64(s: Option<&str>) -> Result<Option<i64>> {
if let Some(s) = s {
try_kaspa_str_to_sompi_i64(s)
} else {
Ok(None)
}
}

pub fn try_kaspa_str_to_sompi<S: Into<String>>(s: S) -> Result<Option<u64>> {
let s: String = s.into();
let amount = s.trim();
if amount.is_empty() {
return Ok(None);
}

let amount = amount.parse::<f64>()? * SOMPI_PER_KASPA as f64;
Ok(Some(amount as u64))
Ok(Some(str_to_sompi(amount)?))
}

pub fn try_kaspa_str_to_sompi_i64<S: Into<String>>(s: S) -> Result<Option<i64>> {
Expand Down Expand Up @@ -90,3 +73,15 @@ pub fn format_address_colors(address: &Address, range: Option<usize>) -> String

format!("{prefix}:{left}:{center}:{right}")
}

fn str_to_sompi(amount: &str) -> Result<u64> {
let Some(dot_idx) = amount.find('.') else {
return Ok(amount.parse::<u64>()? * SOMPI_PER_KASPA);
};
let integer = amount[..dot_idx].parse::<u64>()? * SOMPI_PER_KASPA;
let decimal = &amount[dot_idx + 1..];
let decimal_len = decimal.len();
let decimal =
if decimal_len <= 8 { decimal.parse::<u64>()? * 10u64.pow(8 - decimal_len as u32) } else { decimal[..8].parse::<u64>()? };
Ok(integer + decimal)
}

0 comments on commit 1238659

Please sign in to comment.