7

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.

  • i'm also facing same issue. Did you get any solution? – comeback4you Sep 22 '17 at 12:22
  • @comeback4you no, what i did is i downgraded my webjs from 1.0.0 beta 20-ish version to 0.20 then it worked fine. – Rico Maglayon Sep 23 '17 at 11:41
  • @RicoMaglayon Have you found solution of this problem, without downgrading web3js lib? – Dmytro Zarezenko Oct 3 '17 at 14:26
  • hey @DmytroZarezenko i never looked back once i downgraded web3js library. sorry about that. – Rico Maglayon Oct 3 '17 at 16:40
  • 1
    @RicoMaglayon Since this seems like a fairly common issue, do you mind posting an answer to your own question? See stackoverflow.com/help/self-answer – flygoing Jan 29 '18 at 17:08
3

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.

0

I'm not sure this is the solution of your problem because of your comment about the web3 version.

You are compiling a new version of your smart contract with solc but you never deploying it and updating the contract address, that can be the issue if you have an outdated ABI

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.