Skip to content

Commit

Permalink
feat: add f token supply and updated retained amount (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
gidonkatten authored Sep 24, 2024
1 parent ad50e31 commit f76fe5e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-teachers-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@folks-finance/xchain-sdk": patch
---

add f token supply and updated retained amount
37 changes: 33 additions & 4 deletions src/chains/evm/hub/modules/folks-hub-pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { multicall } from "viem/actions";

import { calcBorrowInterestIndex, calcDepositInterestIndex } from "../../../../common/utils/formulae.js";
import {
calcBorrowInterestIndex,
calcDepositInterestIndex,
calcOverallBorrowInterestRate,
calcRetention,
} from "../../../../common/utils/formulae.js";
import { compoundEveryHour, compoundEverySecond } from "../../../../common/utils/math-lib.js";
import { getHubTokenData } from "../utils/chain.js";
import { getHubPoolContract } from "../utils/contract.js";
Expand Down Expand Up @@ -32,6 +37,7 @@ export async function getPoolInfo(
stableBorrowData,
capsData,
configData,
fTokenCirculatingSupply,
] = await multicall(provider, {
contracts: [
{
Expand Down Expand Up @@ -74,12 +80,23 @@ export async function getPoolInfo(
abi: hubPool.abi,
functionName: "getConfigData",
},
{
address: hubPool.address,
abi: hubPool.abi,
functionName: "totalSupply",
},
],
allowFailure: false,
});

const { flashLoanFee, retentionRate, fTokenFeeRecipient, tokenFeeClaimer, totalRetainedAmount, tokenFeeRecipient } =
feeData;
const {
flashLoanFee,
retentionRate,
fTokenFeeRecipient,
tokenFeeClaimer,
totalRetainedAmount: actualRetained,
tokenFeeRecipient,
} = feeData;
const {
optimalUtilisationRatio,
totalAmount: depositTotalAmount,
Expand Down Expand Up @@ -118,7 +135,18 @@ export async function getPoolInfo(
feeData: {
flashLoanFee: [BigInt(flashLoanFee), 6],
retentionRate: [BigInt(retentionRate), 6],
totalRetainedAmount,
totalRetainedAmount: calcRetention(
actualRetained,
variableBorrowTotalAmount + stableBorrowTotalAmount,
calcOverallBorrowInterestRate(
variableBorrowTotalAmount,
stableBorrowTotalAmount,
[variableBorrowInterestRate, 18],
[stableBorrowAverageInterestRate, 18],
),
[BigInt(retentionRate), 6],
lastUpdateTimestamp,
),
fTokenFeeRecipient: fTokenFeeRecipient as EvmAddress,
tokenFeeClaimer: tokenFeeClaimer as EvmAddress,
tokenFeeRecipient: tokenFeeRecipient as GenericAddress,
Expand Down Expand Up @@ -173,5 +201,6 @@ export async function getPoolInfo(
canMintFToken,
flashLoanSupported,
},
fTokenCirculatingSupply,
};
}
1 change: 1 addition & 0 deletions src/chains/evm/hub/types/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ export type PoolInfo = {
stableBorrowData: StableBorrowData;
capsData: CapsData;
configData: ConfigData;
fTokenCirculatingSupply: bigint;
};
45 changes: 45 additions & 0 deletions src/common/utils/formulae.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ export function calcNextPeriodReset(periodNumber: bigint, offset: bigint, length
return (periodNumber + BigInt(1)) * length - offset;
}

export function calcOverallBorrowInterestRate(
totalVarDebt: bigint,
totalStableDebt: bigint,
variableBorInterestRate: Dnum,
avgStableBorInterestRate: Dnum,
): Dnum {
const totalDebt = totalVarDebt + totalStableDebt;
if (totalDebt === 0n) return dn.from(0, 18);

return dn.div(
dn.add(
dn.mul(totalVarDebt, variableBorInterestRate, { rounding: "ROUND_DOWN", decimals: 0 }),
dn.mul(totalStableDebt, avgStableBorInterestRate, { rounding: "ROUND_DOWN", decimals: 0 }),
),
totalDebt,
{ rounding: "ROUND_DOWN", decimals: 18 },
);
}

export function calcDepositInterestIndex(dirt1: Dnum, diit1: Dnum, latestUpdate: bigint): Dnum {
const dt = BigInt(unixTime()) - latestUpdate;
return dn.mul(
Expand All @@ -35,6 +54,32 @@ export function calcBorrowInterestIndex(birt1: Dnum, biit1: Dnum, latestUpdate:
);
}

export function calcRetention(
actualRetained: bigint,
totalDebt: bigint,
overallBorrowInterestRate: Dnum,
retentionRate: Dnum,
latestUpdate: bigint,
): bigint {
const dt = BigInt(unixTime()) - latestUpdate;

const [retainedDelta] = dn.div(
dn.mul(
dn.mul(dn.mul(totalDebt, overallBorrowInterestRate, { rounding: "ROUND_DOWN", decimals: 0 }), retentionRate, {
rounding: "ROUND_DOWN",
decimals: 0,
}),
dt,
{ rounding: "ROUND_DOWN" },
),
SECONDS_IN_YEAR,
{
rounding: "ROUND_DOWN",
},
);
return actualRetained + retainedDelta;
}

export function calcRewardIndex(used: bigint, ma: bigint, rit1: Dnum, rs: Dnum, latestUpdate: bigint): Dnum {
if (used <= ma) return rit1;
const dt = BigInt(unixTime()) - latestUpdate;
Expand Down

0 comments on commit f76fe5e

Please sign in to comment.