我正在浏览Youtube上的freecodecamp课程,并在我运行时发现了一个错误。当我尝试在Ganache中建立交易时。我在Ganache日志中看到有活动,但在我的事务中找不到它。我再次得到binascii错误
from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
# from dotenv import load_dotenv
# load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# print(simple_storage_file)
# compile our solidity
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
install_solc("0.6.0")
# print(compiled_sol)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# get abi
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
# for connecting to ganache
# for the chain_id, make sure you change the settings from the ganache settings by going to the server and change the chain_id to 1337
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
chain_id = 1337
my_address = "0x43daFF8768E4e2e0b90DdEFd08016D309692b4aA"
private_key = "0x21bxg2MenSJAWYKP9EpgTNK8KaVunCGTre6a96053b7c073f5a05ac70a2531c8"
# print(private_key)
# os.getenv("PRIVATE_KEY")
# Create the contract in python os.getenv("PRIVATE_KEY")
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# Get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# print(nonce)
# 1. Build a transaction
# 2. Sign a transaction
# 3. Send a transaction
# # note if you are following the tutorial from freecodecamp, add ("gasPrice": w3.eth.gas_price), to ur code.
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
}
)
# this print function is to check if the transaction is working before it will be printed out.
# print(transaction)
signed_txn = w3.eth.account.sign_transaction(transaction, private_key = private_key)
# print(signed_txn)
# # set environment variable by using export PRIVATE_KEY= 0x21bvgwyWBnUUNPQ166PQSFuBYG8ETmbsy6a96053b7c073f5a05ac70a2531c8
# # note "export" will give error because it is being used in windows. And export only works for linux. So for windows,make use of "set"
# # send the signed transaction
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
回溯(最近一次调用):
File "C:UsersudohaOneDriveDesktopsolidBlockchaindeploy.py", line 74, in <module>
signed_txn = w3.eth.account.sign_transaction(transaction, private_key = private_key)
File "C:UsersudohaAppDataRoamingPythonPython310site-packageseth_utilsdecorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:UsersudohaAppDataRoamingPythonPython310site-packageseth_accountaccount.py", line 728, in sign_transaction
account = self.from_key(private_key)
File "C:UsersudohaAppDataRoamingPythonPython310site-packageseth_utilsdecorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:UsersudohaAppDataRoamingPythonPython310site-packageseth_accountaccount.py", line 250, in from_key
key = self._parsePrivateKey(private_key)
File "C:UsersudohaAppDataRoamingPythonPython310site-packageseth_utilsdecorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:UsersudohaAppDataRoamingPythonPython310site-packageseth_accountaccount.py", line 775, in _parsePrivateKey
return self._keys.PrivateKey(HexBytes(key))
File "C:UsersudohaAppDataRoamingPythonPython310site-packageshexbytesmain.py", line 23, in __new__
bytesval = to_bytes(val)
File "C:UsersudohaAppDataRoamingPythonPython310site-packageshexbytes_utils.py", line 17, in to_bytes
return hexstr_to_bytes(val)
File "C:UsersudohaAppDataRoamingPythonPython310site-packageshexbytes_utils.py", line 50, in hexstr_to_bytes
return binascii.unhexlify(ascii_hex)
binascii.Error: Non-hexadecimal digit found
binascii错误表示传递的值包含非十六进制元素。十六进制数据的有效值是数字(0 ~ 9)和字母(A ~ F)。
从跟踪日志中,您可以看到这是由private_key参数中有一些无效数据引起的:0x21bxg2enSJWYKP9 epgTNK8KVunGTr Ce6a96053b7c073f5a05ac70a2531c8