2023-02-25 22:24:20 +00:00
|
|
|
use ethers_core::{types::*, utils::Anvil};
|
2020-10-08 15:56:36 +00:00
|
|
|
use ethers_middleware::{
|
|
|
|
gas_escalator::{Frequency, GasEscalatorMiddleware, GeometricGasPrice},
|
2023-02-25 22:24:20 +00:00
|
|
|
MiddlewareBuilder,
|
2020-10-08 15:56:36 +00:00
|
|
|
};
|
2023-02-25 22:24:20 +00:00
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
2021-07-29 20:22:25 +00:00
|
|
|
use ethers_signers::{LocalWallet, Signer};
|
2020-10-08 15:56:36 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[ignore]
|
2023-02-25 22:24:20 +00:00
|
|
|
async fn gas_escalator() {
|
|
|
|
let anvil = Anvil::new().block_time(2u64).spawn();
|
|
|
|
let chain_id = anvil.chain_id();
|
|
|
|
let provider = Provider::<Http>::try_from(anvil.endpoint()).unwrap();
|
|
|
|
|
|
|
|
// wrap with signer
|
|
|
|
let wallet: LocalWallet = anvil.keys().first().unwrap().clone().into();
|
|
|
|
let wallet = wallet.with_chain_id(chain_id);
|
2020-10-08 15:56:36 +00:00
|
|
|
let address = wallet.address();
|
2023-02-25 22:24:20 +00:00
|
|
|
let provider = provider.with_signer(wallet);
|
2020-10-08 15:56:36 +00:00
|
|
|
|
2023-02-25 22:24:20 +00:00
|
|
|
// wrap with escalator
|
2021-07-05 11:03:38 +00:00
|
|
|
let escalator = GeometricGasPrice::new(5.0, 10u64, Some(2_000_000_000_000u64));
|
2023-02-25 22:24:20 +00:00
|
|
|
let provider = GasEscalatorMiddleware::new(provider, escalator, Frequency::Duration(300));
|
2020-10-08 15:56:36 +00:00
|
|
|
|
|
|
|
let nonce = provider.get_transaction_count(address, None).await.unwrap();
|
2023-02-25 22:24:20 +00:00
|
|
|
// 1 gwei default base fee
|
|
|
|
let gas_price = U256::from(1_000_000_000_u64);
|
|
|
|
let tx = TransactionRequest::pay(Address::zero(), 1u64)
|
|
|
|
.gas_price(gas_price)
|
|
|
|
.nonce(nonce)
|
|
|
|
.chain_id(chain_id);
|
2020-10-08 15:56:36 +00:00
|
|
|
|
2023-02-25 22:24:20 +00:00
|
|
|
eprintln!("sending");
|
|
|
|
let pending = provider.send_transaction(tx, None).await.expect("could not send");
|
|
|
|
eprintln!("waiting");
|
|
|
|
let receipt = pending.await.expect("reverted").expect("dropped");
|
|
|
|
assert_eq!(receipt.from, address);
|
|
|
|
assert_eq!(receipt.to, Some(Address::zero()));
|
|
|
|
assert!(receipt.effective_gas_price.unwrap() > gas_price * 2, "{receipt:?}");
|
2020-10-08 15:56:36 +00:00
|
|
|
}
|