import { Observable , combineLatest } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { ApiInterfaceRx } from '@polkadot/api/types' ; import { memo } from '@polkadot/api-derive/util' ; import { CurrencyId , Rate , ExchangeRate , Balance , Position , Ratio } from '@acala-network/types/interfaces' ; import { DerivedLoanConstants , DerivedLoanType , DerivedLoanOverView , CollateralParams } from '../types/loan' ; import { getAllCollateralCurrencyIds } from '../utils' ; name description function loanConstants ( api ) { return { minimumDebitValue : api . consts . cdpEngine . minimumDebitValue as Balance , defaultDebitExchangeRate : api . consts . cdpEngine . defaultDebitExchangeRate as ExchangeRate , defaultLiquidationRatio : api . consts . cdpEngine . defaultLiquidationRatio as Ratio , defaultLiquidationPenalty : api . consts . cdpEngine . defaultLiquidationPenalty as Rate } ; } name description param { ( CurrencyId | string ) } currency export function loanType ( instanceId , api ) { return memo ( instanceId , ( currency ) => { return combineLatest ( [ api . query . cdpEngine . globalInterestRatePerSec < Rate > ( ) , api . query . cdpEngine . debitExchangeRate < Rate > ( currency ) , api . query . cdpEngine . collateralParams < CollateralParams > ( currency ) ] ) . pipe ( map ( ( result ) => { const constants = loanConstants ( api ) ; const [ globalInterestRatePerSec , debitExchangeRate , collateralParams ] = result ; return { currency , debitExchangeRate : debitExchangeRate . isEmpty ? constants . defaultDebitExchangeRate : debitExchangeRate , liquidationPenalty : collateralParams . liquidationPenalty . isEmpty ? constants . defaultLiquidationPenalty : collateralParams . liquidationPenalty , liquidationRatio : collateralParams . liquidationRatio . isEmpty ? constants . defaultLiquidationRatio : collateralParams . liquidationRatio , requiredCollateralRatio : collateralParams . requiredCollateralRatio , interestRatePerSec : collateralParams . interestRatePerSec , globalInterestRatePerSec : globalInterestRatePerSec , maximumTotalDebitValue : collateralParams . maximumTotalDebitValue , minimumDebitValue : constants . minimumDebitValue } ; } ) ) ; } ) ; } name description export function allLoanTypes ( instanceId , api ) { return memo ( instanceId , ( ) => { const collateralCurrencyIds = getAllCollateralCurrencyIds ( api ) ; const loanTypeQuery = loanType ( instanceId , api ) ; return combineLatest ( collateralCurrencyIds . map ( ( currencyId ) => loanTypeQuery ( currencyId ) ) ) ; } ) ; } name description param { ( CurrencyId | string ) } currency export function loanOverview ( instanceId , api ) { return memo ( instanceId , ( currency ) => api . query . loans . totalPositions < Position > ( currency ) . pipe ( map ( ( result ) => { const { collateral , debit } = result ; return { currency , totalDebit : debit , totalCollateral : collateral } ; } ) ) ) ; } name description export function allLoanOverviews ( instanceId , api ) { return memo ( instanceId , ( ) => { const collateralCurrencyIds = getAllCollateralCurrencyIds ( api ) ; const loanOverViewQuery = loanOverview ( instanceId , api ) ; return combineLatest ( collateralCurrencyIds . map ( ( currencyId ) => loanOverViewQuery ( currencyId ) ) ) ; } ) ; }