solidityweb3pyganache

Transaction REVERT error using Web3.py and local Ganache instance


Trying to deploy / transact against a local Ganache instance using web3.py. I'm able to read from a deployed contract. I'm also able to read / write through Truffle.

At first I thought it was something wrong with the contract but I still get the same error using an empty contract:

from solcx import compile_source, compile_files

source = '''
pragma solidity >=0.4.22 <0.9.0;

contract TruffleTutorial {
}
'''
compiled_sol = compile_source(
    source,
    output_values=["abi", "bin-runtime"],
    solc_version="0.8.9")

contract_id, contract_interface = compiled_sol.popitem()
bytecode = contract_interface['bin-runtime']
abi = contract_interface['abi']
dummyContract = w3.eth.contract(abi=abi, bytecode=bytecode)
nonce = w3.eth.getTransactionCount(Web3.toChecksumAddress(address))+1
txn = dummyContract.constructor().buildTransaction({
    "chainId": 1337,
    "gasPrice": w3.eth.gas_price,
    "from": w3.eth.accounts[0],
    "nonce": nonce
})

Error is:

PUSH1
PUSH1
MSTORE
PUSH1
DUP1
REVERT
 <   {
 <     "id": 43,
 <     "jsonrpc": "2.0",
 <     "error": {
 <       "message": "VM Exception while processing transaction: revert",
 <       "code": -32000,
 <       "data": {
 <         "stack": "c: VM Exception while processing transaction: revert\n    at Function.c.fromResults (/Users/me/some/path/node_modules/ganache-cli/build/ganache-core.node.cli.js:4:192416)\n    at e.exports (/Users/me/some/path/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:2089395)",
 <         "name": "c"
 <       }
 <     }
 <   }

Solution

  • I could've sworn there was no error in the stack trace, but after posting this question I saw a reference to "fill_transaction_defaults" so I added gas like the following and it worked!

    txn = dummyContract.constructor().buildTransaction({
      "chainId": 1337,
      "gasPrice": w3.eth.gas_price,
      "gas": 1728712,
      "from": w3.eth.accounts[0],
      "nonce": nonce })