Skip to content

Commit

Permalink
feat: added format eModes to formatReserves
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaquinBattilana committed Oct 11, 2024
1 parent d120e08 commit a8cb792
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
32 changes: 31 additions & 1 deletion packages/math-utils/src/formatters/emode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface EModeCategoryData {
borrowableBitmap: string;
}

interface EModeData {
export interface EModeData {
id: number;
eMode: EModeCategoryData;
}
Expand Down Expand Up @@ -46,3 +46,33 @@ export function formatEModes(eModes: EModeData[]) {
eMode: formatEModeCategory(eMode.eMode),
}));
}

export interface ReserveEmode {
id: number;
collateralEnabled: boolean;
borrowingEnabled: boolean;
eMode: FormattedEModeCategory;
}

export function getAndFormatReserveEModes(
reserveId: string,
eModes: EModeData[],
) {
const reserveIdNumber = Number(reserveId);
return eModes.reduce<ReserveEmode[]>((acc, eMode) => {
const borrowingEnabled =
eMode.eMode.borrowableBitmap[reserveIdNumber] === '1';
const collateralEnabled =
eMode.eMode.collateralBitmap[reserveIdNumber] === '1';
if (borrowingEnabled || collateralEnabled) {
acc.push({
id: eMode.id,
collateralEnabled,
borrowingEnabled,
eMode: formatEModeCategory(eMode.eMode),
});
}

return acc;
}, []);
}
8 changes: 7 additions & 1 deletion packages/math-utils/src/formatters/reserve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { RAY_DECIMALS, SECONDS_PER_YEAR, USD_DECIMALS } from '../../constants';
import { LTV_PRECISION } from '../../index';
import { calculateCompoundedRate } from '../compounded-interest/calculate-compounded-interest';
import { EModeData, getAndFormatReserveEModes } from '../emode';
import {
calculateReserveIncentives,
CalculateReserveIncentivesResponse,
Expand Down Expand Up @@ -376,13 +377,15 @@ export function formatReserveUSD({

export interface FormatReservesUSDRequest<T extends ReserveDataWithPrice> {
reserves: T[];
eModes?: EModeData[];
currentTimestamp: number;
marketReferencePriceInUsd: string;
marketReferenceCurrencyDecimals: number;
}

export function formatReserves<T extends ReserveDataWithPrice>({
reserves,
eModes,
currentTimestamp,
marketReferencePriceInUsd,
marketReferenceCurrencyDecimals,
Expand All @@ -394,7 +397,10 @@ export function formatReserves<T extends ReserveDataWithPrice>({
marketReferencePriceInUsd,
marketReferenceCurrencyDecimals,
});
return { ...reserve, ...formattedReserve };
const formattedEModes = eModes
? getAndFormatReserveEModes(reserve.id, eModes)
: [];
return { ...reserve, ...formattedReserve, eModes: formattedEModes };
});
}

Expand Down

0 comments on commit a8cb792

Please sign in to comment.