3dprogramin
3dprogramin

Reputation: 39

How to use modify_transaction after send_raw_transaction in web3.py

I am using Infura node, thus I had to sign the transaction with w3.eth.account.sign_transaction and then send it with w3.eth.send_raw_transaction.

The gas that I used was too low apparently, and the transaction is pending for 8 hours now.

By looking in the docs I noticed there are two methods that could help me w3.eth.modify_transaction and w3.eth.replace_transaction. The idea would be to use one of them (not sure what's the difference between them though) to modify the transaction gas so it gets confirmed.

The problem is, I don't see in the docs how to use one of those two methods and sign the modified transaction with my private key because both of them make the RPC call to eth_sendTransaction which isn't supported by the shared Infura node.

Upvotes: 1

Views: 653

Answers (2)

ArtemGr
ArtemGr

Reputation: 12547

Example of manually bumping up gas with Web3.py 5

from web3.exceptions import TransactionNotFound

tx, receipt = None, None
try: tx = w3.eth.get_transaction (tx_hash)  # Not 100% reliable!
except TransactionNotFound: pass
try: receipt = w3.eth.get_transaction_receipt (tx_hash)
except TransactionNotFound: pass

if not receipt and tx:
  tx = tx.__dict__
  gas_price = tx['maxFeePerGas'] / 1000000000
  if gas_price <= 10:
    tx['maxPriorityFeePerGas'] = 1230000000
    tx['maxFeePerGas'] = 12300000000
    tx.pop ('blockHash', '')
    tx.pop ('blockNumber', '')
    tx.pop ('transactionIndex', '')
    tx.pop ('gasPrice', '')
    tx.pop ('hash', '')
    tx['data'] = tx.pop ('input')

    signed = w3.eth.account.sign_transaction (tx, pk)
    tid = w3.eth.send_raw_transaction (signed.rawTransaction)
    print (tid.hex())

In my experience it seems like both maxFeePerGas and maxPriorityFeePerGas should be increased. There is some discussion here.

p.s. And if you have the code around which would produce the same transaction again, then you can simply resubmit the transaction without bothering to load the previous version of it from the blockchain.
Just make sure that the gas is increased and that the nonce stays the same (which will be the case with nonce being set to get_transaction_count, as the pending transaction does not count).

Upvotes: 0

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83358

You can use local account signing middleware with Web3.py so you do not need to use send_raw_transaction.

Upvotes: 1

Related Questions