2023-02-25 22:24:20 +00:00
|
|
|
use crate::spawn_anvil;
|
|
|
|
use ethers_core::types::*;
|
2023-01-06 09:29:46 +00:00
|
|
|
use ethers_middleware::MiddlewareBuilder;
|
2023-02-25 22:24:20 +00:00
|
|
|
use ethers_providers::Middleware;
|
2022-12-18 11:45:47 +00:00
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn nonce_manager() {
|
2023-02-25 22:24:20 +00:00
|
|
|
let (provider, anvil) = spawn_anvil();
|
|
|
|
let address = anvil.addresses()[0];
|
|
|
|
let to = anvil.addresses()[1];
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
// the nonce manager must be over the Client so that it overrides the nonce
|
|
|
|
// before the client gets it
|
2023-01-06 09:29:46 +00:00
|
|
|
let provider = provider.nonce_manager(address);
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
let nonce = provider
|
2021-03-16 19:46:07 +00:00
|
|
|
.get_transaction_count(address, Some(BlockNumber::Pending.into()))
|
2020-09-24 21:33:09 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.as_u64();
|
|
|
|
|
2022-03-19 16:41:03 +00:00
|
|
|
let num_tx = 3;
|
2022-03-17 13:47:30 +00:00
|
|
|
let mut tx_hashes = Vec::with_capacity(num_tx);
|
|
|
|
for _ in 0..num_tx {
|
2020-09-24 21:33:09 +00:00
|
|
|
let tx = provider
|
2023-01-06 09:29:46 +00:00
|
|
|
.send_transaction(TransactionRequest::new().from(address).to(to).value(100u64), None)
|
2020-09-24 21:33:09 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-12-17 11:26:01 +00:00
|
|
|
tx_hashes.push(*tx);
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 13:47:30 +00:00
|
|
|
let mut nonces = Vec::with_capacity(num_tx);
|
2020-09-24 21:33:09 +00:00
|
|
|
for tx_hash in tx_hashes {
|
2021-10-29 12:29:35 +00:00
|
|
|
nonces.push(provider.get_transaction(tx_hash).await.unwrap().unwrap().nonce.as_u64());
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 09:29:46 +00:00
|
|
|
assert_eq!(nonces, (nonce..nonce + num_tx as u64).collect::<Vec<_>>());
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|