Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feature: set nonce in fill transaction (#687)
Browse files Browse the repository at this point in the history
* feature: set nonce in fill transaction

* chore: update changelog

* refactor: remove nonce_setting in fill_transaction

* refactor: set nonce in send_escalating
  • Loading branch information
prestwich authored Dec 13, 2021
1 parent 47e9e7d commit 3338cde
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions ethers-middleware/src/nonce_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ where
&self.inner
}

async fn fill_transaction(
&self,
tx: &mut TypedTransaction,
block: Option<BlockId>,
) -> 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`.
Expand Down
8 changes: 8 additions & 0 deletions ethers-providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ pub trait Middleware: Sync + Send + Debug {
) -> Result<EscalatingPending<'a, Self::Provider>, 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)
Expand Down

0 comments on commit 3338cde

Please sign in to comment.