instead of
y = x.budsAccumulationFromRewardsFee;
try to use:
y = value.budsAccumulationFromRewardsFee;
instead of
y = x.budsAccumulationFromRewardsFee;
try to use:
y = value.budsAccumulationFromRewardsFee;
thanks for your response!
okay so basically, one of the functions of userBudsBalance uses Moralis to find the specific token balance of a user.
another function called totalRewards calls on a smart contract on BSC to return information to be displayed.
These are the information that totalRewards returns:
What I am trying to do is calculate the amount of rewards that a user can claim, based on how much tokens they have in their wallet.
The way to get that amount is (userBalance*budsAccumulationFromRewardsFee) / sumOfAllHOLDRBalances
the problem I am having is that the userBalance returned by Moralis is a promise and I don’t know how to use Javascript to turn it into numbers to fit into the above equation.
I have the full set of codes on Github if you’d like to take a look: https://github.com/derekryansnzy/CareBuds-Dapp/blob/main/main.js
or if you’d like me to paste the codes here for you once you understand my question I can do that too.
I’m so sorry if I word things wrong, I’m so new to this that the mess you see is literally the stuff I pull out of my brain lol. Thank you so much for your patience and your willingness to help me!
It returns promise only if you don’t await
it. I sent you a place in the code where it is possible you have this error.
Why do you use:
const tokenBalance = userBudsBalance()
tokenBalance.then(
(value) => {
console.log(value);
document.getElementById("budsBalanceNum").innerHTML = value/10**9;},
function(error) {console.log(error);}
);
Instead of:
userBudsBalance.then(
(value) => {
console.log(value);
document.getElementById("budsBalanceNum").innerHTML = value/10**9;},
function(error) {console.log(error);}
);
Function totalRewards()
doesn’t return anything.
Why do you use variable x
, but I don’t see that it was declared
Most of the codes you see on there are from the Moralis docs and I just did my best to modify them to get something that works lol. Sorry, super new to this and that’s my best explanation.
and here’s the link to how the function totalReWards was set up:
another Moralis member helped me with it. I will go through and check with your codes! Thank you so much, I’ll report back shortly.
Feel free to ask any questions. Looking forward to see your code after refactoring
The Person Who Never Makes a Mistake Will Never Make Anything
b
receives a promise and you don’t await it.
If you want to declare a variable from async function you need to use await
:
const tokenBalance = await userBudsBalance();
Constrcution like this:
const tokenBalance = userBudsBalance();
tokenBalance.then((value) => {
console.log(value)})
works because then
is the same as await
It’s not a problem for me to rewrite your code. But I suggest you to spend some time and try to figure out what the problem is.
Thanks for your help! Yeah, I didn’t learn how to even write a single line of code till like last week. I was actually trying to hire developers for my project because the original developer I was working with said he coudln’t figure out Moralis. But I really wanted to use Moralis technology and couldn’t hire someone in time. So I just started watching videos, took some courses in Ivan On Tech and got to workk.
I am a bit confused right now though. When you say construction like this, because then is the same as result, I pulled the same code from my Github js file:
const tokenBalance = userBudsBalance()
tokenBalance.then(
(value) => {
console.log(value);
document.getElementById("budsBalanceNum").innerHTML = value/10**9;},
function(error) {console.log(error);}
);
Do you mean that I have to add the “await” to const tokenBalance as well?
So it would be like this:
const tokenBalance = await userBudsBalance()
tokenBalance.then(
(value) => {
console.log(value);
document.getElementById("budsBalanceNum").innerHTML = value/10**9;},
function(error) {console.log(error);}
);
Again, thank you for your patience. I went from Wordpress drag and drop to learning how to use Github ( I still don’t know what I’m doing on Github) and now I’m learning javascript as quickly as I can to implement Moralis into my dapp for my projects.
Yes, I do. Because you use the tokenBalance
variable there:
x = totalRewards()
x.then(
function(value) {
console.log(value);
b = tokenBalance;
console.log(b);
})
and that’s why you see a Promise
in console
another question. in order to use await, the entire thing would have to be an async function, right? because if I just wrote
const tokenBalance = await userBudsBalance();
then console shows a reference error and says that await needs to be inside of an async function.
Also, wouldn’t it be repeating the same codes?
Section one of the codes uses Moralis to get the specific token of a user:
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;
}
I think I got this section from a post I saw in this forum:
and then I added this section:
const tokenBalance = await userBudsBalance()
tokenBalance.then((value) => {
console.log(value);
document.getElementById("budsBalanceNum").innerHTML = value / 10 ** 9;
}, function (error) {
console.log(error);
});
Because @cryptokid had helped me in a different issue so I thought maybe the same code would work for two different sections. Am I making any sense? Lol I feel like I got lost in my brain
You can use await
only inside async function
. Try to make the code refactoring . For example:
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 the reason I added a second section was just to make sure the user’s tokenBalance can be displayed on the frontend, somewhere in the html
You can display it without any magic . All you need is async
and await
so then in this case, I can just use (value) to complete the original calculation I was trying to do?
It would be…
((value) * x.budsAccumulationFromRewards) / sumOfAllHOLDRSBalances
would this be correct?