@node-dlc/core
Version:
67 lines • 2.87 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoveredCall = void 0;
const messaging_1 = require("@node-dlc/messaging");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const BigIntUtils_1 = require("../../utils/BigIntUtils");
const HyperbolaPayoutCurve_1 = require("../HyperbolaPayoutCurve");
const buildCurve = (strikePrice, contractSize, oracleBase, oracleDigits) => {
const a = new bignumber_js_1.default(1);
const b = new bignumber_js_1.default(0);
const c = new bignumber_js_1.default(0);
const d = new bignumber_js_1.default((strikePrice * contractSize).toString());
const f_1 = new bignumber_js_1.default(0);
const _f_2 = new bignumber_js_1.default(0);
const _tempHyperbolaPayoutCurve = new HyperbolaPayoutCurve_1.HyperbolaPayoutCurve(a, b, c, d, f_1, _f_2);
const maxOutcome = BigInt(new bignumber_js_1.default(oracleBase).pow(oracleDigits).minus(1).toString(10));
const maxOutcomePayout = _tempHyperbolaPayoutCurve
.getPayout(maxOutcome)
.integerValue();
return {
maxOutcome,
totalCollateral: contractSize - (0, BigIntUtils_1.toBigInt)(maxOutcomePayout),
payoutCurve: new HyperbolaPayoutCurve_1.HyperbolaPayoutCurve(a, b, c, d, f_1, maxOutcomePayout.negated()),
};
};
const buildPayoutFunction = (strikePrice, contractSize, oracleBase, oracleDigits) => {
const { maxOutcome, totalCollateral, payoutCurve } = buildCurve(strikePrice, contractSize, oracleBase, oracleDigits);
const payoutFunction = new messaging_1.PayoutFunction();
// Defensive fix: ensure payoutFunctionPieces is initialized as an array
if (!payoutFunction.payoutFunctionPieces) {
payoutFunction.payoutFunctionPieces = [];
}
const curvePiece = payoutCurve.toPayoutCurvePiece();
// Set the left and right endpoints for the hyperbola piece (matching rust-dlc structure)
curvePiece.leftEndPoint = {
eventOutcome: BigInt(0),
outcomePayout: totalCollateral,
extraPrecision: 0,
};
curvePiece.rightEndPoint = {
eventOutcome: maxOutcome,
outcomePayout: BigInt(0),
extraPrecision: 0,
};
payoutFunction.payoutFunctionPieces.push({
endPoint: {
eventOutcome: maxOutcome,
outcomePayout: BigInt(0),
extraPrecision: 0,
},
payoutCurvePiece: curvePiece,
});
payoutFunction.lastEndpoint = {
eventOutcome: maxOutcome,
outcomePayout: BigInt(0),
extraPrecision: 0,
};
return {
payoutFunction,
totalCollateral,
};
};
exports.CoveredCall = { buildCurve, buildPayoutFunction };
//# sourceMappingURL=CoveredCall.js.map