File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
146146fn 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 (
Original file line number Diff line number Diff 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.
131131pub 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 ( ) {
You can’t perform that action at this time.
0 commit comments