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

Commit

Permalink
fix(middleware): no need to multiply again with GWEI_TO_WEI_U256 (#…
Browse files Browse the repository at this point in the history
…2326)

* fix(middleware): no need to multiply again with `GWEI_TO_WEI_U256`

* add tests to make sure that bug never happens again

* use the helper method to convert `f64` gwei to `U256`

* lint: make clippy happy
  • Loading branch information
shekohex authored Apr 5, 2023
1 parent 478e737 commit 1dd3545
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions 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 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};

Expand Down Expand Up @@ -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(U256::from(gas_price) * GWEI_TO_WEI_U256)
let gas_price = super::from_gwei_f64(gas_price);
Ok(gas_price)
}

async fn estimate_eip1559_fees(&self) -> Result<(U256, U256)> {
Expand Down
7 changes: 6 additions & 1 deletion ethers-middleware/tests/it/gas_oracle.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 1dd3545

Please sign in to comment.