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

feat: add f token supply and updated retained amount #172

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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