From c1ab791d8f96b25102fa80f9395310f3aed1c0f8 Mon Sep 17 00:00:00 2001 From: Shady Khalifa Date: Wed, 5 Apr 2023 20:02:51 +0200 Subject: [PATCH 1/4] fix(middleware): no need to multiply again with `GWEI_TO_WEI_U256` --- ethers-middleware/src/gas_oracle/etherscan.rs | 4 ++-- ethers-middleware/src/gas_oracle/mod.rs | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/ethers-middleware/src/gas_oracle/etherscan.rs b/ethers-middleware/src/gas_oracle/etherscan.rs index 481f18e72..47336e222 100644 --- a/ethers-middleware/src/gas_oracle/etherscan.rs +++ b/ethers-middleware/src/gas_oracle/etherscan.rs @@ -1,4 +1,4 @@ -use super::{GasCategory, GasOracle, GasOracleError, Result, GWEI_TO_WEI_U256}; +use super::{GasCategory, GasOracle, GasOracleError, Result}; use async_trait::async_trait; use ethers_core::{types::U256, utils::parse_units}; use ethers_etherscan::Client; @@ -46,7 +46,7 @@ impl GasOracle for Etherscan { }; // 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) + Ok(gas_price.into()) } async fn estimate_eip1559_fees(&self) -> Result<(U256, U256)> { diff --git a/ethers-middleware/src/gas_oracle/mod.rs b/ethers-middleware/src/gas_oracle/mod.rs index 0c32de415..2b1e94429 100644 --- a/ethers-middleware/src/gas_oracle/mod.rs +++ b/ethers-middleware/src/gas_oracle/mod.rs @@ -36,9 +36,6 @@ use reqwest::Error as ReqwestError; 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([GWEI_TO_WEI, 0, 0, 0]); - pub type Result = std::result::Result; /// Generic [`GasOracle`] gas price categories. From 5102e34d58a005fdbc3f647c437ec20cfd7611dd Mon Sep 17 00:00:00 2001 From: Shady Khalifa Date: Wed, 5 Apr 2023 20:14:14 +0200 Subject: [PATCH 2/4] add tests to make sure that bug never happens again --- ethers-middleware/tests/it/gas_oracle.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ethers-middleware/tests/it/gas_oracle.rs b/ethers-middleware/tests/it/gas_oracle.rs index 14aee0a38..5abb5ca9c 100644 --- a/ethers-middleware/tests/it/gas_oracle.rs +++ b/ethers-middleware/tests/it/gas_oracle.rs @@ -1,5 +1,8 @@ use async_trait::async_trait; -use ethers_core::{types::*, utils::Anvil}; +use ethers_core::{ + types::*, + utils::{parse_ether, Anvil}, +}; use ethers_etherscan::Client; use ethers_middleware::gas_oracle::{ BlockNative, Etherchain, Etherscan, GasCategory, GasNow, GasOracle, GasOracleError, @@ -102,6 +105,8 @@ async fn etherscan() { let gas_price = etherscan_oracle.fetch().await.unwrap(); assert!(gas_price > U256::zero()); + let ten_ethers = parse_ether(10).unwrap(); + assert!(gas_price < ten_ethers, "gas calculation is wrong (too high)"); } #[tokio::test] From fa17c4446f56de55a59607594002ba5a221db7ae Mon Sep 17 00:00:00 2001 From: Shady Khalifa Date: Wed, 5 Apr 2023 20:21:22 +0200 Subject: [PATCH 3/4] use the helper method to convert `f64` gwei to `U256` --- ethers-middleware/src/gas_oracle/etherscan.rs | 4 ++-- ethers-middleware/src/gas_oracle/mod.rs | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ethers-middleware/src/gas_oracle/etherscan.rs b/ethers-middleware/src/gas_oracle/etherscan.rs index 47336e222..12dc17559 100644 --- a/ethers-middleware/src/gas_oracle/etherscan.rs +++ b/ethers-middleware/src/gas_oracle/etherscan.rs @@ -45,8 +45,8 @@ impl GasOracle for Etherscan { _ => unreachable!(), }; // returned gas prices are f64 value in gwei - let gas_price = parse_units(gas_price, "gwei")?; - Ok(gas_price.into()) + let gas_price = super::from_gwei_f64(gas_price); + Ok(gas_price) } async fn estimate_eip1559_fees(&self) -> Result<(U256, U256)> { diff --git a/ethers-middleware/src/gas_oracle/mod.rs b/ethers-middleware/src/gas_oracle/mod.rs index 2b1e94429..0c32de415 100644 --- a/ethers-middleware/src/gas_oracle/mod.rs +++ b/ethers-middleware/src/gas_oracle/mod.rs @@ -36,6 +36,9 @@ use reqwest::Error as ReqwestError; 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([GWEI_TO_WEI, 0, 0, 0]); + pub type Result = std::result::Result; /// Generic [`GasOracle`] gas price categories. From 789f449385ade3269bdab5f86301d64c4ee91f3f Mon Sep 17 00:00:00 2001 From: Shady Khalifa Date: Wed, 5 Apr 2023 20:26:19 +0200 Subject: [PATCH 4/4] lint: make clippy happy --- ethers-middleware/src/gas_oracle/etherscan.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethers-middleware/src/gas_oracle/etherscan.rs b/ethers-middleware/src/gas_oracle/etherscan.rs index 12dc17559..39645eb91 100644 --- a/ethers-middleware/src/gas_oracle/etherscan.rs +++ b/ethers-middleware/src/gas_oracle/etherscan.rs @@ -1,6 +1,6 @@ use super::{GasCategory, GasOracle, GasOracleError, Result}; use async_trait::async_trait; -use ethers_core::{types::U256, utils::parse_units}; +use ethers_core::types::U256; use ethers_etherscan::Client; use std::ops::{Deref, DerefMut};