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

fix(core): block FromStr implementation #2155

Merged
merged 1 commit into from Feb 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions ethers-core/src/types/block.rs
Expand Up @@ -535,8 +535,7 @@ impl FromStr for BlockId {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.strip_prefix("0x").unwrap_or(s);
if s.len() == 64 {
if s.starts_with("0x") && s.len() == 66 {
let hash = s.parse::<H256>().map_err(|e| e.to_string());
hash.map(Self::Hash)
} else {
Expand Down Expand Up @@ -645,15 +644,10 @@ impl FromStr for BlockNumber {
"safe" => Ok(Self::Safe),
"earliest" => Ok(Self::Earliest),
"pending" => Ok(Self::Pending),
n => {
if let Ok(n) = n.parse::<U64>() {
Ok(Self::Number(n))
} else if let Ok(n) = n.parse::<u64>() {
Ok(Self::Number(n.into()))
} else {
Err("Invalid block number".into())
}
}
// hex
n if n.starts_with("0x") => n.parse().map(Self::Number).map_err(|e| e.to_string()),
// decimal
n => n.parse::<u64>().map(|n| Self::Number(n.into())).map_err(|e| e.to_string()),
}
}
}
Expand Down