All files / lib/messages OrderMetadata.ts

85.71% Statements 30/35
25% Branches 1/4
80% Functions 4/5
85.71% Lines 30/35

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 1201x   1x     1x                                                   1x 1x             1x 1x   1x 1x 1x 1x 1x   1x 1x 1x     1x           2x                             2x         2x           2x                       1x 1x   1x 1x 1x 1x 1x   1x 1x   1x                    
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
 
export abstract class OrderMetadata {
  public static deserialize(buf: Buffer): OrderMetadata {
    const reader = new BufferReader(buf);
 
    const type = Number(reader.readBigSize());
 
    switch (type) {
      case MessageType.OrderMetadataV0:
        return OrderMetadataV0.deserialize(buf);
      default:
        throw new Error(`Order metadata TLV type must be OrderMetadataV0`);
    }
  }
 
  public abstract type: number;
 
  public abstract toJSON(): IOrderMetadataJSON;
 
  public abstract serialize(): Buffer;
}
 
/**
 * OrderMetadata message contains information about a node and indicates its
 * desire to enter into a new contract. This is the first step toward
 * order negotiation.
 */
export class OrderMetadataV0 extends OrderMetadata implements IDlcMessage {
  public static type = MessageType.OrderMetadataV0;
 
  /**
   * Deserializes an offer_dlc_v0 message
   * @param buf
   */
  public static deserialize(buf: Buffer): OrderMetadataV0 {
    const instance = new OrderMetadataV0();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize();
    const offerIdLength = reader.readBigSize();
    const offerIdBuf = reader.readBytes(Number(offerIdLength));
    instance.offerId = offerIdBuf.toString();
 
    Eif (!reader.eof) {
      instance.createdAt = reader.readUInt32BE();
      instance.goodTill = reader.readUInt32BE();
    }
 
    return instance;
  }
 
  /**
   * The type for order_metadata_v0 message. order_metadata_v0 = 62774
   */
  public type = OrderMetadataV0.type;
 
  public length: bigint;
 
  /**
   * offerId is a unique identifier for an offer
   * It can be used for updating liquidity for a particular category
   * For example, how much liquidity is remaining for a particular strategy
   * which a market maker is providing liquidity for
   */
  public offerId: string;
 
  /**
   * Timestamp for order creation
   */
  public createdAt = 0;
 
  /**
   * Amount of time order is good untill
   */
  public goodTill = 0;
 
  /**
   * Converts order_metadata_v0 to JSON
   */
  public toJSON(): IOrderMetadataJSON {
    return {
      type: this.type,
      offerId: this.offerId,
      createdAt: this.createdAt,
      goodTill: this.goodTill,
    };
  }
 
  /**
   * Serializes the oracle_event message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
    dataWriter.writeBigSize(this.offerId.length);
    dataWriter.writeBytes(Buffer.from(this.offerId));
    dataWriter.writeUInt32BE(this.createdAt);
    dataWriter.writeUInt32BE(this.goodTill);
 
    writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
 
    return writer.toBuffer();
  }
}
 
export interface IOrderMetadataJSON {
  type: number;
  offerId: string;
  createdAt: number;
  goodTill: number;
}