0

Ok, I'm new into JS and web3 and MetaMask. I'm still lost about Promise and async/await after reading them for 5 times!

I want to call my smart contract balanceOf method to check if there's enough balance of token before I execute a transfer.

How do I actually wait for this balanceOf value to return to my JS script then only I proceed?

You know, getting the balanceOf takes like only 1 sec, but I know sometimes the user's finger is faster than the brain.

I tried async/await function below but my main code doesn't "wait", it just continues on. I'm using web3 1.0.

Then, I execute the transfer in a separate function. What I'm doing now is firstly I set the token balance using setTokenBalance(balance). Then check if I have enough balance by calling getTokenBalance(). If yes, then I do the transfer. Which works but it's kinda funny cos there's a split second that getTokenBalance shows '0' since it hasn't got the correct balance data.

var tokenBalance = 0;
async function retrieveTokenBalance(contract, walletAddress) {

      await contract.methods.balanceOf(walletAddress).call(function(err,res){
            if(!err){
                console.log(res);
                setTokenBalance(res);
            } else {
                console.log(err);
            }
        });

}

function setTokenBalance(balance) {
    tokenBalance = balance;
}

function getTokenBalance() {
    return tokenBalance;
}
1
  • 1
    BTW, you should tag your question under web3.js rather than web3j. May 14, 2019 at 7:31

2 Answers 2

1

Since your retrieveTokenBalance is an async function, try this...

retrieveTokenBalance(smartContract, walletAddress).then(function(result){

    //If sufficient token
        //Do your token transfer
    //Else
        //Do something else like alerting the user

});

You can learn more about async/await in Javascript here - https://tutorialzine.com/2017/07/javascript-async-await-explained which explains a lot better than other sites.

1
  • Awesome! It worked. And thanks for the tutorial tip.
    – warnerque
    May 16, 2019 at 14:03
0

Try this:

const balance = await contract.methods.balanceOf(walletAddress).call();
await setTokenBalance(balance);

From your description, it sounds like function setTokenBalance executes a transfer, which means it has to be an async function, hence the await for its completion.

3
  • The setTokenBalance is just a function to set a variable in JS that stores the token balance. It doesn't do any calls to MetaMask
    – warnerque
    May 15, 2019 at 11:56
  • @warnerque: So the question in the title makes no sense - "before I execute a transfer" - where exactly in your code are you attempting to execute a transfer??? May 15, 2019 at 13:18
  • I execute the transfer in a separate function. The idea is first I set the token balance using setTokenBalance(balance). Then check if I have enough balance by calling getTokenBalance(). If yes, then I do the transfer.
    – warnerque
    May 16, 2019 at 4:36

Your Answer

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

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