将签名交易部署到ganache时,有没有办法解决gas资金不足的问题?

发布于 2025-01-19 12:19:49 字数 2433 浏览 5 评论 0原文

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

PS 我已经将我的天然气价格设置为“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)

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

Good day, everyone! I'm currently taking a 16-hour freecodecamp course on Solidity, Blockchain, and Smart Contracts, and I'm having trouble sending a simple signed transaction to Ganache and I keep getting this Value error message "ValueError: {'message': 'insufficient funds for gas * price + value', 'stack': 'Error: insufficient funds for gas * price + value\n at TransactionPool.prepareTransaction (/home/fingergod/.nvm/versions/node/v17.8.0/lib/node_modules/ganache/dist/node/1.js:2:131154)', 'code': -32003}".

P.S. I've already set my gas price to "gasPrice" while building transactions to be: "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)

Any assistance would be highly appreciated. I've been stranded here for the past two days.

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

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

发布评论

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

评论(3

若有似无的小暗淡 2025-01-26 12:19:49

我认为这与您的煤气限价由于煤气 *价格等于您发送的汽油,而不是运营成本的气体多少。您可能会从您的课程中看到,但要提醒您;当您将交易发送给合同时,您将发送 Gaslimit 属性中指定的汽油量,而不必独立于费用。这意味着您实际上发送的WEI等于Gaslimit * Gas​​price + value 。尝试将交易对象中的 Gaslimit 属性(以一致性的HEX格式)设置为低于网络默认值的值,也许还可能是 gasprice

这应该是帐户余额不足的问题。尝试使用像Transact()这样的高级函数发送相同的交易,以查看是否成功。如果是这样,您可以从build_transaction()的TX详细信息中复制气候和加油。

另一个注意:您可以省略“ gasprice”:w3.eth.gas_price line,因为您要分配网络默认值,如果您省略了,则自动完成,因此没有意义除非您要分配另一个自定义值。

I think this has to do with your gasLimit value due to gas * price being equal to how much gas you are sending and not how much gas the operation costs. You might have seen from the course you are taking, but as a reminder; when you are sending a transaction to a contract, you are sending the amount of gas which is specified in the gasLimit attribute independent of how much it will cost. That means you are actually sending Wei equal to the gasLimit * gasPrice + value. Try to set your gasLimit attribute (in hex format for consistency) in transaction object to a value lower than your network default and maybe the gasPrice as well.

This should be an issue with insufficient account balance. Try sending the same transaction using an high-level function like transact() to see if it succeeds. If so, you can replicate the gasLimit and gasPrice from the tx details for your build_transaction().

Another note: You can omit the "gasPrice": w3.eth.gas_price line, because you are assigning the network default value, which is done automatically if you omit it, so there is no point to it unless you are going to assign another custom value.

三月梨花 2025-01-26 12:19:49

如果你像我一样使用纱线包 Ganache,第一个钱包将无法工作,即使它显示你有 1000eth,它也无法工作,只需输入第二个钱包的密钥并检查它是否有余额

const balance = await wallet.getBalance();
console.log("Account balance:", ethers.utils.formatEther(balance));

这个解决方案对我有用

If you are using the yarn package Ganache like I am the first wallet wont work even if its showing you have 1000eth present it wont work just put the secret key of the second wallet and check if it has balance

const balance = await wallet.getBalance();
console.log("Account balance:", ethers.utils.formatEther(balance));

This solution worked for me

兰花执着 2025-01-26 12:19:49

就我而言,更改价格汽油价格/重新启动甘纳许有效:

要更改汽油价格:转到设置 ->链条设置

确保私钥、合约地址、发送者地址和接收者地址(如果使用)正确,并交叉验证

In my case changing the price gas price/restarting the ganache worked:

To change the gas price: go to settings -> chain setting

Make sure the private key, contract address, sender address, and receiver address if used are correct, and cross-verify.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文