@drift-labs/sdk-browser
Version:
SDK for Drift Protocol
86 lines (85 loc) • 5.23 kB
TypeScript
/// <reference types="bn.js" />
import { SpotMarketAccount, SpotBalanceType, MarginCategory } from '../types';
import { BN } from '@coral-xyz/anchor';
import { OraclePriceData } from '../oracles/types';
import { StrictOraclePrice } from '../oracles/strictOraclePrice';
/**
* Calculates the balance of a given token amount including any accumulated interest. This
* is the same as `SpotPosition.scaledBalance`.
*
* @param {BN} tokenAmount - the amount of tokens
* @param {SpotMarketAccount} spotMarket - the spot market account
* @param {SpotBalanceType} balanceType - the balance type ('deposit' or 'borrow')
* @return {BN} the calculated balance, scaled by `SPOT_MARKET_BALANCE_PRECISION`
*/
export declare function getBalance(tokenAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
/**
* Calculates the spot token amount including any accumulated interest.
*
* @param {BN} balanceAmount - The balance amount, typically from `SpotPosition.scaledBalance`
* @param {SpotMarketAccount} spotMarket - The spot market account details
* @param {SpotBalanceType} balanceType - The balance type to be used for calculation
* @returns {BN} The calculated token amount, scaled by `SpotMarketConfig.precision`
*/
export declare function getTokenAmount(balanceAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
/**
* Returns the signed (positive for deposit,negative for borrow) token amount based on the balance type.
*
* @param {BN} tokenAmount - The token amount to convert (from `getTokenAmount`)
* @param {SpotBalanceType} balanceType - The balance type to determine the sign of the token amount.
* @returns {BN} - The signed token amount, scaled by `SpotMarketConfig.precision`
*/
export declare function getSignedTokenAmount(tokenAmount: BN, balanceType: SpotBalanceType): BN;
/**
* Calculates the value of a given token amount using the worst of the provided oracle price and its TWAP.
*
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
* @param {number} spotDecimals - The number of decimals in the token.
* @param {StrictOraclePrice} strictOraclePrice - Contains oracle price and 5min twap.
* @return {BN} The calculated value of the given token amount, scaled by `PRICE_PRECISION`
*/
export declare function getStrictTokenValue(tokenAmount: BN, spotDecimals: number, strictOraclePrice: StrictOraclePrice): BN;
/**
* Calculates the value of a given token amount in relation to an oracle price data
*
* @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
* @param {number} spotDecimals - The number of decimal places of the token.
* @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
* @return {BN} The value of the token based on the oracle, scaled by `PRICE_PRECISION`
*/
export declare function getTokenValue(tokenAmount: BN, spotDecimals: number, oraclePriceData: Pick<OraclePriceData, 'price'>): BN;
export declare function calculateAssetWeight(balanceAmount: BN, oraclePrice: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
export declare function calculateScaledInitialAssetWeight(spotMarket: SpotMarketAccount, oraclePrice: BN): BN;
export declare function calculateLiabilityWeight(size: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
export declare function calculateUtilization(bank: SpotMarketAccount, delta?: BN): BN;
/**
* calculates max borrow amount where rate would stay below targetBorrowRate
* @param spotMarketAccount
* @param targetBorrowRate
* @returns : Precision: TOKEN DECIMALS
*/
export declare function calculateSpotMarketBorrowCapacity(spotMarketAccount: SpotMarketAccount, targetBorrowRate: BN): {
totalCapacity: BN;
remainingCapacity: BN;
};
export declare function calculateInterestRate(bank: SpotMarketAccount, delta?: BN, currentUtilization?: BN): BN;
export declare function calculateDepositRate(bank: SpotMarketAccount, delta?: BN, currentUtilization?: BN): BN;
export declare function calculateBorrowRate(bank: SpotMarketAccount, delta?: BN, currentUtilization?: BN): BN;
export declare function calculateInterestAccumulated(bank: SpotMarketAccount, now: BN): {
borrowInterest: BN;
depositInterest: BN;
};
export declare function calculateTokenUtilizationLimits(depositTokenAmount: BN, borrowTokenAmount: BN, spotMarket: SpotMarketAccount): {
minDepositTokensForUtilization: BN;
maxBorrowTokensForUtilization: BN;
};
export declare function calculateWithdrawLimit(spotMarket: SpotMarketAccount, now: BN): {
borrowLimit: BN;
withdrawLimit: BN;
minDepositAmount: BN;
maxBorrowAmount: BN;
currentDepositAmount: any;
currentBorrowAmount: any;
};
export declare function getSpotAssetValue(tokenAmount: BN, strictOraclePrice: StrictOraclePrice, spotMarketAccount: SpotMarketAccount, maxMarginRatio: number, marginCategory?: MarginCategory): BN;
export declare function getSpotLiabilityValue(tokenAmount: BN, strictOraclePrice: StrictOraclePrice, spotMarketAccount: SpotMarketAccount, maxMarginRatio: number, marginCategory?: MarginCategory, liquidationBuffer?: BN): BN;