ethers-rs/ethers-middleware/tests/gas_oracle.rs

80 lines
2.7 KiB
Rust
Raw Normal View History

use ethers_core::{types::*, utils::Ganache};
use ethers_middleware::gas_oracle::{
EthGasStation, Etherchain, Etherscan, GasCategory, GasNow, GasOracle, GasOracleMiddleware,
};
use ethers_providers::{Http, Middleware, Provider};
use std::convert::TryFrom;
#[tokio::test]
async fn using_gas_oracle() {
let ganache = Ganache::new().spawn();
let from = ganache.addresses()[0];
// connect to the network
let provider = Provider::<Http>::try_from(ganache.endpoint()).unwrap();
// assign a gas oracle to use
let gas_oracle = GasNow::new().category(GasCategory::Fastest);
let expected_gas_price = gas_oracle.fetch().await.unwrap();
let provider = GasOracleMiddleware::new(provider, gas_oracle);
// broadcast a transaction
let tx = TransactionRequest::new()
.from(from)
.to(Address::zero())
.value(10000);
let tx_hash = provider.send_transaction(tx, None).await.unwrap();
let tx = provider.get_transaction(*tx_hash).await.unwrap().unwrap();
assert_eq!(tx.gas_price, expected_gas_price);
}
#[tokio::test]
#[ignore]
feat: typed txs provider / middleware changes (part 3) (#357) * feat(providers): add eth_feeHistory api * add access list * feat: fill transactions with access list / default sender info * feat: add helpers for operating on the txs enum * feat: send_transaction takes TypedTransaction now * fix(contract): temp wrap all contract txs as Legacy txs * feat(middleware): use TypedTransaction in Transformer * feat(signers): use TypedTransaction in Wallet/Ledger * feat(core): add helpers for setting typed tx fields * feat(signer): use typed transactions * fix(middleware): adjust nonce/gas/escalators for TypedTxs The GPO and the Escalators will throw an error if they are provided an EIP1559 transaction * fix(providers): ensure the correct account's nonce is filled * fix: add .into() to txs until we make the fn call more generic * Revert "fix: add .into() to txs until we make the fn call more generic" This reverts commit 04dc34b26d0e3f418ed3fc69ea35ad538b83dd50. * feat: generalize send_transaction interface * fix: only set the nonce manually in the Signer middleware * fix(transformer): fill the transaction after transformation * chore: fix compilation errors & lints * fix(signer): set the correct account's nonce * feat: make trace_call / call take TypedTransaction * fix: set sender to transaction in signer * chore: ethgasstation broke * chore: cargo fmt / lints * Fix(signer): pass the chain id * fix: final tx encoding fixes 1. Normalize v values for eip1559/2730 2. Make access lists mandatory for 1559 3. do not double-rlp on rlp_signed * fix: set access list only if available * test: check 1559 / 2930 txs * fix: do not prepend a 0 for Legacy txs & test * chore: code review comments * chore: fix aws signer signature
2021-08-09 00:31:11 +00:00
// TODO: Re-enable, EthGasStation changed its response api @ https://ethgasstation.info/api/ethgasAPI.json
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]
async fn etherscan() {
let api_key = std::env::var("ETHERSCAN_API_KEY").unwrap();
let api_key = Some(api_key.as_str());
// initialize and fetch gas estimates from Etherscan
// since etherscan does not support `fastest` category, we expect an error
let etherscan_oracle = Etherscan::new(api_key).category(GasCategory::Fastest);
let data = etherscan_oracle.fetch().await;
assert!(data.is_err());
// but fetching the `standard` gas price should work fine
let etherscan_oracle_2 = Etherscan::new(api_key).category(GasCategory::SafeLow);
let data = etherscan_oracle_2.fetch().await;
assert!(data.is_ok());
}
#[tokio::test]
#[ignore]
feat: typed txs provider / middleware changes (part 3) (#357) * feat(providers): add eth_feeHistory api * add access list * feat: fill transactions with access list / default sender info * feat: add helpers for operating on the txs enum * feat: send_transaction takes TypedTransaction now * fix(contract): temp wrap all contract txs as Legacy txs * feat(middleware): use TypedTransaction in Transformer * feat(signers): use TypedTransaction in Wallet/Ledger * feat(core): add helpers for setting typed tx fields * feat(signer): use typed transactions * fix(middleware): adjust nonce/gas/escalators for TypedTxs The GPO and the Escalators will throw an error if they are provided an EIP1559 transaction * fix(providers): ensure the correct account's nonce is filled * fix: add .into() to txs until we make the fn call more generic * Revert "fix: add .into() to txs until we make the fn call more generic" This reverts commit 04dc34b26d0e3f418ed3fc69ea35ad538b83dd50. * feat: generalize send_transaction interface * fix: only set the nonce manually in the Signer middleware * fix(transformer): fill the transaction after transformation * chore: fix compilation errors & lints * fix(signer): set the correct account's nonce * feat: make trace_call / call take TypedTransaction * fix: set sender to transaction in signer * chore: ethgasstation broke * chore: cargo fmt / lints * Fix(signer): pass the chain id * fix: final tx encoding fixes 1. Normalize v values for eip1559/2730 2. Make access lists mandatory for 1559 3. do not double-rlp on rlp_signed * fix: set access list only if available * test: check 1559 / 2930 txs * fix: do not prepend a 0 for Legacy txs & test * chore: code review comments * chore: fix aws signer signature
2021-08-09 00:31:11 +00:00
// TODO: Etherchain has Cloudflare DDOS protection which makes the request fail
// https://twitter.com/gakonst/status/1421796226316578816
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());
}
#[tokio::test]
async fn gas_now() {
// initialize and fetch gas estimates from SparkPool
let gas_now_oracle = GasNow::new().category(GasCategory::Fastest);
let data = gas_now_oracle.fetch().await;
assert!(data.is_ok());
}