From 3338cdead1588e94a83278eec5752e98863f318f Mon Sep 17 00:00:00 2001 From: James Prestwich <10149425+prestwich@users.noreply.github.com> Date: Mon, 13 Dec 2021 14:50:13 -0800 Subject: [PATCH] feature: set nonce in fill transaction (#687) * feature: set nonce in fill transaction * chore: update changelog * refactor: remove nonce_setting in fill_transaction * refactor: set nonce in send_escalating --- CHANGELOG.md | 2 ++ ethers-middleware/src/nonce_manager.rs | 12 ++++++++++++ ethers-providers/src/lib.rs | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1099c294..18d65384 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ [#624](https://github.com/gakonst/ethers-rs/pull/624). - Fix `fee_history` to first try with `block_count` encoded as a hex `QUANTITY`. [#668](https://github.com/gakonst/ethers-rs/pull/668) +- Fix `fill_transaction` to set nonces in transactions, if the sender is known + and no nonce is specified ## ethers-contract-abigen diff --git a/ethers-middleware/src/nonce_manager.rs b/ethers-middleware/src/nonce_manager.rs index 0ebc3f19..1ff15405 100644 --- a/ethers-middleware/src/nonce_manager.rs +++ b/ethers-middleware/src/nonce_manager.rs @@ -77,6 +77,18 @@ where &self.inner } + async fn fill_transaction( + &self, + tx: &mut TypedTransaction, + block: Option, + ) -> Result<(), Self::Error> { + if tx.nonce().is_none() { + tx.set_nonce(self.get_transaction_count_with_manager(block).await?); + } + + Ok(self.inner().fill_transaction(tx, block).await.map_err(FromErr::from)?) + } + /// Signs and broadcasts the transaction. The optional parameter `block` can be passed so that /// gas cost and nonce calculations take it into account. For simple transactions this can be /// left to `None`. diff --git a/ethers-providers/src/lib.rs b/ethers-providers/src/lib.rs index 83a6a591..2c72a163 100644 --- a/ethers-providers/src/lib.rs +++ b/ethers-providers/src/lib.rs @@ -248,6 +248,14 @@ pub trait Middleware: Sync + Send + Debug { ) -> Result, Self::Error> { let mut original = tx.clone(); self.fill_transaction(&mut original, None).await?; + + // set the nonce, if no nonce is found + if original.nonce().is_none() { + let nonce = + self.get_transaction_count(tx.from().copied().unwrap_or_default(), None).await?; + original.set_nonce(nonce); + } + let gas_price = original.gas_price().expect("filled"); let chain_id = self.get_chainid().await?.low_u64(); let sign_futs: Vec<_> = (0..escalations)