0

web3.js and general ethereum noob here. I have a smart contract written in solidity with a bunch of getter methods, for example:

contract mycontract {
    uint XYZ = 2;
...
...
...
 
    function getXYZ() public view returns (uint){
        return XYZ;
    }
}

I have deployed this smart contract on a private chain and started a provider using geth.

Using web3.py in python, the following code works:

w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545/"))
contract = w3.eth.contract(address=contract_address, abi=abi)
contract.functions.getXYZ().call()

However, the following equivalent code does not work in the browser or nodejs using web3.js-1.3.4

let w3 = new Web3("http://127.0.0.1:8545");
contract = new w3.eth.Contract(abi, contract_address);
contract.methods.getXYZ().call();

The error I get is Error: execution reverted both in the browser console/node js script without any stacktrace. Error in the browser

Is there something obviously wrong that I am doing? I have also tried to get this work with a web-socket provider instead of a http-provider, since the web3.js documentation indicates that the http provider is deprecated. However, I run into the same issue there as well.

Edit with some more information:

  1. I am using a custom geth executable of geth Version: 1.9.26-unstable https://github.com/ethereum/go-ethereum/blob/master/consensus/ethash/consensus.go at Git Commit: 2e5d14170846ae72adc47467a1129e41d6800349 . Only change that was made was the function CalcDifficulty in line 315 which was changed to always return 1 instead of performing the difficulty calculation.

  2. Here is the bash script I am using to start my geth provider.

#!/bin/bash
nice -50 geth --datadir ~/.ethereum/myethereumnet --gasprice 0 --nodiscover --networkid 2020 console --maxpeers 0 --rpc --rpcport 8545 --rpcaddr "0.0.0.0" --rpccorsdomain "*" --rpcapi "eth,net,personal,debug,web3,miner" --preload "startmine.js" --allow-insecure-unlock --nousb console
  1. Here is startmine.js. This is used because continuous mining is not desired for testing/development purposes.
var mining_threads = 1
var txBlock = 0

function checkWork() {
if (eth.getBlock("pending").transactions.length > 0) {
    txBlock = eth.getBlock("pending").number
    if (eth.mining) return;
    console.log("  Transactions pending. Mining...");
    miner.start(mining_threads)
    while (eth.getBlock("latest").number < txBlock + 12) {
      if (eth.getBlock("pending").transactions.length > 0) txBlock = eth.getBlock("pending").number;
        }
    console.log("  12 confirmations achieved; mining stopped.");

    miner.stop()
}
else {
    miner.stop()
     }
}

eth.filter("latest", function(err, block) { checkWork(); });
eth.filter("pending", function(err, block) { checkWork(); });

checkWork();

Edits 1, 2 and 3 were not mentioned before since I did not think they would be relevant since the python client worked. Following this, I run miner.start(1) to start some mining.

Any advise to debug this issue would be much appreciated. Thanks.

7
  • Was the error cause by function call or something else? Is geth client running? What solc version are you using? What opcodes are enabled in the genesis file? – Ismael Feb 12 at 12:18
  • Yes, This particular error occurs when I call any getter methods that are manually. I am running a geth http provider. I am not disabling any opcodes. The functions are working using web3.py. I am only having this issue using web3.js – Ev3ryDay1sL3gDay Feb 13 at 10:51
  • Please include a minimal stand alone test. The javascript snippet is missing await for the call otherwise the syntax is correct and here it works. There's something else in the environment that cause the error. – Ismael Feb 13 at 19:13
  • I have edited the post with some more information about the environment. The javascript and python code is representative of the code that I have. – Ev3ryDay1sL3gDay Feb 14 at 13:28
  • What is the solc version? What geth version are you modifying? What does your genesis file look like? Also geth has options --dev and --dev.period that can be used to configure a chain similar to your scripts. – Ismael Feb 14 at 14:42
1

Nevermind, I found the issue. The javascript code I posted is a little differrent. In the original code, I was fetching the contractAddress using

web3.getTransactionReceipt(..).then(function(receipt){  contract = .... (receipt.contractAddress)});

This code worked in the first version of my contract, but when the contract was updated, obviously the address changed, but getTransactionReceipt still points to the older transaction that migrated the contract originally. So the contractAddress was always incorrect after that point.

I am officially an idiot. Thank you @Ismael. Your comments inspired me to write a standalone test.js, which lead me to realize my mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.