So the problem with my code is when i execute the signed_tx, it starts giving me errors, everything else works fine before that because i have printed everything and it is how it should be, so that is why i do not understand why does it suddenly give me the errors after that line. As for the blockchain, i was using ganache, then i tried to use ganache-cli but still same errors. I have tried looking a lot and changed a lot of settings but nothing seems to work. Its my first time writing a question here, so forgive me if i am not detailed enough or made any mistake, thank you. really looking forward to solving this.
here is my code:
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
You have to use the ChainId as an integer, not a string. it's solved now.