Hey Yomoo, I am still super stuck lol. I got most of the codes to work, but really, theyāre just the codes you guys have helped me out with. I did do a quick crash course on javascript, async and await and I have a much better understanding of it now. Take a look at the codes below:
login = async () => {
const web3 = await Moralis.enable();
try {
currentUser = await Moralis.User.current();
if (!currentUser) {
currentUser = await Moralis.authenticate().then(function (user) {
console.log(user.get('ethAddress'))
})
}
console.log(currentUser);
document.getElementById("connectWalletBtn").style.display = "none";
document.getElementById("connectWalletBtnRt").style.display = "none";
document.getElementById("BudsRewardsDapp").style.display = "block";
document.getElementById("logoutBtn").style.display = "block";
totalRewards();
} catch (error) {
console.log(error);
}
}
logout = async () => {
await Moralis.User.logOut();
console.log(currentUser);
document.getElementById("connectWalletBtn").style.display = "block";
document.getElementById("connectWalletBtnRt").style.display = "block";
document.getElementById("BudsRewardsDapp").style.display = "none";
}
userBudsBalance = async () => {
const chainOptions = {
chain: "bsc"
};
const balances = await Moralis.Web3API.account.getTokenBalances(chainOptions);
const tokenAddress = "0x058cdf0ff70f19629d4f50fac03610302e746e58"; // Buds Contract Address
const tokenBalance = balances.find((token) => token.token_address === tokenAddress);
if (tokenBalance) {
return tokenBalance.balance;
} else {
return null;
}
}
// after userBudsBalance is called
// this is the section that returns the balance
userBudsBalance().then((balance) => {
updateHTML(balance);
console.log(balance);
})
// I need the balance from this function
// to calculate the amount of rewards
// an user is eligible for
updateHTML = async (value) => {
document.getElementById("budsBalanceNum").innerHTML = value / 10 ** 9;
}
totalRewards = async () => {
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)
console.log(options);
}
x = totalRewards()
x.then(function (value) {
document.getElementById("budsRewardPool").innerHTML = x.budsAccumulationFromRewardsFee / 10 ** 9;
document.getElementById("rewardsCycle").innerHTML = x.theCurrentRewardsCycle;
document.getElementById("sumOfHolders").innerHTML = x.sumOfAllHOLDRBalances / 10 ** 9;
document.getElementById("budsDividendsPool").innerHTML = x.sumOfAllHOLDRBalances / 10 ** 9;
})
// so I thought maybe I'd do another function
// to calculate claimableRewards:
claimableRewards = async () => {
const userEligibility = await userBudsBalance();
console.log(userEligibility);
// I'm not sure how to call balance from userBudsBalance.then(balance)
};
async function claimBudsRewards() {
const web3 = await Moralis.enable();
const Rewards = {
contractAddress: "0x058cdF0fF70f19629D4F50faC03610302e746e58",
functionName: "claimRewards",
abi: window.abi
};
const BudsRewards = await Moralis.executeFunction(Rewards);
console.log(Rewards);
}
document.getElementById("connectWalletBtn").onclick = login;
document.getElementById("connectWalletBtnRt").onclick = login;
document.getElementById("logoutBtn").onclick = logout;
document.getElementById("budsClaimRewards").onclick = claimBudsRewards;
I left some comments in the code, but Iāll explain here as well, just in case itās still unclear.
I am trying to get the balance from userBudsBalance to put into another equation.
The equation would be to find out claimableRewards.
The equation is:
(balance * x.budsAccumulationFromRewardsFee) / sumOfAllHOLDRSBalances
Hope this makes sense!