this looks pretty magical to someone whoās used to looking at Wordpress drag and drop lol. Coding is literally kicking my butt. I like the challenge and I love learning but wow, doing it all from scratch and knowing absolutely nothing about javascript is intense
I replaced the two sections of codes with yours:
async function userBudsBalance(){
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);
return tokenBalance.balance;
}
async function updateHTML(value) {
document.getElementById("budsBalanceNum").innerHTML = value/10**9;
}
userBudsBalance().then((balance) => {
updateHTML(balance);
})
and console returned error, uncaught (in promise), tokenBalance is not defined
full code please
The error message speaks about the error itself
maybe I should just change my title in the forum to asking if any developers are for hire and can get this job done within a couple days lol. By the time I learn Javascript itāll be 2025
Full code:
Moralis.initialize("KVsRENKf8Y0FrI0l57LMYMjRJsQTd8UbYP36qFV0"); // Application id from moralis.io
Moralis.serverURL = "https://gdbymlyoujf7.moralishost.com:2053/server"; // Server url from moralis.io
async function login() {
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";
}
/*
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);
const tokenBalance = balances.find((token) => token.token_address === tokenAddress);
console.log(tokenBalance.balance); // do your log here
return tokenBalance.balance;
}
const tokenBalance = await userBudsBalance()
tokenBalance.then((value) => {
console.log(value);
document.getElementById("budsBalanceNum").innerHTML = value / 10 ** 9;
}, function (error) {
console.log(error);
});
*/
async function userBudsBalance(){
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);
return tokenBalance.balance;
}
async function updateHTML(value) {
document.getElementById("budsBalanceNum").innerHTML = value/10**9;
}
userBudsBalance().then((balance) => {
updateHTML(balance);
})
async function claimBudsRewards() {
const web3 = await Moralis.enable();
const Rewards = {
contractAddress: "0x058cdF0fF70f19629D4F50faC03610302e746e58",
functionName: "claimRewards",
abi: window.abi
};
const BudsRewards = await Moralis.executeFunction(Rewards);
}
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);
b = tokenBalance;
console.log(b);
// need a way to parse b
y = x.budsAccumulationFromRewardsFee / 10 ** 9;
console.log(y);
parseInt(y);
console.log(parseInt(y));
z = x.sumOfAllHOLDRBalances / 10 ** 9;
console.log(z);
a = y / z;
console.log(a);
// this worked
/* 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;
document.getElementById("rewardsCycle").innerHTML = x.theCurrentRewardsCycle;
document.getElementById("budsDividendsPool").innerHTML = x.dividendsLeftFromReservedSupplyForHOLDRs / 10 ** 9;
document.getElementById("sumOfHolders").innerHTML = x.sumOfAllHOLDRBalances / 10**9;
// document.getElementById("rewardsAmount").innerHTML = a;
}, function (error) {
console.log(error);
});
document.getElementById("connectWalletBtn").onclick = login;
document.getElementById("connectWalletBtnRt").onclick = login;
document.getElementById("logoutBtn").onclick = logout;
document.getElementById("budsClaimRewards").onclick = claimBudsRewards;
youāre right, I am a big doofus. I forgot to comment out
b = tokenBalance
console.log(b)
leave it up to me to make the simplest mistakes lol Iām sorry
So does it work correctly now?
Also I suggest you to āupgradeā return for this function:
async function userBudsBalance() {
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;
}
}
it called the balance of the user correctly, but Iām trying to figure out how to use that balance to calculate the amount of rewards that the user gets.
async function userBudsBalance(){
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);
return tokenBalance.balance;
}
async function updateHTML(value) {
document.getElementById("budsBalanceNum").innerHTML = value/10**9;
}
userBudsBalance().then((balance) => {
updateHTML(balance);
})
I tried going to the browser to open up the console where the html/js file are on the VS code live server and I typed in these values:
- userBudsBalance - which returned the async function
- value - as soon I click enter, it turns into valueOf
- balance - this comes back as undefined
so now Iām not sure which of these values I can use to add to the equation to get the userās reward amount
thanks lol Iāve been using the html to fill in āBuy Some Budsā if the function calls back they have zero balance. Your method works way better
Sorry for the late response! I fell asleep last night lol and spent most of today figuring out stuff for my BSC project. I am back to the javascript codes now. I will report back shortly with results. Really appreciate you checking in on me
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!
Hey Yoomoo, I left an update here about 4 days ago, I was wondering if you had time to take a look at the more cleaned up codes for me? Still stuck on that one calculation bit lol
given this equation, what is the problem that you have? what it doesnāt work? do you have the right data in the variables? the result is not the expected one? the formatting of the result is not the e expected one?
In the codes itself I left some comments because I think it doesnāt quite make sense if I asked the question without showing the codes.
Thereās an async function userBudsBalance that uses Moralis to find the balance of the token. After that, userBudsBalance().then((balance) => {} shows the value of the balance.
Further down in the code, after using another function totalRewards to get the other information I need for the equation, I created another async function called claimableRewards. I need to figure out how to call the (balance) from userBudsBalance in order to use it for the equation (balance * x.budsAccumulationFromRewardsFee) / sumOfAllHOLDRSBalances
Does this make sense?
I just looked at random lines, here donāt you want to log BudsRewards instead of Rewards.
Donāt you also want to return something from this function?
You should be able to call userBudsBalance
like this: n = await userBudsBalance()
Actually right now I just ran into a different problemā¦all of the codes stopped working and I havenāt change anything for the last few days because I was waiting on a response from this thread.
Right now console shows this error:
moralis.js:6325 Uncaught (in promise) Error: Web3Api not initialized, run Moralis.start() first
but I have web3 enabledā¦am I missing something?
regarding your question about logging BudsRewards, I just did log Rewards because I was checking to make sure the numbers came back. Youāre the person that helped me write the rewards codes here: