Skip to content

Commit

Permalink
fix(general): get ci green on 6219 (#6711)
Browse files Browse the repository at this point in the history
* chore: update doctests

* chore: update cargo lock to make anvil test pass

* chore: bump cargo lock again

* fix: parse unit logics (#6713)

---------

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
  • Loading branch information
Evalir and DaniPopes authored Jan 5, 2024
1 parent 6099171 commit f984087
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,17 +1235,25 @@ impl SimpleCast {
/// assert_eq!(Cast::to_unit("1 wei", "wei")?, "1");
/// assert_eq!(Cast::to_unit("1", "wei")?, "1");
/// assert_eq!(Cast::to_unit("1ether", "wei")?, "1000000000000000000");
/// assert_eq!(Cast::to_unit("100 gwei", "gwei")?, "100");
/// # Ok::<_, eyre::Report>(())
/// ```
pub fn to_unit(value: &str, unit: &str) -> Result<String> {
let value = DynSolType::coerce_str(&DynSolType::Uint(256), value)?
.as_uint()
.wrap_err("could not convert to uint")?
.wrap_err("Could not convert to uint")?
.0;
let unit: alloy_primitives::utils::Unit = unit.parse().wrap_err("could not parse units")?;
let parsed = alloy_primitives::utils::ParseUnits::parse_units(&value.to_string(), unit)?;
Ok(parsed.to_string())
let unit = unit.parse().wrap_err("could not parse units")?;
let mut formatted = ParseUnits::U256(value).format_units(unit);

// Trim empty fractional part.
if let Some(dot) = formatted.find('.') {
let fractional = &formatted[dot + 1..];
if fractional.chars().all(|c: char| c == '0') {
formatted = formatted[..dot].to_string();
}
}

Ok(formatted)
}

/// Converts wei into an eth amount
Expand Down Expand Up @@ -1274,14 +1282,14 @@ impl SimpleCast {
/// ```
/// use cast::SimpleCast as Cast;
///
/// assert_eq!(Cast::to_wei("1", "")?, "1000000000000000000");
/// assert_eq!(Cast::to_wei("100", "gwei")?, "100000000000");
/// assert_eq!(Cast::to_wei("100", "eth")?, "100000000000000000000");
/// assert_eq!(Cast::to_wei("1000", "ether")?, "1000000000000000000000");
/// # Ok::<_, eyre::Report>(())
/// ```
pub fn to_wei(value: &str, unit: &str) -> Result<String> {
Ok(ParseUnits::parse_units(value, unit.parse()?)?.to_string())
let unit = unit.parse().wrap_err("could not parse units")?;
Ok(ParseUnits::parse_units(value, unit)?.to_string())
}

/// Decodes rlp encoded list with hex data
Expand Down

0 comments on commit f984087

Please sign in to comment.