All files / src/data-point DataPoint.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 5/5
100% Lines 13/13

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 454x 4x 4x                       4x   42x 42x 42x   42x       146x       3x               74x       72x 72x      
import { base64, concat } from "ethers/lib/utils";
import { Serializable } from "../common/Serializable";
import { ConvertibleToBytes32, convertStringToBytes32 } from "../common/utils";
import { INumericDataPoint } from "./NumericDataPoint";
 
export interface IStandardDataPoint {
  dataFeedId: ConvertibleToBytes32;
  value: string; // base64-encoded bytes
  metadata?: Metadata;
}
export type DataPointPlainObj = IStandardDataPoint | INumericDataPoint;
 
export type Metadata = Record<string, unknown>;
 
export class DataPoint extends Serializable {
  constructor(
    public readonly dataFeedId: ConvertibleToBytes32,
    public readonly value: Uint8Array,
    public readonly metadata?: Metadata
  ) {
    super();
  }
 
  serializeDataFeedId(): Uint8Array {
    return convertStringToBytes32(this.dataFeedId);
  }
 
  toObj(): DataPointPlainObj {
    return {
      dataFeedId: this.dataFeedId,
      value: base64.encode(this.value),
      metadata: this.metadata,
    };
  }
 
  getValueByteSize(): number {
    return this.value.length;
  }
 
  toBytes(): Uint8Array {
    const serializedDataFeedId = this.serializeDataFeedId();
    return concat([serializedDataFeedId, this.value]);
  }
}