# All files / lib/messages OrderNegotiationFields.ts

87.72% Statements 50/57
55.56% Branches 10/18
100% Functions 12/12
87.72% Lines 50/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
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    2111x

    1x

    1x

    6x
    6x

    6x

    5x

    1x

    2x

    2x

    2x

    1x

    1x

    1x

    1x

    1x

    1x

    5x
    5x

    5x

    5x

    10x
    10x

    1x

    10x
    10x

    10x

    1x

    1x

    1x

    1x
    1x

    1x
    1x

    1x
    1x
    1x
    1x

    1x

    5x
    5x

    1x

    2x
    2x

    2x
    2x
    2x

    2x

    1x
    1x

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

    import { IOrderOfferJSON, OrderOffer } from './OrderOffer';

    /**
     * Order negotiation fields for order contract negotiation.
     * Follows the same pattern as NegotiationFields with Single and Disjoint variants.
     */
    export abstract class OrderNegotiationFields {
      public static deserialize(buf: Buffer): OrderNegotiationFields {
        const reader = new BufferReader(buf);
        const discriminator = Number(reader.readBigSize());

        switch (discriminator) {
          case 0:
            return SingleOrderNegotiationFields.deserialize(buf);
          case 1:
            return DisjointOrderNegotiationFields.deserialize(buf);
          default:
            throw new Error(
              `Invalid OrderNegotiationFields discriminator: ${discriminator}. Must be 0 (Single) or 1 (Disjoint)`,
            );
        }
      }

      /**
       * Creates OrderNegotiationFields from JSON data
       * @param json JSON object representing order negotiation fields
       */
      // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
      public static fromJSON(json: any): OrderNegotiationFields {
        Iif (!json || typeof json !== 'object') {
          throw new Error('Invalid JSON input for OrderNegotiationFields');
        }

        const variant = json.variant;

        switch (variant) {
          case 'Single':
            return SingleOrderNegotiationFields.fromJSON(json);
          case 'Disjoint':
            return DisjointOrderNegotiationFields.fromJSON(json);
          default:
            throw new Error(
              `Unknown order negotiation fields variant: ${variant}. Must be 'Single' or 'Disjoint'`,
            );
        }
      }

      public abstract variant: 'Single' | 'Disjoint';
      public abstract discriminator: number;
      public abstract serialize(): Buffer;
      public abstract toJSON(): IOrderNegotiationFieldsJSON;
    }

    /**
     * Order negotiation fields for contract based on a single event (basic/empty).
     */
    export class SingleOrderNegotiationFields extends OrderNegotiationFields {
      /**
       * Creates a SingleOrderNegotiationFields from JSON data
       * @param json JSON object representing single order negotiation fields
       */
      // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
      public static fromJSON(json: any): SingleOrderNegotiationFields {
        const instance = new SingleOrderNegotiationFields();

        Iif (json.variant !== 'Single') {
          throw new Error(
            `Invalid variant for SingleOrderNegotiationFields: expected 'Single', got ${json.variant}`,
          );
        }

        // Single order negotiation fields are currently empty/basic
        return instance;
      }

      /**
       * Deserializes single order negotiation fields
       * @param buf
       */
      public static deserialize(buf: Buffer): SingleOrderNegotiationFields {
        const instance = new SingleOrderNegotiationFields();
        const reader = new BufferReader(buf);

        reader.readBigSize(); // read discriminator (0)
        // Single order negotiation fields are currently empty

        return instance;
      }

      public variant = 'Single' as const;
      public discriminator = 0;

      /**
       * Converts single order negotiation fields to JSON
       */
      public toJSON(): ISingleOrderNegotiationFieldsJSON {
        return {
          variant: this.variant,
        };
      }

      /**
       * Serializes the single order negotiation fields into a Buffer
       */
      public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeBigSize(this.discriminator);
        // Single order negotiation fields are currently empty

        return writer.toBuffer();
      }
    }

    /**
     * Order negotiation fields for contract based on multiple events.
     */
    export class DisjointOrderNegotiationFields extends OrderNegotiationFields {
      /**
       * Creates a DisjointOrderNegotiationFields from JSON data
       * @param json JSON object representing disjoint order negotiation fields
       */
      // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
      public static fromJSON(json: any): DisjointOrderNegotiationFields {
        const instance = new DisjointOrderNegotiationFields();

        Iif (json.variant !== 'Disjoint') {
          throw new Error(
            `Invalid variant for DisjointOrderNegotiationFields: expected 'Disjoint', got ${json.variant}`,
          );
        }

        Eif (!json.orderOffer) {
          throw new Error(
            'DisjointOrderNegotiationFields requires orderOffer field',
          );
        }

        instance.orderOffer = OrderOffer.fromJSON
          ? OrderOffer.fromJSON(json.orderOffer)
          : OrderOffer.deserialize(Buffer.from(json.orderOffer, 'hex'));

        return instance;
      }

      /**
       * Deserializes disjoint order negotiation fields
       * @param buf
       */
      public static deserialize(buf: Buffer): DisjointOrderNegotiationFields {
        const instance = new DisjointOrderNegotiationFields();
        const reader = new BufferReader(buf);

        reader.readBigSize(); // read discriminator (1)
        const orderOfferLength = reader.readBigSize();
        const orderOfferBuf = reader.readBytes(Number(orderOfferLength));
        instance.orderOffer = OrderOffer.deserialize(orderOfferBuf);

        return instance;
      }

      public variant = 'Disjoint' as const;
      public discriminator = 1;
      public orderOffer: OrderOffer;

      /**
       * Converts disjoint order negotiation fields to JSON
       */
      public toJSON(): IDisjointOrderNegotiationFieldsJSON {
        return {
          variant: this.variant,
          orderOffer: this.orderOffer.toJSON(),
        };
      }

      /**
       * Serializes the disjoint order negotiation fields into a Buffer
       */
      public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeBigSize(this.discriminator);

        const orderOfferData = this.orderOffer.serialize();
        writer.writeBigSize(orderOfferData.length);
        writer.writeBytes(orderOfferData);

        return writer.toBuffer();
      }
    }

    export type IOrderNegotiationFieldsJSON =
      | ISingleOrderNegotiationFieldsJSON
      | IDisjointOrderNegotiationFieldsJSON;

    export interface ISingleOrderNegotiationFieldsJSON {
      variant: 'Single';
    }

    export interface IDisjointOrderNegotiationFieldsJSON {
      variant: 'Disjoint';
      orderOffer: IOrderOfferJSON;
    }

    // Legacy exports for backward compatibility - map to new structure
    export const OrderNegotiationFieldsV0 = SingleOrderNegotiationFields; // V0 was basic/empty
    export const OrderNegotiationFieldsV1 = DisjointOrderNegotiationFields; // V1 had OrderOffer

    export type IOrderNegotiationFieldsV0JSON = ISingleOrderNegotiationFieldsJSON;
    export type IOrderNegotiationFieldsV1JSON = IDisjointOrderNegotiationFieldsJSON;