Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
handle string case
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Nov 4, 2023
1 parent f80c610 commit c9d68be
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions ethers-core/src/utils/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashMap;
use std::{collections::HashMap, str::FromStr};

use crate::{
types::{
serde_helpers::{
deserialize_stringified_eth_u64, deserialize_stringified_eth_u64_opt,
deserialize_stringified_numeric, deserialize_stringified_numeric_opt,
deserialize_stringified_u64_opt,
deserialize_stringified_u64_opt, Numeric,
},
Address, Bytes, H256, U256, U64,
},
Expand Down Expand Up @@ -354,6 +354,13 @@ where
match Option::<serde_json::Value>::deserialize(deserializer)? {
None => Ok(None),
Some(val) => {
if let Some(num) = val.as_str() {
return Numeric::from_str(num)
.map(U256::from)
.map(Some)
.map_err(serde::de::Error::custom);
}

if let serde_json::Value::Number(num) = val {
// mainnet ttd is serialized as number, which serde is unable to deserialize as
// integer
Expand All @@ -362,11 +369,13 @@ where
} else {
num.as_u64()
.map(U256::from)
.ok_or_else(|| serde::de::Error::custom("expected a number"))
.ok_or_else(|| {
serde::de::Error::custom(format!("expected a number got {num}"))
})
.map(Some)
}
} else {
Err(serde::de::Error::custom("expected a number"))
Err(serde::de::Error::custom(format!("expected a number, got {:?}", val)))
}
}
}
Expand Down

0 comments on commit c9d68be

Please sign in to comment.