@node-dlc/core
Version:
76 lines (63 loc) • 1.72 kB
text/typescript
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.payoutFunctionPieces.push({
endPoint: {
eventOutcome: maxOutcome,
outcomePayout: totalCollateral,
extraPrecision: 0,
},
payoutCurvePiece: payoutCurve.toPayoutCurvePiece(),
});
payoutFunction.lastEndpoint = {
eventOutcome: maxOutcome,
outcomePayout: totalCollateral,
extraPrecision: 0,
};
return {
payoutFunction,
};
};
export const ShortPut = { buildCurve, buildPayoutFunction };