2021-08-23 09:56:44 +00:00
|
|
|
#![cfg(not(target_arch = "wasm32"))]
|
2020-09-24 21:33:09 +00:00
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(not(feature = "celo"))]
|
|
|
|
async fn nonce_manager() {
|
|
|
|
use ethers_core::types::*;
|
2020-10-08 15:56:36 +00:00
|
|
|
use ethers_middleware::{nonce_manager::NonceManagerMiddleware, signer::SignerMiddleware};
|
2020-09-24 21:33:09 +00:00
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
2021-07-29 20:22:25 +00:00
|
|
|
use ethers_signers::{LocalWallet, Signer};
|
2021-10-29 12:29:35 +00:00
|
|
|
use std::{convert::TryFrom, time::Duration};
|
2020-09-24 21:33:09 +00:00
|
|
|
|
|
|
|
let provider =
|
|
|
|
Provider::<Http>::try_from("https://rinkeby.infura.io/v3/fd8b88b56aa84f6da87b60f5441d6778")
|
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(2000u64));
|
2021-07-29 20:22:25 +00:00
|
|
|
let chain_id = provider.get_chainid().await.unwrap().as_u64();
|
2020-09-24 21:33:09 +00:00
|
|
|
|
2021-10-30 11:12:42 +00:00
|
|
|
let wallet = std::env::var("RINKEBY_PRIVATE_KEY")
|
|
|
|
.unwrap()
|
2020-10-02 08:41:16 +00:00
|
|
|
.parse::<LocalWallet>()
|
2021-07-29 20:22:25 +00:00
|
|
|
.unwrap()
|
|
|
|
.with_chain_id(chain_id);
|
2020-09-24 21:33:09 +00:00
|
|
|
let address = wallet.address();
|
|
|
|
|
2020-10-08 15:56:36 +00:00
|
|
|
let provider = SignerMiddleware::new(provider, wallet);
|
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
|
2020-10-08 15:56:36 +00:00
|
|
|
let provider = NonceManagerMiddleware::new(provider, 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();
|
|
|
|
|
|
|
|
let mut tx_hashes = Vec::new();
|
|
|
|
for _ in 0..10 {
|
|
|
|
let tx = provider
|
2022-01-30 19:21:16 +00:00
|
|
|
.send_transaction(
|
|
|
|
Eip1559TransactionRequest::new().to(address).value(100u64).chain_id(chain_id),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-30 09:33:06 +00:00
|
|
|
// sleep a bit to ensure there's no flakiness in the test
|
|
|
|
std::thread::sleep(std::time::Duration::new(3, 0));
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
let mut nonces = Vec::new();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(nonces, (nonce..nonce + 10).collect::<Vec<_>>())
|
|
|
|
}
|