2021-08-23 09:56:44 +00:00
|
|
|
#![cfg(not(target_arch = "wasm32"))]
|
2021-10-16 15:47:17 +00:00
|
|
|
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
use ethers_core::{types::*, utils::Ganache};
|
|
|
|
use ethers_middleware::gas_oracle::{
|
2021-10-16 15:47:17 +00:00
|
|
|
EthGasStation, Etherchain, Etherscan, GasCategory, GasOracle, GasOracleError,
|
|
|
|
GasOracleMiddleware,
|
2020-09-24 21:33:09 +00:00
|
|
|
};
|
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
2021-11-25 06:57:22 +00:00
|
|
|
use serial_test::serial;
|
2021-10-16 15:47:17 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct FakeGasOracle {
|
|
|
|
gas_price: U256,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
|
|
|
impl GasOracle for FakeGasOracle {
|
|
|
|
async fn fetch(&self) -> Result<U256, GasOracleError> {
|
|
|
|
Ok(self.gas_price)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn estimate_eip1559_fees(&self) -> Result<(U256, U256), GasOracleError> {
|
|
|
|
Err(GasOracleError::Eip1559EstimationNotSupported)
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn using_gas_oracle() {
|
|
|
|
let ganache = Ganache::new().spawn();
|
|
|
|
|
2020-10-02 08:41:16 +00:00
|
|
|
let from = ganache.addresses()[0];
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
// connect to the network
|
|
|
|
let provider = Provider::<Http>::try_from(ganache.endpoint()).unwrap();
|
|
|
|
|
|
|
|
// assign a gas oracle to use
|
2021-10-29 12:29:35 +00:00
|
|
|
let gas_oracle = FakeGasOracle { gas_price: 1337.into() };
|
2020-09-24 21:33:09 +00:00
|
|
|
let expected_gas_price = gas_oracle.fetch().await.unwrap();
|
|
|
|
|
|
|
|
let provider = GasOracleMiddleware::new(provider, gas_oracle);
|
|
|
|
|
|
|
|
// broadcast a transaction
|
2021-10-29 12:29:35 +00:00
|
|
|
let tx = TransactionRequest::new().from(from).to(Address::zero()).value(10000);
|
2020-09-24 21:33:09 +00:00
|
|
|
let tx_hash = provider.send_transaction(tx, None).await.unwrap();
|
|
|
|
|
2020-12-17 11:26:01 +00:00
|
|
|
let tx = provider.get_transaction(*tx_hash).await.unwrap().unwrap();
|
2021-08-12 16:01:52 +00:00
|
|
|
assert_eq!(tx.gas_price, Some(expected_gas_price));
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn eth_gas_station() {
|
|
|
|
// initialize and fetch gas estimates from EthGasStation
|
|
|
|
let eth_gas_station_oracle = EthGasStation::new(None);
|
|
|
|
let data = eth_gas_station_oracle.fetch().await;
|
|
|
|
assert!(data.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2021-11-25 06:57:22 +00:00
|
|
|
#[serial]
|
2020-09-24 21:33:09 +00:00
|
|
|
async fn etherscan() {
|
2021-11-25 05:38:28 +00:00
|
|
|
let etherscan_client = ethers_etherscan::Client::new_from_env(Chain::Mainnet).unwrap();
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
// initialize and fetch gas estimates from Etherscan
|
|
|
|
// since etherscan does not support `fastest` category, we expect an error
|
2021-11-25 05:38:28 +00:00
|
|
|
let etherscan_oracle = Etherscan::new(etherscan_client.clone()).category(GasCategory::Fastest);
|
2020-09-24 21:33:09 +00:00
|
|
|
let data = etherscan_oracle.fetch().await;
|
|
|
|
assert!(data.is_err());
|
|
|
|
|
|
|
|
// but fetching the `standard` gas price should work fine
|
2021-11-25 06:06:15 +00:00
|
|
|
let etherscan_oracle = Etherscan::new(etherscan_client).category(GasCategory::SafeLow);
|
2020-09-24 21:33:09 +00:00
|
|
|
|
2021-11-25 06:06:15 +00:00
|
|
|
let data = etherscan_oracle.fetch().await;
|
2020-09-24 21:33:09 +00:00
|
|
|
assert!(data.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn etherchain() {
|
|
|
|
// initialize and fetch gas estimates from Etherchain
|
|
|
|
let etherchain_oracle = Etherchain::new().category(GasCategory::Fast);
|
|
|
|
let data = etherchain_oracle.fetch().await;
|
|
|
|
assert!(data.is_ok());
|
|
|
|
}
|