Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

fix: parse_units was generating an error on some values #701

Merged
merged 1 commit into from
Dec 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ethers-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ where
pub fn parse_units<K, S>(amount: S, units: K) -> Result<U256, ConversionError>
where
S: ToString,
K: TryInto<Units, Error = ConversionError>,
K: TryInto<Units, Error = ConversionError> + Copy,
{
let float_n: f64 =
amount.to_string().parse::<f64>()? * 10u64.pow(units.try_into()?.as_num()) as f64;
let u256_n: U256 = U256::from_dec_str(&float_n.to_string())?;
let u256_n: U256 = U256::from_dec_str(&float_n.round().to_string())?;
Ok(u256_n)
}
/// The address for an Ethereum contract is deterministically computed from the
Expand Down Expand Up @@ -430,6 +430,9 @@ mod tests {
let gwei = parse_units(1.5, 9).unwrap();
assert_eq!(gwei.as_u64(), 15e8 as u64);

let token = parse_units(1163.56926418, 8).unwrap();
assert_eq!(token.as_u64(), 116356926418);

let eth_dec_float = parse_units(1.39563324, "ether").unwrap();
assert_eq!(eth_dec_float, U256::from_dec_str("1395633240000000000").unwrap());

Expand Down