21

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

20

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
    Feb 6 '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 6 '19 at 15:40
11

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.

1
  • 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. Jul 11 '19 at 7:56
1

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
    Oct 10 '18 at 13:44
  • 1
    Check your contract address
    – Badr Bellaj
    Oct 10 '18 at 20:27
  • Already fixed. Have added my answer below. Got help from GitHub.
    – RafiAlhamd
    Oct 11 '18 at 9:52
1

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.

0

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.

0

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.

0

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

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