【问题标题】:Is there a way to solve this insufficient funds for gas when deploying a signed transaction to ganache?在将签名交易部署到 ganache 时,有没有办法解决 gas 资金不足的问题?
【发布时间】:2022-07-18 08:42:18
【问题描述】:

大家好!我目前正在参加关于 Solidity、区块链和智能合约的 16 小时 freecodecamp 课程,我在向 Ganache 发送简单的签名交易时遇到问题,并且我不断收到此值错误消息“ValueError:{'message':' gas * price + value 资金不足', 'stack': 'Error: TransactionPool.prepareTransaction (/home/fingergod/.nvm/versions/node/v17.8.0/lib/ node_modules/ganache/dist/node/1.js:2:131154)', 'code': -32003}"。

附:在构建交易时,我已经将我的 gas 价格设置为“gasPrice”:“gasPrice”:w3.eth.gas _price

from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
from dotenv import load_dotenv

load_dotenv()

install_solc("0.8.13")

with open("./SimpleStorage.sol", "r") as file:
    simple_storage_file = file.read()

# compile solidity file
Compiled_solFile = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourcecode"]}
            }
        },
    },
    solc_version="0.8.13",
)
# print(Compiled_solFile)

with open("compiled_code.json", "w") as file:
    json.dump(Compiled_solFile, file)

    # get bytecode
    bytecode = Compiled_solFile["contracts"]["SimpleStorage.sol"]["simpleStorage"][
        "evm"
    ]["bytecode"]["object"]
    # get abi
    abi = Compiled_solFile["contracts"]["SimpleStorage.sol"]["simpleStorage"]["abi"]
# print(abi)

# for conneecting to ganache
url = "hTTP://127.0.0.1:8545"
w3 = Web3(Web3.HTTPProvider(url))
chain_id = 1337
my_address = "0x15f029FEB462294b117AD56b1736c551c64a4D80"
private_key = os.getenv("PRIVATE_KEY")
print(private_key)

# Create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
print(SimpleStorage)

# get the nonce/latest transaction count
nonce = w3.eth.getTransactionCount(my_address)
print(nonce)

# 1. Build the transacion(needs;chainid,address,nonce)
# 2. Sign the transaction(needs;transaction,privatekey)
# 3. Send the signed transaction

# 1.
transaction = SimpleStorage.constructor().buildTransaction(
    {
        "chainId": chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": my_address,
        "nonce": nonce,
    }
)
print(transaction)

# 2.
signed_txn = w3.eth.account.signTransaction(transaction, private_key=private_key)
print(signed_txn)

# 3.
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

任何帮助将不胜感激。这两天我被困在这里了。

【问题讨论】:

  • 你检查my_address余额了吗?
  • 怎么样? Ganache 为我们提供了 100 ETH 用于测试目的,而我要做的就是向他们提交签名交易。
  • 使用w3.eth.get_balance(my_address)查看余额

标签: ethereum smartcontracts ganache web3py


【解决方案1】:

我认为这与您的 gasLimit 值有关,因为 gas * price 等于您发送的 gas 量,而不是运营成本的 gas。你可能已经从你正在学习的课程中看到了,但作为一个提醒;当您向合约发送交易时,您发送的是在 gasLimit 属性中指定的气体量,与它的成本无关。这意味着您实际上发送的 Wei 等于 gasLimit * gasPrice + value。尝试将交易对象中的 gasLimit 属性(为保持一致性以十六进制格式)设置为低于网络默认值的值,也可能设置为 gasPrice。

这应该是账户余额不足的问题。尝试使用像 transact() 这样的高级函数发送相同的事务,看看它是否成功。如果是这样,您可以从您的 build_transaction() 的 tx 详细信息中复制 gasLimit 和 gasPrice。

另外注意:你可以省略 "gasPrice": w3.eth.gas_price 行,因为你是在分配网络默认值,如果你省略它会自动完成,所以没有除非您要分配另一个自定义值,否则请指向它。

【讨论】:

    【解决方案2】:

    我有一个非常相似的问题,除了我处于同一课程的这个阶段:

    # get the latest transactions
    nonce = w3.eth.getTransactionCount(my_address)
    print(nonce)
    

    我收到了这个错误:

    ValueError: {'message': 'insufficient funds for gas * price + value', 'stack': 'Error: insufficient funds for gas * price + value\n    at TransactionPool.prepareTransaction (/usr/lib/node_modules/ganache/dist/node/1.js:2:133762)', 'code': -32003}
    

    不太确定如何继续前进,您从...开始尝试过什么?

    【讨论】:

      猜你喜欢
      • 2019-04-18
      • 2019-08-14
      • 2023-04-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2018-05-03
      • 2018-04-30
      相关资源
      最近更新 更多