From f76fe5ebf0d7d8ff27a394d77f13d38be50e7f51 Mon Sep 17 00:00:00 2001 From: Gidon Katten <55109236+gidonkatten@users.noreply.github.com> Date: Tue, 24 Sep 2024 09:54:10 +0100 Subject: [PATCH] feat: add f token supply and updated retained amount (#172) --- .changeset/twelve-teachers-greet.md | 5 +++ src/chains/evm/hub/modules/folks-hub-pool.ts | 37 ++++++++++++++-- src/chains/evm/hub/types/pool.ts | 1 + src/common/utils/formulae.ts | 45 ++++++++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 .changeset/twelve-teachers-greet.md diff --git a/.changeset/twelve-teachers-greet.md b/.changeset/twelve-teachers-greet.md new file mode 100644 index 0000000..ebe0c38 --- /dev/null +++ b/.changeset/twelve-teachers-greet.md @@ -0,0 +1,5 @@ +--- +"@folks-finance/xchain-sdk": patch +--- + +add f token supply and updated retained amount diff --git a/src/chains/evm/hub/modules/folks-hub-pool.ts b/src/chains/evm/hub/modules/folks-hub-pool.ts index fc54d35..68dfecd 100644 --- a/src/chains/evm/hub/modules/folks-hub-pool.ts +++ b/src/chains/evm/hub/modules/folks-hub-pool.ts @@ -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"; @@ -32,6 +37,7 @@ export async function getPoolInfo( stableBorrowData, capsData, configData, + fTokenCirculatingSupply, ] = await multicall(provider, { contracts: [ { @@ -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, @@ -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, @@ -173,5 +201,6 @@ export async function getPoolInfo( canMintFToken, flashLoanSupported, }, + fTokenCirculatingSupply, }; } diff --git a/src/chains/evm/hub/types/pool.ts b/src/chains/evm/hub/types/pool.ts index c369474..f6ed3b3 100644 --- a/src/chains/evm/hub/types/pool.ts +++ b/src/chains/evm/hub/types/pool.ts @@ -68,4 +68,5 @@ export type PoolInfo = { stableBorrowData: StableBorrowData; capsData: CapsData; configData: ConfigData; + fTokenCirculatingSupply: bigint; }; diff --git a/src/common/utils/formulae.ts b/src/common/utils/formulae.ts index 17fd67a..fb8d0e9 100644 --- a/src/common/utils/formulae.ts +++ b/src/common/utils/formulae.ts @@ -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( @@ -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;