为什么sign_transaction会给我这个错误



所以我的代码的问题是,当我执行signed_tx时,它开始给我错误,在此之前其他一切都很好,因为我已经打印了所有内容,而且它应该是这样的,所以我不明白为什么它在那一行之后突然给我错误。至于区块链,我使用了ganache,然后我尝试使用ganache-cli,但仍然存在相同的错误。我试着看了很多,改变了很多设置,但似乎都不起作用。这是我第一次在这里写问题,所以如果我不够详细或犯了任何错误,请原谅我,谢谢。真的很期待解决这个问题。

这是我的代码:

from solcx import compile_standard
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()

Compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
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 collecting to Ganache
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))
chain_id = "1337"
my_address = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"
private_key = os.getenv("PK")
# create the contract in python
Simplestorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# get the nounce
nonce = w3.eth.getTransactionCount(my_address)
# 1: Build a Transaction
# 2: Sign a Transaction
# 3: Send a Transaction
transaction = Simplestorage.constructor().buildTransaction(
{
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
"gasPrice": 20000000000,
"gas": 6721975,
}
)
print(transaction)
signed_t = w3.eth.account.sign_transaction(transaction, private_key=private_key)```
here is the Error:
> Blockquote
Traceback (most recent call last):
File "/Users/madscientist/demos/web3_py_simpleStorage/deploy.py", line 60, in <module>
signed_t = w3.eth.account.sign_transaction(transaction, private_key=private_key)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/eth_utils/decorators.py", line 18, in _wrapper
return self.method(obj, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/eth_account/account.py", line 748, in sign_transaction
) = sign_transaction_dict(account._key_obj, sanitized_transaction)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/eth_account/_utils/signing.py", line 32, in sign_transaction_dict
unsigned_transaction = serializable_unsigned_transaction_from_dict(transaction_dict)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/eth_account/_utils/legacy_transactions.py", line 44, in serializable_unsigned_transaction_from_dict
assert_valid_fields(transaction_dict)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/eth_account/_utils/legacy_transactions.py", line 109, in assert_valid_fields
raise TypeError("Transaction had invalid fields: %r" % invalid)
TypeError: Transaction had invalid fields: {'chainId': '1337'}
> Blockquote

您必须将ChainId用作整数,而不是字符串。现在已经解决了。

相关内容

最新更新