import { BigNumber } from 'bignumber.js' ; import { isInteger , makeBigNumber } from '../utils' ; const POSSIBLE_DECIMALS = [ 18 , 17 , 16 , 15 , 14 , 13 , 12 , 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ] ; type digits = typeof POSSIBLE_DECIMALS [ number ] ; const BZZ_DECIMALS = 16 ; export class Token { private amount : BigNumber ; private readonly decimals : digits ; constructor ( amount , decimals = BZZ_DECIMALS ) { const a = makeBigNumber ( amount ) ; if ( ! isInteger ( a ) || ! POSSIBLE_DECIMALS . includes ( decimals ) ) throw new TypeError ( 'Not a valid token values' ) ; this . amount = a ; this . decimals = decimals ; } param amount param decimals throws returns static fromDecimal ( amount , decimals = BZZ_DECIMALS ) { const a = makeBigNumber ( amount ) ; const t = a . multipliedBy ( new BigNumber ( 10 ) . pow ( decimals ) ) ; return new Token ( t , decimals ) ; } get toBigInt ( ) : BigInt { return BigInt ( this . amount . toFixed ( 0 ) ) ; } get toString ( ) : string { return this . amount . toFixed ( 0 ) ; } get toBigNumber ( ) : BigNumber { return new BigNumber ( this . amount ) ; } get toDecimal ( ) : BigNumber { return this . amount . dividedBy ( new BigNumber ( 10 ) . pow ( this . decimals ) ) ; } toFixedDecimal ( digits = 7 ) { return this . toDecimal . toFixed ( digits ) ; } }