All files / src/clarity/types intCV.ts

75% Statements 15/20
37.5% Branches 3/8
100% Functions 2/2
73.68% Lines 14/19

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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53              21x 21x             21x 27x 27x       27x       27x         27x               21x 73x   73x   73x         73x     21x  
import {
  ClarityType,
  CLARITY_INT_SIZE,
  MAX_I128,
  MIN_I128,
  MIN_U128,
  MAX_U128,
} from '../common/constants';
import { IntegerType, intToBigInt, intToBytes } from 'micro-stacks/common';
 
interface IntCV {
  readonly type: ClarityType.Int;
  readonly value: bigint;
}
 
const intCV = (value: IntegerType): IntCV => {
  const bigInt = intToBigInt(value, true);
  Iif (bigInt > MAX_I128) {
    throw new RangeError(
      `Cannot construct clarity integer from value greater than ${MAX_I128.toString()}`
    );
  } else Iif (bigInt < MIN_I128) {
    throw new RangeError(
      `Cannot construct clarity integer form value less than ${MIN_I128.toString()}`
    );
  } else Iif (intToBytes(bigInt).byteLength > CLARITY_INT_SIZE) {
    throw new RangeError(
      `Cannot construct clarity integer from value greater than ${CLARITY_INT_SIZE} bits`
    );
  }
  return { type: ClarityType.Int, value: bigInt };
};
 
interface UIntCV {
  readonly type: ClarityType.UInt;
  readonly value: bigint;
}
 
const uintCV = (value: IntegerType): UIntCV => {
  const bigInt = intToBigInt(value);
 
  Iif (bigInt < MIN_U128) {
    throw new RangeError('Cannot construct unsigned clarity integer from negative value');
  } else Iif (bigInt > MAX_U128) {
    throw new RangeError(
      `Cannot construct unsigned clarity integer greater than ${MAX_U128.toString()}`
    );
  }
  return { type: ClarityType.UInt, value: bigInt };
};
 
export { IntCV, UIntCV, intCV, uintCV };