All files / app/utils unit-convert.ts

94.44% Statements 17/18
66.66% Branches 2/3
100% Functions 4/4
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 255x 5x     44x 44x 43x     5x 18x 18x 17x     5x 26x 26x     5x 8x 8x    
import BigNumber from 'bignumber.js';
import BN from 'bn.js';
 
function parseNumber(num: string | number | BN | BigNumber) {
  Iif (BigNumber.isBigNumber(num)) return num;
  if (BN.isBN(num)) return new BigNumber(num.toString());
  return new BigNumber(num);
}
 
export function microStxToStx(microStx: string | number | BN | BigNumber): BigNumber {
  const amount = parseNumber(microStx);
  if (!amount.isInteger()) throw new Error('Micro STX can only be represented as integers');
  return amount.dividedBy(10 ** 6);
}
 
export function stxToMicroStx(microStx: string | number | BN | BigNumber) {
  const amount = parseNumber(microStx);
  return amount.multipliedBy(1000000);
}
 
export function toHumanReadableStx(microStx: string | number | BN | BigNumber): string {
  const amount = microStxToStx(microStx);
  return amount.toFormat() + ' STX';
}