diff --git a/packages/money-market/src/hooks/app-data-provider/useAppDataProvider.tsx b/packages/money-market/src/hooks/app-data-provider/useAppDataProvider.tsx index fc21088604..6e80efef55 100644 --- a/packages/money-market/src/hooks/app-data-provider/useAppDataProvider.tsx +++ b/packages/money-market/src/hooks/app-data-provider/useAppDataProvider.tsx @@ -1,6 +1,5 @@ import { formatGhoReserveData, - formatGhoUserData, FormattedGhoReserveData, FormattedGhoUserData, formatUserSummaryWithDiscount, @@ -33,6 +32,7 @@ import { ExternalApyData } from "@/types" import { getUserApyValues } from "@/utils" import { formatGhoReserve, + formatGhoUserDataPatched, GHO_SUPPORTED_MARKETS, GHO_SYMBOL, isGho, @@ -114,7 +114,7 @@ export const AppDataProvider: React.FC<{ ghoReserveData, }, ) - const formattedGhoUserData: FormattedGhoUserData = formatGhoUserData({ + const formattedGhoUserData: FormattedGhoUserData = formatGhoUserDataPatched({ ghoReserveData, ghoUserData, currentTimestamp, diff --git a/packages/money-market/src/utils/ghoUtilities.ts b/packages/money-market/src/utils/ghoUtilities.ts index 064b487a59..fee0329b2c 100644 --- a/packages/money-market/src/utils/ghoUtilities.ts +++ b/packages/money-market/src/utils/ghoUtilities.ts @@ -1,4 +1,12 @@ -import { FormattedGhoReserveData } from "@aave/math-utils" +import { + FormattedGhoReserveData, + FormattedGhoUserData, + getCompoundedBalance, + GhoReserveData, + GhoUserData, + normalize, + rayMul, +} from "@aave/math-utils" import Big from "big.js" import { ComputedReserveData } from "@/hooks/commonTypes" @@ -130,3 +138,63 @@ export const getGhoBorrowApyRange = ( return isSameDisplayValue ? minVal * 100 : [minVal * 100, maxVal * 100] } + +export function formatGhoUserDataPatched({ + ghoReserveData, + ghoUserData, + currentTimestamp, +}: { + ghoReserveData: GhoReserveData + ghoUserData: GhoUserData + currentTimestamp: number +}): FormattedGhoUserData { + const formattedUserDiscountTokenBalance = Number( + normalize(ghoUserData.userDiscountTokenBalance, 18), + ) + + const formattedRequiredTokenBalanceForDiscount = Number( + normalize(ghoReserveData.ghoMinDiscountTokenBalanceForDiscount, 18), + ) + + let userGhoAvailableToBorrowAtDiscount = + Number(normalize(ghoReserveData.ghoDiscountedPerToken, 18)) * + formattedUserDiscountTokenBalance + + if ( + formattedUserDiscountTokenBalance < formattedRequiredTokenBalanceForDiscount + ) { + userGhoAvailableToBorrowAtDiscount = 0 + } + + const userBalancePreDiscount = getCompoundedBalance({ + principalBalance: ghoUserData.userGhoScaledBorrowBalance, + reserveIndex: ghoReserveData.ghoCurrentBorrowIndex, + reserveRate: ghoReserveData.ghoBaseVariableBorrowRate, + lastUpdateTimestamp: Number(ghoReserveData.ghoReserveLastUpdateTimestamp), + currentTimestamp, + }) + const accruedInterest = userBalancePreDiscount.minus( + rayMul( + ghoUserData.userGhoScaledBorrowBalance, + ghoUserData.userPreviousGhoBorrowIndex, + ), + ) + + /** + * Patched discount logic to use the userGhoDiscountPercent instead of the 1 - userGhoDiscountPercent + */ + const discount = accruedInterest.multipliedBy( + Number(normalize(ghoUserData.userGhoDiscountPercent, 4)), + ) + + const userBorrowBalance = userBalancePreDiscount.minus(discount) + return { + userGhoDiscountPercent: Number( + normalize(ghoUserData.userGhoDiscountPercent, 4), + ), + userDiscountTokenBalance: formattedUserDiscountTokenBalance, + userGhoBorrowBalance: Number(normalize(userBorrowBalance, 18)), + userDiscountedGhoInterest: Number(normalize(discount, 18)), + userGhoAvailableToBorrowAtDiscount, + } +}