Passing variable from one function to another

Hi guys,

I have two functions. One that gets a user’s dividends from the smart contract with their referral bonus and one that gets their dividends without their referral bonus. I need to calculate the difference between the two figures to get the user’s referral earnings. I believe that the issue I am having is to do with the scope of the displayDividendsOf variable. It is pulling a 0 value in the referral dividends function.

Any help would be much appreciated. Code is below:

    /* Dividends */
    const dividendsOfOptions = {
        contractAddress: contractAddress,
        functionName: "myDividends",
        abi: ABI,
        params: {
           _includeReferralBonus: true,
        },
    }; 

    const dividendsOf = Moralis.executeFunction(dividendsOfOptions).then(function(displayDividendsOf) {
        displayDividendsOf = convertWeiToEth(displayDividendsOf);
        $('#my_dividends').html("<p>" + displayDividendsOf.toLocaleString() + " ETH</p>");

        dividendsCurrency = displayDividendsOf * ethPrice;
        dividendsCurrency = dividendsCurrency.toFixed(2);
        $('#my_dividends_currency').html("<p>" + parseFloat(dividendsCurrency).toLocaleString() + " " + currency + "</p>");
    });
	
	/* Referral Dividends */
	
    const dividendsNoRefOptions = {
        contractAddress: contractAddress,
        functionName: "myDividends",
        abi: ABI,
        params: {
           _includeReferralBonus: false,
        },
    };
    
    const dividendsNoRef = Moralis.executeFunction(dividendsNoRefOptions).then(function(displayDividendsNoRef) {
		
        displayDividendsNoRef = convertWeiToEth(displayDividendsNoRef);
        var dividendsNorefCurrency = displayDividendsNoRef * ethPrice;
        dividendsNorefCurrency = dividendsNorefCurrency.toFixed(2);

        $('#noref_dividends').html("<p>" + displayDividendsNoRef.toLocaleString() + " ETH</p>");
        $('#noref_dividends_currency').html("<p>" + parseFloat(dividendsCurrency).toLocaleString() + " " + currency + "</p>");

		referralDividends = displayDividendsOf - displayDividendsNoRef;
		console.log("Divis no ref: " + displayDividendsNoRef);
		console.log("Referral: " + referralDividends);
		
        $('#referral_dividends').html("<p>" + referralDividends.toLocaleString() + " ETH</p>");
    });	```
1 Like

wouldn’t be easier to use await syntax and get the response that way for Moralis.executeFunction()?

Maybe - I followed a tutorial and came up with this. Everything is working apart from this one thing.