UNPKG

@node-dlc/messaging

Version:
103 lines 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PayoutFunctionV0 = exports.PayoutFunction = void 0; const bufio_1 = require("@node-dlc/bufio"); const MessageType_1 = require("../MessageType"); const getTlv_1 = require("../serialize/getTlv"); const PayoutCurvePiece_1 = require("./PayoutCurvePiece"); class PayoutFunction { static deserialize(buf) { const reader = new bufio_1.BufferReader(buf); const type = Number(reader.readBigSize()); switch (type) { case MessageType_1.MessageType.PayoutFunctionV0: return PayoutFunctionV0.deserialize(buf); default: throw new Error(`Payout function TLV type must be PayoutFunctionV0`); } } } exports.PayoutFunction = PayoutFunction; /** * PayoutFunction V0 */ class PayoutFunctionV0 extends PayoutFunction { constructor() { super(...arguments); /** * The type for payout_function_v0 message. payout_function_v0 = 42790 */ this.type = PayoutFunctionV0.type; this.pieces = []; } /** * Deserializes an payout_function_v0 message * @param buf */ static deserialize(buf) { const instance = new PayoutFunctionV0(); const reader = new bufio_1.BufferReader(buf); reader.readBigSize(); // read type instance.length = reader.readBigSize(); // need to fix this reader.readUInt16BE(); // num_pieces instance.endpoint0 = reader.readBigSize(); instance.endpointPayout0 = reader.readBigSize(); instance.extraPrecision0 = reader.readUInt16BE(); while (!reader.eof) { const payoutCurvePiece = PayoutCurvePiece_1.PayoutCurvePiece.deserialize((0, getTlv_1.getTlv)(reader)); const endpoint = reader.readBigSize(); const endpointPayout = reader.readBigSize(); const extraPrecision = reader.readUInt16BE(); instance.pieces.push({ payoutCurvePiece, endpoint, endpointPayout, extraPrecision, }); } return instance; } /** * Converts payout_function_v0 to JSON */ toJSON() { return { type: this.type, endpoint0: Number(this.endpoint0), endpointPayout0: Number(this.endpointPayout0), extraPrecision0: this.extraPrecision0, pieces: this.pieces.map((piece) => { return { payoutCurvePiece: piece.payoutCurvePiece.toJSON(), endpoint: Number(piece.endpoint), endpointPayout: Number(piece.endpointPayout), extraPrecision: piece.extraPrecision, }; }), }; } /** * Serializes the payout_function_v0 message into a Buffer */ serialize() { const writer = new bufio_1.BufferWriter(); writer.writeBigSize(this.type); const dataWriter = new bufio_1.BufferWriter(); dataWriter.writeUInt16BE(this.pieces.length); dataWriter.writeBigSize(this.endpoint0); dataWriter.writeBigSize(this.endpointPayout0); dataWriter.writeUInt16BE(this.extraPrecision0); for (const piece of this.pieces) { dataWriter.writeBytes(piece.payoutCurvePiece.serialize()); dataWriter.writeBigSize(piece.endpoint); dataWriter.writeBigSize(piece.endpointPayout); dataWriter.writeUInt16BE(piece.extraPrecision); } writer.writeBigSize(dataWriter.size); writer.writeBytes(dataWriter.toBuffer()); return writer.toBuffer(); } } exports.PayoutFunctionV0 = PayoutFunctionV0; PayoutFunctionV0.type = MessageType_1.MessageType.PayoutFunctionV0; //# sourceMappingURL=PayoutFunction.js.map