Skip to content

Instantly share code, notes, and snippets.

@samlaf
Created May 23, 2022 13:15
Show Gist options
  • Save samlaf/17ece7022dca825f9aeafc06b5f2f992 to your computer and use it in GitHub Desktop.
Save samlaf/17ece7022dca825f9aeafc06b5f2f992 to your computer and use it in GitHub Desktop.
Gas Oracles (ethers-rs)
use ethers::prelude::*;
use eyre::Result;
use gas_oracle::{
EthGasStation, Etherchain, Etherscan,
GasCategory::{Fast, Fastest, SafeLow, Standard},
GasOracle,
};
#[tokio::main]
async fn main() -> Result<()> {
let url = std::env::var("ETHEREUM_INFURA_HTTPS_ENDPOINT")?;
let client = Provider::<Http>::try_from(url)?;
let etherscan_key = std::env::var("ETHERSCAN_KEY")?;
let eth_estimate_gas = client.get_gas_price().await?;
println!("eth_estimateGas: {}", eth_estimate_gas);
println!("Compared to EthGasStation / Etherscan / Etherchain");
for category in [Fastest, Fast, Standard, SafeLow] {
let eth_gas_station_oracle = EthGasStation::new(None).category(category);
let etherscan_oracle =
Etherscan::new(Client::new(Chain::Mainnet, &etherscan_key)?).category(category);
let etherchain_oracle = Etherchain::new().category(category);
let gas_station_gas_price = eth_gas_station_oracle.fetch().await?;
let etherscan_gas_price = etherscan_oracle.fetch().await.unwrap_or_default();
let etherchain_gas_price = etherchain_oracle.fetch().await?;
println!(
"Category ({:?}): {} {} {}",
category, gas_station_gas_price, etherscan_gas_price, etherchain_gas_price
);
}
Ok(())
}
// prints:
// eth_estimateGas: 18325612325
// Compared to EthGasStation / Etherscan / Etherchain
// Category (Fastest): 39000000000 0 2000000000
// Category (Fast): 37000000000 19000000000 1000000000
// Category (Standard): 30000000000 18000000000 1000000000
// Category (SafeLow): 27000000000 18000000000 1000000000
// also see https://twitter.com/samlafer/status/1528723169162342400
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment