9

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

7

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.

  • 2
    The question is about the Remix IDE, which executes in the browser. – Ismael Feb 5 at 4:42
  • 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 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 at 15:40
6

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
    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 at 7:56
0

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.

  • 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

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.