Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing calculation of APY for farming validators #2538

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

askulikov
Copy link
Contributor

@askulikov askulikov commented Mar 22, 2022

This branch was created for discussing and fixing the APY for farming validators.

Acceptance criteria:

  • Issue found
  • Issue solved

@stefanopepe @esaminu

@stefanopepe
Copy link
Contributor

@askulikov please open a github ticket, so we keep this thread only for code review and implementation discussion. cc @agileurbanite for the allocation of this task

@askulikov
Copy link
Contributor Author

askulikov commented Mar 23, 2022

@stefanopepe @esaminu, could you please describe the problem with APY? All calculations were done by the formulas provided earlier. Could you please check this, before we start?

The function itself in code

export const calculateAPY = (poolSummary, tokenPrices) => {
    // Handle if there are no farms:
    const activeFarms = poolSummary?.farms;
    if (!activeFarms) return 0;

    try {

        if (activeFarms.some((farm) => !+tokenPrices[farm.token_id]?.usd)) return 0;
        const totalStakedBalance = nearTo(poolSummary.total_staked_balance);

        const summaryAPY = activeFarms.reduce((acc, farm) => {
            const tokenPriceInUSD = +tokenPrices[farm.token_id].usd;
            const nearPriceInUSD = +tokenPrices[NEAR_TOKEN_ID].usd;

            const rewardsPerSecond = farm.amount / ((farm.end_date - farm.start_date) * 1e9);
            const rewardsPerSecondInUSD = rewardsPerSecond * tokenPriceInUSD;
            const totalStakedBalanceInUSD = totalStakedBalance * nearPriceInUSD;
            const farmAPY = rewardsPerSecondInUSD * SECONDS_IN_YEAR / totalStakedBalanceInUSD * 100;
            return acc + farmAPY;
        }, 0);

        return summaryAPY.toFixed(2);
    }
    catch (e) {
        console.error('Error during calculating APY', e);
        return '-';
    }
};

The only thing, that I see now, is that the checks and rules might be incorrect. They were written by me at the beginning and I guess we have to review and fix them:

  • instead of checking the amount of farms, we have to check the amount of active farms.
  • instead of checking that some of farms haven't prices, we have to filter and calculated only for those, which have prices.

Could you please review this?

@stefanopepe
Copy link
Contributor

+1 on the proposed fix (check only active farms + only tokens with an USD price). I am not sure we have the portion of code able to parse the USD value of fungible tokens. This may require some work on the contract helper side.

@MaximusHaximus MaximusHaximus deleted the branch near:master March 24, 2022 03:45
@MaximusHaximus MaximusHaximus changed the base branch from feat-farming-validator-ui-rebased to master March 24, 2022 04:09
@MaximusHaximus
Copy link
Contributor

@askulikov could you take a look at the comment here and see if the issue I described there will cause problems with APY calculations per-farm? I think it will.

It seems like tracking some rewards by token_id and others by farm_id causes some problems, because the value returned by the get_unclaimed_reward method includes a sum of both, but rewards accrued from previous sessions could be from multiple different farms that happen to farm the same token.

@askulikov
Copy link
Contributor Author

@MaximusHaximus It seems that without changes on the contract side, there is nothing to change on the frontend side. Because we can't calculate the correct values because of the lack of the correct data. And also it will be much more clear after clarifying the rules of calculation, which is described by @stefanopepe in this thread. Correct me if I'm wrong, please. I carefully re-read your comments a few times, and it looks like there aren't any options to fix this in the current implementation. Also, I let a small comment about calculating rewards just for clarifying in the thread

@netlify
Copy link

netlify bot commented Mar 24, 2022

👷 Deploy request for near-wallet-staging pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 84a084a

@askulikov
Copy link
Contributor Author

The last commit just fixes the initial conditions for selecting farms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create the new version of UI/UX for displaying APY for farming validators
3 participants