Web3.py – Resolving Wrong transaction_count After Mining TX

web3.py

I'm using a (public) http provider endpoint on FTM.

After sending a transaction with tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction) , I loop over tx_receipt = w3.eth.get_transaction_receipt(tx_hash) until i get the receipt back. This confirms that my TX was included in a block.

Sometimes, immediately after this, need to start another transaction. But when I use
nonce = w3.eth.get_transaction_count(my_address) to set the new transaction's nonce, sign it and try to send it as raw transaction (as above), the nonce is wrong. It uses the just-confirmed-transaction's nonce instead, so it's too low by 1. So far, I can only avoid this by adding a 30-60s sleep after a confirmed TX.

First of all… why? Since I got a receipt, my web3-http-endpoint positively KNOWS that the previous transaction confirmed. Is there some caching going on? Why is it giving me the old transaction count?

And how can I avoid this?

Best Answer

This fixed the issue, if we include pending transactions into the transaction count: nonce = w3.eth.get_transaction_count(my_address, 'pending') (thanks to B3nac_Sec from the web3.py Discord for pointing this out to me!)

I'd still be very interested to know why that's needed though, because the web3-provider does know of the confirmed TX (it reported it back to me!), yet doesnt count it immediately.

Related Topic