How to Calculate the Numbers Pulled from Blockchain by Moralis?

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:

yes, something changed from yesterday, now you have to use a different syntax:
instead of:

         Moralis.initialize("asgasdasdfasdf");
         Moralis.serverURL = "https://afasdfasdf:2053/server";

Now you can write:

        serverUrl = "https://asdfadf:2053/server"
        appId =  "asdfasgasdgasg" 
        Moralis.start({ serverUrl, appId});

Thanks. I thought I broke something lol. Did anything else change? Because the totalRewards stopped working too. And I got this cool response from console:

Uncaught (in promise) Error: Returned values aren’t valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.

You may get that error because your MetaMask is not connected to Binance chain.

haha! sorry for that silly mistake, I usually never connect to other chains. I was testing NFTs yesterday and forgot to switch back. we’re back in business! now back to what I was asking about earlier.

you suggested that I can use n = await userBudsBalance(), which was what I did in my codes:

claimableRewards = async () => {
    const userEligibility = await userBudsBalance();
    console.log(userEligibility);
};

but the log doesn’t show anything…

To get this equation (balance * x.budsAccumulationFromRewardsFee) / sumOfAllHOLDRSBalances

I need the (balance) from this section:

userBudsBalance = async () => {
    const web3 = await Moralis.enable();
    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;
    }
}

userBudsBalance().then((balance) => {
    updateHTML(balance);
    console.log(balance);
})

and x.budsAccumulationFromRewardsFee and sumOfAllHOLDRSBalances
from this section:

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;
})

Individually, they work fine. But now I am using a function to call the values from these two other functions and I just can’t get them to work

you can also set the address there in case that you current selected address from metamask doesn’t have tokens

ah okay I’ll play around with that after I get the calculation problem solved. Right now I’m kinda baffled because the claimRewards functions stopped working a few days ago too and I can’t figure it out either lol. My codes are exactly the same as they were when they were working. But anyway, right now the focus is the calculation problem. I’m sure the claimRewards function is something I accidentally changed and didn’t even know it.