The transaction declared chain ID 5777, but the connected node is on 1337
Asked Answered
M

7

13

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:\Users\evens\demos\web3_py_simple_storage\deploy.py", line 
52, in <module>
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\Python310\lib\site-packages\eth_utils\decorators.py", line 
18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\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\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\_utils\transactions.py", line 114, in 
fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\_utils\transactions.py", line 60, in <lambda>
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\eth.py", line 820, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\module.py", line 57, in caller
result = w3.manager.request_blocking(method_str,
File "C:\Users\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\manager.py", line 197, in request_blocking
response = self._make_request(method, params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\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\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\formatting.py", line 76, in 
apply_formatters
response = make_request(method, params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\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\evens\AppData\Roaming\Python\Python310\site- 
packages\web3\middleware\formatting.py", line 74, in 
apply_formatters
response = make_request(method, formatted_params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\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\evens\AppData\Roaming\Python\Python310\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\evens\AppData\Roaming\Python\Python310\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:\Python310\lib\site-packages\eth_utils\decorators.py", line 
91, in wrapper
return ReturnType(result)  # type: ignore
File "C:\Python310\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__
File "cytoolz/functoolz.pyx", line 250, in 
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Python310\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\evens\AppData\Roaming\Python\Python310\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
Madelle answered 16/1, 2022 at 15:27 Comment(0)
R
37

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

Ringleader answered 17/1, 2022 at 18:17 Comment(3)
Awesome changed the ChainId on ganache and it worked!! ThanksTirpitz
you're the man, I'm also working through the exact same solidity course atm! I was getting stuck in the next step already and then I saw your comment about the gas price. Thank you!Tennies
@Tennies this is a great resource for issues on the course github.com/smartcontractkit/full-blockchain-solidity-course-py/… The Eth stack exchange is also good. It gets to a point where every section on the course becomes a battle to get stuff working... stay strong!Ringleader
B
12

this line of code is wrong

chain_id = 5777

Ganache chain id is not 5777. This is network id. Network id is used by nodes to transfer data between nodes that are on the same network. Network id is not included in blocks and it is not used for signing transactions or mining blocks.

 chain_id = 1377

Chain ID is not included in blocks either, but it is used during the transaction signing and verification process.

Bindweed answered 17/1, 2022 at 19:14 Comment(4)
In case someone is confused, there is a typo above, it should be chain_id = 1337Knucklehead
is there a place in some setting where I Can actually <<SEE>> that the chain id is 1337 ?Ruff
@Ruff Chain ID is usually used as an identification of the network. you use chain id to tell metamask which network you are going to communicate with. you cannot change the chain id of ganacheBindweed
I see! I thought it's similar to those cases where you choose which port is used by an appRuff
P
8

It works for me, I get value chain_id from w3.eth.chain_id

transaction = SimpleStorage.constructor().buildTransaction(
{
    "gasPrice": w3.eth.gas_price,
    "chainId": w3.eth.chain_id,
    "from": my_address,
    "nonce": nonce,
}

)

Delegates to eth_chainId RPC Method

Returns an integer value for the currently configured “Chain Id” value introduced in EIP-155. Returns None if no Chain Id is available.

Prayer answered 10/3, 2022 at 16:36 Comment(0)
C
2

Hey it happened to me too, you need to build the constructor like this

 SimpleStorage.constructor().buildTransaction( {
    "gasPrice": w3.eth.gas_price, 
    "chainId": chain_id, 
    "from": my_address, 
    "nonce": nonce, 

You need to add the gas price part. Worked for me

Cracking answered 5/2, 2022 at 22:0 Comment(0)
D
2

I encountered this issue today and after debugging, I am certain that the issue is from the dotenv config variables imported. All the variables are been seen by python as strings, and for some reason, the web3 library swaps the value 5777 if the type of your chain id is not an integer.

The perfect fix is to cast your chain_id into type int before deploying your smart contract.

Hope this saves y'all some pondering time, peace out!

Snippet example below:

transaction = SimpleStorage.constructor().buildTransaction(
{
    "gasPrice": w3.eth.gas_price,
    "chainId": int(chain_id),
    "from": my_address,
    "nonce": nonce,
}

)

Daimon answered 16/9, 2022 at 22:9 Comment(0)
H
0

Perhaps, you are following FreeCodeCamp tutorial. I have got the same problem

In my case, it is working after adding gas price, "gasPrice": w3.eth.gas_price

transaction = SimpleStorage.constructor().buildTransaction({ "gasPrice": w3.eth.gas_price, "chainId" : chain_id, "from": my_address, "nonce": nonce})
print(transaction)
Hyperthyroidism answered 23/7, 2022 at 15:31 Comment(0)
M
0

You can check your chainId using the following code

const chainId = await wallet.getChainId();
console.log("chain Id",chainId);

For me it was 1337

Marchal answered 26/11, 2022 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.