diff --git a/crates/cast/src/args.rs b/crates/cast/src/args.rs index 4538512f93c19..4ca715f4f3e0c 100644 --- a/crates/cast/src/args.rs +++ b/crates/cast/src/args.rs @@ -1,7 +1,7 @@ use crate::{ Cast, SimpleCast, cmd::erc20::IERC20, - opts::{Cast as CastArgs, CastSubcommand, ToBaseArgs}, + opts::{Cast as CastArgs, CastSubcommand}, traces::identifier::SignaturesIdentifier, }; use alloy_consensus::transaction::{Recovered, SignerRecoverable}; @@ -64,122 +64,6 @@ pub async fn run_command(args: CastArgs) -> Result<()> { CastSubcommand::HashZero => { sh_println!("{:?}", B256::ZERO)?; } - - // Conversions & transformations - CastSubcommand::FromUtf8 { text } => { - let value = stdin::unwrap(text, false)?; - sh_println!("{}", SimpleCast::from_utf8(&value))? - } - CastSubcommand::ToAscii { hexdata } => { - let value = stdin::unwrap(hexdata, false)?; - sh_println!("{}", SimpleCast::to_ascii(value.trim())?)? - } - CastSubcommand::ToUtf8 { hexdata } => { - let value = stdin::unwrap(hexdata, false)?; - sh_println!("{}", SimpleCast::to_utf8(&value)?)? - } - CastSubcommand::FromFixedPoint { value, decimals } => { - let (value, decimals) = stdin::unwrap2(value, decimals)?; - sh_println!("{}", SimpleCast::from_fixed_point(&value, &decimals)?)? - } - CastSubcommand::ToFixedPoint { value, decimals } => { - let (value, decimals) = stdin::unwrap2(value, decimals)?; - sh_println!("{}", SimpleCast::to_fixed_point(&value, &decimals)?)? - } - CastSubcommand::ConcatHex { data } => { - if data.is_empty() { - let s = stdin::read(true)?; - sh_println!("{}", SimpleCast::concat_hex(s.split_whitespace()))? - } else { - sh_println!("{}", SimpleCast::concat_hex(data))? - } - } - CastSubcommand::FromBin => { - let hex = stdin::read_bytes(false)?; - sh_println!("{}", hex::encode_prefixed(hex))? - } - CastSubcommand::ToHexdata { input } => { - let value = stdin::unwrap_line(input)?; - let output = match value { - s if s.starts_with('@') => hex::encode(std::env::var(&s[1..])?), - s if s.starts_with('/') => hex::encode(fs::read(s)?), - s => s.split(':').map(|s| s.trim_start_matches("0x").to_lowercase()).collect(), - }; - sh_println!("0x{output}")? - } - CastSubcommand::ToCheckSumAddress { address, chain_id } => { - let value = stdin::unwrap_line(address)?; - sh_println!("{}", value.to_checksum(chain_id))? - } - CastSubcommand::ToUint256 { value } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::to_uint256(&value)?)? - } - CastSubcommand::ToInt256 { value } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::to_int256(&value)?)? - } - CastSubcommand::ToUnit { value, unit } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::to_unit(&value, &unit)?)? - } - CastSubcommand::ParseUnits { value, unit } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::parse_units(&value, unit)?)?; - } - CastSubcommand::FormatUnits { value, unit } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::format_units(&value, unit)?)?; - } - CastSubcommand::FromWei { value, unit } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::from_wei(&value, &unit)?)? - } - CastSubcommand::ToWei { value, unit } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::to_wei(&value, &unit)?)? - } - CastSubcommand::FromRlp { value, as_int } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::from_rlp(value, as_int)?)? - } - CastSubcommand::ToRlp { value } => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::to_rlp(&value)?)? - } - CastSubcommand::ToHex(ToBaseArgs { value, base_in }) => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::to_base(&value, base_in.as_deref(), "hex")?)? - } - CastSubcommand::ToDec(ToBaseArgs { value, base_in }) => { - let value = stdin::unwrap_line(value)?; - sh_println!("{}", SimpleCast::to_base(&value, base_in.as_deref(), "dec")?)? - } - CastSubcommand::ToBase { base: ToBaseArgs { value, base_in }, base_out } => { - let (value, base_out) = stdin::unwrap2(value, base_out)?; - sh_println!("{}", SimpleCast::to_base(&value, base_in.as_deref(), &base_out)?)? - } - CastSubcommand::ToBytes32 { bytes } => { - let value = stdin::unwrap_line(bytes)?; - sh_println!("{}", SimpleCast::to_bytes32(&value)?)? - } - CastSubcommand::Pad { data, right, left: _, len } => { - let value = stdin::unwrap_line(data)?; - sh_println!("{}", SimpleCast::pad(&value, right, len)?)? - } - CastSubcommand::FormatBytes32String { string } => { - let value = stdin::unwrap_line(string)?; - sh_println!("{}", SimpleCast::format_bytes32_string(&value)?)? - } - CastSubcommand::ParseBytes32String { bytes } => { - let value = stdin::unwrap_line(bytes)?; - sh_println!("{}", SimpleCast::parse_bytes32_string(&value)?)? - } - CastSubcommand::ParseBytes32Address { bytes } => { - let value = stdin::unwrap_line(bytes)?; - sh_println!("{}", SimpleCast::parse_bytes32_address(&value)?)? - } - // ABI encoding & decoding CastSubcommand::DecodeAbi { sig, calldata, input } => { let tokens = SimpleCast::abi_decode(&sig, &calldata, input)?; @@ -756,6 +640,9 @@ pub async fn run_command(args: CastArgs) -> Result<()> { CastSubcommand::DAEstimate(cmd) => { cmd.run().await?; } + CastSubcommand::Convert { command } => { + command.run().await?; + } }; /// Prints slice of tokens using [`format_tokens`] or [`serialize_value_as_json`] depending diff --git a/crates/cast/src/cmd/convert.rs b/crates/cast/src/cmd/convert.rs new file mode 100644 index 0000000000000..8b54a29e1b115 --- /dev/null +++ b/crates/cast/src/cmd/convert.rs @@ -0,0 +1,435 @@ +use crate::SimpleCast; +use alloy_primitives::{Address, hex}; +use clap::Parser; +use eyre::Result; +use foundry_common::{sh_println, stdin}; +use std::{env, fs}; + +/// CLI arguments for `cast --to-base`. +#[derive(Debug, Parser)] +pub struct ToBaseArgs { + /// The value to convert. + #[arg(allow_hyphen_values = true)] + pub value: Option, + + /// The input base. + #[arg(long, short = 'i')] + pub base_in: Option, +} +#[derive(Debug, Parser)] +pub enum ConvertSubCommand { + /// Convert wei into an ETH amount. + /// + /// Consider using --to-unit. + #[command(visible_aliases = &["--from-wei", "fw"])] + FromWei { + /// The value to convert. + #[arg(allow_hyphen_values = true)] + value: Option, + + /// The unit to convert from (ether, gwei, wei). + #[arg(default_value = "eth")] + unit: String, + }, + /// Convert an ETH amount to wei. + /// + /// Consider using --to-unit. + #[command(visible_aliases = &["--to-wei", "tw", "2w"])] + ToWei { + /// The value to convert. + #[arg(allow_hyphen_values = true)] + value: Option, + + /// The unit to convert from (ether, gwei, wei). + #[arg(default_value = "eth")] + unit: String, + }, + /// Convert an ETH amount into another unit (ether, gwei or wei). + /// + /// Examples: + /// - 1ether wei + /// - "1 ether" wei + /// - 1ether + /// - 1 gwei + /// - 1gwei ether + #[command(visible_aliases = &["--to-unit", "tun", "2un"])] + ToUnit { + /// The value to convert. + value: Option, + + /// The unit to convert to (ether, gwei, wei). + #[arg(default_value = "wei")] + unit: String, + }, + /// Convert an integer into a fixed point number. + #[command(visible_aliases = &["--to-fix", "tf", "2f"])] + ToFixedPoint { + /// The number of decimals to use. + decimals: Option, + + /// The value to convert. + #[arg(allow_hyphen_values = true)] + value: Option, + }, + /// Convert a fixed point number into an integer. + #[command(visible_aliases = &["--from-fix", "ff"])] + FromFixedPoint { + /// The number of decimals to use. + decimals: Option, + + /// The value to convert. + #[arg(allow_hyphen_values = true)] + value: Option, + }, + /// Format a number from smallest unit to decimal with arbitrary decimals. + /// + /// Examples: + /// - 1000000 6 (for USDC, result: 1.0) + /// - 2500000000000 12 (for 12 decimals, result: 2.5) + /// - 1230 3 (for 3 decimals, result: 1.23) + #[command(visible_aliases = &["--format-units", "fun"])] + FormatUnits { + /// The value to format. + value: Option, + + /// The unit to format to. + #[arg(default_value = "18")] + unit: u8, + }, + /// Convert a number from decimal to smallest unit with arbitrary decimals. + /// + /// Examples: + /// - 1.0 6 (for USDC, result: 1000000) + /// - 2.5 12 (for 12 decimals token, result: 2500000000000) + /// - 1.23 3 (for 3 decimals token, result: 1230) + #[command(visible_aliases = &["--parse-units", "pun"])] + ParseUnits { + /// The value to convert. + value: Option, + + /// The unit to convert to. + #[arg(default_value = "18")] + unit: u8, + }, + /// Converts a number of one base to another + #[command(visible_aliases = &["--to-hex", "th", "2h"])] + ToHex(ToBaseArgs), + + /// Converts a number of one base to decimal + #[command(visible_aliases = &["--to-dec", "td", "2d"])] + ToDec(ToBaseArgs), + /// Converts a number of one base to another + #[command( + visible_aliases = &["--to-base", + "--to-radix", + "to-radix", + "tr", + "2r"] + )] + ToBase { + #[command(flatten)] + base: ToBaseArgs, + + /// The output base. + #[arg(value_name = "BASE")] + base_out: Option, + }, + /// Convert a number to a hex-encoded int256. + #[command(name = "to-int256", visible_aliases = &["--to-int256", "ti", "2i"])] + ToInt256 { + /// The value to convert. + value: Option, + }, + /// Convert a number to a hex-encoded uint256. + #[command(name = "to-uint256", visible_aliases = &["--to-uint256", "tu", "2u"])] + ToUint256 { + /// The value to convert. + value: Option, + }, + + // New commands copied from opts.rs + /// Convert UTF-8 text to hex. + #[command( + visible_aliases = &[ + "--from-ascii", + "--from-utf8", + "from-ascii", + "fu", + "fa"] + )] + FromUtf8 { + /// The text to convert. + text: Option, + }, + + /// Convert hex data to a utf-8 string. + #[command(visible_aliases = &["--to-utf8", "tu8", "2u8"])] + ToUtf8 { + /// The hex data to convert. + hexdata: Option, + }, + + /// Convert hex data to an ASCII string. + #[command(visible_aliases = &["--to-ascii", "tas", "2as"])] + ToAscii { + /// The hex data to convert. + hexdata: Option, + }, + + /// Convert binary data into hex data. + #[command(visible_aliases = &["--from-bin", "from-binx", "fb"])] + FromBin, + + /// Right-pads hex data to 32 bytes. + #[command(visible_aliases = &["--to-bytes32", "tb", "2b"])] + ToBytes32 { + /// The hex data to convert. + bytes: Option, + }, + + /// Normalize the input to lowercase, 0x-prefixed hex. + /// + /// The input can be: + /// - mixed case hex with or without 0x prefix + /// - 0x prefixed hex, concatenated with a ':' + /// - an absolute path to file + /// - @tag, where the tag is defined in an environment variable + #[command(visible_aliases = &["--to-hexdata", "thd", "2hd"])] + ToHexdata { + /// The input to normalize. + input: Option, + }, + + /// Formats a string into bytes32 encoding. + #[command(name = "format-bytes32-string", visible_aliases = &["--format-bytes32-string"])] + FormatBytes32String { + /// The string to format. + string: Option, + }, + + /// Parses a string from bytes32 encoding. + #[command(name = "parse-bytes32-string", visible_aliases = &["--parse-bytes32-string"])] + ParseBytes32String { + /// The string to parse. + bytes: Option, + }, + + /// Parses a checksummed address from bytes32 encoding. + #[command(name = "parse-bytes32-address", visible_aliases = &["--parse-bytes32-address"])] + ParseBytes32Address { + #[arg(value_name = "BYTES")] + bytes: Option, + }, + + /// Convert an address to a checksummed format (EIP-55). + #[command( + visible_aliases = &["--to-checksum-address", + "--to-checksum", + "to-checksum", + "ta", + "2a"] + )] + ToCheckSumAddress { + /// The address to convert. + address: Option
, + /// EIP-155 chain ID to encode the address using EIP-1191. + chain_id: Option, + }, + + /// Concatenate hex strings. + #[command(visible_aliases = &["--concat-hex", "ch"])] + ConcatHex { + /// The data to concatenate. + data: Vec, + }, + + /// Pads hex data to a specified length. + #[command(visible_aliases = &["pd"])] + Pad { + /// The hex data to pad. + data: Option, + + /// Right-pad the data (instead of left-pad). + #[arg(long)] + right: bool, + + /// Left-pad the data (default). + #[arg(long, conflicts_with = "right")] + left: bool, + + /// Target length in bytes (default: 32). + #[arg(long, default_value = "32")] + len: usize, + }, + + /// RLP encode the given JSON data. + /// + /// Example: cast to-rlp "[\"0x61\"]" -> `0xc161` + /// - `cast to-rlp "[\"0x61\"]"` -> `0xc161` + /// - `cast to-rlp "[\"0xf1\", \"f2\"]"` -> `0xc481f181f2` + #[command(visible_aliases = &["--to-rlp"])] + ToRlp { + /// The value to convert. + /// + /// This is a hex-encoded string, or an array of hex-encoded strings. + /// Can be arbitrarily recursive. + value: Option, + }, + + /// Decodes RLP hex-encoded data. + #[command(visible_aliases = &["--from-rlp"])] + FromRlp { + /// The RLP hex-encoded data. + value: Option, + + /// Decode the RLP data as int + #[arg(long, alias = "int")] + as_int: bool, + }, +} + +impl ConvertSubCommand { + pub async fn run(self) -> Result<()> { + match self { + // Wei/Ether unit conversions + Self::FromWei { value, unit } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::from_wei(&value, &unit)?)?; + } + Self::ToWei { value, unit } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::to_wei(&value, &unit)?)?; + } + Self::ToUnit { value, unit } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::to_unit(&value, &unit)?)?; + } + + // Fixed point conversions + Self::ToFixedPoint { value, decimals } => { + let (value, decimals) = stdin::unwrap2(value, decimals)?; + sh_println!("{}", SimpleCast::to_fixed_point(&value, &decimals)?)?; + } + Self::FromFixedPoint { value, decimals } => { + let (value, decimals) = stdin::unwrap2(value, decimals)?; + sh_println!("{}", SimpleCast::from_fixed_point(&value, &decimals)?)?; + } + + // Unit parsing and formatting + Self::FormatUnits { value, unit } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::format_units(&value, unit)?)?; + } + Self::ParseUnits { value, unit } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::parse_units(&value, unit)?)?; + } + + // Base conversions + Self::ToHex(ToBaseArgs { value, base_in }) => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::to_base(&value, base_in.as_deref(), "hex")?)?; + } + Self::ToDec(ToBaseArgs { value, base_in }) => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::to_base(&value, base_in.as_deref(), "dec")?)?; + } + Self::ToBase { base: ToBaseArgs { value, base_in }, base_out } => { + let (value, base_out) = stdin::unwrap2(value, base_out)?; + sh_println!("{}", SimpleCast::to_base(&value, base_in.as_deref(), &base_out)?)?; + } + + // Integer conversions + Self::ToInt256 { value } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::to_int256(&value)?)?; + } + Self::ToUint256 { value } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::to_uint256(&value)?)?; + } + + // String/Hex conversions + Self::FromUtf8 { text } => { + let value = stdin::unwrap(text, false)?; + sh_println!("{}", SimpleCast::from_utf8(&value))?; + } + Self::ToUtf8 { hexdata } => { + let value = stdin::unwrap(hexdata, false)?; + sh_println!("{}", SimpleCast::to_utf8(&value)?)?; + } + Self::ToAscii { hexdata } => { + let value = stdin::unwrap(hexdata, false)?; + sh_println!("{}", SimpleCast::to_ascii(value.trim())?)?; + } + + // Binary conversion + Self::FromBin => { + let hex = stdin::read_bytes(false)?; + sh_println!("{}", hex::encode_prefixed(hex))?; + } + + // Bytes32 conversions + Self::ToBytes32 { bytes } => { + let value = stdin::unwrap_line(bytes)?; + sh_println!("{}", SimpleCast::to_bytes32(&value)?)?; + } + + // Hex data normalization + Self::ToHexdata { input } => { + let value = stdin::unwrap_line(input)?; + let output = match value { + s if s.starts_with('@') => hex::encode(env::var(&s[1..])?), + s if s.starts_with('/') => hex::encode(fs::read(s)?), + s => s.split(':').map(|s| s.trim_start_matches("0x").to_lowercase()).collect(), + }; + sh_println!("0x{output}")?; + } + + // Bytes32 string operations + Self::FormatBytes32String { string } => { + let value = stdin::unwrap_line(string)?; + sh_println!("{}", SimpleCast::format_bytes32_string(&value)?)?; + } + Self::ParseBytes32String { bytes } => { + let value = stdin::unwrap_line(bytes)?; + sh_println!("{}", SimpleCast::parse_bytes32_string(&value)?)?; + } + Self::ParseBytes32Address { bytes } => { + let value = stdin::unwrap_line(bytes)?; + sh_println!("{}", SimpleCast::parse_bytes32_address(&value)?)?; + } + + // Address checksum + Self::ToCheckSumAddress { address, chain_id } => { + let value = stdin::unwrap_line(address)?; + sh_println!("{}", value.to_checksum(chain_id))?; + } + + // Hex operations + Self::ConcatHex { data } => { + if data.is_empty() { + let s = stdin::read(true)?; + sh_println!("{}", SimpleCast::concat_hex(s.split_whitespace()))?; + } else { + sh_println!("{}", SimpleCast::concat_hex(data))?; + } + } + Self::Pad { data, right, left: _, len } => { + let value = stdin::unwrap_line(data)?; + sh_println!("{}", SimpleCast::pad(&value, right, len)?)?; + } + + // RLP operations + Self::ToRlp { value } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::to_rlp(&value)?)?; + } + Self::FromRlp { value, as_int } => { + let value = stdin::unwrap_line(value)?; + sh_println!("{}", SimpleCast::from_rlp(value, as_int)?)?; + } + } + Ok(()) + } +} diff --git a/crates/cast/src/cmd/mod.rs b/crates/cast/src/cmd/mod.rs index 782a34071cf71..174e8857250d8 100644 --- a/crates/cast/src/cmd/mod.rs +++ b/crates/cast/src/cmd/mod.rs @@ -11,6 +11,7 @@ pub mod b2e_payload; pub mod bind; pub mod call; pub mod constructor_args; +pub mod convert; pub mod create2; pub mod creation_code; pub mod da_estimate; diff --git a/crates/cast/src/opts.rs b/crates/cast/src/opts.rs index b52d38c4c73c8..9374531e42e3e 100644 --- a/crates/cast/src/opts.rs +++ b/crates/cast/src/opts.rs @@ -1,10 +1,11 @@ use crate::cmd::{ access_list::AccessListArgs, artifact::ArtifactArgs, b2e_payload::B2EPayloadArgs, - bind::BindArgs, call::CallArgs, constructor_args::ConstructorArgsArgs, create2::Create2Args, - creation_code::CreationCodeArgs, da_estimate::DAEstimateArgs, erc20::Erc20Subcommand, - estimate::EstimateArgs, find_block::FindBlockArgs, interface::InterfaceArgs, logs::LogsArgs, - mktx::MakeTxArgs, rpc::RpcArgs, run::RunArgs, send::SendTxArgs, storage::StorageArgs, - txpool::TxPoolSubcommands, wallet::WalletSubcommands, + bind::BindArgs, call::CallArgs, constructor_args::ConstructorArgsArgs, + convert::ConvertSubCommand, create2::Create2Args, creation_code::CreationCodeArgs, + da_estimate::DAEstimateArgs, erc20::Erc20Subcommand, estimate::EstimateArgs, + find_block::FindBlockArgs, interface::InterfaceArgs, logs::LogsArgs, mktx::MakeTxArgs, + rpc::RpcArgs, run::RunArgs, send::SendTxArgs, storage::StorageArgs, txpool::TxPoolSubcommands, + wallet::WalletSubcommands, }; use alloy_ens::NameOrAddress; use alloy_primitives::{Address, B256, Selector, U256}; @@ -66,135 +67,6 @@ pub enum CastSubcommand { #[command(visible_aliases = &["--hash-zero", "hz"])] HashZero, - /// Convert UTF8 text to hex. - #[command( - visible_aliases = &[ - "--from-ascii", - "--from-utf8", - "from-ascii", - "fu", - "fa"] - )] - FromUtf8 { - /// The text to convert. - text: Option, - }, - - /// Concatenate hex strings. - #[command(visible_aliases = &["--concat-hex", "ch"])] - ConcatHex { - /// The data to concatenate. - data: Vec, - }, - - /// Convert binary data into hex data. - #[command(visible_aliases = &["--from-bin", "from-binx", "fb"])] - FromBin, - - /// Normalize the input to lowercase, 0x-prefixed hex. - /// - /// The input can be: - /// - mixed case hex with or without 0x prefix - /// - 0x prefixed hex, concatenated with a ':' - /// - an absolute path to file - /// - @tag, where the tag is defined in an environment variable - #[command(visible_aliases = &["--to-hexdata", "thd", "2hd"])] - ToHexdata { - /// The input to normalize. - input: Option, - }, - - /// Convert an address to a checksummed format (EIP-55). - #[command( - visible_aliases = &["--to-checksum-address", - "--to-checksum", - "to-checksum", - "ta", - "2a"] - )] - ToCheckSumAddress { - /// The address to convert. - address: Option
, - /// EIP-155 chain ID to encode the address using EIP-1191. - chain_id: Option, - }, - - /// Convert hex data to an ASCII string. - #[command(visible_aliases = &["--to-ascii", "tas", "2as"])] - ToAscii { - /// The hex data to convert. - hexdata: Option, - }, - - /// Convert hex data to a utf-8 string. - #[command(visible_aliases = &["--to-utf8", "tu8", "2u8"])] - ToUtf8 { - /// The hex data to convert. - hexdata: Option, - }, - - /// Convert a fixed point number into an integer. - #[command(visible_aliases = &["--from-fix", "ff"])] - FromFixedPoint { - /// The number of decimals to use. - decimals: Option, - - /// The value to convert. - #[arg(allow_hyphen_values = true)] - value: Option, - }, - - /// Right-pads hex data to 32 bytes. - #[command(visible_aliases = &["--to-bytes32", "tb", "2b"])] - ToBytes32 { - /// The hex data to convert. - bytes: Option, - }, - - /// Pads hex data to a specified length. - #[command(visible_aliases = &["pd"])] - Pad { - /// The hex data to pad. - data: Option, - - /// Right-pad the data (instead of left-pad). - #[arg(long)] - right: bool, - - /// Left-pad the data (default). - #[arg(long, conflicts_with = "right")] - left: bool, - - /// Target length in bytes (default: 32). - #[arg(long, default_value = "32")] - len: usize, - }, - - /// Convert an integer into a fixed point number. - #[command(visible_aliases = &["--to-fix", "tf", "2f"])] - ToFixedPoint { - /// The number of decimals to use. - decimals: Option, - - /// The value to convert. - #[arg(allow_hyphen_values = true)] - value: Option, - }, - - /// Convert a number to a hex-encoded uint256. - #[command(name = "to-uint256", visible_aliases = &["--to-uint256", "tu", "2u"])] - ToUint256 { - /// The value to convert. - value: Option, - }, - - /// Convert a number to a hex-encoded int256. - #[command(name = "to-int256", visible_aliases = &["--to-int256", "ti", "2i"])] - ToInt256 { - /// The value to convert. - value: Option, - }, - /// Perform a left shifting operation #[command(name = "shl")] LeftShift { @@ -231,138 +103,6 @@ pub enum CastSubcommand { base_out: String, }, - /// Convert an ETH amount into another unit (ether, gwei or wei). - /// - /// Examples: - /// - 1ether wei - /// - "1 ether" wei - /// - 1ether - /// - 1 gwei - /// - 1gwei ether - #[command(visible_aliases = &["--to-unit", "tun", "2un"])] - ToUnit { - /// The value to convert. - value: Option, - - /// The unit to convert to (ether, gwei, wei). - #[arg(default_value = "wei")] - unit: String, - }, - - /// Convert a number from decimal to smallest unit with arbitrary decimals. - /// - /// Examples: - /// - 1.0 6 (for USDC, result: 1000000) - /// - 2.5 12 (for 12 decimals token, result: 2500000000000) - /// - 1.23 3 (for 3 decimals token, result: 1230) - #[command(visible_aliases = &["--parse-units", "pun"])] - ParseUnits { - /// The value to convert. - value: Option, - - /// The unit to convert to. - #[arg(default_value = "18")] - unit: u8, - }, - - /// Format a number from smallest unit to decimal with arbitrary decimals. - /// - /// Examples: - /// - 1000000 6 (for USDC, result: 1.0) - /// - 2500000000000 12 (for 12 decimals, result: 2.5) - /// - 1230 3 (for 3 decimals, result: 1.23) - #[command(visible_aliases = &["--format-units", "fun"])] - FormatUnits { - /// The value to format. - value: Option, - - /// The unit to format to. - #[arg(default_value = "18")] - unit: u8, - }, - - /// Convert an ETH amount to wei. - /// - /// Consider using --to-unit. - #[command(visible_aliases = &["--to-wei", "tw", "2w"])] - ToWei { - /// The value to convert. - #[arg(allow_hyphen_values = true)] - value: Option, - - /// The unit to convert from (ether, gwei, wei). - #[arg(default_value = "eth")] - unit: String, - }, - - /// Convert wei into an ETH amount. - /// - /// Consider using --to-unit. - #[command(visible_aliases = &["--from-wei", "fw"])] - FromWei { - /// The value to convert. - #[arg(allow_hyphen_values = true)] - value: Option, - - /// The unit to convert from (ether, gwei, wei). - #[arg(default_value = "eth")] - unit: String, - }, - - /// RLP encodes hex data, or an array of hex data. - /// - /// Accepts a hex-encoded string, or an array of hex-encoded strings. - /// Can be arbitrarily recursive. - /// - /// Examples: - /// - `cast to-rlp "[]"` -> `0xc0` - /// - `cast to-rlp "0x22"` -> `0x22` - /// - `cast to-rlp "[\"0x61\"]"` -> `0xc161` - /// - `cast to-rlp "[\"0xf1\", \"f2\"]"` -> `0xc481f181f2` - #[command(visible_aliases = &["--to-rlp"])] - ToRlp { - /// The value to convert. - /// - /// This is a hex-encoded string, or an array of hex-encoded strings. - /// Can be arbitrarily recursive. - value: Option, - }, - - /// Decodes RLP hex-encoded data. - #[command(visible_aliases = &["--from-rlp"])] - FromRlp { - /// The RLP hex-encoded data. - value: Option, - - /// Decode the RLP data as int - #[arg(long, alias = "int")] - as_int: bool, - }, - - /// Converts a number of one base to another - #[command(visible_aliases = &["--to-hex", "th", "2h"])] - ToHex(ToBaseArgs), - - /// Converts a number of one base to decimal - #[command(visible_aliases = &["--to-dec", "td", "2d"])] - ToDec(ToBaseArgs), - - /// Converts a number of one base to another - #[command( - visible_aliases = &["--to-base", - "--to-radix", - "to-radix", - "tr", - "2r"] - )] - ToBase { - #[command(flatten)] - base: ToBaseArgs, - - /// The output base. - #[arg(value_name = "BASE")] - base_out: Option, - }, /// Create an access list for a transaction. #[command(visible_aliases = &["ac", "acl"])] AccessList(AccessListArgs), @@ -1091,26 +831,6 @@ pub enum CastSubcommand { #[command(visible_alias = "rp")] Rpc(RpcArgs), - /// Formats a string into bytes32 encoding. - #[command(name = "format-bytes32-string", visible_aliases = &["--format-bytes32-string"])] - FormatBytes32String { - /// The string to format. - string: Option, - }, - - /// Parses a string from bytes32 encoding. - #[command(name = "parse-bytes32-string", visible_aliases = &["--parse-bytes32-string"])] - ParseBytes32String { - /// The string to parse. - bytes: Option, - }, - #[command(name = "parse-bytes32-address", visible_aliases = &["--parse-bytes32-address"])] - #[command(about = "Parses a checksummed address from bytes32 encoding.")] - ParseBytes32Address { - #[arg(value_name = "BYTES")] - bytes: Option, - }, - /// Decodes a raw signed EIP 2718 typed transaction #[command(visible_aliases = &["dt", "decode-tx"])] DecodeTransaction { tx: Option }, @@ -1146,6 +866,12 @@ pub enum CastSubcommand { #[command(subcommand)] command: Erc20Subcommand, }, + /// Convert operations. + #[command(visible_alias = "convert")] + Convert { + #[command(subcommand)] + command: ConvertSubCommand, + }, } /// CLI arguments for `cast --to-base`.