All files / src/data-point NumericDataPoint.ts

92.3% Statements 12/13
0% Branches 0/2
66.66% Functions 2/3
91.66% Lines 11/12

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 534x       4x         4x                   4x         4x 32x 32x       32x       32x         32x               8x          
import {
  DEFAULT_NUM_VALUE_BS,
  DEFAULT_NUM_VALUE_DECIMALS,
} from "../common/redstone-constants";
import {
  ConvertibleToBytes32,
  convertNumberToBytes,
  useDefaultIfUndefined,
} from "../common/utils";
import { DataPoint, Metadata } from "./DataPoint";
 
export interface INumericDataPoint {
  dataFeedId: ConvertibleToBytes32;
  value: number;
  decimals?: number;
  valueByteSize?: number;
  metadata?: Metadata;
}
 
export const getNumericDataPointDecimals = (
  dataPoint: INumericDataPoint
): number => dataPoint.decimals ?? DEFAULT_NUM_VALUE_DECIMALS;
 
// This data point does not store information about data size in its serialized value
export class NumericDataPoint extends DataPoint {
  constructor(private readonly numericDataPointArgs: INumericDataPoint) {
    const decimals = useDefaultIfUndefined(
      numericDataPointArgs.decimals,
      DEFAULT_NUM_VALUE_DECIMALS
    );
    const valueByteSize = useDefaultIfUndefined(
      numericDataPointArgs.valueByteSize,
      DEFAULT_NUM_VALUE_BS
    );
    const valueBytes = convertNumberToBytes(
      numericDataPointArgs.value,
      decimals,
      valueByteSize
    );
    super(
      numericDataPointArgs.dataFeedId,
      valueBytes,
      numericDataPointArgs.metadata
    );
  }
 
  override toObj(): INumericDataPoint {
    return {
      ...this.numericDataPointArgs,
    };
  }
}