Skip to content

Commit

Permalink
feat/cast: adds --to-unit (#511)
Browse files Browse the repository at this point in the history
* add --to-unit

* trim if only decimal 0s are found

* lint

* add another relevant example

* fix doctest
  • Loading branch information
charisma98 committed Jan 19, 2022
1 parent 529fda7 commit a536e06
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
34 changes: 33 additions & 1 deletion cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
//! TODO
use chrono::NaiveDateTime;
use ethers_core::{
abi::{Abi, AbiParser, Token},
abi::{
token::{LenientTokenizer, Tokenizer},
Abi, AbiParser, Token,
},
types::{Chain, *},
utils::{self, keccak256},
};
Expand Down Expand Up @@ -786,6 +789,35 @@ impl SimpleCast {
Ok(format!("0x{}{}", "0".repeat(64 - num_hex.len()), num_hex))
}

/// Converts an eth amount into a specified unit
///
/// ```
/// use cast::SimpleCast as Cast;
///
/// fn main() -> eyre::Result<()> {
/// assert_eq!(Cast::to_unit("1 wei".to_string(), "wei".to_string())?, "1");
/// assert_eq!(Cast::to_unit("1".to_string(), "wei".to_string())?, "1");
/// assert_eq!(Cast::to_unit("1ether".to_string(), "wei".to_string())?, "1000000000000000000");
/// assert_eq!(Cast::to_unit("100 gwei".to_string(), "gwei".to_string())?, "100");
///
/// Ok(())
/// }
/// ```
pub fn to_unit(value: String, unit: String) -> Result<String> {
let value = U256::from(LenientTokenizer::tokenize_uint(&value)?);

Ok(match &unit[..] {
"ether" => ethers_core::utils::format_units(value, 18)?
.trim_end_matches(".000000000000000000")
.to_string(),
"gwei" | "nano" | "nanoether" => ethers_core::utils::format_units(value, 9)?
.trim_end_matches(".000000000")
.to_string(),
"wei" => ethers_core::utils::format_units(value, 0)?.trim_end_matches(".0").to_string(),
_ => return Err(eyre::eyre!("invalid unit")),
})
}

/// Converts an eth amount into wei
///
/// ```
Expand Down
4 changes: 4 additions & 0 deletions cli/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ async fn main() -> eyre::Result<()> {
let val = unwrap_or_stdin(value)?;
println!("{}", SimpleCast::to_uint256(&val)?);
}
Subcommands::ToUnit { value, unit } => {
let val = unwrap_or_stdin(value)?;
println!("{}", SimpleCast::to_unit(val, unit.unwrap_or_else(|| String::from("wei")))?);
}
Subcommands::ToWei { value, unit } => {
let val = unwrap_or_stdin(value)?;
println!(
Expand Down
16 changes: 14 additions & 2 deletions cli/src/opts/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,23 @@ pub enum Subcommands {
#[clap(name = "--to-uint256")]
#[clap(about = "convert a number into uint256 hex string with 0x prefix")]
ToUint256 { value: Option<String> },
#[clap(name = "--to-unit")]
#[clap(
about = r#"convert an ETH amount into a specified unit: ether, gwei or wei (default: wei).
Usage:
- 1ether wei | converts 1 ether to wei
- "1 ether" wei | converts 1 ether to wei
- 1ether | converts 1 ether to wei
- 1 gwei | converts 1 wei to gwei
- 1gwei ether | converts 1 gwei to ether
"#
)]
ToUnit { value: Option<String>, unit: Option<String> },
#[clap(name = "--to-wei")]
#[clap(about = "convert an ETH amount into wei")]
#[clap(about = "convert an ETH amount into wei. Consider using --to-unit.")]
ToWei { value: Option<String>, unit: Option<String> },
#[clap(name = "--from-wei")]
#[clap(about = "convert wei into an ETH amount")]
#[clap(about = "convert wei into an ETH amount. Consider using --to-unit.")]
FromWei { value: Option<String>, unit: Option<String> },
#[clap(name = "block")]
#[clap(
Expand Down

0 comments on commit a536e06

Please sign in to comment.