// 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>