All files / lib/dlc/finance LinearPayout.ts

94.12% Statements 16/17
50% Branches 1/2
100% Functions 1/1
94.12% Lines 16/17

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 951x 1x   1x   1x                 90x         90x                 90x                       90x               90x     90x       90x                 90x                 90x                 90x           90x         1x  
import { PayoutFunction } from '@node-dlc/messaging';
import BN from 'bignumber.js';
 
import { PolynomialPayoutCurve } from '../PolynomialPayoutCurve';
 
const buildPayoutFunction = (
  minPayout: bigint,
  maxPayout: bigint,
  startOutcome: bigint,
  endOutcome: bigint,
  oracleBase: number,
  oracleDigits: number,
): { payoutFunction: PayoutFunction } => {
  // Max outcome limited by the oracle
  const maxOutcome = BigInt(
    new BN(oracleBase).pow(oracleDigits).minus(1).toString(10),
  );
 
  // max loss line
  const payoutCurveMaxLoss = new PolynomialPayoutCurve([
    { outcome: new BN(0), payout: new BN(Number(minPayout)) },
    {
      outcome: new BN(Number(startOutcome)),
      payout: new BN(Number(minPayout)),
    },
  ]);
 
  // payout line
  const payoutCurve = new PolynomialPayoutCurve([
    {
      outcome: new BN(Number(startOutcome)),
      payout: new BN(Number(minPayout)),
    },
    {
      outcome: new BN(Number(endOutcome)),
      payout: new BN(Number(maxPayout)),
    },
  ]);
 
  // max gain line
  const payoutCurveMaxGain = new PolynomialPayoutCurve([
    { outcome: new BN(Number(endOutcome)), payout: new BN(Number(maxPayout)) },
    {
      outcome: new BN(Number(maxOutcome)),
      payout: new BN(Number(maxPayout)),
    },
  ]);
 
  const payoutFunction = new PayoutFunction();
 
  // Defensive fix: ensure payoutFunctionPieces is initialized as an array
  Iif (!payoutFunction.payoutFunctionPieces) {
    payoutFunction.payoutFunctionPieces = [];
  }
 
  payoutFunction.payoutFunctionPieces.push({
    endPoint: {
      eventOutcome: startOutcome,
      outcomePayout: minPayout,
      extraPrecision: 0,
    },
    payoutCurvePiece: payoutCurveMaxLoss.toPayoutCurvePiece(),
  });
 
  payoutFunction.payoutFunctionPieces.push({
    endPoint: {
      eventOutcome: endOutcome,
      outcomePayout: maxPayout,
      extraPrecision: 0,
    },
    payoutCurvePiece: payoutCurve.toPayoutCurvePiece(),
  });
 
  payoutFunction.payoutFunctionPieces.push({
    endPoint: {
      eventOutcome: maxOutcome,
      outcomePayout: maxPayout,
      extraPrecision: 0,
    },
    payoutCurvePiece: payoutCurveMaxGain.toPayoutCurvePiece(),
  });
 
  payoutFunction.lastEndpoint = {
    eventOutcome: maxOutcome,
    outcomePayout: maxPayout,
    extraPrecision: 0,
  };
 
  return {
    payoutFunction,
  };
};
 
export const LinearPayout = { buildPayoutFunction };