Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dyn-abi): support parse scientific number #835

Merged
merged 7 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions crates/dyn-abi/src/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use parser::{
};
use winnow::{
ascii::{alpha0, alpha1, digit1, hex_digit0, hex_digit1, space0},
combinator::{cut_err, dispatch, empty, fail, opt, preceded, trace},
combinator::{alt, cut_err, dispatch, empty, fail, opt, preceded, trace},
error::{
AddContext, ContextError, ErrMode, ErrorKind, FromExternalError, StrContext,
AddContext, ContextError, ErrMode, ErrorKind, FromExternalError, ParserError, StrContext,
StrContextValue,
},
stream::Stream,
Expand Down Expand Up @@ -422,16 +422,49 @@ fn prefixed_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
fn int_units(input: &mut Input<'_>) -> PResult<usize> {
trace(
"int_units",
dispatch! {alpha0;
"ether" => empty.value(18),
"gwei" | "nano" | "nanoether" => empty.value(9),
"" | "wei" => empty.value(0),
_ => fail,
},
alt((
dispatch! {alpha0;
"ether" => empty.value(18),
"gwei" | "nano" | "nanoether" => empty.value(9),
"" | "wei" => empty.value(0),
_ => fail,
},
scientific_notation,
)),
)
.parse_next(input)
}

#[inline]
fn scientific_notation(input: &mut Input<'_>) -> PResult<usize> {
// Check if we have 'e' or 'E' followed by an optional sign and digits
if let Some(c) = input.chars().next() {
if c == 'e' || c == 'E' {
let _ = input.next_token();

// Parse optional sign
let sign = int_sign(input)?;

// Parse digits
let exp = digit1
.parse_next(input)?
.parse::<usize>()
.map_err(|e| ErrMode::from_external_error(input, ErrorKind::Verify, e))?;

return if sign.is_negative() {
Err(ErrMode::from_external_error(
input,
ErrorKind::Verify,
crate::Error::NegativeExponent(exp),
))
} else {
Ok(exp)
};
}
}
Err(ErrMode::from_error_kind(input, ErrorKind::Fail))
}

#[inline]
fn fixed_bytes<'i>(len: usize) -> impl Parser<Input<'i>, Word, ContextError> {
#[cfg(feature = "debug")]
Expand Down Expand Up @@ -1283,4 +1316,26 @@ mod tests {
}
assert_eq!(value, DynSolValue::Bool(true));
}

#[test]
fn coerce_uint_scientific() {
assert_eq!(
DynSolType::Uint(256).coerce_str("1e18").unwrap(),
DynSolValue::Uint(U256::from_str("1000000000000000000").unwrap(), 256)
);

assert_eq!(
DynSolType::Uint(256).coerce_str("74258.225772486694040708e18").unwrap(),
DynSolValue::Uint(U256::from_str("74258225772486694040708").unwrap(), 256)
);

assert_eq!(
DynSolType::Uint(256).coerce_str("1.5e18").unwrap(),
DynSolValue::Uint(U256::from_str("1500000000000000000").unwrap(), 256)
);

assert!(DynSolType::Uint(256).coerce_str("1e-18").is_err());
assert!(DynSolType::Uint(256).coerce_str("1ex").is_err());
assert!(DynSolType::Uint(256).coerce_str("1e").is_err());
}
}
3 changes: 3 additions & 0 deletions crates/dyn-abi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub enum Error {
TypeParser(TypeParserError),
/// [`alloy_sol_types`] error.
SolTypes(SolTypesError),
/// Negative exponent in fixed-point number.
NegativeExponent(usize),
}

impl From<FromHexError> for Error {
Expand Down Expand Up @@ -140,6 +142,7 @@ impl fmt::Display for Error {
Self::Hex(e) => e.fmt(f),
Self::TypeParser(e) => e.fmt(f),
Self::SolTypes(e) => e.fmt(f),
Self::NegativeExponent(exp) => write!(f, "negative exponent e-{exp} not allowed"),
}
}
}
Expand Down