UNPKG

@node-dlc/messaging

Version:
96 lines 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RoundingIntervals = void 0; const bufio_1 = require("@node-dlc/bufio"); const MessageType_1 = require("../MessageType"); const util_1 = require("../util"); /** * RoundingIntervals defines rounding intervals for numeric outcome contracts. * Updated to match dlcspecs format (no longer uses TLV). */ class RoundingIntervals { constructor() { /** * The type for rounding_intervals message. rounding_intervals = 42788 */ this.type = RoundingIntervals.type; this.intervals = []; } /** * Creates a RoundingIntervals from JSON data * @param json JSON object representing rounding intervals */ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any static fromJSON(json) { const instance = new RoundingIntervals(); const intervals = json.intervals || []; // eslint-disable-next-line @typescript-eslint/no-explicit-any instance.intervals = intervals.map((interval) => ({ beginInterval: (0, util_1.toBigInt)(interval.beginInterval || interval.begin_interval), roundingMod: (0, util_1.toBigInt)(interval.roundingMod || interval.rounding_mod), })); return instance; } /** * Deserializes a rounding_intervals message * @param buf */ static deserialize(buf) { const instance = new RoundingIntervals(); const reader = new bufio_1.BufferReader(buf); const numRoundingIntervals = Number(reader.readBigSize()); for (let i = 0; i < numRoundingIntervals; i++) { const beginInterval = reader.readUInt64BE(); const roundingMod = reader.readUInt64BE(); instance.intervals.push({ beginInterval, roundingMod }); } return instance; } /** * 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 */ validate() { // 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 to JSON */ toJSON() { return { intervals: this.intervals.map((interval) => { return { beginInterval: Number(interval.beginInterval), roundingMod: Number(interval.roundingMod), }; }), }; } /** * Serializes the rounding_intervals message into a Buffer */ serialize() { const writer = new bufio_1.BufferWriter(); writer.writeBigSize(this.intervals.length); for (const interval of this.intervals) { writer.writeUInt64BE(interval.beginInterval); writer.writeUInt64BE(interval.roundingMod); } return writer.toBuffer(); } } exports.RoundingIntervals = RoundingIntervals; RoundingIntervals.type = MessageType_1.MessageType.RoundingIntervals; //# sourceMappingURL=RoundingIntervals.js.map