import BigNumber from 'bignumber.js' ; import { Codec } from '@polkadot/types/types' ; constant type { ( 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ) } description export type ROUND_MODE = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 ; type NumLike = number | string ; class description export class Fixed18 { private inner : BigNumber ; constant description static PRECISION = 10 ** 18 ; constant description static ZERO = Fixed18 . fromNatural ( 0 ) ; description param { ( number | string | BigNumber ) } origin constructor ( origin ) { if ( origin instanceof BigNumber ) { this . inner = origin ; } else { this . inner = new BigNumber ( origin || 0 ) ; } return this ; } name description public getInner ( ) { return this . inner ; } name description param { number } dp param { number } rm public toString ( dp = 6 , rm = 3 ) { let result = this . inner . div ( Fixed18 . PRECISION ) ; result = result . decimalPlaces ( dp , rm ) ; return result . toString ( ) ; } name description param { number } dp param { number } rm public toFixed ( dp = 6 , rm = 3 ) { let result = this . inner . div ( Fixed18 . PRECISION ) ; result = result . decimalPlaces ( dp , rm ) ; return result . toFixed ( ) ; } name description param { number } dp param { number } rm public innerToString ( dp = 0 , rm = 3 ) { if ( ! this . isFinity ( ) ) { return '0' ; } return this . inner . decimalPlaces ( dp , rm ) . toFixed ( ) ; } name description param { number } dp param { number } rm public innerToNumber ( dp = 0 , rm = 3 ) { if ( ! this . isFinity ( ) ) { return 0 ; } return this . inner . decimalPlaces ( dp , rm ) . toNumber ( ) ; } name description param { number } dp param { number } rm public toNumber ( dp = 6 , rm = 3 ) { let result = this . inner . div ( Fixed18 . PRECISION ) ; result = result . decimalPlaces ( dp , rm ) ; return result . toNumber ( ) ; } name description param { number } dp param { number } rm public decimalPlaces ( dp = 18 , rm = 3 ) { return Fixed18 . fromNatural ( this . toNumber ( dp , rm ) ) ; } name description param { ( string | number ) } target static fromNatural ( target ) { return new Fixed18 ( new BigNumber ( target ) . times ( Fixed18 . PRECISION ) ) ; } name description param { ( string | number ) } parts static fromParts ( parts ) { return new Fixed18 ( parts ) ; } name param { ( string | number ) } n param { ( string | number ) } d static fromRational ( n , d ) { const _n = new BigNumber ( n ) ; const _d = new BigNumber ( d ) ; return new Fixed18 ( _n . times ( Fixed18 . PRECISION ) . div ( _d ) . decimalPlaces ( 0 , 3 ) ) ; } name description param { Fixed18 } target public add ( target ) { return new Fixed18 ( this . inner . plus ( target . inner ) . decimalPlaces ( 0 , 3 ) ) ; } name description param { Fixed18 } target public sub ( target ) { return new Fixed18 ( this . inner . minus ( target . inner ) . decimalPlaces ( 0 , 3 ) ) ; } name description param { Fixed18 } target public mul ( target ) { const inner = this . inner . times ( target . inner ) . div ( Fixed18 . PRECISION ) . decimalPlaces ( 0 , 3 ) ; return new Fixed18 ( inner ) ; } name description param { Fixed18 } target public div ( target ) { const inner = this . inner . div ( target . inner ) . times ( Fixed18 . PRECISION ) . decimalPlaces ( 0 , 3 ) ; return new Fixed18 ( inner ) ; } name description param { Fixed18 } target public isLessThan ( target ) { return this . inner . isLessThan ( target . inner ) ; } name description param { Fixed18 } target public isGreaterThan ( target ) { return this . inner . isGreaterThan ( target . inner ) ; } name description param { Fixed18 } target public isEqualTo ( target ) { return this . inner . isEqualTo ( target . inner ) ; } name description param { ... Fixed18 } target public max ( ... targets ) { return new Fixed18 ( BigNumber . max . apply ( null , [ this . inner , ... targets . map ( ( i ) => i . inner ) ] ) ) ; } name description param { ... Fixed18 } target public min ( ... targets ) { return new Fixed18 ( BigNumber . min . apply ( null , [ this . inner , ... targets . map ( ( i ) => i . inner ) ] ) ) ; } name description public negated ( ) { return new Fixed18 ( this . inner . negated ( ) ) ; } name description public isZero ( ) { return this . inner . isZero ( ) ; } name description public isNaN ( ) { return this . inner . isNaN ( ) ; } name description public isFinity ( ) { return this . inner . isFinite ( ) ; } } export function convertToFixed18 ( data ) { if ( data instanceof Fixed18 ) { return data ; } else if ( typeof data === 'number' ) { return Fixed18 . fromNatural ( data ) ; } if ( 'toString' in data ) { return Fixed18 . fromParts ( data . toString ( ) ) ; } return Fixed18 . ZERO ; }