1

这是我第一次尝试使用 web3 进行任何操作,但我的交易没有发送区块链和我一样,我也可以查询以查看链上的帐户余额,但在尝试发送交易时我不断收到相同的错误。

from web3 import Web3


gnache_url = "HTTP://127.0.0.1:7545"
web3 = Web3(Web3.HTTPProvider(gnache_url))

print(web3.isConnected())

account1 = "0x9D7700325246447A5e481f355167637e1a2f2a0A"
account2 = "0x3F764C22F69Da4754b16796cD4D2e5383FE43a5e"

private_key = "9aa34d6f42d04aa53146cdbba11a3e7e45d10971ae5b2bc49683d64b21cfa2b1"

#building the transaction
balance1 = web3.eth.get_balance(account1)
print(balance1)
nonce = web3.eth.getTransaction(account1)
transaction_info = {
    'nonce': nonce,
    'to': account2,
    'value': web3.toWei(1, 'ether'),
    'gas': 2000000,
    'gasPrice': web3.toWei('50','gwei')
}
signed_transaction = web3.eth.account.signTransaction(transaction_info,private_key)
print(signed_transaction.rawTransaction)

给出这个输出

True
100000000000000000000
Traceback (most recent call last):
  File "/home/th3shadow/Documents/BlockChain Stuff/Dapp Begin.py", line 17, in <module>
    nonce = web3.eth.getTransaction(account1)
  File "/usr/local/lib/python3.8/dist-packages/web3/module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str, params, error_formatters)
  File "/usr/local/lib/python3.8/dist-packages/web3/manager.py", line 160, in request_blocking
    apply_error_formatters(error_formatters, response, params)
  File "/usr/local/lib/python3.8/dist-packages/web3/manager.py", line 65, in apply_error_formatters
    formatted_response = pipe(params, error_formatters)
  File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
  File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
  File "/usr/local/lib/python3.8/dist-packages/web3/_utils/method_formatters.py", line 604, in raise_transaction_not_found
    raise TransactionNotFound(f"Transaction with hash: {transaction_hash} not found.")
web3.exceptions.TransactionNotFound: Transaction with hash: 0x9d7700325246447a5e481f355167637e1a2f2a0a000000000000000000000000 not found.
4

1 回答 1

3

定义 nonce 变量时记得使用

 web3.eth.get_transaction_count

而不仅仅是

web3.eth.getTransaction

于 2021-05-25T23:34:26.770 回答