BlockChain (VM Exception while processing transaction)

48 Views Asked by At

I am sending a transaction using solidity with python and ganache
Every time I send the transaction it gives me error like "VM Exception while processing transaction: invalid opcode', 'stack': 'RuntimeError: VM Exception while processing transaction: invalid opcode"
Please give me solution .
i am trying it but i can't .
Help please.

My code is here

from solcx import compile_standard
from web3 import Web3
from dotenv import load_dotenv
load_dotenv()
import json
import os

with open("./SimpleStorage.sol", "r") as file:
    simple_storage_file = file.read()
    
    # Compile our solidity
    compiled_sol = compile_standard(
        {
            "language": "Solidity",
            "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
            "settings": {
                "outputSelection": {
                    "*": {"*": ["abi","metadata", "evm.bytecode", "evm.sourceMap"]}
                }
            },
        },
        solc_version="0.8.22",
    )
    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(blockchain)
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
my_adress = os.environ.get("My_Addres")
private_key = os.environ.get("PRIVATE_KEY")
chain_id = 1337
nonce = w3.eth.get_transaction_count(my_adress)
# Creating the contract in python
SimpleStorage = w3.eth.contract(abi= abi,bytecode=bytecode)
transaction = SimpleStorage.constructor().build_transaction({
    "chainId": chain_id,
    "from": my_adress,
    "nonce": nonce,
    "value": w3.to_wei(1, "wei"),
    "gas": 2000000,
    "gasPrice": w3.to_wei(20, "gwei")  # Set an appropriate gas price
})
signed_txn = w3.eth.account.sign_transaction(transaction,private_key)
txn_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

and the error is

Traceback (most recent call last):
  File "D:\BlockChain\PythonVersion\Demo\web3_py_simple_storage\deploy.py", line 47, in <module>
    txn_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cyber\AppData\Local\Programs\Python\Python312\Lib\site-packages\web3\eth\eth.py", line 391, in send_raw_transaction
    return self._send_raw_transaction(transaction)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cyber\AppData\Local\Programs\Python\Python312\Lib\site-packages\web3\module.py", line 75, in caller
    result = w3.manager.request_blocking(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cyber\AppData\Local\Programs\Python\Python312\Lib\site-packages\web3\manager.py", line 324, in request_blocking
    return self.formatted_response(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cyber\AppData\Local\Programs\Python\Python312\Lib\site-packages\web3\manager.py", line 287, in formatted_response
    raise ValueError(error)
ValueError: {'message': 'VM Exception while processing transaction: invalid opcode', 'stack': 'RuntimeError: VM Exception while processing transaction: invalid opcode\n    at LegacyTransaction.fillFromResult (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:12745)\n    at Miner.<anonymous> (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:36703)\n    at async Miner.<anonymous> (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:35116)\n    at async Miner.mine (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:39680)\n    at async Blockchain.mine (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:60063)\n    at async Promise.all (index 0)\n    at async TransactionPool.emit (C:\\Program Files\\WindowsApps\\GanacheUI_2.7.1.0_x64__rb4352f0jd4m2\\app\\resources\\static\\node\\node_modules\\ganache\\node_modules\\emittery\\index.js:303:3)', 'code': -32000, 'name': 'RuntimeError', 'data': {'hash': '0x3bba0ecd5797aa613201f463ebc8b7560886ec492800485604c495864d2fef0a', 'programCounter': 12, 'result': '0x3bba0ecd5797aa613201f463ebc8b7560886ec492800485604c495864d2fef0a', 'reason': None, 'message': 'invalid opcode'}}
0

There are 0 best solutions below