From 5917e842d748370707a7f5ef27890304c6976f6e Mon Sep 17 00:00:00 2001 From: Andrea Simeoni Date: Fri, 6 Jan 2023 10:29:46 +0100 Subject: [PATCH] ci: fix nonce manager test (#2014) * Use Anvil instead of Goerli to test the nonce manager * restore comment * fix: explicitly assign `inner.max_priority_fee_per_gas` Co-authored-by: Andrea Simeoni <> --- ethers-middleware/tests/nonce_manager.rs | 36 ++++++++---------------- ethers-providers/src/provider.rs | 4 +-- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/ethers-middleware/tests/nonce_manager.rs b/ethers-middleware/tests/nonce_manager.rs index 1be27fec..85368aa2 100644 --- a/ethers-middleware/tests/nonce_manager.rs +++ b/ethers-middleware/tests/nonce_manager.rs @@ -1,28 +1,22 @@ #![cfg(all(not(target_arch = "wasm32"), not(feature = "celo")))] -use ethers_core::types::*; -use ethers_middleware::{nonce_manager::NonceManagerMiddleware, signer::SignerMiddleware}; -use ethers_providers::Middleware; -use ethers_signers::{LocalWallet, Signer}; -use std::time::Duration; +use ethers_core::{types::*, utils::Anvil}; +use ethers_middleware::MiddlewareBuilder; +use ethers_providers::{Http, Middleware, Provider}; #[tokio::test] async fn nonce_manager() { - let provider = ethers_providers::GOERLI.provider().interval(Duration::from_millis(2000)); - let chain_id = provider.get_chainid().await.unwrap().as_u64(); + let anvil = Anvil::new().spawn(); + let endpoint = anvil.endpoint(); - let wallet = std::env::var("GOERLI_PRIVATE_KEY") - .expect("GOERLI_PRIVATE_KEY is not defined") - .parse::() - .unwrap() - .with_chain_id(chain_id); - let address = wallet.address(); - - let provider = SignerMiddleware::new(provider, wallet); + let provider = Provider::::try_from(endpoint).unwrap(); + let accounts = provider.get_accounts().await.unwrap(); + let address = accounts[0]; + let to = accounts[1]; // the nonce manager must be over the Client so that it overrides the nonce // before the client gets it - let provider = NonceManagerMiddleware::new(provider, address); + let provider = provider.nonce_manager(address); let nonce = provider .get_transaction_count(address, Some(BlockNumber::Pending.into())) @@ -34,22 +28,16 @@ async fn nonce_manager() { let mut tx_hashes = Vec::with_capacity(num_tx); for _ in 0..num_tx { let tx = provider - .send_transaction( - Eip1559TransactionRequest::new().to(address).value(100u64).chain_id(chain_id), - None, - ) + .send_transaction(TransactionRequest::new().from(address).to(to).value(100u64), None) .await .unwrap(); tx_hashes.push(*tx); } - // sleep a bit to ensure there's no flakiness in the test - tokio::time::sleep(Duration::from_secs(10)).await; - let mut nonces = Vec::with_capacity(num_tx); for tx_hash in tx_hashes { nonces.push(provider.get_transaction(tx_hash).await.unwrap().unwrap().nonce.as_u64()); } - assert_eq!(nonces, (nonce..nonce + num_tx as u64).collect::>()) + assert_eq!(nonces, (nonce..nonce + num_tx as u64).collect::>()); } diff --git a/ethers-providers/src/provider.rs b/ethers-providers/src/provider.rs index 11083891..54fd646e 100644 --- a/ethers-providers/src/provider.rs +++ b/ethers-providers/src/provider.rs @@ -359,10 +359,10 @@ impl Middleware for Provider

{ // - first: if set, set to the min(current value, MFPG) // - second, if still unset, use the RPC estimated amount let mfpg = inner.max_fee_per_gas.get_or_insert(max_fee_per_gas); - inner + inner.max_priority_fee_per_gas = inner .max_priority_fee_per_gas .map(|tip| std::cmp::min(tip, *mfpg)) - .get_or_insert(max_priority_fee_per_gas); + .or(Some(max_priority_fee_per_gas)); }; } }