Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add long option payout curve support #196

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/core/__tests__/dlc/finance/Builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import sinon from 'sinon';
import {
buildCoveredCallOrderOffer,
buildCustomStrategyOrderOffer,
buildLongCallOrderOffer,
buildLongPutOrderOffer,
buildRoundingIntervalsFromIntervals,
buildShortPutOrderOffer,
computeRoundingModulus,
Expand Down Expand Up @@ -64,6 +66,36 @@ describe('OrderOffer Builder', () => {
expect(() => orderOffer.validate()).to.not.throw(Error);
});

it('should build a long call OrderOffer correctly', () => {
const orderOffer = buildLongCallOrderOffer(
oracleAnnouncement,
contractSize,
strikePrice,
premium * 3,
premium,
12,
10000,
'bitcoin',
);

expect(() => orderOffer.validate()).to.not.throw(Error);
});

it('should build a long put OrderOffer correctly', () => {
const orderOffer = buildLongPutOrderOffer(
oracleAnnouncement,
contractSize,
strikePrice,
premium * 3,
premium,
12,
10000,
'bitcoin',
);

expect(() => orderOffer.validate()).to.not.throw(Error);
});

it('should fail to build an OrderOffer with an invalid oracleAnnouncement', () => {
oracleAnnouncement.announcementSig = Buffer.from('deadbeef', 'hex');

Expand Down
55 changes: 55 additions & 0 deletions packages/core/__tests__/dlc/finance/LongCall.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { HyperbolaPayoutCurvePiece } from '@node-dlc/messaging';
import { expect } from 'chai';

import { LongCall } from '../../../lib/dlc/finance/LongCall';
import { HyperbolaPayoutCurve } from '../../../lib/dlc/HyperbolaPayoutCurve';

describe('LongCall', () => {
describe('1BTC-50k-base2-18digit curve', () => {
const strikePrice = BigInt(50000);
const contractSize = BigInt(10) ** BigInt(8);
const oracleBase = 2;
const oracleDigits = 18;

const { payoutCurve } = LongCall.buildCurve(
strikePrice,
contractSize,
oracleBase,
oracleDigits,
);

describe('payout', () => {
const priceIncrease = BigInt(1000);

it('should be positive as the position goes ITM', () => {
expect(
payoutCurve
.getPayout(strikePrice + priceIncrease)
.integerValue()
.toNumber(),
).to.equal(
Number(
(contractSize * priceIncrease) / (strikePrice + priceIncrease),
),
);
});

it('should be zero at strike price', () => {
expect(payoutCurve.getPayout(strikePrice).toNumber()).to.equal(0);
});
});

it('should serialize and deserialize properly', () => {
const payout = payoutCurve.getPayout(strikePrice);

const _tlv = payoutCurve.toPayoutCurvePiece().serialize();
const pf = HyperbolaPayoutCurvePiece.deserialize(_tlv);

const deserializedCurve = HyperbolaPayoutCurve.fromPayoutCurvePiece(pf);

expect(payout.toNumber()).to.be.eq(
deserializedCurve.getPayout(strikePrice).toNumber(),
);
});
});
});
55 changes: 55 additions & 0 deletions packages/core/__tests__/dlc/finance/LongPut.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { HyperbolaPayoutCurvePiece } from '@node-dlc/messaging';
import { expect } from 'chai';

import { LongPut } from '../../../lib';
import { HyperbolaPayoutCurve } from '../../../lib/dlc/HyperbolaPayoutCurve';

describe('LongPut', () => {
describe('1BTC-50k-base2-18digit curve', () => {
const strikePrice = BigInt(50000);
const contractSize = BigInt(10) ** BigInt(8);
const oracleBase = 2;
const oracleDigits = 18;

const { payoutCurve } = LongPut.buildCurve(
strikePrice,
contractSize,
oracleBase,
oracleDigits,
);

describe('payout', () => {
const priceDecrease = BigInt(1000);

it('should be positive as the position goes ITM', () => {
expect(
payoutCurve
.getPayout(strikePrice - priceDecrease)
.integerValue()
.toNumber(),
).to.equal(
Number(
(contractSize * priceDecrease) / (strikePrice - priceDecrease),
),
);
});

it('should be zero at strike price', () => {
expect(payoutCurve.getPayout(strikePrice).toNumber()).to.equal(0);
});
});

it('should serialize and deserialize properly', () => {
const payout = payoutCurve.getPayout(strikePrice);

const _tlv = payoutCurve.toPayoutCurvePiece().serialize();
const pf = HyperbolaPayoutCurvePiece.deserialize(_tlv);

const deserializedCurve = HyperbolaPayoutCurve.fromPayoutCurvePiece(pf);

expect(payout.toNumber()).to.be.eq(
deserializedCurve.getPayout(strikePrice).toNumber(),
);
});
});
});
Loading
Loading