import { Fixed18 , convertToFixed18 } from './fixed-18' ; import { Codec } from '@polkadot/types/types' ; name description param { Fixed18 } quantity param { Fixed18 } debitExchangeRate returns { Fixed18 } export function debitToStableCoin ( quantity , debitExchangeRate ) { return quantity . mul ( debitExchangeRate ) ; } name description param { Fixed18 } amount param { Fixed18 } debitExchangeRate returns { Fixed18 } export function stableCoinToDebit ( amount , debitExchangeRate ) { return debitExchangeRate . isZero ( ) ? Fixed18 . ZERO : amount . div ( debitExchangeRate ) ; } name description param { Fixed18 } quantity param { Fixed18 } debitExchangeRate param { Fixed18 } stableCoinPrice returns { Fixed18 } export function debitToUSD ( quantity , debitExchangeRate , stableCoinPrice ) { return debitToStableCoin ( quantity , debitExchangeRate ) . mul ( stableCoinPrice ) ; } name description param { Fixed18 } amount param { Fixed18 } debitExchangeRate param { Fixed18 } stableCoinPrice returns { Fixed18 } export function USDToDebit ( amount , debitExchangeRate , stableCoinPrice ) { return stableCoinPrice . isZero ( ) ? Fixed18 . ZERO : stableCoinToDebit ( amount . div ( stableCoinPrice ) , debitExchangeRate ) ; } name description param { Fixed18 } quantity param { Fixed18 } collateralPrice returns { Fixed18 } export function collateralToUSD ( quantity , collateralPrice ) { return quantity . mul ( collateralPrice ) ; } name description param { Fixed18 } amount param { Fixed18 } collateralPrice returns { Fixed18 } export function USDToCollateral ( amount , collateralPrice ) { return amount . div ( collateralPrice ) ; } name description param { Fixed18 } collateralAmount param { Fixed18 } debitAmount returns { Fixed18 } export function calcCollateralRatio ( collateralAmount , debitAmount ) { return collateralAmount . div ( debitAmount ) ; } const YEAR = 365 * 24 * 60 * 60 ; name description param { Fixed18 } stableFee param { number } expectedBlockTime return { Fixed18 } export function calcStableFeeAPR ( stableFee , blockTime ) { return Fixed18 . fromNatural ( ( 1 + stableFee . toNumber ( 18 , 3 ) ) ** ( ( YEAR / blockTime ) * 1000 ) - 1 ) ; } name description param { Fixed18 } debitAmount param { Fixed18 } requiredCollateralRatio param { Fixed18 } collateralPrice returns { Fixed18 } export function calcRequiredCollateral ( debitAmount , requiredCollateralRatio , collateralPrice ) { const result = debitAmount . mul ( requiredCollateralRatio ) . div ( collateralPrice ) ; if ( result . isLessThan ( Fixed18 . ZERO ) || ! result . isFinity ( ) ) { return Fixed18 . ZERO ; } return result ; } name description param { Fixed18 } collateralQuantity param { Fixed18 } debitAmount param { Fixed18 } liquidationRatio returns { Fixed18 } export function calcLiquidationPrice ( collateralQuantity , debitAmount , liquidationRatio ) { const result = debitAmount . mul ( liquidationRatio ) . div ( collateralQuantity ) ; if ( result . isLessThan ( Fixed18 . ZERO ) || result . isEqualTo ( Fixed18 . ZERO ) || ! result . isFinity ( ) ) { return Fixed18 . fromNatural ( NaN ) ; } return result ; } name description param { Fixed18 } collateralAmount param { Fixed18 } currentDebitAmount param { Fixed18 } requiredCollateralRatio param { Fixed18 } stableCoinPrice param { Fixed18 } slippage returns { Fixed18 } export function calcCanGenerate ( collateralAmount , currentDebitAmount , requiredCollateralRatio , stableCoinPrice , slippage = Fixed18 . ZERO ) { const result = collateralAmount . div ( requiredCollateralRatio ) . sub ( currentDebitAmount ) . div ( stableCoinPrice ) . sub ( slippage ) ; if ( result . isLessThan ( Fixed18 . ZERO ) || ! result . isFinity ( ) ) { return Fixed18 . ZERO ; } return result ; } interface LoanParams { debits : Fixed18 | Codec ; collaterals : Fixed18 | Codec ; requiredCollateralRatio : Fixed18 | Codec ; stableFee : Fixed18 | Codec ; globalStableFee : Fixed18 | Codec ; expectedBlockTime : number ; liquidationRatio : Fixed18 | Codec ; collateralPrice : Fixed18 | Codec ; stableCoinPrice : Fixed18 | Codec ; debitExchangeRate : Fixed18 | Codec ; } class classdesc export class LoanHelper { public debits : Fixed18 ; public collaterals : Fixed18 ; public requiredCollateralRatio : Fixed18 ; public stableFee : Fixed18 ; public globalStableFee : Fixed18 ; public expectedBlockTime : number ; public liquidationRatio : Fixed18 ; public collateralPrice : Fixed18 ; public stableCoinPrice : Fixed18 ; public debitExchangeRate : Fixed18 ; constructor ( params ) { this . debits = convertToFixed18 ( params . debits ) ; this . collaterals = convertToFixed18 ( params . collaterals ) ; this . requiredCollateralRatio = convertToFixed18 ( params . requiredCollateralRatio ) ; this . stableFee = convertToFixed18 ( params . stableFee ) ; this . globalStableFee = convertToFixed18 ( params . globalStableFee ) ; this . expectedBlockTime = params . expectedBlockTime ; this . liquidationRatio = convertToFixed18 ( params . liquidationRatio ) ; this . collateralPrice = convertToFixed18 ( params . collateralPrice ) ; this . debitExchangeRate = convertToFixed18 ( params . debitExchangeRate ) ; this . collateralPrice = convertToFixed18 ( params . collateralPrice ) ; this . stableCoinPrice = convertToFixed18 ( params . stableCoinPrice ) ; } property get debitAmount ( ) : Fixed18 { return debitToUSD ( this . debits , this . debitExchangeRate , this . stableCoinPrice ) ; } property get collateralAmount ( ) : Fixed18 { return collateralToUSD ( this . collaterals , this . collateralPrice ) ; } property get collateralRatio ( ) : Fixed18 { return calcCollateralRatio ( this . collateralAmount , this . debitAmount ) ; } property get requiredCollateral ( ) : Fixed18 { return calcRequiredCollateral ( this . debitAmount , this . requiredCollateralRatio , this . collateralPrice ) ; } property get stableFeeAPR ( ) : Fixed18 { return calcStableFeeAPR ( this . stableFee . add ( this . globalStableFee ) , this . expectedBlockTime ) ; } property get liquidationPrice ( ) : Fixed18 { return calcLiquidationPrice ( this . collaterals , this . debitAmount , this . liquidationRatio ) ; } property get canGenerate ( ) : Fixed18 { return calcCanGenerate ( this . collateralAmount , this . debitAmount , this . requiredCollateralRatio , this . stableCoinPrice , Fixed18 . fromNatural ( 0.000001 ) ) ; } property get canPayBack ( ) : Fixed18 { return this . debitAmount ; } }