web3 python的buildtransaction太慢了

发布于 2025-01-28 23:23:30 字数 273 浏览 3 评论 0原文

我正在尝试从Web3 Python库构建交易。

swap_transaction = transaction.buildTransaction(
    {
        "from": Address,
        "gas": 300000,
        "gasPrice": w3.eth.gas_price,
        "nonce": nonce,
    }
)

我遇到了一个问题,即这种构建需要太多时间,例如2-3分钟。 如何使其更快?

I'm trying to build transaction from web3 python library.

swap_transaction = transaction.buildTransaction(
    {
        "from": Address,
        "gas": 300000,
        "gasPrice": w3.eth.gas_price,
        "nonce": nonce,
    }
)

I met a problem that this build takes too much time, like 2-3 mins.
How to make it faster?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情 2025-02-04 23:23:30

我发现了您的问题,因为我的构建时间也很慢。25秒钟左右。我不知道为什么这么慢,但是我认为该工具实际上并没有做太多。

如果您查看buildtransaction函数的结果,它只是一个简单的dict,其中包含一些添加(a'至'''字段,链条,如果您调用合同功能调用,数据字段)。
这些字段很容易自行插入。如果要添加合同功能调用的数据,可以使用EncodeAbi函数(例如:

myContract = web3.eth.contract(contractAddress, abi=contractAbi)
encodedData = myContract.encodeABI(fn_name='myFunctionName', args=['foo','bar'])

然后将数据添加到TX参数阵列中)获得。

这样做将我的TX的建立时间从25秒的时间减少到毫秒。

I found your question as I too have a very slow build time.. 25 seconds or so. I have no idea why it is so slow, but I figured that the tool wasn't actually doing much.

If you look at the result of the buildTransaction function, it's just a simple dict with a few additions (a 'to' field, chainId, and if you are calling a contract function call, a data field).
These fields are easily inserted on your own. If you want to add data for a contract function call, you can get that by using the encodeABI function, eg:

myContract = web3.eth.contract(contractAddress, abi=contractAbi)
encodedData = myContract.encodeABI(fn_name='myFunctionName', args=['foo','bar'])

and then add the data to your TX parameters array.

Doing it this way reduced my TX build time from something like 25 seconds to milliseconds.

痴梦一场 2025-02-04 23:23:30
store_transaction = simple_storage.functions.store(15).build_transaction(
    {
        "chainId": chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": my_address,
        "nonce": nonce + 1,
    }
)
store_transaction = simple_storage.functions.store(15).build_transaction(
    {
        "chainId": chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": my_address,
        "nonce": nonce + 1,
    }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文