I am trying to deploy my SimpleStorage.sol contract to a ganache local chain by making a transaction using python. It seems to have trouble connecting to the chain.
from solcx import compile_standard from web3 import Web3 import json 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"] # to connect to ganache blockchain w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545")) chain_id = 5777 my_address = "0xca1EA31e644F13E3E36631382686fD471c62267A" private_key = os.getenv("PRIVATE_KEY") # create the contract in python SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode) # get the latest transaction 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} ) print(transaction)
It seems to be connected to the ganache chain because it prints the nonce, but when I build and try to print the transaction here is the entire traceback call I am receiving
Traceback (most recent call last): File "C:Usersevensdemosweb3_py_simple_storagedeploy.py", line 52, in <module> transaction = SimpleStorage.constructor().buildTransaction( File "C:Python310libsite-packageseth_utilsdecorators.py", line 18, in _wrapper return self.method(obj, *args, **kwargs) File "C:UsersevensAppDataRoamingPythonPython310site- packagesweb3contract.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:UsersevensAppDataRoamingPythonPython310site- packagesweb3_utilstransactions.py", line 114, in fill_transaction_defaults default_val = default_getter(web3, transaction) File "C:UsersevensAppDataRoamingPythonPython310site- packagesweb3_utilstransactions.py", line 60, in <lambda> 'gas': lambda web3, tx: web3.eth.estimate_gas(tx), File "C:UsersevensAppDataRoamingPythonPython310site- packagesweb3eth.py", line 820, in estimate_gas return self._estimate_gas(transaction, block_identifier) File "C:UsersevensAppDataRoamingPythonPython310site- packagesweb3module.py", line 57, in caller result = w3.manager.request_blocking(method_str, File "C:UsersevensAppDataRoamingPythonPython310site- packagesweb3manager.py", line 197, in request_blocking response = self._make_request(method, params) File "C:UsersevensAppDataRoamingPythonPython310site- packagesweb3manager.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:UsersevensAppDataRoamingPythonPython310site- packagesweb3middlewareformatting.py", line 76, in apply_formatters response = make_request(method, params) File "C:UsersevensAppDataRoamingPythonPython310site- packagesweb3middlewaregas_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:UsersevensAppDataRoamingPythonPython310site- packagesweb3middlewareformatting.py", line 74, in apply_formatters response = make_request(method, formatted_params) File "C:UsersevensAppDataRoamingPythonPython310site- packagesweb3middlewareattrdict.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:UsersevensAppDataRoamingPythonPython310site- packagesweb3middlewareformatting.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:UsersevensAppDataRoamingPythonPython310site- packagesweb3middlewareformatting.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:Python310libsite-packageseth_utilsdecorators.py", line 91, in wrapper return ReturnType(result) # type: ignore File "C:Python310libsite-packageseth_utilsapplicators.py", line 22, in apply_formatter_at_index yield formatter(item) File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__ File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__ return self.func(*args, **kwargs) File "C:Python310libsite-packageseth_utilsapplicators.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:UsersevensAppDataRoamingPythonPython310site- packagesweb3middlewarevalidation.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
Advertisement
Answer
Had this issue myself, apparently it’s some sort of Ganache CLI error but the simplest fix I could find was to change the network id in Ganache through settings>server to 1337. It restarts the session so you’d then need to change the address and private key variable.
If it’s the same tutorial I’m doing, you’re likely to come unstuck after this… the code for transaction should be:
transaction = SimpleStorage.constructor().buildTransaction( { "gasPrice": w3.eth.gas_price, "chainId": chain_id, "from": my_address, "nonce": nonce, }) print(transaction)
Otherwise you get a value error if you don’t set the gasPrice