From 2b178e9cf79572a5905bb2d45a0061bf058b9675 Mon Sep 17 00:00:00 2001 From: teebaumcrypto <72811287+teebaumcrypto@users.noreply.github.com> Date: Mon, 31 Jan 2022 20:04:26 +0100 Subject: [PATCH] Add pub fn initialize_nonce in NonceMiddleManager (#840) * Public function added "initialize_nonce" Initialize the nonce manager with the current nonce * Update CHANGELOG.md * return generic * Added current nonce as return value Will now compile. * Load nonce to return, fixes compile. * chore: fmt Co-authored-by: Georgios Konstantopoulos --- CHANGELOG.md | 1 + ethers-middleware/src/nonce_manager.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) 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,