All files / lib/messages PayoutFunction.ts

94.64% Statements 53/56
58.33% Branches 14/24
100% Functions 7/7
94.55% Lines 52/55

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 2431x   1x 1x   1x                     1x 1x               23x       23x 23x     44x                       44x 2x     2x     2x                 2x 2x               44x         23x 23x 23x                           23x               36x 36x     36x   36x   66x 66x 66x     66x 66x         66x 66x   66x                     36x           36x           67x   67x               67x             13x 24x                                       150x     150x   150x   281x 281x 281x     281x       150x 150x 150x   150x         1x                                                                      
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { bigIntToNumber, toBigInt } from '../util';
import { IDlcMessage } from './DlcMessage';
import {
  HyperbolaPayoutCurvePiece,
  HyperbolaPayoutCurvePieceJSON,
  PayoutCurvePiece,
  PolynomialPayoutCurvePieceJSON,
} from './PayoutCurvePiece';
 
/**
 * PayoutFunction contains the payout curve definition for numeric outcome contracts.
 * Updated to match rust-dlc format exactly.
 */
export class PayoutFunction implements IDlcMessage {
  public static type = MessageType.PayoutFunction;
 
  /**
   * Creates a PayoutFunction from JSON data
   * @param json JSON object representing a payout function
   */
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
  public static fromJSON(json: any): PayoutFunction {
    const instance = new PayoutFunction();
 
    // Parse payout function pieces
    const pieces =
      json.payoutFunctionPieces || json.payout_function_pieces || [];
    instance.payoutFunctionPieces = pieces.map(
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      (pieceJson: any, index: number) => {
        const piece = {
          endPoint: {
            eventOutcome: toBigInt(pieceJson.endPoint?.eventOutcome),
            outcomePayout: toBigInt(pieceJson.endPoint?.outcomePayout),
            extraPrecision: pieceJson.endPoint?.extraPrecision || 0,
          },
          payoutCurvePiece: PayoutCurvePiece.fromJSON(
            pieceJson.payoutCurvePiece || pieceJson.payout_curve_piece,
          ),
        };
 
        // For HyperbolaPayoutCurvePiece, set the left and right end points
        if (piece.payoutCurvePiece instanceof HyperbolaPayoutCurvePiece) {
          const hyperbola = piece.payoutCurvePiece as HyperbolaPayoutCurvePiece;
 
          // Left end point is this piece's endPoint
          hyperbola.leftEndPoint = piece.endPoint;
 
          // Right end point is the next piece's endPoint, or lastEndpoint if this is the last piece
          Iif (index < pieces.length - 1) {
            const nextPiece = pieces[index + 1];
            hyperbola.rightEndPoint = {
              eventOutcome: toBigInt(nextPiece.endPoint?.eventOutcome),
              outcomePayout: toBigInt(nextPiece.endPoint?.outcomePayout),
              extraPrecision: nextPiece.endPoint?.extraPrecision || 0,
            };
          } else {
            // Use lastEndpoint for the final piece
            const lastEndpoint = json.lastEndpoint || json.last_endpoint || {};
            hyperbola.rightEndPoint = {
              eventOutcome: toBigInt(lastEndpoint.eventOutcome),
              outcomePayout: toBigInt(lastEndpoint.outcomePayout),
              extraPrecision: lastEndpoint.extraPrecision || 0,
            };
          }
        }
 
        return piece;
      },
    );
 
    // Parse last endpoint
    const lastEndpoint = json.lastEndpoint || json.last_endpoint;
    Eif (lastEndpoint) {
      instance.lastEndpoint = {
        eventOutcome: toBigInt(lastEndpoint.eventOutcome),
        outcomePayout: toBigInt(lastEndpoint.outcomePayout),
        extraPrecision: lastEndpoint.extraPrecision || 0,
      };
    } else {
      // Default last endpoint if not provided
      instance.lastEndpoint = {
        eventOutcome: BigInt(0),
        outcomePayout: BigInt(0),
        extraPrecision: 0,
      };
    }
 
    return instance;
  }
 
  /**
   * Deserializes a payout_function message
   * @param buf
   */
  public static deserialize(buf: Buffer): PayoutFunction {
    const instance = new PayoutFunction();
    const reader = new BufferReader(buf);
 
    // Read payout function pieces (as vec)
    const numPieces = Number(reader.readBigSize());
 
    for (let i = 0; i < numPieces; i++) {
      // Read end_point first
      const eventOutcome = reader.readUInt64BE();
      const outcomePayout = reader.readUInt64BE();
      const extraPrecision = reader.readUInt16BE();
 
      // Read payout curve piece
      const payoutCurvePieceStartPos = reader.position;
      const payoutCurvePiece = PayoutCurvePiece.deserialize(
        reader.buffer.subarray(reader.position),
      );
 
      // Skip past the payout curve piece bytes
      const payoutCurvePieceSize = payoutCurvePiece.serialize().length;
      reader.position = payoutCurvePieceStartPos + payoutCurvePieceSize;
 
      instance.payoutFunctionPieces.push({
        endPoint: {
          eventOutcome,
          outcomePayout,
          extraPrecision,
        },
        payoutCurvePiece,
      });
    }
 
    // Read last_endpoint
    instance.lastEndpoint = {
      eventOutcome: reader.readUInt64BE(),
      outcomePayout: reader.readUInt64BE(),
      extraPrecision: reader.readUInt16BE(),
    };
 
    return instance;
  }
 
  /**
   * The type for payout_function message. payout_function = 42790
   */
  public type = PayoutFunction.type;
 
  public payoutFunctionPieces: IPayoutFunctionPiece[] = [];
  public lastEndpoint: IPayoutPoint;
 
  /**
   * Constructor that ensures proper initialization
   */
  constructor() {
    // Explicitly initialize arrays to handle file-linking issues
    this.payoutFunctionPieces = [];
  }
 
  /**
   * Converts payout_function to JSON
   */
  public toJSON(): PayoutFunctionJSON {
    return {
      payoutFunctionPieces: this.payoutFunctionPieces.map((piece) => ({
        endPoint: {
          eventOutcome: bigIntToNumber(piece.endPoint.eventOutcome),
          outcomePayout: bigIntToNumber(piece.endPoint.outcomePayout),
          extraPrecision: piece.endPoint.extraPrecision,
        },
        payoutCurvePiece: piece.payoutCurvePiece.toJSON(),
      })),
      lastEndpoint: {
        eventOutcome: bigIntToNumber(this.lastEndpoint.eventOutcome),
        outcomePayout: bigIntToNumber(this.lastEndpoint.outcomePayout),
        extraPrecision: this.lastEndpoint.extraPrecision,
      },
    };
  }
 
  /**
   * Serializes the payout_function message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
 
    // Write payout_function_pieces as vec (length + elements)
    writer.writeBigSize(this.payoutFunctionPieces.length);
 
    for (const piece of this.payoutFunctionPieces) {
      // Write end_point first (matches rust order)
      writer.writeUInt64BE(piece.endPoint.eventOutcome);
      writer.writeUInt64BE(piece.endPoint.outcomePayout);
      writer.writeUInt16BE(piece.endPoint.extraPrecision);
 
      // Write payout_curve_piece second
      writer.writeBytes(piece.payoutCurvePiece.serialize());
    }
 
    // Write last_endpoint
    writer.writeUInt64BE(this.lastEndpoint.eventOutcome);
    writer.writeUInt64BE(this.lastEndpoint.outcomePayout);
    writer.writeUInt16BE(this.lastEndpoint.extraPrecision);
 
    return writer.toBuffer();
  }
}
 
// Legacy support
export const PayoutFunctionV0 = PayoutFunction;
export type PayoutFunctionV0 = PayoutFunction;
 
interface IPayoutPoint {
  eventOutcome: bigint;
  outcomePayout: bigint;
  extraPrecision: number;
}
 
interface IPayoutPointJSON {
  eventOutcome: number;
  outcomePayout: number;
  extraPrecision: number;
}
 
interface IPayoutFunctionPiece {
  endPoint: IPayoutPoint;
  payoutCurvePiece: PayoutCurvePiece;
}
 
interface IPayoutFunctionPieceJSON {
  endPoint: IPayoutPointJSON;
  payoutCurvePiece:
    | PolynomialPayoutCurvePieceJSON
    | HyperbolaPayoutCurvePieceJSON;
}
 
export interface PayoutFunctionJSON {
  type?: number; // Optional for rust-dlc compatibility
  payoutFunctionPieces: IPayoutFunctionPieceJSON[];
  lastEndpoint: IPayoutPointJSON;
}
 
// Legacy interface
export type PayoutFunctionV0JSON = PayoutFunctionJSON;