Skip to content

Commit

Permalink
option to include xalgo staking apr in net rate and yield calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
gidonkatten committed Jan 2, 2025
1 parent 03b3261 commit 4cd7bf9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-waves-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@folks-finance/algorand-sdk": patch
---

option to include xalgo staking apr in net rate and yield calculations
11 changes: 9 additions & 2 deletions src/lend/loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ async function retrieveLoanLocalState(
* @param poolManagerAppId - pool manager application to query about
* @param oracle - oracle to query
* @param userAddr - account address for the user
* @param xAlgoStakingRateBps - optional xALGO staking rate (in bps) to include in loan net rate/yield
* @returns Promise<UserLoanInfo[]> user loans infos
*/
async function retrieveUserLoansInfo(
Expand All @@ -154,6 +155,7 @@ async function retrieveUserLoansInfo(
poolManagerAppId: number,
oracle: Oracle,
userAddr: string,
xAlgoStakingRateBps?: bigint,
): Promise<UserLoanInfo[]> {
const userLoanInfos: UserLoanInfo[] = [];

Expand All @@ -178,7 +180,10 @@ async function retrieveUserLoansInfo(
);
if (state === undefined) throw Error(`Could not find loan ${loanAppId} in escrow ${escrowAddr}`);
const localState = loanLocalState(state, loanAppId, escrowAddr);
userLoanInfos.push({ ...userLoanInfo(localState, poolManagerInfo, loanInfo, oraclePrices), currentRound });
userLoanInfos.push({
...userLoanInfo(localState, poolManagerInfo, loanInfo, oraclePrices, xAlgoStakingRateBps),
currentRound,
});
}

return userLoanInfos;
Expand All @@ -193,6 +198,7 @@ async function retrieveUserLoansInfo(
* @param poolManagerAppId - pool manager application to query about
* @param oracle - oracle to query
* @param escrowAddr - account address for the loan escrow
* @param xAlgoStakingRateBps - optional xALGO staking rate (in bps) to include in loan net rate/yield
* @returns Promise<UserLoanInfo> user loan info
*/
async function retrieveUserLoanInfo(
Expand All @@ -201,6 +207,7 @@ async function retrieveUserLoanInfo(
poolManagerAppId: number,
oracle: Oracle,
escrowAddr: string,
xAlgoStakingRateBps?: bigint,
): Promise<UserLoanInfo> {
// get all prerequisites
const loanInfoReq = retrieveLoanInfo(client, loanAppId);
Expand All @@ -212,7 +219,7 @@ async function retrieveUserLoanInfo(
const { currentRound, localState: state } = await getAccountApplicationLocalState(client, loanAppId, escrowAddr);
if (state === undefined) throw Error(`Could not find loan ${loanAppId} in escrow ${escrowAddr}`);
const localState = loanLocalState(state, loanAppId, escrowAddr);
return { ...userLoanInfo(localState, poolInfo, loanInfo, oraclePrices), currentRound };
return { ...userLoanInfo(localState, poolInfo, loanInfo, oraclePrices, xAlgoStakingRateBps), currentRound };
}

/**
Expand Down
21 changes: 20 additions & 1 deletion src/lend/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { encodeAddress, getApplicationAddress } from "algosdk";

import { compoundEverySecond, maximum, mulScale, ONE_10_DP, ONE_16_DP, ONE_4_DP, SECONDS_IN_YEAR } from "../math-lib";
import {
compoundEverySecond,
maximum,
mulScale,
ONE_10_DP,
ONE_12_DP,
ONE_16_DP,
ONE_4_DP,
SECONDS_IN_YEAR
} from "../math-lib";
import { enc, fromIntToByteHex, getParsedValueFromState, parseUint64s, unixTime } from "../utils";

import {
Expand Down Expand Up @@ -31,6 +40,7 @@ import type {
} from "./types";
import type { Indexer } from "algosdk";
import type { TealKeyValue } from "algosdk/dist/types/client/v2/algod/models/types";

Check failure on line 42 in src/lend/utils.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups
import {MainnetPools} from "./constants/mainnet-constants";

Check failure on line 43 in src/lend/utils.ts

View workflow job for this annotation

GitHub Actions / lint

`./constants/mainnet-constants` import should occur before import of `./formulae`

export async function getEscrows(
indexerClient: Indexer,
Expand Down Expand Up @@ -359,13 +369,15 @@ export function loanLocalState(state: TealKeyValue[], loanAppId: number, escrowA
* @param poolManagerInfo - pool manager info which is returned by retrievePoolManagerInfo function
* @param loanInfo - loan info which is returned by retrieveLoanInfo function
* @param oraclePrices - oracle prices which is returned by getOraclePrices function
* @param xAlgoStakingRateBps - optional xALGO staking rate (in bps) to include in loan net rate/yield
* @returns Promise<UserLoansInfo> user loans info
*/
export function userLoanInfo(
localState: LoanLocalState,
poolManagerInfo: PoolManagerInfo,
loanInfo: LoanInfo,
oraclePrices: OraclePrices,
xAlgoStakingRateBps?: bigint,
): UserLoanInfo {
const { pools: poolManagerPools } = poolManagerInfo;
const { pools: loanPools } = loanInfo;
Expand Down Expand Up @@ -403,6 +415,13 @@ export function userLoanInfo(
netRate += balanceValue * depositInterestRate;
netYield += balanceValue * depositInterestYield;

// add xALGO staking apr if requested (must be mainnet)
if (assetId === MainnetPools.xALGO.assetId && xAlgoStakingRateBps) {
// multiply by 1e12 to standardise at 16 d.p.
netRate += balanceValue * xAlgoStakingRateBps * ONE_12_DP;
netYield += balanceValue * xAlgoStakingRateBps * ONE_12_DP;
}

collaterals.push({
poolAppId,
assetId,
Expand Down
2 changes: 2 additions & 0 deletions src/math-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const HOURS_IN_YEAR = BigInt(365 * 24);
const ONE_2_DP = BigInt(1e2);
const ONE_4_DP = BigInt(1e4);
const ONE_10_DP = BigInt(1e10);
const ONE_12_DP = BigInt(1e12);
const ONE_14_DP = BigInt(1e14);
const ONE_16_DP = BigInt(1e16);

Expand Down Expand Up @@ -81,6 +82,7 @@ export {
HOURS_IN_YEAR,
ONE_16_DP,
ONE_14_DP,
ONE_12_DP,
ONE_10_DP,
ONE_4_DP,
ONE_2_DP,
Expand Down

0 comments on commit 4cd7bf9

Please sign in to comment.