34

Deployed a contract using Remix IDE to Rinkeby testnet.

Etherscan contract address and code is here.

contract SimpleCounter {
    int counter;

    constructor() public {
        counter = 0;
    }

    function getCounter() public view returns (int){
        return counter;
    }

    function increment() public {
        counter += 1;
    }

    function decrement() public {
        counter -= 1;
    }
}

JavaScript client code:


    var contract;

    $(document).ready(function(){
        initContract();
        getCounterValue();
    })

    function initContract(){
        web3 = new Web3(web3.currentProvider);

        var address = "0xc6482382047fb50e8e7b4658425c9756b28f995c";
        var abi = [
                    ...
                    ];

        console.log('Create contract...');
        contract = new web3.eth.Contract(abi, address);
        console.log(contract)

    } //initContract()


    function getCounterValue(){
        console.log('getCounter()...');
        contract.methods.getCounter().call().then((result) => {
            console.log(result);
        }).catch(function(err){
            console.log('err...\n'+err);
        });
    }

Error:

Create contract...

(index):86 o {_requestManager: e, givenProvider: MetamaskInpageProvider, providers: {…}, _provider: MetamaskInpageProvider, …}

(index):94 getCounter()...

(index):99 err...

Error: Returned values aren't valid, did it run Out of Gas?

NOTE:

This works fine in Remix IDE (chrome). I'm able to increment/decrement/getCounter... with compiler version:0.4.25+commit.59dbf8f1.Emscripten.clang .

Why does it fail, when accessing it using 'dist/web3.min.js' of Web3.js (Branch 1.0)?

8 Answers 8

28

I fixed the similar error by deleting .json files from build/contracts folder and then running truffle migrate in the terminal.

However, the error occurred in Visual Studio Code, not Remix IDE. It appeared after running truffle migrate --reset.

I'd like to add to this comment, after you delete .json files from build/contracts folder the problem persisted until I deleted files from the trash🗑 😅 So make sure trash is empty

2
  • 1
    @monkrus The issue I had reported was due to using 'account address', instead of 'contract address'!. Deleting .json wouldn't fix that. You might want to add 'exact problem' along with error - to differentiate 'your scenario' and related fix, for clarity.
    – RafiAlhamd
    Commented Feb 6, 2019 at 15:33
  • @Ismael Thanks for pointing it out. To add more clarity... the issue worked in Remix IDE, but not in custom JavaScript code.
    – RafiAlhamd
    Commented Feb 6, 2019 at 15:40
18

I got help from Veniamin.

I had error "Error: Couldn't decode from ABI: 0x." when call get request with wrong contract address

I made a mistake... I was using 'account address', instead of 'contract address'!

The code works, once correct 'contract address' was used.

2
  • 1
    It is worthy of noting that the address of the contract is different than the address of the account that creates the contract and it is generated when the contract is created. Commented Jul 11, 2019 at 7:56
  • 1
    i think, every new comer did this mistake. it is very confusing to which address is required at what place. Commented Oct 26, 2021 at 14:39
7

Had the same issue and it turned out i was on Metamask Mainnet instead of Rinkeby where my contract was deployed.

2
  • I was not even logged into metamask.
    – Vasudev
    Commented Nov 7, 2021 at 15:20
  • Switching the network fixed the issue, go for it.
    – anaszaman
    Commented Nov 18, 2021 at 16:42
2

Your code seems to be correct. I think your problem is due to a Web3js bug. Have a look at the issue reported in Github :

https://github.com/ethereum/web3.js/issues/1916

I recommand you to use web3.js 1.0.0-beta.35 instead.

3
  • Switched to version web3.js 1.0.0-beta.35. But, this time the error is different: Error: Couldn't decode from ABI: 0x.
    – RafiAlhamd
    Commented Oct 10, 2018 at 13:44
  • 1
    Check your contract address
    – Badr Bellaj
    Commented Oct 10, 2018 at 20:27
  • Already fixed. Have added my answer below. Got help from GitHub.
    – RafiAlhamd
    Commented Oct 11, 2018 at 9:52
2

I had this error too. It got fixed after I changed my web3 version to 2.0.0-alpha which was 1.0.0-beta.55 before.

  1. So just uninstall your web3 using npm uninstall web3.
  2. Then do npm install [email protected].

Hopefully, this will fix it for you.

2

In my case (I'm using Azure blockchain service), I modified my contract file but forgot the build and deploy it again to my Azure blockchain service. Instead, I jump to the truffle exec command directly. So, after I re-do those two missing steps, everything works fine.

2

This happened to me on my react app.

I deployed to contract to Ropsten network, but metamask was using the Rinkeby account. So make sure whichever network you deployed, metamask should be using account from that network.

Another time I had this in next.js. this route is a dynamic route means will fetch the contract by ${contractAddress}:

      Router.push(`/campaigns/${recipient}/requests`);

instead of {recipient} address, I had to put the address of the contract to fetch

1
  • 1
    +1 on this. I still had the ropsten Alchemy endpoint configured in my .env file for my react app but forgotten I had redeployed to Rinkeby testnet. This plagued me for nearly 2 days. Thank you for suggesting this! Commented Sep 29, 2021 at 14:07
1

In my case, I was invoking a "view" method of a contract deployed in local hardhat.

and my nodejs code looks like: contract.methods().getterName().call() and got this error.

I spent hours on this and no luck. At last I switched to Rinkeby Test Network, deploy the contract, and call the "view" method with almost the same code, everything goes well.

so, the solution is:

If you are using HardHat and meet this strange issue, switch to Rinkeby!

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