Skip to content

Commit

Permalink
fix(core): block FromStr implementation (#2155)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Feb 14, 2023
1 parent d859720 commit a69036f
Showing 1 changed file with 5 additions and 11 deletions.
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

0 comments on commit a69036f

Please sign in to comment.