Justin Cho
Justin Cho

Reputation: 31

Python Web3 Ganache - Traceback error on BuildTransaction

I am going through the freeCodeCamp course on YouTube and have caught an error when I run the program. As I am trying to build a transaction into Ganache. I see in Ganache logs that there is activity as well. First time posting, so let me know what other information should I provide!

The course: Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial

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

The error that the terminal returns:

Traceback (most recent call last):
  File "C:\Users\Justin\demos\web3_py_simple_storage\deploy.py",
line 60, in <module>
    transaction = SimpleStorage.constructor().buildTransaction(
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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__
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\web3\_utils\transactions.py", line 121, in fill_transaction_defaults
    default_val = default_getter(web3, transaction)
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\web3\_utils\transactions.py", line 67, in <lambda>
    'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\web3\eth.py", line 759, in estimate_gas
    return self._estimate_gas(transaction, block_identifier)
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\web3\module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str,
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\web3\manager.py", line 197, in request_blocking
    response = self._make_request(method, params)
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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__
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\web3\middleware\formatting.py", line 76, in apply_formatters
    response = make_request(method, params)
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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__
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\web3\middleware\formatting.py", line 74, in apply_formatters
    response = make_request(method, formatted_params)
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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__
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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__
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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__
  File "cytoolz\functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\eth_utils\decorators.py", line 91, in wrapper
    return ReturnType(result)  # type: ignore
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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 "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\eth_utils\functional.py", line 45, in inner
    return callback(fn(*args, **kwargs))
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\eth_utils\applicators.py", line 84, in apply_formatters_to_dict
    yield key, formatters[key](item)
  File "cytoolz\functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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__
  File "C:\Users\Justin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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

The Full Code that I used is below:

from solcx import compile_standard, install_solc
import json
from web3 import Web3

with open("./SimpleStorage.sol", "r") as file:
    simple_storage_file = file.read()

# Compile Our Solidity
print("Installing...")
install_solc("0.6.0")

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {
                    "*": [
                        "abi",
                        "metadata",
                        "evm.bytecode",
                        "evm.bytecode.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 connecting to ganache
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
chain_id = 5777
my_address = "0xf2580f5ddfFb89e69A12a7CbCa8CA175Df4cBe08"
private_key = "0x937073896304af4297e6bbb3a3c623689d48388aa8595058752fafb522b31a13"

# Create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
print(SimpleStorage)

# Get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
print(nonce)

# 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)

Upvotes: 3

Views: 1750

Answers (5)

Code on the Rocks
Code on the Rocks

Reputation: 17794

Your error states the following:

web3.exceptions.ValidationError: The transaction declared chain ID 5777, but the connected node is on 1337

In Ganache, you can change the network ID under Settings > Server > Network ID. Change this to 1337 and restart Ganache.

enter image description here

Upvotes: 0

Robert Johnstone
Robert Johnstone

Reputation: 5371

I was having this exact same problem. Including "gasPrice": w3.eth.gas_price, solved it originally, but it came back.

After some tweaking, I found that casting chain_id (because I was getting that from .env as well) as an int solved my problems.

So the full code looked like this:

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

Upvotes: 0

Muhindo Galien
Muhindo Galien

Reputation: 81

The transaction declared chain ID 5777, but the connected node is on 1337. You have to change your chain_id:

chain_id = 5777❌

But use the one below

chain_id = 1337✔

Upvotes: 1

Kavipriyan
Kavipriyan

Reputation: 51

As of 5.25.0 of web3.py, we now need to add gasPrice to our transactions with a local Ganache chain. Adding "gasPrice": w3.eth.gas_price should fix your issue in the transactions.

Full Example:

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

Upvotes: 5

Renzo Tello
Renzo Tello

Reputation: 66

Use the following line in .buildTransaction:

transaction = SimpleStorage.constructor().buildTransaction(
{'from': address, 'nonce':nonce, 'chainId':chain_id,'gas': 1728712,
'gasPrice': w3.toWei('21', 'gwei')})

And change the chain_id = 1337.

Upvotes: 2

Related Questions