All files / lib/dlc/finance ShortPut.ts

100% Statements 22/22
100% Branches 0/0
100% Functions 2/2
100% Lines 22/22

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 711x 1x   1x   1x                     10x 10x 10x 10x   10x 10x   10x   10x       10x             1x             4x               4x 4x 4x 4x   4x             4x         1x  
import { PayoutFunctionV0 } from '@node-dlc/messaging';
import BN from 'bignumber.js';
 
import { HyperbolaPayoutCurve } from '../HyperbolaPayoutCurve';
 
const buildCurve = (
  strikePrice: bigint,
  contractSize: bigint,
  totalCollateral: bigint,
  oracleBase: number,
  oracleDigits: number,
): {
  maxOutcome: bigint;
  totalCollateral: bigint;
  payoutCurve: HyperbolaPayoutCurve;
} => {
  const a = new BN(-1);
  const b = new BN(0);
  const c = new BN(0);
  const d = new BN((strikePrice * contractSize).toString());
 
  const f_1 = new BN(0);
  const f_2 = new BN(Number(contractSize)).plus(Number(totalCollateral));
 
  const payoutCurve = new HyperbolaPayoutCurve(a, b, c, d, f_1, f_2);
 
  const maxOutcome = BigInt(
    new BN(oracleBase).pow(oracleDigits).minus(1).toString(10),
  );
 
  return {
    maxOutcome,
    totalCollateral,
    payoutCurve,
  };
};
 
const buildPayoutFunction = (
  strikePrice: bigint,
  contractSize: bigint,
  totalCollateral: bigint,
  oracleBase: number,
  oracleDigits: number,
): { payoutFunction: PayoutFunctionV0 } => {
  const { maxOutcome, payoutCurve } = buildCurve(
    strikePrice,
    contractSize,
    totalCollateral,
    oracleBase,
    oracleDigits,
  );
 
  const payoutFunction = new PayoutFunctionV0();
  payoutFunction.endpoint0 = BigInt(0);
  payoutFunction.endpointPayout0 = BigInt(0);
  payoutFunction.extraPrecision0 = 0;
 
  payoutFunction.pieces.push({
    payoutCurvePiece: payoutCurve.toPayoutCurvePiece(),
    endpoint: maxOutcome,
    endpointPayout: totalCollateral,
    extraPrecision: 0,
  });
 
  return {
    payoutFunction,
  };
};
 
export const ShortPut = { buildCurve, buildPayoutFunction };