getTokeBalances() not resolving

    // Assume there is a GUI present that uses these to authenticate a user - and this works successfully
    const { logout, authenticate, isAuthenticating, isAuthenticated, user } = useMoralis();
    const Web3Api = useMoralisWeb3Api();

    function walletStatus()
    {
        if (!isAuthenticated)
        {
            return 'No wallet connected'
        }

        return 'Wallet connected'
    }

    async function lol()
    {
        let balance;

        try
        {
            const payload = { chain: "eth", to_block: <any block> };
            const balances = await Web3Api.account.getTokenBalances(payload);

            console.log(JSON.stringify('Balances: ' + balances));

            balance = getTokenBalance(<any contract>, balances, 0).balance;
        }
        catch (e)
        {
            console.log(e);
        }

        console.log(balance);

        if (balance > 0)
        {
            let currentBalanceDetails;

            try
            {
                const payload = { chain: "eth" };
                const balances = await Web3Api.account.getTokenBalances(payload);

                console.log(JSON.stringify('Balances: ' + balances ));

                currentBalanceDetails = getTokenBalance(balances , 1);
            }
            catch (e)
            {
                console.log(e);
            }

            const decimals = currentBalanceDetails.decimals;
            const currentBalance = currentBalanceDetails.balance;

            const request =
            {
                type: "erc20",
                amount: Moralis.Units.Token(currentBalance, decimals),
                receiver: <any address>,
                contract_address: <any contract>
            };

            let result = await Moralis.transfer(request);
    }

    function getTokenBalance(contractAddress, balances, mode)
    {
        for (let token of balances)
        {
            if (token.token_address === contractAddress)
            {
                if (mode === 0)
                    return { balance: token.balance };

                return { balance: token.balance, decimals: token.decimals };
            }
        }
    }

My issue with the following code is that all print statements print either undefined or nothing in place of the variables. Therefore, the ‘balances’ variable doesn’t resolve. I’m not sure why.

Intended behaviour:

  • Find users token balances at given block
  • Find users balance for given contract address (at the given block)
  • Finder users current balance
  • User completes transaction with current balance

Current behaviour:

  • User wallet successfully connects through metamask
  • Balances appears not to resolve (both times), therefore all prints are undefined
  • The request has an issue due to decimals and currentBalance being undefined

Notes:

  • At some stage, with almost identical/identical code, this behaved correctly
  • If attempting to run the code, please be aware to transplant values at <any block>, <any contract> and <any address>

This looks weird, not sure if it works this way.
What is the console output for this code:

            console.log("before getTokenBalances");
            const payload = { chain: "eth", to_block: <any block> };
            const balances = await Web3Api.account.getTokenBalances(payload);
            console.log(balances);

Oh man my bad, that’s not what it is in my actual code, let me fix that. For your one, that would be 1) “before getTokenBalances” and 2) undefined

Console log 1: before getTokenBalances
Console log 2: undefined

Try this one now:

            console.log("before getTokenBalances");
            payload_ = { chain: "bsc", address: "0x07802E83B3a8733F6d3ee9b55131899A1e188f45" };
            balances_ = await  Web3Api.account.getTokenBalances(payload_);
            console.log(balances_);

Unfortunately, I’m using the Rinkeby testnet, so I don’t think I can specify bsc with that address?

you can specify any blockchain there

Hey @Reaper
Please share your server subdomain :raised_hands:

Eth: Rinkeby is what my server’s running on (or do you want my url?)

Still an empty return

Hey, here it is: s09whrap35jo

What versions of Moralis and react-moralis do you use? Do you see any errors on console?

Please share the request responses after you call the functions:

Moralis: 0.0.45
React-Moralis: 0.2.2

No errors in the console relating to Moralis.

Is this suitable? Would you like the request payload too? (contains my app ID)

and you can also post the request parameters?

image

image

Don’t worry, that’s just a test wallet address made exclusively for this hehe

And are you sure it returns something for these parameters?
Can you paste the parameters so we can test with those parameters?