All files / lib/utils Precision.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 2/2
100% Lines 7/7

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 241x                   1x 545x             1x 19x 1x 18x    
import BigNumber from 'bignumber.js';
 
// Precision refers to the number of digits after the decimal point.
// Refer to https://github.com/discreetlogcontracts/dlcspecs/blob/master/PayoutCurve.md#version-0-payout_function
 
/**
 * Extract the precision of a number
 * @param num
 * @returns The precision of the number
 */
export const getPrecision = (num: BigNumber): number =>
  num.decimalPlaces(16).abs().modulo(1).shiftedBy(16).toNumber();
 
/**
 * Creates a precise number from a precision
 * @param precision
 * @returns The number with the given precision
 */
export const fromPrecision = (precision: number): BigNumber => {
  if (precision.toString().length > 16)
    throw new Error('Precision is too large');
  return new BigNumber(precision).shiftedBy(-16);
};