import BN from "bignumber.js" ; import { reverseHex } from "./misc" ; const DECIMALS = 100000000 ; const MAX_FIXED8_HEX = new BN ( 2 ) . pow ( 63 ) . minus ( 1 ) ; const MIN_FIXED8_HEX = new BN ( 2 ) . pow ( 63 ) . negated ( ) ; const TOTAL_FIXED8_HEX = new BN ( 2 ) . pow ( 64 ) ; extends BN export class Fixed8 extends BN { public static readonly MAX_VALUE = new Fixed8 ( MAX_FIXED8_HEX . div ( DECIMALS ) ) ; public static readonly MIN_VALUE = new Fixed8 ( MIN_FIXED8_HEX . div ( DECIMALS ) ) ; public static fromHex ( hex ) { if ( hex . length > 16 ) { throw new Error ( `expected hex string to have length less or equal than 16: got ${ hex . length } for hex = ${ hex }` ) ; } let n = new BN ( hex , 16 ) ; if ( n . isGreaterThan ( MAX_FIXED8_HEX ) ) { n = n . minus ( TOTAL_FIXED8_HEX ) ; } n = n . div ( DECIMALS ) ; return new Fixed8 ( n , 10 ) ; } public static fromReverseHex ( hex ) { return this . fromHex ( reverseHex ( hex ) ) ; } public constructor ( input ? , base = 10 ) { if ( ! input ) { input = "0" ; } if ( typeof input === "number" ) { input = input . toFixed ( 8 ) ; } super ( input , base ) ; if ( this . isGreaterThan ( Fixed8 . MAX_VALUE ) ) { throw new Error ( `expected input to be less than ${ Fixed8 . MAX_VALUE }. Got input = ${ this }` ) ; } if ( this . isLessThan ( Fixed8 . MIN_VALUE ) ) { throw new Error ( `expected input to be greater than ${ Fixed8 . MIN_VALUE }. Got input = ${ this }` ) ; } Object . setPrototypeOf ( this , Fixed8 . prototype ) ; } public toHex ( ) { let hexstring = "" ; const num = this . toRawNumber ( ) ; hexstring = num . isLessThan ( 0 ) ? TOTAL_FIXED8_HEX . plus ( num ) . toString ( 16 ) : num . toString ( 16 ) ; return "0" . repeat ( 16 - hexstring . length ) + hexstring ; } public toReverseHex ( ) { return reverseHex ( this . toHex ( ) ) ; } /**
     * Returns a raw number represetation of Fixed8.
     */ public toRawNumber ( ) { return super . times ( DECIMALS ) ; } /**
     * Returns a Fixed8 whose value is rounded upwards to the next whole number.
     */ public ceil ( ) { return new Fixed8 ( super . decimalPlaces ( 0 , BN . ROUND_CEIL ) ) ; } /**
     * Returns a Fixed8 whose value is rounded downwards to the previous whole number.
     */ public floor ( ) { return new Fixed8 ( super . decimalPlaces ( 0 , BN . ROUND_FLOOR ) ) ; } /**
     * Returns true if the value is equivalent.
     */ public equals ( other ) { return super . eq ( other ) ; } param dp param rm return { Fixed8 } public round ( dp = 0 , rm ? ) { return new Fixed8 ( super . decimalPlaces ( dp , rm ) ) ; } alias public dividedBy ( n , base ? ) { return new Fixed8 ( super . dividedBy ( n , base ) ) ; } public div ( n , base ? ) { return this . dividedBy ( n , base ) ; } alias public times ( n , base ? ) { return new Fixed8 ( super . times ( n , base ) ) ; } public mul ( n , base ? ) { return this . times ( n , base ) ; } alias public plus ( n , base ? ) { return new Fixed8 ( super . plus ( n , base ) ) ; } public add ( n , base ? ) { return this . plus ( n , base ) ; } alias public minus ( n , base ? ) { return new Fixed8 ( super . minus ( n , base ) ) ; } public sub ( n , base ? ) { return this . minus ( n , base ) ; } } export default Fixed8 ;