@kamino-finance/klend-sdk
Version:
Typescript SDK for interacting with the Kamino Lending (klend) protocol
287 lines • 14.7 kB
TypeScript
import { PublicKey } from '@solana/web3.js';
import Decimal from 'decimal.js';
import { KaminoReserve } from './reserve';
import { Obligation } from '../idl_codegen/accounts';
import { ElevationGroupDescription, KaminoMarket } from './market';
import { ObligationCollateral, ObligationLiquidity } from '../idl_codegen/types';
import { ObligationType } from '../utils';
import { ActionType } from './action';
import { KaminoObligationOrder } from './obligationOrder';
export type Position = {
reserveAddress: PublicKey;
mintAddress: PublicKey;
mintFactor: Decimal;
/**
* Amount of tokens in lamports, including decimal places for interest accrued (no borrow factor weighting)
*/
amount: Decimal;
/**
* Market value of the position in USD (no borrow factor weighting)
*/
marketValueRefreshed: Decimal;
};
export type ObligationStats = {
userTotalDeposit: Decimal;
userTotalCollateralDeposit: Decimal;
userTotalLiquidatableDeposit: Decimal;
userTotalBorrow: Decimal;
userTotalBorrowBorrowFactorAdjusted: Decimal;
borrowLimit: Decimal;
borrowLiquidationLimit: Decimal;
borrowUtilization: Decimal;
netAccountValue: Decimal;
/**
* The obligation's current LTV, *suitable for UI display*.
*
* Technically, this is a ratio:
* - of a sum of all borrows' values multiplied by reserves' borrowFactor (i.e. `userTotalBorrowBorrowFactorAdjusted`)
* - to a sum of values of all deposits having reserve's loanToValue > 0 (i.e. `userTotalCollateralDeposit`)
*
* Please note that this is different from the smart contract's definition of LTV (which divides by a sum of values
* of strictly all deposits, i.e. `userTotalDeposit`). Some parts of the SDK (e.g. obligation orders) need to use the
* smart contract's LTV definition.
*/
loanToValue: Decimal;
/**
* The LTV at which the obligation becomes subject to liquidation, *suitable for UI display*.
*
* Technically, this is a ratio:
* - of a sum of values of all deposits multiplied by reserves' liquidationLtv (i.e. `borrowLiquidationLimit`)
* - to a sum of values of all deposits having reserve's liquidationLtv > 0 (i.e. `userTotalLiquidatableDeposit`)
*
* Please note that this is different from the smart contract's definition of liquidation LTV (which divides by a sum
* of values of strictly all deposits, i.e. `userTotalDeposit`). Some parts of the SDK (e.g. obligation orders) need
* to use the smart contract's LTV definition.
*/
liquidationLtv: Decimal;
leverage: Decimal;
potentialElevationGroupUpdate: number;
};
interface BorrowStats {
borrows: Map<PublicKey, Position>;
userTotalBorrow: Decimal;
userTotalBorrowBorrowFactorAdjusted: Decimal;
positions: number;
}
interface DepositStats {
deposits: Map<PublicKey, Position>;
userTotalDeposit: Decimal;
userTotalCollateralDeposit: Decimal;
userTotalLiquidatableDeposit: Decimal;
borrowLimit: Decimal;
liquidationLtv: Decimal;
borrowLiquidationLimit: Decimal;
}
export declare class KaminoObligation {
obligationAddress: PublicKey;
state: Obligation;
/**
* Deposits stored in a map of reserve address to position
*/
deposits: Map<PublicKey, Position>;
/**
* Borrows stored in a map of reserve address to position
*/
borrows: Map<PublicKey, Position>;
refreshedStats: ObligationStats;
obligationTag: number;
/**
* Initialise a new Obligation from the deserialized state
* @param market
* @param obligationAddress
* @param obligation
* @param collateralExchangeRates - rates from the market by reserve address, will be calculated if not provided
* @param cumulativeBorrowRates - rates from the market by reserve address, will be calculated if not provided
*/
constructor(market: KaminoMarket, obligationAddress: PublicKey, obligation: Obligation, collateralExchangeRates: Map<PublicKey, Decimal>, cumulativeBorrowRates: Map<PublicKey, Decimal>);
getObligationId(market: KaminoMarket, mintAddress1?: PublicKey, mintAddress2?: PublicKey): number;
static load(kaminoMarket: KaminoMarket, obligationAddress: PublicKey): Promise<KaminoObligation | null>;
static loadAll(kaminoMarket: KaminoMarket, obligationAddresses: PublicKey[], slot?: number): Promise<(KaminoObligation | null)[]>;
/**
* @returns the obligation borrows as a list
*/
getBorrows(): Array<Position>;
/**
* @returns the obligation borrows as a list
*/
getDeposits(): Array<Position>;
/**
* Returns obligation orders (including the null ones, i.e. non-active positions in the orders' array).
*/
getOrders(): Array<KaminoObligationOrder | null>;
/**
* Returns active obligation orders (i.e. ones that *may* have their condition met).
*/
getActiveOrders(): Array<KaminoObligationOrder>;
/**
* @returns the total deposited value of the obligation (sum of all deposits)
*/
getDepositedValue(): Decimal;
/**
* @returns the total borrowed value of the obligation (sum of all borrows -- no borrow factor)
*/
getBorrowedMarketValue(): Decimal;
/**
* @returns the total borrowed value of the obligation (sum of all borrows -- with borrow factor weighting)
*/
getBorrowedMarketValueBFAdjusted(): Decimal;
/**
* @returns total borrow power of the obligation, relative to max LTV of each asset's reserve
*/
getMaxAllowedBorrowValue(): Decimal;
/**
* @returns the borrow value at which the obligation gets liquidatable
* (relative to the liquidation threshold of each asset's reserve)
*/
getUnhealthyBorrowValue(): Decimal;
/**
*
* @returns Market value of the deposit in the specified obligation collateral/deposit asset (USD)
*/
getDepositMarketValue(deposit: ObligationCollateral): Decimal;
getBorrowByReserve(reserve: PublicKey): Position | undefined;
getDepositByReserve(reserve: PublicKey): Position | undefined;
getBorrowByMint(mint: PublicKey): Position | undefined;
getBorrowAmountByReserve(reserve: KaminoReserve): Decimal;
getDepositByMint(mint: PublicKey): Position | undefined;
getDepositAmountByReserve(reserve: KaminoReserve): Decimal;
/**
*
* @returns Market value of the borrow in the specified obligation liquidity/borrow asset (USD) (no borrow factor weighting)
*/
getBorrowMarketValue(borrow: ObligationLiquidity): Decimal;
/**
*
* @returns Market value of the borrow in the specified obligation liquidity/borrow asset (USD) (with borrow factor weighting)
*/
getBorrowMarketValueBFAdjusted(borrow: ObligationLiquidity): Decimal;
/**
* Calculates the current ratio of borrowed value to deposited value (taking *all* deposits into account).
*
* Please note that the denominator here is different from the one found in `refreshedStats`:
* - the {@link ObligationStats#loanToValue} contains a value appropriate for display on the UI (i.e. taking into
* account *only* the deposits having `reserve.loanToValue > 0`).
* - the computation below follows the logic used by the KLend smart contract, and is appropriate e.g. for evaluating
* LTV-based obligation orders.
*/
loanToValue(): Decimal;
/**
* Calculates the ratio of borrowed value to deposited value (taking *all* deposits into account) at which the
* obligation is subject to liquidation.
*
* Please note that the denominator here is different from the one found in `refreshedStats`:
* - the {@link ObligationStats#liquidationLtv} contains a value appropriate for display on the UI (i.e. taking into
* account *only* the deposits having `reserve.liquidationLtv > 0`).
* - the computation below follows the logic used by the KLend smart contract, and is appropriate e.g. for evaluating
* LTV-based obligation orders.
*/
liquidationLtv(): Decimal;
/**
* Calculate the current ratio of borrowed value to deposited value, disregarding the borrow factor.
*/
noBfLoanToValue(): Decimal;
/**
* @returns the total number of positions (deposits + borrows)
*/
getNumberOfPositions(): number;
getNetAccountValue(): Decimal;
/**
* Get the loan to value and liquidation loan to value for a collateral token reserve as ratios, accounting for the obligation elevation group if it is active
*/
getLtvForReserve(market: KaminoMarket, reserveAddress: PublicKey): {
maxLtv: Decimal;
liquidationLtv: Decimal;
};
/**
* @returns the potential elevation groups the obligation qualifies for
*/
getElevationGroups(kaminoMarket: KaminoMarket): Array<number>;
static getElevationGroupsForReserves(reserves: Array<KaminoReserve>): Array<number>;
simulateDepositChange(obligationDeposits: ObligationCollateral[], changeInLamports: number, changeReserve: PublicKey, collateralExchangeRates: Map<PublicKey, Decimal>): ObligationCollateral[];
simulateBorrowChange(obligationBorrows: ObligationLiquidity[], changeInLamports: number, changeReserve: PublicKey, cumulativeBorrowRate: Decimal): ObligationLiquidity[];
/**
* Calculate the newly modified stats of the obligation
*/
getSimulatedObligationStats(params: {
amountCollateral?: Decimal;
amountDebt?: Decimal;
action: ActionType;
mintCollateral?: PublicKey;
mintDebt?: PublicKey;
market: KaminoMarket;
reserves: Map<PublicKey, KaminoReserve>;
slot: number;
elevationGroupOverride?: number;
}): {
stats: ObligationStats;
deposits: Map<PublicKey, Position>;
borrows: Map<PublicKey, Position>;
};
/**
* Calculates the stats of the obligation after a hypothetical collateral swap.
*/
getPostSwapCollObligationStats(params: {
withdrawAmountLamports: Decimal;
withdrawReserveAddress: PublicKey;
depositAmountLamports: Decimal;
depositReserveAddress: PublicKey;
newElevationGroup: number;
market: KaminoMarket;
slot: number;
}): ObligationStats;
estimateObligationInterestRate: (market: KaminoMarket, reserve: KaminoReserve, borrow: ObligationLiquidity, currentSlot: number) => Decimal;
static getOraclePx: (reserve: KaminoReserve) => Decimal;
calculatePositions(market: KaminoMarket, obligationDeposits: ObligationCollateral[], obligationBorrows: ObligationLiquidity[], elevationGroup: number, collateralExchangeRates: Map<PublicKey, Decimal>, cumulativeBorrowRates: Map<PublicKey, Decimal> | null, getOraclePx?: (reserve: KaminoReserve) => Decimal): {
borrows: Map<PublicKey, Position>;
deposits: Map<PublicKey, Position>;
refreshedStats: ObligationStats;
};
static calculateObligationDeposits(market: KaminoMarket, obligationDeposits: ObligationCollateral[], collateralExchangeRates: Map<PublicKey, Decimal> | null, elevationGroup: number, getPx: (reserve: KaminoReserve) => Decimal): DepositStats;
static calculateObligationBorrows(market: KaminoMarket, obligationBorrows: ObligationLiquidity[], cumulativeBorrowRates: Map<PublicKey, Decimal> | null, elevationGroup: number, getPx: (reserve: KaminoReserve) => Decimal): BorrowStats;
getMaxLoanLtvGivenElevationGroup(market: KaminoMarket, elevationGroup: number, slot: number): Decimal;
getBorrowPower(market: KaminoMarket, liquidityMint: PublicKey, slot: number, elevationGroup?: number): Decimal;
getMaxBorrowAmountV2(market: KaminoMarket, liquidityMint: PublicKey, slot: number, elevationGroup?: number): Decimal;
isLoanEligibleForElevationGroup(market: KaminoMarket, slot: number, elevationGroup: number): boolean;
getElevationGroupsForObligation(market: KaminoMarket): ElevationGroupDescription[];
getMaxBorrowAmount(market: KaminoMarket, liquidityMint: PublicKey, slot: number, requestElevationGroup: boolean): Decimal;
getMaxWithdrawAmount(market: KaminoMarket, tokenMint: PublicKey, slot: number): Decimal;
getObligationLiquidityByReserve(reserveAddress: PublicKey): ObligationLiquidity;
/**
*
* @returns Total borrowed amount for the specified obligation liquidity/borrow asset
*/
static getBorrowAmount(borrow: ObligationLiquidity): Decimal;
/**
*
* @returns Cumulative borrow rate for the specified obligation liquidity/borrow asset
*/
static getCumulativeBorrowRate(borrow: ObligationLiquidity): Decimal;
static getRatesForObligation(kaminoMarket: KaminoMarket, obligation: Obligation, slot: number, additionalReserves?: PublicKey[]): {
collateralExchangeRates: Map<PublicKey, Decimal>;
cumulativeBorrowRates: Map<PublicKey, Decimal>;
};
static addRatesForObligation(kaminoMarket: KaminoMarket, obligation: Obligation, collateralExchangeRates: Map<PublicKey, Decimal>, cumulativeBorrowRates: Map<PublicKey, Decimal>, slot: number): void;
static getCollateralExchangeRatesForObligation(kaminoMarket: KaminoMarket, obligation: Obligation, slot: number, additionalReserves: PublicKey[]): Map<PublicKey, Decimal>;
static addCollateralExchangeRatesForObligation(kaminoMarket: KaminoMarket, collateralExchangeRates: Map<PublicKey, Decimal>, obligation: Obligation, slot: number): void;
static getCumulativeBorrowRatesForObligation(kaminoMarket: KaminoMarket, obligation: Obligation, slot: number, additionalReserves?: PublicKey[]): Map<PublicKey, Decimal>;
static addCumulativeBorrowRatesForObligation(kaminoMarket: KaminoMarket, cumulativeBorrowRates: Map<PublicKey, Decimal>, obligation: Obligation, slot: number): void;
/**
* Get the borrow factor for a borrow reserve, accounting for the obligation elevation group if it is active
* @param reserve
* @param elevationGroup
*/
static getBorrowFactorForReserve(reserve: KaminoReserve, elevationGroup: number): Decimal;
/**
* Get the loan to value and liquidation loan to value for a collateral reserve as ratios, accounting for the obligation elevation group if it is active
* @param market
* @param reserve
* @param elevationGroup
*/
static getLtvForReserve(market: KaminoMarket, reserve: KaminoReserve, elevationGroup: number): {
maxLtv: Decimal;
liquidationLtv: Decimal;
};
}
export declare function isKaminoObligation(obligation: KaminoObligation | ObligationType): obligation is KaminoObligation;
export {};
//# sourceMappingURL=obligation.d.ts.map