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)?

Tanveer Singh
  • 145
  • 12
RafiAlhamd
  • 755
  • 1
  • 6
  • 13

8 Answers8

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

Mike
  • 3
  • 2
monkrus
  • 642
  • 7
  • 17
  • 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 Feb 06 '19 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 Feb 06 '19 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.

RafiAlhamd
  • 755
  • 1
  • 6
  • 13
  • 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. – Ahmed Akhtar Jul 11 '19 at 07:56
  • 1
    i think, every new comer did this mistake. it is very confusing to which address is required at what place. – Nitin Tripathi Oct 26 '21 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.

squiff
  • 71
  • 1
  • 1
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

Yilmaz
  • 1,580
  • 10
  • 24
  • 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! – Ben Jacobson Sep 29 '21 at 14:07
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.

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
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 web3@2.0.0-alpha.

Hopefully, this will fix it for you.

Yusuf
  • 21
  • 1
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.

Alex
  • 121
  • 1
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!

Siwei
  • 322
  • 2
  • 10