0

I am following this tutorial. Everything worked fine. Now I want to getBalanceOf tokens on my nodejs app, but I keep getting this error:

tokenContract.methods.balanceOf is not a function.

My code is as follows:

var provider = new web3.providers.HttpProvider(node.url);
            var w3 = new web3(provider);

            var tokenContract = new w3.eth.Contract(abi, query.token_address);

            console.log(tokenContract);

            w3.eth.call({
                to: query.pub_address,
                data: tokenContract.methods.balanceOf(query.pub_address).encodeABI()
            }).then(function(balance) {
                console.log(balance);
            }).catch(function(error) {
                console.log(error)
                reject(error)
            })

If I check my tokenContract methods in console, by printing (tokenContract.methods) I see there is no balanceOf method exposed.

Any ideas?

7
  • Have you tried calling tokenContract.balanceOf() separately? Commented Dec 18, 2017 at 22:04
  • @RomanFrolov yes, I tried I get: tokenContract.balanceOf is not a function. If I print on console: console.log(tokenContract.methods);. I do not see balanceOf method, if that is the problem how do I add it?
    – Eduardo
    Commented Dec 18, 2017 at 22:07
  • do you use correct contract abi? Commented Dec 18, 2017 at 22:08
  • @RomanFrolov I just found the issue, in the tutorial, they show to contracts: Coin and CrowdSale, I was using the CrowdSale which has no method getBalance since it seems is a different type of contract. Do you know the difference?
    – Eduardo
    Commented Dec 18, 2017 at 22:34
  • you can see the difference here Commented Dec 18, 2017 at 22:37

1 Answer 1

1

As it was mentioned in the comment section below the question, the problem was that the CrowdSale contract was used instead of Token contract. The reason of the error was that the contract CrowdSale doesn't have balanceOf() method.

Read more about Crowd Sale contract.

Read more about Token contract.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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