Web3 Python Gas Gwei 不工作:内在气体太低

发布于 2025-01-16 23:08:36 字数 588 浏览 4 评论 0原文

为什么gas费不起作用?如果我去掉汽油费,交易就会成功。但如果我添加燃气费,交易就会失败并返回错误:

{'code': -32000, 'message': '内在燃气太低'}

web3matic = Web3(Web3.HTTPProvider(matic))

                    nonce = web3matic.eth.get_transaction_count(walletAddress)
                    result = contract.functions.buy(item, int(price)).buildTransaction({
                        'from': walletAddress,
                        'nonce': nonce,
                        'gas': 21000,
                        'gasPrice': web3matic.toWei(700, 'gwei'),
                    })
                    print(result)

Why is the gas fee not working? If I remove the gas fee, the transaction works. But if I add a gas fee, the transaction fails and returns error:

{'code': -32000, 'message': 'intrinsic gas too low'}

web3matic = Web3(Web3.HTTPProvider(matic))

                    nonce = web3matic.eth.get_transaction_count(walletAddress)
                    result = contract.functions.buy(item, int(price)).buildTransaction({
                        'from': walletAddress,
                        'nonce': nonce,
                        'gas': 21000,
                        'gasPrice': web3matic.toWei(700, 'gwei'),
                    })
                    print(result)

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

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

发布评论

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

评论(2

薆情海 2025-01-23 23:08:36

您收到此错误是因为您将 Gas 字段设置为 21000,这是简单价值/硬币转移交易的正确 Gas 量。

由于您在智能合约中调用函数,因此必须提供更多的 Gas:21000 + 您发送的每个字节的费用(零字节为 4 Gas,非零字节为 68 Gas))。

您应该有一个数据字段您可以在其中检查发送了多少字节:)

You get this error because you are setting the gas field to 21000, which is the correct amount of gas for a simple value/coin transfer transaction.

Since you are calling a function in a smart contract, you have to provide more gas: 21000 + a fee for every byte you are sending (4 gas for a zero byte, 68 gas for non-zeros)).

You should have a data field where you can check how many bytes you are sending :)

感性不性感 2025-01-23 23:08:36

尝试将 Gas 设置为 3000000 并使用 w3.eth.gasPrice 作为 GasPrice

上下文:Polygon 在 2021 年 10 月将 1 Gwei 的最低 Gas 价格提高到 30
https://forum.matic.network/t/recommished- min-gas-price-setting/2531

web3matic = Web3(Web3.HTTPProvider(matic))

                    nonce = web3matic.eth.get_transaction_count(walletAddress)
                    result = contract.functions.buy(item, int(price)).buildTransaction({
                        'from': walletAddress,
                        'nonce': nonce,
                        'gas': 3000000,
                        'gasPrice': web3matic.eth.gasPrice,
                    })
                    print(result)

Try setting gas to 3000000 and using w3.eth.gasPrice for gasPrice

Context: Polygon increased the min gas price for 1 Gwei to 30 in Oct 2021
https://forum.matic.network/t/recommended-min-gas-price-setting/2531

web3matic = Web3(Web3.HTTPProvider(matic))

                    nonce = web3matic.eth.get_transaction_count(walletAddress)
                    result = contract.functions.buy(item, int(price)).buildTransaction({
                        'from': walletAddress,
                        'nonce': nonce,
                        'gas': 3000000,
                        'gasPrice': web3matic.eth.gasPrice,
                    })
                    print(result)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文