[Ethereum] Unhandled rejection Error: Couldn’t decode uint256 from ABI

nodejssolcsoliditytestrpcweb3js

I am working on using a sample Contract using NodeJS, Web3, and Solc but I encountered some error that the given address in the Contract that I've created couldn't decode. Am I doing something wrong? Here is the JS code that I'm working on

const Web3  = require('web3');
const fs    = require('fs');
const solc  = require('solc');

let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));

const source = fs.readFileSync(__dirname + '/solidity/Token.sol');
const output = solc.compile(source.toString(), 1);
const bytecode = output.contracts[':Token'].bytecode;
const abi = JSON.parse(output.contracts[':Token'].interface);

const contract = new web3.eth.Contract(abi, '0x5bbf74f1e804bfe671d55cd6b9f3ada66568d5dd', {
  from: web3.eth.coinbase,
  gasPrice: 90000*2
});

contract.methods.totalSupply().call().then(function (err, result) {
  console.log('error', err);
  console.log('result', result);
});

Here is the solidity code that I am using.

pragma solidity ^0.4.4;

contract Token {
    // stores the balances of the addresses
    mapping(address => uint) balances;
    mapping(address => mapping(address => uint)) approved;

    // number of tokens in circulation
    uint supply;

    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    function Token() {
        // constructor
        // balances[tx.origin] = 10000;
    }

    function totalSupply() constant returns (uint) {
        return supply;
    }

    function balanceOf(address _owner) constant returns (uint balance) {
        return balances[_owner];
    }

    function transfer(address _to, uint _value) returns (bool success) {
        if (balances[msg.sender] >= _value
            && _value > 0
        ) {
            balances[msg.sender] -= _value;
            balances[_to] += _value;

            // trigger an event
            Transfer(msg.sender, _to, _value);
            return true;
        }

        return false;
    }

    // sets how many can a spender spend from a certain address
    function approve(address _spender, uint _value) returns (bool success) {
        if (balances[msg.sender] > _value) {
            approved[msg.sender][_spender] = _value;
            return true;
        }

        return false;
    }

    // check the value of the spender can spend
    function allowance(address _owner, address _spender) constant returns (uint remaining) {
        return approved[_owner][_spender];
    }

    // transfer the approved value to spend
    function transferFrom(address _from, address _to, uint _value) constant returns (bool success) {
        if (balances[_from] >= _value
            && approved[_from][msg.sender] >= _value
            && _value > 0
        ) {
            balances[_from] -= _value;
            approved[_from][msg.sender] -= _value;


            balances[_to] += _value;
            return true;
        }

        return false;
    }
}

PS: I'm using testrpc as my ethereum provider.

Best Answer

I was totally stuck on this issue myself until I realized I had metamask set to the wrong network (mainnet instead of rinkeby). It's worth checking that setting... I definitely felt silly once I realized after blowing quite a bit of time trying to find the issue.

Related Topic