diff --git a/CHANGELOG.md b/CHANGELOG.md index a92aa447..a251d4cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -182,6 +182,7 @@ [#568](https://github.com/gakonst/ethers-rs/pull/568) - Removes GasNow as a gas price oracle [#508](https://github.com/gakonst/ethers-rs/pull/508) +- add initialize_nonce public function to initialize NonceMiddleManager ### 0.5.3 diff --git a/ethers-middleware/src/nonce_manager.rs b/ethers-middleware/src/nonce_manager.rs index 1ff15405..f9e221a0 100644 --- a/ethers-middleware/src/nonce_manager.rs +++ b/ethers-middleware/src/nonce_manager.rs @@ -30,6 +30,24 @@ where nonce.into() } + pub async fn initialize_nonce( + &self, + block: Option, + ) -> Result> { + // initialize the nonce the first time the manager is called + if !self.initialized.load(Ordering::SeqCst) { + let nonce = self + .inner + .get_transaction_count(self.address, block) + .await + .map_err(FromErr::from)?; + self.nonce.store(nonce.as_u64(), Ordering::SeqCst); + self.initialized.store(true, Ordering::SeqCst); + } + // return current nonce + Ok(self.nonce.load(Ordering::SeqCst).into()) + } + async fn get_transaction_count_with_manager( &self, block: Option,