All files / lib/dlc/finance LinearPayout.ts

100% Statements 17/17
100% Branches 0/0
100% Functions 1/1
100% Lines 17/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 811x 1x   1x   1x                 90x         90x                 90x                       90x               90x 90x 90x 90x   90x             90x             90x             90x         1x  
import { PayoutFunctionV0 } 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: PayoutFunctionV0 } => {
  // 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 PayoutFunctionV0();
  payoutFunction.endpoint0 = BigInt(0);
  payoutFunction.endpointPayout0 = minPayout;
  payoutFunction.extraPrecision0 = 0;
 
  payoutFunction.pieces.push({
    payoutCurvePiece: payoutCurveMaxLoss.toPayoutCurvePiece(),
    endpoint: startOutcome,
    endpointPayout: minPayout,
    extraPrecision: 0,
  });
 
  payoutFunction.pieces.push({
    payoutCurvePiece: payoutCurve.toPayoutCurvePiece(),
    endpoint: endOutcome,
    endpointPayout: maxPayout,
    extraPrecision: 0,
  });
 
  payoutFunction.pieces.push({
    payoutCurvePiece: payoutCurveMaxGain.toPayoutCurvePiece(),
    endpoint: maxOutcome,
    endpointPayout: maxPayout,
    extraPrecision: 0,
  });
 
  return {
    payoutFunction,
  };
};
 
export const LinearPayout = { buildPayoutFunction };