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

fix(etherscan)!: change the GasOracle fields to be f64 #2325

Merged
merged 3 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 28 additions & 3 deletions ethers-etherscan/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::{collections::HashMap, str::FromStr};
#[serde(rename_all = "PascalCase")]
pub struct GasOracle {
#[serde(deserialize_with = "deserialize_number_from_string")]
pub safe_gas_price: u64,
pub safe_gas_price: f64,
#[serde(deserialize_with = "deserialize_number_from_string")]
pub propose_gas_price: u64,
pub propose_gas_price: f64,
#[serde(deserialize_with = "deserialize_number_from_string")]
pub fast_gas_price: u64,
pub fast_gas_price: f64,
#[serde(deserialize_with = "deserialize_number_from_string")]
pub last_block: u64,
#[serde(deserialize_with = "deserialize_number_from_string")]
Expand Down Expand Up @@ -82,3 +82,28 @@ impl Client {
Ok(response.result)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn response_works() {
// Response from Polygon mainnet at 2023-04-05
let v = r#"{
"status": "1",
"message": "OK",
"result": {
"LastBlock": "41171167",
"SafeGasPrice": "119.9",
"ProposeGasPrice": "141.9",
"FastGasPrice": "142.9",
"suggestBaseFee": "89.82627877",
"gasUsedRatio": "0.399191166666667,0.4847166,0.997667533333333,0.538075133333333,0.343416033333333",
"UsdPrice": "1.15"
}
}"#;
let gas_oracle: Response<GasOracle> = serde_json::from_str(v).unwrap();
assert_eq!(gas_oracle.message, "OK");
}
}
6 changes: 3 additions & 3 deletions ethers-etherscan/tests/it/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ async fn gas_oracle_success() {

let oracle = result.unwrap();

assert!(oracle.safe_gas_price > 0);
assert!(oracle.propose_gas_price > 0);
assert!(oracle.fast_gas_price > 0);
assert!(oracle.safe_gas_price > 0f64);
assert!(oracle.propose_gas_price > 0f64);
assert!(oracle.fast_gas_price > 0f64);
assert!(oracle.last_block > 0);
assert!(oracle.suggested_base_fee > 0.0);
assert!(!oracle.gas_used_ratio.is_empty());
Expand Down
4 changes: 3 additions & 1 deletion ethers-middleware/src/gas_oracle/etherscan.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{GasCategory, GasOracle, GasOracleError, Result, GWEI_TO_WEI_U256};
use async_trait::async_trait;
use ethers_core::types::U256;
use ethers_core::{types::U256, utils::parse_units};
use ethers_etherscan::Client;
use std::ops::{Deref, DerefMut};

Expand Down Expand Up @@ -44,6 +44,8 @@ impl GasOracle for Etherscan {
GasCategory::Fast => result.fast_gas_price,
_ => unreachable!(),
};
// returned gas prices are f64 value in gwei
let gas_price = parse_units(gas_price, "gwei")?;
Ok(U256::from(gas_price) * GWEI_TO_WEI_U256)
}

Expand Down
2 changes: 2 additions & 0 deletions ethers-middleware/src/gas_oracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ pub enum GasOracleError {
/// Error thrown when the provider failed.
#[error("Provider error: {0}")]
ProviderError(#[from] Box<dyn Error + Send + Sync>),
#[error("Failed to parse gas values: {0}")]
ConversionError(#[from] ethers_core::utils::ConversionError),
}

/// An Ethereum gas price oracle.
Expand Down