The first snippet of code gets the user’s balance tokenBalance using Moralis
async function userBudsBalance(){
const chainOptions = { chain: "bsc" }
const balances = await Moralis.Web3API.account.getTokenBalances(chainOptions);
// const tokenAddress = "0x720b40cd711df11e62dfd0c88797da100d5f09e6"; // 420 Contract Address
const tokenAddress = "0x058cdf0ff70f19629d4f50fac03610302e746e58"; // Buds Contract Address
const tokenBalance = balances.find((token) => token.token_address === tokenAddress);
console.log(tokenBalance.balance); //do your log here
return tokenBalance.balance;
}
const tokenBalance = userBudsBalance()
tokenBalance.then(
function(value) {
console.log(value);
document.getElementById("budsBalanceNum").innerHTML = value/10**9;},
function(error) {console.log(error);}
);
the second snippet of code calls a smart contract for information to be displayed:
async function totalRewards(){
const web3 = await Moralis.enable();
let contractAbi = [{"inputs":[],
"name":"viewRewardsAndLiquidityInfo",
"outputs":[
{"internalType":"uint256","name":"budsAccumulationFromRewardsFee","type":"uint256"},
{"internalType":"uint256","name":"sumOfAllHOLDRBalances","type":"uint256"},
{"internalType":"uint256","name":"theCurrentRewardsCycle","type":"uint256"},
{"internalType":"uint256","name":"lengthOfRewardsCycle","type":"uint256"},
{"internalType":"uint256","name":"budsAccumulationFromLiquidityFee","type":"uint256"},
{"internalType":"uint256","name":"lastTimeDividendsWereReleased","type":"uint256"},
{"internalType":"uint256","name":"dividendsClaimableNow","type":"uint256"},
{"internalType":"uint256","name":"dividendsLeftFromReservedSupplyForHOLDRs","type":"uint256"}
],"stateMutability":"view","type":"function"}];
const options = {
contractAddress: "0x058cdF0fF70f19629D4F50faC03610302e746e58",
functionName: "viewRewardsAndLiquidityInfo",
abi: contractAbi,
};
x = await Moralis.executeFunction(options)
}
x = totalRewards()
x.then(
function(value) {
console.log(value);
const calcStepOne = tokenBalance;
console.log(calcStepOne);
y = x.budsAccumulationFromRewardsFee;
console.log(y);
z = y * calcStepOne;
console.log(z);
/* amount of rewards is calculated by:
((tokenBalance * x.budsAccumulationFromRewardsFee) / x.sumOfAllHOLDRBalances) / 10**9;
problem is the first two steps are already resulting in NaN
*/
document.getElementById("budsRewardPool").innerHTML = y / 10**9;
document.getElementById("rewardsCycle").innerHTML = x.theCurrentRewardsCycle;
document.getElementById("budsDividendsPool").innerHTML = x.dividendsLeftFromReservedSupplyForHOLDRs/10**9;},
function(error) {console.log(error);}
);
I am trying to calculate the amount of rewards a signed in user can get by doing the equation
((tokenBalance * x.budsAccumulationFromRewardsFee) / x.sumOfAllHOLDRBalances) / 10**9;
but I guess because one of them is a “promise fulfilled” object and the others are strings, I am getting a NaN error? Google led me to promise.resolve() but I am wildly confused on how to use that.
Thanks for the help!