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

feat: add cast --from-wei #261

Merged
merged 2 commits into from
Dec 19, 2021
Merged

feat: add cast --from-wei #261

merged 2 commits into from
Dec 19, 2021

Conversation

ncitron
Copy link
Contributor

@ncitron ncitron commented Dec 19, 2021

One thing I was not sure about here was how to handle the default behavior when a unit string is not supplied. In dapptools, it defaults to ether, however this is not consistent with the behavior of both dapptools and foundry for --to-wei, which defaults to wei when no unit string is supplied. I chose to make it default to wei, but can change it to ether if that is preferred.

/// Ok(())
/// }
/// ```
pub fn from_wei(value: U256, unit: String) -> Result<String> {
Copy link
Member

Choose a reason for hiding this comment

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

Have you considered using the ethers_core::utils::parse_units and format_units methods? https://github.com/gakonst/ethers-rs/blob/master/ethers-core/src/utils/mod.rs#L103-L147

/// assert_eq!(Cast::from_wei(12340000005u64.into(), "gwei".to_string())?, "12.340000005");
/// assert_eq!(Cast::from_wei(10.into(), "ether".to_string())?, "0.00000000000000001");
/// assert_eq!(Cast::from_wei(100.into(), "eth".to_string())?, "0.0000000000000001");
/// assert_eq!(Cast::from_wei(17.into(), "".to_string())?, "17");
Copy link
Member

Choose a reason for hiding this comment

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

Yeah I agree with defaulting to this

@gakonst gakonst merged commit 5275fff into foundry-rs:master Dec 19, 2021
@gakonst gakonst mentioned this pull request Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants