Skip to content

Commit

Permalink
Seth: --to-uint256 rough
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish-Agnihotri committed Sep 20, 2021
1 parent 3314129 commit 4cf8b59
Show file tree
Hide file tree
Showing 3 changed files with 24 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 @@ -25,6 +25,9 @@ async fn main() -> eyre::Result<()> {
Subcommands::ToFix { decimals, value } => {
println!("{}", SimpleSeth::to_fix(unwrap_or_stdin(decimals)?, unwrap_or_stdin(value)?));
}
Subcommands::ToUint256 { value } => {
println!("{}", SimpleSeth::to_uint256(value));
}
Subcommands::ToWei { value, unit } => {
println!("{}", SimpleSeth::to_wei(unwrap_or_stdin(value)?, unit.unwrap_or(String::from("wei"))));
}
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 @@ -26,6 +26,9 @@ pub enum Subcommands {
#[structopt(name = "--to-fix")]
#[structopt(about = "convert integers into fixed point with specified decimals")]
ToFix { decimals: Option<u128>, value: Option<u128> },
#[structopt(name = "--to-uint256")]
#[structopt(about = "convert a number into uint256 hex string with 0x prefix")]
ToUint256 { value: String },
#[structopt(name = "--to-wei")]
#[structopt(about = "convert an ETH amount into wei")]
ToWei { value: Option<u128>, unit: Option<String> },
Expand Down
18 changes: 18 additions & 0 deletions seth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,24 @@ impl SimpleSeth {
format!("{:#x}", u)
}

/// Converts a number into uint256 hex string with 0x prefix
///
/// ```
/// use seth::SimpleSeth as Seth;
///
/// assert_eq!(Seth::to_uint256("100".to_string()), "0x0000000000000000000000000000000000000000000000000000000000000064");
/// assert_eq!(Seth::to_uint256("192038293923".to_string()), "0x0000000000000000000000000000000000000000000000000000002cb65fd1a3");
/// assert_eq!(
/// Seth::to_uint256("115792089237316195423570985008687907853269984665640564039457584007913129639935".to_string()),
/// "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
/// );
/// ```
pub fn to_uint256(value: String) -> String {
let num_u256 = U256::from_str_radix(&value, 10).expect("Could not parse string");
let num_hex = format!("{:x}", num_u256);
format!("0x{}{}", "0".repeat(64 - num_hex.len()), num_hex)
}

/// Converts an eth amount into wei
///
/// ```
Expand Down

0 comments on commit 4cf8b59

Please sign in to comment.