This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 794
feature: very permissive etherscan gwei deser #2327
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,27 +1,57 @@ | ||||||||||||
use crate::{Client, EtherscanError, Response, Result}; | ||||||||||||
use ethers_core::types::U256; | ||||||||||||
use ethers_core::{types::U256, utils::parse_units}; | ||||||||||||
use serde::{de, Deserialize, Deserializer}; | ||||||||||||
use std::{collections::HashMap, str::FromStr}; | ||||||||||||
|
||||||||||||
const WEI_PER_GWEI: u64 = 1_000_000_000; | ||||||||||||
|
||||||||||||
#[derive(Deserialize, Clone, Debug)] | ||||||||||||
#[serde(rename_all = "PascalCase")] | ||||||||||||
pub struct GasOracle { | ||||||||||||
#[serde(deserialize_with = "deserialize_number_from_string")] | ||||||||||||
pub safe_gas_price: f64, | ||||||||||||
#[serde(deserialize_with = "deserialize_number_from_string")] | ||||||||||||
pub propose_gas_price: f64, | ||||||||||||
#[serde(deserialize_with = "deserialize_number_from_string")] | ||||||||||||
pub fast_gas_price: f64, | ||||||||||||
/// Safe Gas Price in wei | ||||||||||||
#[serde(deserialize_with = "deser_gwei_amount")] | ||||||||||||
pub safe_gas_price: U256, | ||||||||||||
/// Propose Gas Price in wei | ||||||||||||
#[serde(deserialize_with = "deser_gwei_amount")] | ||||||||||||
pub propose_gas_price: U256, | ||||||||||||
/// Fast Gas Price in wei | ||||||||||||
#[serde(deserialize_with = "deser_gwei_amount")] | ||||||||||||
pub fast_gas_price: U256, | ||||||||||||
/// Last Block | ||||||||||||
#[serde(deserialize_with = "deserialize_number_from_string")] | ||||||||||||
pub last_block: u64, | ||||||||||||
#[serde(deserialize_with = "deserialize_number_from_string")] | ||||||||||||
/// Suggested Base Fee in wei | ||||||||||||
#[serde(deserialize_with = "deser_gwei_amount")] | ||||||||||||
#[serde(rename = "suggestBaseFee")] | ||||||||||||
pub suggested_base_fee: f64, | ||||||||||||
pub suggested_base_fee: U256, | ||||||||||||
/// Gas Used Ratio | ||||||||||||
#[serde(deserialize_with = "deserialize_f64_vec")] | ||||||||||||
#[serde(rename = "gasUsedRatio")] | ||||||||||||
pub gas_used_ratio: Vec<f64>, | ||||||||||||
} | ||||||||||||
|
||||||||||||
// This function is used to deserialize a string or number into a U256 with an | ||||||||||||
// amount of gwei. If the contents is a number, deserialize it. If the contents | ||||||||||||
// is a string, attempt to deser as first a decimal f64 then a decimal U256. | ||||||||||||
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
fn deser_gwei_amount<'de, D>(deserializer: D) -> Result<U256, D::Error> | ||||||||||||
where | ||||||||||||
D: Deserializer<'de>, | ||||||||||||
{ | ||||||||||||
#[derive(Deserialize)] | ||||||||||||
#[serde(untagged)] | ||||||||||||
enum StringOrInt { | ||||||||||||
Number(u64), | ||||||||||||
String(String), | ||||||||||||
} | ||||||||||||
|
||||||||||||
match StringOrInt::deserialize(deserializer)? { | ||||||||||||
StringOrInt::Number(i) => Ok(U256::from(i) * WEI_PER_GWEI), | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't exist in this package, that's in the middleware, not in the etherscan There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see, gotcha! |
||||||||||||
StringOrInt::String(s) => { | ||||||||||||
parse_units(s, "gwei").map(Into::into).map_err(serde::de::Error::custom) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn deserialize_number_from_string<'de, T, D>(deserializer: D) -> Result<T, D::Error> | ||||||||||||
where | ||||||||||||
D: Deserializer<'de>, | ||||||||||||
|
@@ -105,5 +135,42 @@ mod tests { | |||||||||||
}"#; | ||||||||||||
let gas_oracle: Response<GasOracle> = serde_json::from_str(v).unwrap(); | ||||||||||||
assert_eq!(gas_oracle.message, "OK"); | ||||||||||||
assert_eq!( | ||||||||||||
gas_oracle.result.propose_gas_price, | ||||||||||||
parse_units("141.9", "gwei").unwrap().into() | ||||||||||||
); | ||||||||||||
|
||||||||||||
let v = r#"{ | ||||||||||||
"status":"1", | ||||||||||||
"message":"OK", | ||||||||||||
"result":{ | ||||||||||||
"LastBlock":"13053741", | ||||||||||||
"SafeGasPrice":"20", | ||||||||||||
"ProposeGasPrice":"22", | ||||||||||||
"FastGasPrice":"24", | ||||||||||||
"suggestBaseFee":"19.230609716", | ||||||||||||
"gasUsedRatio":"0.370119078777807,0.8954731,0.550911766666667,0.212457033333333,0.552463633333333" | ||||||||||||
} | ||||||||||||
}"#; | ||||||||||||
let gas_oracle: Response<GasOracle> = serde_json::from_str(v).unwrap(); | ||||||||||||
assert_eq!(gas_oracle.message, "OK"); | ||||||||||||
assert_eq!(gas_oracle.result.propose_gas_price, parse_units(22, "gwei").unwrap().into()); | ||||||||||||
|
||||||||||||
// remove quotes around integers | ||||||||||||
let v = r#"{ | ||||||||||||
"status":"1", | ||||||||||||
"message":"OK", | ||||||||||||
"result":{ | ||||||||||||
"LastBlock":13053741, | ||||||||||||
"SafeGasPrice":20, | ||||||||||||
"ProposeGasPrice":22, | ||||||||||||
"FastGasPrice":24, | ||||||||||||
"suggestBaseFee":"19.230609716", | ||||||||||||
"gasUsedRatio":"0.370119078777807,0.8954731,0.550911766666667,0.212457033333333,0.552463633333333" | ||||||||||||
} | ||||||||||||
}"#; | ||||||||||||
let gas_oracle: Response<GasOracle> = serde_json::from_str(v).unwrap(); | ||||||||||||
assert_eq!(gas_oracle.message, "OK"); | ||||||||||||
assert_eq!(gas_oracle.result.propose_gas_price, parse_units(22, "gwei").unwrap().into()); | ||||||||||||
} | ||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe use
super::GWEI_TO_WEI
instead?