Skip to content

Commit

Permalink
fix: fix rewards apr
Browse files Browse the repository at this point in the history
  • Loading branch information
gidonkatten committed Nov 19, 2024
1 parent 5f93493 commit f975390
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-shrimps-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@folks-finance/xchain-sdk": patch
---

fix rewards apr
2 changes: 1 addition & 1 deletion src/common/utils/formulae.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function toUnderlyingAmount(fAmount: bigint, diit: Dnum): bigint {
return underlyingAmount;
}

function calcAssetDollarValue(amount: bigint, tokenPrice: Dnum, tokenDecimals: number): Dnum {
export function calcAssetDollarValue(amount: bigint, tokenPrice: Dnum, tokenDecimals: number): Dnum {
return dn.mul(tokenPrice, [amount, tokenDecimals], {
rounding: "ROUND_DOWN",
});
Expand Down
21 changes: 19 additions & 2 deletions src/xchain/modules/folks-rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { FolksHubRewards } from "../../chains/evm/hub/modules/index.js";
import { getHubChain, getHubTokensData } from "../../chains/evm/hub/utils/chain.js";
import { convertFromGenericAddress } from "../../common/utils/address.js";
import { assertHubChainSelected, getSignerGenericAddress } from "../../common/utils/chain.js";
import { calcAssetDollarValue } from "../../common/utils/formulae.js";
import { SECONDS_IN_YEAR, unixTime } from "../../common/utils/math-lib.js";
import { FolksCore } from "../core/folks-core.js";

import type { PrepareUpdateUserPointsInLoans } from "../../chains/evm/common/types/index.js";
import type { LoanTypeInfo } from "../../chains/evm/hub/types/loan.js";
import type { OraclePrices } from "../../chains/evm/hub/types/oracle.js";
import type { PoolInfo } from "../../chains/evm/hub/types/pool.js";
import type {
ActiveEpochs,
Expand Down Expand Up @@ -179,10 +181,17 @@ export const read = {
};

export const util = {
activeEpochsInfo(poolsInfo: Partial<Record<FolksTokenId, PoolInfo>>, activeEpochs: ActiveEpochs): ActiveEpochsInfo {
activeEpochsInfo(
poolsInfo: Partial<Record<FolksTokenId, PoolInfo>>,
activeEpochs: ActiveEpochs,
oraclePrices: OraclePrices,
): ActiveEpochsInfo {
const activeEpochsInfo: ActiveEpochsInfo = {};
const currTimestamp = BigInt(unixTime());

const avaxPrice = oraclePrices.AVAX;
if (!avaxPrice) throw Error("AVAX price unavailable");

for (const [folksTokenId, activeEpoch] of Object.entries(activeEpochs)) {
// calculations assumes reward rate is constant and consistent
const remainingTime = activeEpoch.endTimestamp - BigInt(currTimestamp);
Expand All @@ -194,8 +203,16 @@ export const util = {
// apr is total rewards over the total deposit, scaling by epoch length
const poolInfo = poolsInfo[folksTokenId as FolksTokenId];
if (!poolInfo) throw new Error(`Unknown folks token id ${folksTokenId}`);

const tokenPrice = oraclePrices[folksTokenId as FolksTokenId];
if (!tokenPrice) throw Error(`${tokenPrice} price unavailable`);

const rewardsApr = dn.mul(
dn.div(activeEpoch.totalRewards, poolInfo.depositData.totalAmount, { decimals: 18 }),
dn.div(
calcAssetDollarValue(remainingRewards, avaxPrice.price, avaxPrice.decimals),
calcAssetDollarValue(poolInfo.depositData.totalAmount, tokenPrice.price, tokenPrice.decimals),
{ decimals: 18 },
),
dn.div(SECONDS_IN_YEAR, remainingTime, { decimals: 18 }),
);

Expand Down

0 comments on commit f975390

Please sign in to comment.