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

Commit

Permalink
fix: reset nonce if nonce manager errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Aug 21, 2020
1 parent 0d8452a commit c5ec606
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
25 changes: 25 additions & 0 deletions ethers-signers/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ where
self.fill_transaction(&mut tx, block).await?;

// sign the transaction and broadcast it
let mut tx_clone = tx.clone();
let result = self.submit_transaction(tx).await;

// if we got a nonce error, get the account's latest nonce and re-submit
let tx_hash = if result.is_err() {
let mut nonce_manager = self.nonce_manager.write().await;
let nonce = self
.provider
.get_transaction_count(self.address(), block)
.await?;
if nonce != nonce_manager.nonce {
nonce_manager.nonce = nonce;
tx_clone.nonce = Some(nonce);
self.submit_transaction(tx_clone).await?
} else {
result?
}
} else {
result?
};

Ok(tx_hash)
}

async fn submit_transaction(&self, tx: TransactionRequest) -> Result<TxHash, ClientError> {
Ok(if let Some(ref signer) = self.signer {
let signed_tx = signer.sign_transaction(tx).map_err(Into::into)?;
self.provider.send_raw_transaction(&signed_tx).await?
Expand Down
14 changes: 12 additions & 2 deletions ethers-signers/tests/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,23 @@ mod eth_tests {

let mut tx_hashes = Vec::new();
for _ in 0..10 {
let tx = client.send_transaction(TransactionRequest::pay(wallet2.address(), 100u64), None).await.unwrap();
let tx = client
.send_transaction(TransactionRequest::pay(wallet2.address(), 100u64), None)
.await
.unwrap();
tx_hashes.push(tx);
}

let mut nonces = Vec::new();
for tx_hash in tx_hashes {
nonces.push(client.get_transaction(tx_hash).await.unwrap().nonce.as_u64());
nonces.push(
client
.get_transaction(tx_hash)
.await
.unwrap()
.nonce
.as_u64(),
);
}

assert_eq!(nonces, (0..10).collect::<Vec<_>>())
Expand Down

0 comments on commit c5ec606

Please sign in to comment.