PatrickAlphaC / web3_py_simple_storage

47 stars 78 forks source link

web3.exceptions.ContractLogicError: execution reverted: VM Exception while processing transaction: invalid opcode #44

Closed demos2022 closed 1 year ago

demos2022 commented 1 year ago

I am running Python 3.10 and below is my code snippet

import solcx solcx.install_solc('0.6.0') from solcx import compile_standard import json from web3 import Web3

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

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",

)

print(compiled_sol)

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"] print(abi)

w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545")) chain_id = 1337 my_address = "0x5208C982B2946250EAc0f5feb02B11c8A96Ce7F8" private_key = "0x290e015a96bc346f57cf44fe8c1ceaa8f0a9bb52370a0df0***"

Create contract in Ganache

SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)

Get the latest transaction

nonce = w3.eth.get_transaction_count(my_address) print(nonce)

Build a transaction

Sign a transaction

Send a transaction

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

print(transaction)

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

print(transaction)

signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)

I get an error at this line - transaction = SimpleStorage.constructor().build_transaction({"chainId" : chain_id, "gasPrice": w3.eth.gas_price,"from" : my_address,

Error: File "cytoolz\functoolz.pyx", line 680, in cytoolz.functoolz.pipe File "cytoolz\functoolz.pyx", line 655, in cytoolz.functoolz.c_pipe File "C:\Users\sutti\AppData\Local\Programs\Python\Python310\lib\site-packages\web3_utils\contract_error_handling.py", line 82, in raise_contract_logic_error_on_revert raise ContractLogicError(f"execution reverted: {message}", data=data) web3.exceptions.ContractLogicError: execution reverted: VM Exception while processing transaction: invalid opcode

demos2022 commented 1 year ago

Additional info: I have restarted Ganache several times

PatrickAlphaC commented 1 year ago

Thanks for making this. I'm planning on making a major update to this course soonish. For the moment, it might make sense to skip this part as we are going to use a "better" tool with brownie in the freecodecamp course your taking next.

demos2022 commented 1 year ago

@PatrickAlphaC - Just FYI, I switched to Ganache CLI and this worked fine. Also with pipx install Brownie throws a truck load of error even with using the latest version of Python. Solution - git clone https://github.com/eth-brownie/brownie.git. Downloading from this repo does not throwing any error. Thought, this workaround would be useful when you roll out updated version.

Also, Thank you for the great course. :-)