From fcef7c70ad8163f53732069eddc544ab254891fd Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 30 Dec 2022 11:46:52 +0100 Subject: [PATCH] fix: gwei wei wrong u256 constant (#1992) --- ethers-middleware/src/gas_oracle/mod.rs | 14 +++++++++++++- ethers-middleware/src/gas_oracle/polygon.rs | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ethers-middleware/src/gas_oracle/mod.rs b/ethers-middleware/src/gas_oracle/mod.rs index 9be17750..40c1bf42 100644 --- a/ethers-middleware/src/gas_oracle/mod.rs +++ b/ethers-middleware/src/gas_oracle/mod.rs @@ -37,7 +37,7 @@ use std::{error::Error, fmt::Debug}; use thiserror::Error; pub(crate) const GWEI_TO_WEI: u64 = 1_000_000_000; -pub(crate) const GWEI_TO_WEI_U256: U256 = U256([0, 0, 0, GWEI_TO_WEI]); +pub(crate) const GWEI_TO_WEI_U256: U256 = U256([GWEI_TO_WEI, 0, 0, 0]); pub type Result = std::result::Result; @@ -153,3 +153,15 @@ pub trait GasOracle: Send + Sync + Debug { pub(crate) fn from_gwei_f64(gwei: f64) -> U256 { ethers_core::types::u256_from_f64_saturating(gwei) * GWEI_TO_WEI_U256 } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_gwei_wei_constants() { + let as_u256: U256 = GWEI_TO_WEI.into(); + assert_eq!(as_u256, GWEI_TO_WEI_U256); + assert_eq!(GWEI_TO_WEI_U256.as_u64(), GWEI_TO_WEI); + } +} diff --git a/ethers-middleware/src/gas_oracle/polygon.rs b/ethers-middleware/src/gas_oracle/polygon.rs index 5fd63545..f7acf85a 100644 --- a/ethers-middleware/src/gas_oracle/polygon.rs +++ b/ethers-middleware/src/gas_oracle/polygon.rs @@ -20,7 +20,7 @@ pub struct Polygon { /// The response from the Polygon gas station API. /// -/// Gas prices are in Gwei. +/// Gas prices are in __Gwei__. #[derive(Debug, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Response { @@ -103,3 +103,14 @@ impl Polygon { Ok(response) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_polygon_gas_station_response() { + let s = r#"{"safeLow":{"maxPriorityFee":2.1267086610666666,"maxFee":2.1267086760666665},"standard":{"maxPriorityFee":2.3482958369333335,"maxFee":2.3482958519333335},"fast":{"maxPriorityFee":2.793454819,"maxFee":2.793454834},"estimatedBaseFee":1.5e-8,"blockTime":2,"blockNumber":30328888}"#; + let _resp: Response = serde_json::from_str(s).unwrap(); + } +}