Skip to content

Commit

Permalink
feat: include rewards apr in loan net apr/apy calculation (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanofa authored Nov 23, 2024
1 parent 63511a9 commit 515b6ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-lizards-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@folks-finance/xchain-sdk": patch
---

Consider rewards apr when calculating net apr/apy on loans
9 changes: 7 additions & 2 deletions src/chains/evm/hub/modules/folks-hub-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import type {
} from "../types/loan.js";
import type { OraclePrice, OraclePrices } from "../types/oracle.js";
import type { PoolInfo } from "../types/pool.js";
import type { ActiveEpochsInfo } from "../types/rewards.js";
import type { HubTokenData } from "../types/token.js";
import type { Dnum } from "dnum";
import type {
Expand Down Expand Up @@ -334,6 +335,7 @@ export function getUserLoansInfo(
poolsInfo: Partial<Record<FolksTokenId, PoolInfo>>,
loanTypesInfo: Partial<Record<LoanTypeId, LoanTypeInfo>>,
oraclePrices: OraclePrices,
activeEpochsInfo?: ActiveEpochsInfo,
): Record<LoanId, UserLoanInfo> {
const poolIdToFolksTokenId = new Map(
Object.values(poolsInfo).map(({ folksTokenId, poolId }) => [poolId, folksTokenId]),
Expand Down Expand Up @@ -361,6 +363,9 @@ export function getUserLoansInfo(
const folksTokenId = poolIdToFolksTokenId.get(poolId);
if (!folksTokenId) throw new Error(`Unknown pool id ${poolId}`);

const activeEpochInfo = activeEpochsInfo?.[folksTokenId];
const rewardsApr = activeEpochInfo?.rewardsApr ?? dn.from(0, 18);

const poolInfo = poolsInfo[folksTokenId];
const loanPoolInfo = loanTypeInfo.pools[folksTokenId];
const oraclePrice = oraclePrices[folksTokenId];
Expand All @@ -382,8 +387,8 @@ export function getUserLoansInfo(

totalCollateralBalanceValue = dn.add(totalCollateralBalanceValue, balanceValue);
totalEffectiveCollateralBalanceValue = dn.add(totalEffectiveCollateralBalanceValue, effectiveBalanceValue);
netRate = dn.add(netRate, dn.mul(balanceValue, interestRate));
netYield = dn.add(netYield, dn.mul(balanceValue, interestYield));
netRate = dn.add(netRate, dn.mul(balanceValue, dn.add(interestRate, rewardsApr)));
netYield = dn.add(netYield, dn.mul(balanceValue, dn.add(interestYield, rewardsApr)));

collaterals[folksTokenId] = {
folksTokenId,
Expand Down
4 changes: 3 additions & 1 deletion src/xchain/modules/folks-loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { FolksCore } from "../core/folks-core.js";
import type { LoanChange, LoanManagerUserLoan, LoanTypeInfo, UserLoanInfo } from "../../chains/evm/hub/types/loan.js";
import type { OraclePrice, OraclePrices } from "../../chains/evm/hub/types/oracle.js";
import type { PoolInfo } from "../../chains/evm/hub/types/pool.js";
import type { ActiveEpochsInfo } from "../../chains/evm/hub/types/rewards.js";
import type { TokenRateLimit } from "../../chains/evm/spoke/types/pool.js";
import type { FolksChainId } from "../../common/types/chain.js";
import type { AccountId, LoanId, LoanName, Nonce } from "../../common/types/lending.js";
Expand Down Expand Up @@ -1193,9 +1194,10 @@ export const util = {
poolsInfo: Partial<Record<FolksTokenId, PoolInfo>>,
loanTypesInfo: Partial<Record<LoanTypeId, LoanTypeInfo>>,
oraclePrices: OraclePrices,
activeEpochsInfo?: ActiveEpochsInfo,
): Record<LoanId, UserLoanInfo> {
// get info of each user loan
return FolksHubLoan.getUserLoansInfo(userLoansMap, poolsInfo, loanTypesInfo, oraclePrices);
return FolksHubLoan.getUserLoansInfo(userLoansMap, poolsInfo, loanTypesInfo, oraclePrices, activeEpochsInfo);
},

emptyLoanForSimulate(accountId: AccountId, loanTypeId: LoanTypeId): LoanManagerUserLoan {
Expand Down

0 comments on commit 515b6ef

Please sign in to comment.