Skip to content

Commit 5e497f5

Browse files
figtracerzerosnacksampcode-com
authored
fix: accept 0x-prefixed value inputs (#14406)
* fix: accept 0x-prefixed value inputs * use U256::from_str for 0x-prefixed hex parsing, add 0X support and expand tests Amp-Thread-ID: https://ampcode.com/threads/T-019db45a-2ee1-771d-8127-2052dc6df2a3 Co-authored-by: Amp <amp@ampcode.com> --------- Co-authored-by: zerosnacks <zerosnacks@protonmail.com> Co-authored-by: Amp <amp@ampcode.com>
1 parent 42de88e commit 5e497f5

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

crates/cast/src/call_spec.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ impl FromStr for CallSpec {
145145
/// Parse a value string that can be in ether notation (e.g., "0.1ether") or raw wei.
146146
fn parse_ether_or_wei(s: &str) -> Result<U256> {
147147
// Use alloy's DynSolType coercion which handles "1ether", "1gwei", "1000" etc.
148-
if s.starts_with("0x") {
149-
U256::from_str_radix(s, 16).map_err(|e| eyre!("Invalid hex value '{}': {}", s, e))
148+
if s.starts_with("0x") || s.starts_with("0X") {
149+
U256::from_str(s).map_err(|e| eyre!("Invalid hex value '{}': {}", s, e))
150150
} else {
151151
alloy_dyn_abi::DynSolType::coerce_str(&alloy_dyn_abi::DynSolType::Uint(256), s)
152152
.wrap_err_with(|| format!("Invalid value '{s}'"))?
@@ -180,6 +180,12 @@ mod tests {
180180
assert!(spec.sig.is_none());
181181
}
182182

183+
#[test]
184+
fn test_parse_hex_value() {
185+
assert_eq!(parse_ether_or_wei("0x10").unwrap(), U256::from(16));
186+
assert_eq!(parse_ether_or_wei("0X10").unwrap(), U256::from(16));
187+
}
188+
183189
#[test]
184190
fn test_parse_with_sig() {
185191
let spec = CallSpec::parse(

crates/cli/src/utils/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ where
129129
/// If the string represents an untagged amount (e.g. "100") then
130130
/// it is interpreted as wei.
131131
pub fn parse_ether_value(value: &str) -> Result<U256> {
132-
Ok(if value.starts_with("0x") {
133-
U256::from_str_radix(value, 16)?
132+
Ok(if value.starts_with("0x") || value.starts_with("0X") {
133+
U256::from_str(value)?
134134
} else {
135135
alloy_dyn_abi::DynSolType::coerce_str(&alloy_dyn_abi::DynSolType::Uint(256), value)?
136136
.as_uint()
@@ -844,6 +844,16 @@ mod tests {
844844
assert!(!p.is_sol_test());
845845
}
846846

847+
#[test]
848+
fn parse_ether_value_accepts_hex_prefixed_wei() {
849+
assert_eq!(parse_ether_value("0x10").unwrap(), U256::from(16));
850+
assert_eq!(parse_ether_value("0X10").unwrap(), U256::from(16));
851+
assert_eq!(parse_ether_value("0x12").unwrap(), U256::from(0x12));
852+
assert_eq!(parse_ether_value("0xff").unwrap(), U256::from(0xff));
853+
assert_eq!(parse_ether_value("100").unwrap(), U256::from(100));
854+
assert_eq!(parse_ether_value("1ether").unwrap(), U256::from(1000000000000000000u128));
855+
}
856+
847857
// loads .env from cwd and project dir, See [`find_project_root()`]
848858
#[test]
849859
fn can_load_dotenv() {

0 commit comments

Comments
 (0)