【发布时间】: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