All files / lib/messages BatchFundingGroup.ts

96.3% Statements 52/54
100% Branches 0/0
77.78% Functions 7/9
96.08% Lines 49/51

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 1421x 1x   1x                                                       1x 1x             12x 12x   12x 12x   12x 12x 5x 5x     12x 12x 5x 5x     12x   12x 12x 54x 54x 54x     12x           15x       15x   15x       15x           2x                         19x 19x   19x   19x 19x 15x 15x     19x 19x 15x 15x     19x   19x 19x 77x 77x 77x     19x 19x   19x                      
import { Value } from '@node-dlc/bitcoin';
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
 
/**
 * The BatchFundingGroup TLV contains information about the intent to
 * enter multiple DLCs simulatenously within one batch dlc funding
 * transaction in the contract negotiation stage of the peer protocol
 *
 * This is the first step toward creating a batch dlc funding transaction
 *
 * A DlcOffer or DlcAccept can contain one or multiple BatchFundingInfo
 * TLVs to specify one or more groupings. This allows specification of
 * collateral put towards different types of contracts, such as options
 * contracts, futures contracts, or other investment types.
 *
 * Attributes:
 * - tempContractIds: Temporary identifiers for contracts proposed in DlcOffers.
 * - contractIds: Identifiers for contracts that have been accepted and are
 *   part of the funding transaction. These are derived from DlcOffers and DlcAccepts.
 * - allocatedCollateral: The amount of collateral allocated to the contracts
 *   within this group. This is specified early in the negotiation process.
 * - eventIds: Oracle event identifiers for the contracts in this group. These
 *   are also specified early in the negotiation process.
 *
 * Note: During the early stages of the negotiation protocol, only allocatedCollateral
 * and eventIds are specified. tempContractIds and contractIds are added to the
 * DlcAccept upon creation.
 */
export class BatchFundingGroup implements IDlcMessage {
  public static type = MessageType.BatchFundingGroup;
 
  /**
   * Deserializes a batch_contract_info message
   * @param buf
   */
  public static deserialize(buf: Buffer): BatchFundingGroup {
    const instance = new BatchFundingGroup();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize();
 
    const tempContractIdsCount = reader.readBigSize();
    for (let i = 0; i < Number(tempContractIdsCount); i++) {
      const length = reader.readBigSize();
      instance.tempContractIds.push(reader.readBytes(Number(length)));
    }
 
    const contractIdsCount = reader.readBigSize();
    for (let i = 0; i < Number(contractIdsCount); i++) {
      const length = reader.readBigSize();
      instance.contractIds.push(reader.readBytes(Number(length)));
    }
 
    instance.allocatedCollateral = Value.fromSats(reader.readUInt64BE());
 
    const eventIdsCount = reader.readBigSize();
    for (let i = 0; i < Number(eventIdsCount); i++) {
      const eventIdLength = reader.readBigSize();
      const eventIdBuf = reader.readBytes(Number(eventIdLength));
      instance.eventIds.push(eventIdBuf.toString());
    }
 
    return instance;
  }
 
  /**
   * The type for batch_contract_info message.
   */
  public type = BatchFundingGroup.type;
 
  public length: bigint;
 
  public tempContractIds: Buffer[] = [];
 
  public contractIds: Buffer[] = [];
 
  public allocatedCollateral: Value;
 
  public eventIds: string[] = [];
 
  /**
   * Converts batch_funding_info to JSON
   */
  public toJSON(): IBatchFundingGroupJSON {
    return {
      type: this.type,
      tempContractIds: this.tempContractIds.map((id) => id.toString('hex')),
      contractIds: this.contractIds.map((id) => id.toString('hex')),
      totalCollateral: Number(this.allocatedCollateral.sats),
      eventIds: this.eventIds,
    };
  }
 
  /**
   * Serializes the batch_funding_info message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
 
    dataWriter.writeBigSize(this.tempContractIds.length);
    this.tempContractIds.forEach((id) => {
      dataWriter.writeBigSize(id.length);
      dataWriter.writeBytes(id);
    });
 
    dataWriter.writeBigSize(this.contractIds.length);
    this.contractIds.forEach((id) => {
      dataWriter.writeBigSize(id.length);
      dataWriter.writeBytes(id);
    });
 
    dataWriter.writeUInt64BE(this.allocatedCollateral.sats);
 
    dataWriter.writeBigSize(this.eventIds.length);
    this.eventIds.forEach((id) => {
      const idBuffer = Buffer.from(id);
      dataWriter.writeBigSize(id.length);
      dataWriter.writeBytes(idBuffer);
    });
 
    writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
 
    return writer.toBuffer();
  }
}
 
export interface IBatchFundingGroupJSON {
  type: number;
  tempContractIds: string[];
  contractIds: string[];
  totalCollateral: number;
  eventIds: string[];
}