web3 python 以太坊原始交易

web3 python ethereum raw transaction

我在尝试使用我的本地 geth 节点发送以太坊时遇到此错误。

ValueError: {'code': -32000, 'message': 'insufficient funds for gas * price + value'}

这是我构建交易平台的方式

from web3 import Web3

w3 = Web3(Web3.HTTPProvider("http://localhost:8545"))

amount = 0.01
from_address = "0xF2........."
private_key = "cf.........."
address_to = "0x..."
nonce = w3.eth.getTransactionCount(from_address)

tx = {
  'from': from_address,
  'to': address_to,
  'value': w3.toWei(amount, 'ether'),
  'gas': 21000,
  'gasPrice': w3.eth.gas_price,
  'nonce': nonce,
  'chainId': 1
}
signed_txn = w3.eth.account.sign_transaction(tx, private_key=private_key)
send = w3.eth.sendRawTransaction(signed_txn.rawTransaction)
txid = send.hex()

我的以太坊账户有超过 2 个 ETH 可用 我究竟做错了什么?谢谢!

节点错误不会说谎,所以节点认为你没有可用的2个ETH

您可以使用 web3.eth.getBalance() 轻松查看可用的数量。