import { BigNumberish, providers } from 'ethers'; import { isAddress } from 'ethers/lib/utils'; import { DefaultReserveInterestRateStrategy } from './typechain/DefaultReserveInterestRateStrategy'; import { DefaultReserveInterestRateStrategy__factory } from './typechain/DefaultReserveInterestRateStrategy__factory'; export interface ReserveInterestRateStrategyContext { reserveStrategy: string; provider: providers.Provider; } export interface CalculateInterestRatesParams { liquidityAdded: BigNumberish; liquidityTaken: BigNumberish; totalVariableDebt: BigNumberish; reserveFactor: BigNumberish; reserve: string; xToken: string; } export interface CalculateInterestRatesResult { currentLiquidityRate: string; currentVariableBorrowRate: string; } export interface DefaultReserveInterestRateStrategyInterface { calculateInterestRates( params: CalculateInterestRatesParams, ): Promise; } export class ReserveInterestRateStrategy implements DefaultReserveInterestRateStrategyInterface { private readonly _contract: DefaultReserveInterestRateStrategy; /** * Constructor * @param context The ReserveInterestRateStrategy context */ public constructor(context: ReserveInterestRateStrategyContext) { if (!isAddress(context.reserveStrategy)) { throw new Error('contract address is not valid'); } this._contract = DefaultReserveInterestRateStrategy__factory.connect( context.reserveStrategy, context.provider, ); } public async calculateInterestRates( params: CalculateInterestRatesParams, ): Promise { const rst = await this._contract.calculateInterestRates({ liquidityAdded: params.liquidityAdded, liquidityTaken: params.liquidityTaken, totalVariableDebt: params.totalVariableDebt, reserveFactor: params.reserveFactor, reserve: params.reserve, xToken: params.xToken, }); return { currentLiquidityRate: rst[0].toString(), currentVariableBorrowRate: rst[1].toString(), }; } }