简体   繁体   English

如何在 web3.py 中的 send_raw_transaction 之后使用 modify_transaction

[英]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 .我正在使用 Infura 节点,因此我必须使用w3.eth.send_raw_transaction w3.eth.account.sign_transaction它。

The gas that I used was too low apparently, and the transaction is pending for 8 hours now.我用的 gas 显然太低了,现在交易等待 8 小时。

By looking in the docs I noticed there are two methods that could help me w3.eth.modify_transaction and w3.eth.replace_transaction .通过查看文档,我注意到有两种方法可以帮助我w3.eth.modify_transactionw3.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.问题是,我在文档中看不到如何使用这两种方法之一并使用我的私钥签署修改后的交易,因为它们都对eth_sendTransaction进行 RPC 调用,共享 Infura 节点不支持。

您可以将本地帐户签名中间件与 Web3.py 一起使用,因此您不需要使用send_raw_transaction

Example of manually bumping up gas with Web3.py 5使用 Web3.py 5 手动增加 gas 的示例

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.根据我的经验,似乎maxFeePerGasmaxPriorityFeePerGas都应该增加。 There is some discussion here . 这里有一些讨论。

ps 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. ps 如果你有代码可以再次产生相同的交易,那么你可以简单地重新提交交易,而不必费心从区块链加载它的以前版本。
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).只需确保 gas 增加并且nonce保持不变(将nonce设置为get_transaction_count就是这种情况,因为挂起的交易不计算在内)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM