Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(general): get ci green on 6219 #6711

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
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
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")?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.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
Loading