# All files / lib/messages OrderPositionInfo.ts

91.23% Statements 52/57
66.67% Branches 16/24
100% Functions 4/4
91.23% Lines 52/57

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
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    1481x

    1x

    1x
    1x

    5x
    5x

    5x
    5x

    5x
    5x

    5x
    2x
    3x
    2x

    1x

    4x

    4x
    1x
    1x
    1x

    1x

    1x
    1x

    1x
    1x

    1x

    1x

    4x

    13x

    13x

    13x

    13x

    13x

    13x

    13x

    13x

    3x

    6x
    6x

    6x
    6x

    6x

    6x
    1x
    1x

    1x

    1x

    1x

    1x

    6x
    6x

    6x

     import { BufferReader, BufferWriter } from '@node-dlc/bufio';

    import { MessageType } from '../MessageType';
    import { IDlcMessage } from './DlcMessage';

    export type DlcParty = 'offeror' | 'acceptor' | 'neither';

    /**
     * OrderPositionInfo message
     */
    export class OrderPositionInfo implements IDlcMessage {
      public static type = MessageType.OrderPositionInfo;

      /**
       * Deserializes an OrderPositionInfo message
       * @param buf
       */
      public static deserialize(buf: Buffer): OrderPositionInfo {
        const instance = new OrderPositionInfo();
        const reader = new BufferReader(buf);

        reader.readBigSize(); // read type
        instance.length = reader.readBigSize();

        const encodedShiftForFees = reader.readUInt8();
        Iif (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();
          Iif (direction === 0) {
            instance.direction = 'neither';
          } else Eif (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;
      }

      /**
       * The type for OrderPositionInfo message
       */
      public type = OrderPositionInfo.type;

      public length: bigint;

      public shiftForFees: DlcParty = 'neither';

      public fees = BigInt(0);

      public instrumentName: string | undefined = undefined;

      public contractSize = BigInt(0);

      public direction: 'buy' | 'sell' | 'neither' = 'neither';

      public price = BigInt(0); // Can be BTC or USD depending on the instrument

      public extraPrecision = 0;

      /**
       * Converts OrderPositionInfo to JSON
       */
      public toJSON(): IOrderPositionInfoJSON {
        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 OrderPositionInfo message into a Buffer
       */
      public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeBigSize(this.type);

        const dataWriter = new 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();
      }
    }

    export interface IOrderPositionInfoJSON {
      type: number;
      shiftForFees: string;
      fees: number;
      instrumentName: string;
      direction: string;
      price: number;
      extraPrecision: number;
    }