Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

ci: fix nonce manager test #2014

Merged
merged 3 commits into from
Jan 6, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions ethers-middleware/tests/nonce_manager.rs
Original file line number Diff line number Diff line change
@@ -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::<LocalWallet>()
.unwrap()
.with_chain_id(chain_id);
let address = wallet.address();

let provider = SignerMiddleware::new(provider, wallet);
let provider = Provider::<Http>::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()))
Expand All @@ -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::<Vec<_>>())
assert_eq!(nonces, (nonce..nonce + num_tx as u64).collect::<Vec<_>>());
}