Skip to content

Commit

Permalink
Seth: --to-dec
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish-Agnihotri committed Sep 20, 2021
1 parent 4cf8b59 commit 4364e44
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dapptools/src/seth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ async fn main() -> eyre::Result<()> {
Subcommands::ToBytes32 { bytes } => {
println!("{}", SimpleSeth::to_bytes32(&bytes)?);
}
Subcommands::ToDec { hexvalue } => {
println!("{}", SimpleSeth::to_dec(hexvalue));
}
Subcommands::ToFix { decimals, value } => {
println!("{}", SimpleSeth::to_fix(unwrap_or_stdin(decimals)?, unwrap_or_stdin(value)?));
}
Expand Down
3 changes: 3 additions & 0 deletions dapptools/src/seth_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub enum Subcommands {
#[structopt(name = "--to-bytes32")]
#[structopt(about = "left-pads a hex bytes string to 32 bytes)")]
ToBytes32 { bytes: String },
#[structopt(name = "--to-dec")]
#[structopt(about = "convert hex value into a decimal number")]
ToDec { hexvalue: String },
#[structopt(name = "--to-fix")]
#[structopt(about = "convert integers into fixed point with specified decimals")]
ToFix { decimals: Option<u128>, value: Option<u128> },
Expand Down
13 changes: 13 additions & 0 deletions seth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,19 @@ impl SimpleSeth {
format!("0x{}", s)
}

/// Converts hex input to decimal
///
/// ```
/// use seth::SimpleSeth as Seth;
///
/// assert_eq!(424242, Seth::to_dec("0x67932"));
/// assert_eq!(1234, Seth::to_dec("0x4d2"));
/// ```
pub fn to_dec(hex: String) -> u128 {
let hex_trimmed = hex.trim_start_matches("0x");
u128::from_str_radix(&hex_trimmed, 16).expect("Could not parse hex")
}

/// Converts integers with specified decimals into fixed point numbers
///
/// ```
Expand Down

0 comments on commit 4364e44

Please sign in to comment.