Reputation: 13
I'm watching the tutorial of freeCodeCamp.org, Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial (link of the video course) and I'm stucked at 04:06:47 because when I try to build a transaction and sign it, my compiler gives me back a lot of errors:
INFORMAZIONI: impossibile trovare file corrispondenti ai criteri di ricerca indicati. Traceback (most recent call last): File "C:\Users\giuse\OneDrive\Desktop\Sol\web3_py_simple_storage\deploy.py", line 44, in transaction = SimpleStorage.constructor().buildTransaction( File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper return self.method(obj, *args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\contract.py", line 684, in buildTransaction return fill_transaction_defaults(self.web3, built_transaction) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3_utils\transactions.py", line 121, in fill_transaction_defaults default_val = default_getter(web3, transaction) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3_utils\transactions.py", line 67, in 'gas': lambda web3, tx: web3.eth.estimate_gas(tx), File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\eth.py", line 759, in estimate_gas return self._estimate_gas(transaction, block_identifier) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\module.py", line 57, in caller result = w3.manager.request_blocking(method_str, File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\manager.py", line 197, in request_blocking response = self._make_request(method, params) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\manager.py", line 150, in _make_request return request_func(method, params) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\formatting.py", line 76, in apply_formatters response = make_request(method, params) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\gas_price_strategy.py", line 90, in middleware return make_request(method, params) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\formatting.py", line 74, in apply_formatters response = make_request(method, formatted_params) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\attrdict.py", line 33, in middleware response = make_request(method, params) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\formatting.py", line 74, in apply_formatters response = make_request(method, formatted_params) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\formatting.py", line 73, in apply_formatters formatted_params = formatter(params) File "cytoolz/functoolz.pyx", line 503, in cytoolz.functoolz.Compose.call ret = PyObject_Call(self.first, args, kwargs) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\decorators.py", line 91, in wrapper return ReturnType(result) # type: ignore File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\applicators.py", line 22, in apply_formatter_at_index yield formatter(item) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\functional.py", line 45, in inner return callback(fn(*args, **kwargs)) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\applicators.py", line 84, in apply_formatters_to_dict yield key, formatterskey File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\eth_utils\applicators.py", line 72, in apply_formatter_if return formatter(value) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.call return self.func(*args, **kwargs) File "C:\Users\giuse\AppData\Local\Programs\Python\Python39\lib\site-packages\web3\middleware\validation.py", line 57, in validate_chain_id raise ValidationError( web3.exceptions.ValidationError: The transaction declared chain ID 5777, but the connected node is on 1337
I first searched on internet for solutions, but the only one was to add in the transation build one more parameter (the one of the gasPrice), but it didn't solve my problems.
I hope someone can help me, here is the full code I wrote:
from solcx import compile_standard, install_solc
import json
from web3 import Web3
from dotenv import load_dotenv
import os
load_dotenv()
install_solc("0.6.0")
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("compiledCode.json", "w") as file:
json.dump(compiled_sol, file)
bytecode = compiled_sol["contracts"]["simpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
abi = compiled_sol["contracts"]["simpleStorage.sol"]["SimpleStorage"]["abi"]
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 5777
my_address = "0xd8BADAe3766759e7e298931dF01F452616dc6dde"
pvt_key = os.getenv("PRIVATE_KEY")
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
nonce = w3.eth.getTransactionCount(my_address)
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
},
)
signed_txn = w3.eth.sign_transaction(transaction, private_key=pvt_key)
Upvotes: 0
Views: 639
Reputation: 142
If you are using ganache quickstart, instead use New Workspace option so you can save it.
Go to settings on top right corner (If you are using GUI from tutorial video)
Go to server tab and select following values and save.
Change values accordingly in your code.
Also change create trasaction code to this
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
}
)
Doing these 2 steps should fix your problem. I just did it for myself.
Upvotes: 0