0

I am trying to read the token balance of a certain address as well as reading the amount of ETH that has been sent to a smart contract from that address.

Currently I have:

var web3 = new Web3(new 
Web3.providers.HttpProvider('https://ropsten.infura.io/'));

abi = JSON.parse('[{"constant":true,"inputs": 
[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"}]');
TokenContract = web3.eth.contract(abi);

contractInstance = TokenContract.at('[TOKEN CONTRACT ADDRESS]');
var balance = contractInstance.balanceOf(userAddress);

The abi is a standard ERC20 abi, but balanceOf returns an object (r {s: 1, e: 0, c: Array(1)}), shouldn't it return an integer? How should I get the token balance from this?

0

The result value is a bignumber. You can just use toString to get value.

balance.toString()
0

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.