UNPKG

@node-dlc/messaging

Version:
125 lines 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrderPositionInfoV0 = exports.OrderPositionInfo = void 0; const bufio_1 = require("@node-dlc/bufio"); const MessageType_1 = require("../MessageType"); class OrderPositionInfo { static deserialize(buf) { const reader = new bufio_1.BufferReader(buf); const type = Number(reader.readBigSize()); switch (type) { case MessageType_1.MessageType.OrderPositionInfoV0: return OrderPositionInfoV0.deserialize(buf); default: throw new Error(`Order cso info TLV type must be OrderPositionInfoV0`); } } } exports.OrderPositionInfo = OrderPositionInfo; /** * OrderCsoInfo message */ class OrderPositionInfoV0 extends OrderPositionInfo { constructor() { super(...arguments); /** * The type for order_metadata_v0 message. order_metadata_v0 = 62774 */ this.type = OrderPositionInfoV0.type; this.shiftForFees = 'neither'; this.fees = BigInt(0); this.instrumentName = undefined; this.contractSize = BigInt(0); this.direction = 'neither'; this.price = BigInt(0); // Can be BTC or USD depending on the instrument this.extraPrecision = 0; } /** * Deserializes an offer_dlc_v0 message * @param buf */ static deserialize(buf) { const instance = new OrderPositionInfoV0(); const reader = new bufio_1.BufferReader(buf); reader.readBigSize(); // read type instance.length = reader.readBigSize(); const encodedShiftForFees = reader.readUInt8(); if (encodedShiftForFees === 0) { instance.shiftForFees = 'neither'; } else if (encodedShiftForFees === 1) { instance.shiftForFees = 'offeror'; } else if (encodedShiftForFees === 2) { instance.shiftForFees = 'acceptor'; } else { throw new Error(`Invalid shift for fees value: ${encodedShiftForFees}`); } instance.fees = reader.readUInt64BE(); if (!reader.eof) { const instrumentNameLength = reader.readBigSize(); const instrumentName = reader.readBytes(Number(instrumentNameLength)); instance.instrumentName = instrumentName.toString(); instance.contractSize = reader.readUInt64BE(); const direction = reader.readUInt8(); if (direction === 0) { instance.direction = 'neither'; } else if (direction === 1) { instance.direction = 'buy'; } else if (direction === 2) { instance.direction = 'sell'; } else { throw new Error(`Invalid direction value: ${direction}`); } instance.price = reader.readUInt64BE(); instance.extraPrecision = reader.readUInt16BE(); } return instance; } /** * Converts order_metadata_v0 to JSON */ toJSON() { return { type: this.type, shiftForFees: this.shiftForFees, fees: Number(this.fees), instrumentName: this.instrumentName, direction: this.direction, price: Number(this.price), extraPrecision: this.extraPrecision, }; } /** * Serializes the oracle_event message into a Buffer */ serialize() { const writer = new bufio_1.BufferWriter(); writer.writeBigSize(this.type); const dataWriter = new bufio_1.BufferWriter(); dataWriter.writeUInt8(this.shiftForFees === 'neither' ? 0 : this.shiftForFees === 'offeror' ? 1 : 2); dataWriter.writeUInt64BE(this.fees); if (this.instrumentName) { dataWriter.writeBigSize(this.instrumentName.length); dataWriter.writeBytes(Buffer.from(this.instrumentName)); dataWriter.writeUInt64BE(this.contractSize); dataWriter.writeUInt8(this.direction === 'neither' ? 0 : this.direction === 'buy' ? 1 : 2); dataWriter.writeUInt64BE(this.price); dataWriter.writeUInt16BE(this.extraPrecision); } writer.writeBigSize(dataWriter.size); writer.writeBytes(dataWriter.toBuffer()); return writer.toBuffer(); } } exports.OrderPositionInfoV0 = OrderPositionInfoV0; OrderPositionInfoV0.type = MessageType_1.MessageType.OrderPositionInfoV0; //# sourceMappingURL=OrderPositionInfo.js.map