diff --git a/README.md b/README.md index f4d0d24b2dd7..e61c326d0214 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ ARGS: * seth * [x] `--from-ascii` + * [x] `--to-hex` * [x] `--to-checksum-address` * [x] `--to-bytes32` * [x] `block` diff --git a/dapptools/src/seth.rs b/dapptools/src/seth.rs index 846771f0bd9a..63e88a6e11d3 100644 --- a/dapptools/src/seth.rs +++ b/dapptools/src/seth.rs @@ -18,6 +18,9 @@ pub enum Subcommands { #[structopt(name = "--from-ascii")] #[structopt(about = "convert text data into hexdata")] FromAscii { text: String }, + #[structopt(name = "--to-hex")] + #[structopt(about = "convert a decimal number into hex")] + ToHex { decimal: Option }, #[structopt(name = "--to-checksum-address")] #[structopt(about = "convert an address to a checksummed format (EIP-55)")] ToCheckSumAddress { address: Address }, @@ -131,6 +134,9 @@ async fn main() -> eyre::Result<()> { Subcommands::FromAscii { text } => { println!("{}", SimpleSeth::from_ascii(&text)); } + Subcommands::ToHex { decimal } => { + println!("{}", SimpleSeth::to_hex(unwrap_or_stdin(decimal)?)); + } Subcommands::ToCheckSumAddress { address } => { println!("{}", SimpleSeth::to_checksum_address(&address)?); } diff --git a/seth/src/lib.rs b/seth/src/lib.rs index a4e77a7141ca..fed3ffd42399 100644 --- a/seth/src/lib.rs +++ b/seth/src/lib.rs @@ -211,6 +211,17 @@ impl SimpleSeth { let s: String = s.as_bytes().to_hex(); format!("0x{}", s) } + /// Converts decimal input to hex + /// + /// ``` + /// use seth::SimpleSeth as Seth; + /// + /// assert_eq!(Seth::to_hex(424242), "0x67932"); + /// assert_eq!(Seth::to_hex(1234), "0x4d2"); + /// ``` + pub fn to_hex(u: u128) -> String { + format!("{:#x}", u) + } /// Converts an Ethereum address to its checksum format /// according to [EIP-55](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md)