All files / lib/messages RoundingIntervalsV0.ts

100% Statements 35/35
100% Branches 4/4
100% Functions 6/6
100% Lines 34/34

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1171x   1x           1x 1x             7x 7x   7x 7x 7x   7x 10x 10x   10x     7x           12x       12x                 5x 7x 1x         4x 2x     1x                 2x     4x                       6x 6x   6x 6x   6x 8x 8x     6x 6x   6x                                    
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
 
/**
 * RoundingIntervals V0
 */
export class RoundingIntervalsV0 implements IDlcMessage {
  public static type = MessageType.RoundingIntervalsV0;
 
  /**
   * Deserializes an rounding_intervals_v0 tlv
   * @param buf
   */
  public static deserialize(buf: Buffer): RoundingIntervalsV0 {
    const instance = new RoundingIntervalsV0();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize();
    reader.readUInt16BE(); // num_rounding_intervals
 
    while (!reader.eof) {
      const beginInterval = reader.readBigSize();
      const roundingMod = reader.readBigSize();
 
      instance.intervals.push({ beginInterval, roundingMod });
    }
 
    return instance;
  }
 
  /**
   * The type for rounding_intervals_v0 tlv. rounding_intervals_v0 = 42788
   */
  public type = RoundingIntervalsV0.type;
 
  public length: bigint;
 
  public intervals: IInterval[] = [];
 
  /**
   * Validates correctness of all fields in the message
   * https://github.com/discreetlogcontracts/dlcspecs/blob/master/NumericOutcome.md#requirements
   * @throws Will throw an error if validation fails
   */
  public validate(): void {
    // 1. Intervals must be non-negative
    for (const interval of this.intervals) {
      if (interval.beginInterval < 0) {
        throw new Error('beginInterval must be non-negative');
      }
    }
 
    // 2. Intervals must be strictly increasing
    for (let i = 1; i < this.intervals.length; ++i) {
      if (
        this.intervals[i - 1].beginInterval >= this.intervals[i].beginInterval
      ) {
        throw new Error(`Intervals must be strictly increasing`);
      }
    }
  }
 
  /**
   * Converts rounding_intervals_v0 to JSON
   */
  public toJSON(): IRoundingIntervalsV0JSON {
    return {
      type: this.type,
      intervals: this.intervals.map((interval) => {
        return {
          beginInterval: Number(interval.beginInterval),
          roundingMod: Number(interval.roundingMod),
        };
      }),
    };
  }
 
  /**
   * Serializes the rounding_intervals_v0 tlv into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
    dataWriter.writeUInt16BE(this.intervals.length);
 
    for (const interval of this.intervals) {
      dataWriter.writeBigSize(interval.beginInterval);
      dataWriter.writeBigSize(interval.roundingMod);
    }
 
    writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
 
    return writer.toBuffer();
  }
}
 
interface IInterval {
  beginInterval: bigint;
  roundingMod: bigint;
}
 
interface IIntervalJSON {
  beginInterval: number;
  roundingMod: number;
}
 
export interface IRoundingIntervalsV0JSON {
  type: number;
  intervals: IIntervalJSON[];
}