# All files / lib/messages NegotiationFields.ts

87.18% Statements 68/78
58.33% Branches 14/24
100% Functions 14/14
87.01% Lines 67/77

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
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    2571x

    1x

    1x

    7x
    7x

    7x

    5x

    2x

    4x

    4x

    4x

    3x

    1x

    1x

    4x

    4x

    4x

    4x

    4x

    5x
    5x

    5x

    5x
    5x

    5x

    19x
    19x

    2x

    8x
    8x
    8x

    8x

    1x

    2x

    2x

    2x

    2x
    2x

    2x

    2x
    2x

    2x
    2x

    2x

    3x
    3x

    3x

    3x

    3x
    3x
    3x

    3x

    2x

    8x
    8x
    8x

    1x

    1x

    3x
    3x
    3x

    3x
    5x

    3x

    1x
    1x
    1x

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

    import { RoundingIntervals } from './RoundingIntervals';
    import { IRoundingIntervalsJSON } from './RoundingIntervals';

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

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

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

        const variant = json.variant;

        switch (variant) {
          case 'Single':
            return SingleNegotiationFields.fromJSON(json);
          case 'Disjoint':
            return DisjointNegotiationFields.fromJSON(json);
          default:
            throw new Error(
              `Unknown 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(): INegotiationFieldsJSON;
    }

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

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

        Iif (!json.roundingIntervals) {
          throw new Error(
            'SingleNegotiationFields requires roundingIntervals field',
          );
        }

        instance.roundingIntervals = RoundingIntervals.fromJSON(
          json.roundingIntervals,
        );

        return instance;
      }

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

        reader.readBigSize(); // read discriminator (0)

        // Read remaining bytes as raw RoundingIntervals data
        const remainingBytes = reader.readBytes();
        instance.roundingIntervals = RoundingIntervals.deserialize(remainingBytes);

        return instance;
      }

      public variant = 'Single' as const;
      public discriminator = 0;
      public roundingIntervals: RoundingIntervals;

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

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

        return writer.toBuffer();
      }
    }

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

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

        Iif (!json.negotiationFields || !Array.isArray(json.negotiationFields)) {
          throw new Error(
            'DisjointNegotiationFields requires negotiationFields array',
          );
        }

        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        instance.negotiationFields = json.negotiationFields.map((fieldJson: any) =>
          NegotiationFields.fromJSON(fieldJson),
        );

        return instance;
      }

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

        reader.readBigSize(); // read discriminator (1)
        const numFields = Number(reader.readBigSize());

        for (let i = 0; i < numFields; i++) {
          // For simplicity, let's read the nested field by looking ahead
          // to determine its length based on its discriminator
          const startPos = reader.position;
          const discriminator = Number(reader.readBigSize());

          Eif (discriminator === 0) {
            // Single field: discriminator + RoundingIntervals data
            // RoundingIntervals has its own length, so we need to parse it
            const roundingIntervals = RoundingIntervals.deserialize(
              reader.readBytes(),
            );

            // Reset and read the complete field
            reader.position = startPos;
            const fieldLength = 1 + roundingIntervals.serialize().length; // discriminator + data length
            const fieldData = reader.readBytes(fieldLength);

            instance.negotiationFields.push(
              NegotiationFields.deserialize(fieldData),
            );
          } else if (discriminator === 1) {
            throw new Error('Nested disjoint fields not yet supported');
          } else {
            throw new Error(`Unknown discriminator: ${discriminator}`);
          }
        }

        return instance;
      }

      public variant = 'Disjoint' as const;
      public discriminator = 1;
      public negotiationFields: NegotiationFields[] = [];

      /**
       * Converts disjoint negotiation fields to JSON
       */
      public toJSON(): IDisjointNegotiationFieldsJSON {
        return {
          variant: this.variant,
          negotiationFields: this.negotiationFields.map((field) => field.toJSON()),
        };
      }

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

        for (const negotiationField of this.negotiationFields) {
          writer.writeBytes(negotiationField.serialize());
        }

        return writer.toBuffer();
      }
    }

    export type INegotiationFieldsJSON =
      | ISingleNegotiationFieldsJSON
      | IDisjointNegotiationFieldsJSON;

    export interface ISingleNegotiationFieldsJSON {
      variant: 'Single';
      roundingIntervals: IRoundingIntervalsJSON;
    }

    export interface IDisjointNegotiationFieldsJSON {
      variant: 'Disjoint';
      negotiationFields: INegotiationFieldsJSON[];
    }

    // Legacy exports for backward compatibility - map to new structure
    export const NegotiationFieldsV0 = SingleNegotiationFields; // V0 was empty, now maps to Single
    export const NegotiationFieldsV1 = SingleNegotiationFields; // V1 had rounding intervals
    export const NegotiationFieldsV2 = DisjointNegotiationFields; // V2 had list of fields

    export type INegotiationFieldsV0JSON = ISingleNegotiationFieldsJSON;
    export type INegotiationFieldsV1JSON = ISingleNegotiationFieldsJSON;
    export type INegotiationFieldsV2JSON = IDisjointNegotiationFieldsJSON;