All files / src/data-point data-point-deserializer.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 1/1
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 224x 4x 4x           4x     11x 8x   3x              
import { base64 } from "ethers/lib/utils";
import { DataPoint, DataPointPlainObj } from "./DataPoint";
import { INumericDataPoint, NumericDataPoint } from "./NumericDataPoint";
 
// This function was moved to a separate file, because it was not
// possible to make it as a static method in the DataPoint class.
// It would cause circularly importing classes, which is not supported
// More info here: https://stackoverflow.com/a/44727578
export const deserializeDataPointFromObj = (
  plainObject: DataPointPlainObj
): DataPoint => {
  if (typeof plainObject.value == "number") {
    return new NumericDataPoint(plainObject as INumericDataPoint);
  } else {
    return new DataPoint(
      plainObject.dataFeedId,
      base64.decode(plainObject.value),
      plainObject.metadata
    );
  }
};