@atomicfinance/bitcoin-wallet-provider 4.2.8 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -4
- package/.turbo/turbo-lint$colon$fix.log +4 -0
- package/.turbo/turbo-lint.log +5 -0
- package/.turbo/turbo-test.log +0 -0
- package/.yalc/@node-dlc/core/.nyc_output/687a3c14-9765-45fd-a1fe-f7db3d56fee4.json +1 -0
- package/.yalc/@node-dlc/core/.nyc_output/processinfo/687a3c14-9765-45fd-a1fe-f7db3d56fee4.json +1 -0
- package/.yalc/@node-dlc/core/.nyc_output/processinfo/index.json +1 -0
- package/.yalc/@node-dlc/core/README.md +11 -0
- package/.yalc/@node-dlc/core/__tests__/core.spec.ts +7 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/CETCalculator.spec.ts +1029 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/CoinSelect.spec.ts +179 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/PolynomialPayoutCurve.spec.ts +345 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/TxBuilder.spec.ts +424 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/finance/Builder.spec.ts +492 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/finance/CoveredCall.spec.ts +112 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/finance/CsoInfo.spec.ts +1124 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/finance/LongCall.spec.ts +55 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/finance/LongPut.spec.ts +55 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/finance/OptionInfo.spec.ts +226 -0
- package/.yalc/@node-dlc/core/__tests__/dlc/finance/ShortPut.spec.ts +62 -0
- package/.yalc/@node-dlc/core/__tests__/tsconfig.json +8 -0
- package/.yalc/@node-dlc/core/__tests__/utils/precision.spec.ts +40 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/base.css +224 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/block-navigation.js +79 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/favicon.png +0 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/index.html +171 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/AsyncProcessingQueue.ts.html +269 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/Base32.ts.html +86 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/BigIntUtils.ts.html +86 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/BitField.ts.html +86 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ChannelId.ts.html +86 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/LinkedList.ts.html +413 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/LinkedListNode.ts.html +122 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/Queue.ts.html +188 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ShortChannelId.ts.html +86 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ShortChannelIdUtils.ts.html +107 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/CETCalculator.ts.html +1430 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/CoinSelect.ts.html +431 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/HyperbolaPayoutCurve.ts.html +644 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/PolynomialPayoutCurve.ts.html +878 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/TxBuilder.ts.html +1088 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/TxFinalizer.ts.html +731 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/Builder.ts.html +2270 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/CoveredCall.ts.html +410 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/CsoInfo.ts.html +1574 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LinearPayout.ts.html +362 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LongCall.ts.html +302 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LongPut.ts.html +302 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/OptionInfo.ts.html +626 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/ShortPut.ts.html +305 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/index.html +216 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/index.html +186 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/index.html +261 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/index.ts.html +278 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ChannelId.ts.html +242 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ChannelKeys.ts.html +407 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentNumber.ts.html +362 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentSecret.ts.html +203 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentSecretStore.ts.html +332 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/Htlc.ts.html +236 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/HtlcDirection.ts.html +131 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ScriptFactory.ts.html +530 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/TxFactory.ts.html +1094 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/index.html +231 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/BigIntUtils.ts.html +272 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/Precision.ts.html +149 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/index.html +126 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/prettify.css +1 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/prettify.js +2 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/.yalc/@node-dlc/core/coverage/lcov-report/sorter.js +170 -0
- package/.yalc/@node-dlc/core/coverage/lcov.info +2426 -0
- package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.d.ts +22 -0
- package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.js +55 -0
- package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/Base32.d.ts +1 -0
- package/.yalc/@node-dlc/core/dist/Base32.js +7 -0
- package/.yalc/@node-dlc/core/dist/Base32.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/BigIntUtils.d.ts +1 -0
- package/.yalc/@node-dlc/core/dist/BigIntUtils.js +8 -0
- package/.yalc/@node-dlc/core/dist/BigIntUtils.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/BitField.d.ts +1 -0
- package/.yalc/@node-dlc/core/dist/BitField.js +7 -0
- package/.yalc/@node-dlc/core/dist/BitField.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/ChannelId.d.ts +1 -0
- package/.yalc/@node-dlc/core/dist/ChannelId.js +7 -0
- package/.yalc/@node-dlc/core/dist/ChannelId.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/LinkedList.d.ts +30 -0
- package/.yalc/@node-dlc/core/dist/LinkedList.js +104 -0
- package/.yalc/@node-dlc/core/dist/LinkedList.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/LinkedListNode.d.ts +9 -0
- package/.yalc/@node-dlc/core/dist/LinkedListNode.js +15 -0
- package/.yalc/@node-dlc/core/dist/LinkedListNode.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/Queue.d.ts +22 -0
- package/.yalc/@node-dlc/core/dist/Queue.js +38 -0
- package/.yalc/@node-dlc/core/dist/Queue.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/ShortChannelId.d.ts +1 -0
- package/.yalc/@node-dlc/core/dist/ShortChannelId.js +7 -0
- package/.yalc/@node-dlc/core/dist/ShortChannelId.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.d.ts +1 -0
- package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.js +12 -0
- package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.d.ts +50 -0
- package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.js +332 -0
- package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.d.ts +23 -0
- package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.js +77 -0
- package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.d.ts +20 -0
- package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.js +117 -0
- package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.d.ts +5 -0
- package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.js +3 -0
- package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.d.ts +52 -0
- package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.js +173 -0
- package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.d.ts +55 -0
- package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.js +217 -0
- package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.d.ts +40 -0
- package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.js +146 -0
- package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.d.ts +193 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.js +422 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.d.ts +13 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.js +67 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.d.ts +114 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.js +290 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.d.ts +6 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.js +79 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.d.ts +13 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.js +47 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.d.ts +13 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.js +47 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.d.ts +20 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.js +101 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.d.ts +12 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.js +46 -0
- package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/index.d.ts +39 -0
- package/.yalc/@node-dlc/core/dist/index.js +78 -0
- package/.yalc/@node-dlc/core/dist/index.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/ChannelId.d.ts +31 -0
- package/.yalc/@node-dlc/core/dist/lightning/ChannelId.js +52 -0
- package/.yalc/@node-dlc/core/dist/lightning/ChannelId.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.d.ts +55 -0
- package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.js +105 -0
- package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.d.ts +55 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.js +76 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.d.ts +26 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.js +44 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.d.ts +41 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.js +79 -0
- package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/Htlc.d.ts +46 -0
- package/.yalc/@node-dlc/core/dist/lightning/Htlc.js +46 -0
- package/.yalc/@node-dlc/core/dist/lightning/Htlc.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.d.ts +16 -0
- package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.js +21 -0
- package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.d.ts +76 -0
- package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.js +102 -0
- package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/lightning/TxFactory.d.ts +83 -0
- package/.yalc/@node-dlc/core/dist/lightning/TxFactory.js +222 -0
- package/.yalc/@node-dlc/core/dist/lightning/TxFactory.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.d.ts +9 -0
- package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.js +59 -0
- package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.js.map +1 -0
- package/.yalc/@node-dlc/core/dist/utils/Precision.d.ts +13 -0
- package/.yalc/@node-dlc/core/dist/utils/Precision.js +28 -0
- package/.yalc/@node-dlc/core/dist/utils/Precision.js.map +1 -0
- package/.yalc/@node-dlc/core/lib/AsyncProcessingQueue.ts +63 -0
- package/.yalc/@node-dlc/core/lib/Base32.ts +2 -0
- package/.yalc/@node-dlc/core/lib/BigIntUtils.ts +2 -0
- package/.yalc/@node-dlc/core/lib/BitField.ts +2 -0
- package/.yalc/@node-dlc/core/lib/ChannelId.ts +2 -0
- package/.yalc/@node-dlc/core/lib/LinkedList.ts +111 -0
- package/.yalc/@node-dlc/core/lib/LinkedListNode.ts +14 -0
- package/.yalc/@node-dlc/core/lib/Queue.ts +36 -0
- package/.yalc/@node-dlc/core/lib/ShortChannelId.ts +2 -0
- package/.yalc/@node-dlc/core/lib/ShortChannelIdUtils.ts +9 -0
- package/.yalc/@node-dlc/core/lib/dlc/CETCalculator.ts +450 -0
- package/.yalc/@node-dlc/core/lib/dlc/CoinSelect.ts +117 -0
- package/.yalc/@node-dlc/core/lib/dlc/HyperbolaPayoutCurve.ts +188 -0
- package/.yalc/@node-dlc/core/lib/dlc/PayoutCurve.ts +6 -0
- package/.yalc/@node-dlc/core/lib/dlc/PolynomialPayoutCurve.ts +266 -0
- package/.yalc/@node-dlc/core/lib/dlc/TxBuilder.ts +336 -0
- package/.yalc/@node-dlc/core/lib/dlc/TxFinalizer.ts +217 -0
- package/.yalc/@node-dlc/core/lib/dlc/finance/Builder.ts +730 -0
- package/.yalc/@node-dlc/core/lib/dlc/finance/CoveredCall.ts +110 -0
- package/.yalc/@node-dlc/core/lib/dlc/finance/CsoInfo.ts +498 -0
- package/.yalc/@node-dlc/core/lib/dlc/finance/LinearPayout.ts +94 -0
- package/.yalc/@node-dlc/core/lib/dlc/finance/LongCall.ts +74 -0
- package/.yalc/@node-dlc/core/lib/dlc/finance/LongPut.ts +74 -0
- package/.yalc/@node-dlc/core/lib/dlc/finance/OptionInfo.ts +182 -0
- package/.yalc/@node-dlc/core/lib/dlc/finance/ShortPut.ts +75 -0
- package/.yalc/@node-dlc/core/lib/index.ts +66 -0
- package/.yalc/@node-dlc/core/lib/lightning/ChannelId.ts +54 -0
- package/.yalc/@node-dlc/core/lib/lightning/ChannelKeys.ts +109 -0
- package/.yalc/@node-dlc/core/lib/lightning/CommitmentNumber.ts +94 -0
- package/.yalc/@node-dlc/core/lib/lightning/CommitmentSecret.ts +41 -0
- package/.yalc/@node-dlc/core/lib/lightning/CommitmentSecretStore.ts +84 -0
- package/.yalc/@node-dlc/core/lib/lightning/Htlc.ts +52 -0
- package/.yalc/@node-dlc/core/lib/lightning/HtlcDirection.ts +17 -0
- package/.yalc/@node-dlc/core/lib/lightning/ScriptFactory.ts +150 -0
- package/.yalc/@node-dlc/core/lib/lightning/TxFactory.ts +338 -0
- package/.yalc/@node-dlc/core/lib/utils/BigIntUtils.ts +64 -0
- package/.yalc/@node-dlc/core/lib/utils/Precision.ts +23 -0
- package/.yalc/@node-dlc/core/package.json +38 -0
- package/.yalc/@node-dlc/core/tsconfig.json +8 -0
- package/.yalc/@node-dlc/core/yalc.sig +1 -0
- package/CHANGELOG.md +11 -0
- package/package.json +6 -6
- package/yalc.lock +10 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { PayoutFunction } from '@node-dlc/messaging';
|
|
2
|
+
import BN from 'bignumber.js';
|
|
3
|
+
|
|
4
|
+
import { toBigInt } from '../../utils/BigIntUtils';
|
|
5
|
+
import { HyperbolaPayoutCurve } from '../HyperbolaPayoutCurve';
|
|
6
|
+
|
|
7
|
+
const buildCurve = (
|
|
8
|
+
strikePrice: bigint,
|
|
9
|
+
contractSize: bigint,
|
|
10
|
+
oracleBase: number,
|
|
11
|
+
oracleDigits: number,
|
|
12
|
+
): {
|
|
13
|
+
maxOutcome: bigint;
|
|
14
|
+
totalCollateral: bigint;
|
|
15
|
+
payoutCurve: HyperbolaPayoutCurve;
|
|
16
|
+
} => {
|
|
17
|
+
const a = new BN(1);
|
|
18
|
+
const b = new BN(0);
|
|
19
|
+
const c = new BN(0);
|
|
20
|
+
const d = new BN((strikePrice * contractSize).toString());
|
|
21
|
+
|
|
22
|
+
const f_1 = new BN(0);
|
|
23
|
+
const _f_2 = new BN(0);
|
|
24
|
+
|
|
25
|
+
const _tempHyperbolaPayoutCurve = new HyperbolaPayoutCurve(
|
|
26
|
+
a,
|
|
27
|
+
b,
|
|
28
|
+
c,
|
|
29
|
+
d,
|
|
30
|
+
f_1,
|
|
31
|
+
_f_2,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const maxOutcome = BigInt(
|
|
35
|
+
new BN(oracleBase).pow(oracleDigits).minus(1).toString(10),
|
|
36
|
+
);
|
|
37
|
+
const maxOutcomePayout = _tempHyperbolaPayoutCurve
|
|
38
|
+
.getPayout(maxOutcome)
|
|
39
|
+
.integerValue();
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
maxOutcome,
|
|
43
|
+
totalCollateral: contractSize - toBigInt(maxOutcomePayout),
|
|
44
|
+
payoutCurve: new HyperbolaPayoutCurve(
|
|
45
|
+
a,
|
|
46
|
+
b,
|
|
47
|
+
c,
|
|
48
|
+
d,
|
|
49
|
+
f_1,
|
|
50
|
+
maxOutcomePayout.negated(),
|
|
51
|
+
),
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const buildPayoutFunction = (
|
|
56
|
+
strikePrice: bigint,
|
|
57
|
+
contractSize: bigint,
|
|
58
|
+
oracleBase: number,
|
|
59
|
+
oracleDigits: number,
|
|
60
|
+
): { payoutFunction: PayoutFunction; totalCollateral: bigint } => {
|
|
61
|
+
const { maxOutcome, totalCollateral, payoutCurve } = buildCurve(
|
|
62
|
+
strikePrice,
|
|
63
|
+
contractSize,
|
|
64
|
+
oracleBase,
|
|
65
|
+
oracleDigits,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const payoutFunction = new PayoutFunction();
|
|
69
|
+
|
|
70
|
+
// Defensive fix: ensure payoutFunctionPieces is initialized as an array
|
|
71
|
+
if (!payoutFunction.payoutFunctionPieces) {
|
|
72
|
+
payoutFunction.payoutFunctionPieces = [];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const curvePiece = payoutCurve.toPayoutCurvePiece();
|
|
76
|
+
|
|
77
|
+
// Set the left and right endpoints for the hyperbola piece (matching rust-dlc structure)
|
|
78
|
+
curvePiece.leftEndPoint = {
|
|
79
|
+
eventOutcome: BigInt(0),
|
|
80
|
+
outcomePayout: totalCollateral,
|
|
81
|
+
extraPrecision: 0,
|
|
82
|
+
};
|
|
83
|
+
curvePiece.rightEndPoint = {
|
|
84
|
+
eventOutcome: maxOutcome,
|
|
85
|
+
outcomePayout: BigInt(0),
|
|
86
|
+
extraPrecision: 0,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
payoutFunction.payoutFunctionPieces.push({
|
|
90
|
+
endPoint: {
|
|
91
|
+
eventOutcome: maxOutcome,
|
|
92
|
+
outcomePayout: BigInt(0),
|
|
93
|
+
extraPrecision: 0,
|
|
94
|
+
},
|
|
95
|
+
payoutCurvePiece: curvePiece,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
payoutFunction.lastEndpoint = {
|
|
99
|
+
eventOutcome: maxOutcome,
|
|
100
|
+
outcomePayout: BigInt(0),
|
|
101
|
+
extraPrecision: 0,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
payoutFunction,
|
|
106
|
+
totalCollateral,
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const CoveredCall = { buildCurve, buildPayoutFunction };
|
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import { Value } from '@node-dlc/bitcoin';
|
|
2
|
+
import {
|
|
3
|
+
ContractDescriptorType,
|
|
4
|
+
ContractInfo,
|
|
5
|
+
ContractInfoType,
|
|
6
|
+
DigitDecompositionEventDescriptor,
|
|
7
|
+
MessageType,
|
|
8
|
+
MultiOracleInfo,
|
|
9
|
+
NumericalDescriptor,
|
|
10
|
+
OrderPositionInfo,
|
|
11
|
+
PayoutCurvePieceType,
|
|
12
|
+
PayoutFunction,
|
|
13
|
+
PolynomialPayoutCurvePiece,
|
|
14
|
+
SingleContractInfo,
|
|
15
|
+
SingleOracleInfo,
|
|
16
|
+
} from '@node-dlc/messaging';
|
|
17
|
+
import assert from 'assert';
|
|
18
|
+
import Decimal from 'decimal.js';
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
DlcParty,
|
|
22
|
+
roundToNearestMultiplier,
|
|
23
|
+
roundUpToNearestMultiplier,
|
|
24
|
+
UNIT_MULTIPLIER,
|
|
25
|
+
} from './Builder';
|
|
26
|
+
import { HasContractInfo, HasOfferCollateral, HasType } from './OptionInfo';
|
|
27
|
+
|
|
28
|
+
export interface CsoInfo {
|
|
29
|
+
normalizedMaxGain: Value; // Max Gain Relative to 1 BTC Contract
|
|
30
|
+
normalizedMaxLoss: Value; // Max Loss Relative to 1 BTC Contract
|
|
31
|
+
maxGainForContractSize: Value; // Max Gain Relative to Contract Size
|
|
32
|
+
maxLossForContractSize: Value; // Max Loss Relative to Contract Size
|
|
33
|
+
minPayout: bigint;
|
|
34
|
+
maxPayout: bigint;
|
|
35
|
+
contractSize: Value;
|
|
36
|
+
offerCollateral: Value;
|
|
37
|
+
totalCollateral: Value;
|
|
38
|
+
expiry: Date;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface CsoInfoParams {
|
|
42
|
+
normalizedMaxGain: Value; // Max Gain Relative to 1 BTC Contract
|
|
43
|
+
normalizedMaxLoss: Value; // Max Loss Relative to 1 BTC Contract
|
|
44
|
+
maxGainForContractSize: Value; // Max Gain Relative to Contract Size
|
|
45
|
+
maxLossForContractSize: Value; // Max Loss Relative to Contract Size
|
|
46
|
+
offerCollateral: Value; // Offer Collateral
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const ONE_BTC_CONTRACT = Value.fromBitcoin(1);
|
|
50
|
+
|
|
51
|
+
export type MaybeHasPositionInfo = {
|
|
52
|
+
positionInfo?: OrderPositionInfo;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* getCsoInfoParamsFromContractInfo V0
|
|
57
|
+
*
|
|
58
|
+
* Old getCsoInfoParamsFromContractInfo implementation
|
|
59
|
+
*
|
|
60
|
+
* @param {Value} contractSize - The size of the contract in terms of value.
|
|
61
|
+
* @param {Value} collateral - The collateral value put up for the contract.
|
|
62
|
+
* @param {DlcParty} shiftForFees - Specifies which party ('offeror' or 'acceptor') will bear the fees, affecting the outcome values.
|
|
63
|
+
* @param {Value} fees - The fees associated with the contract.
|
|
64
|
+
* @param {string} unit - The unit of measurement for the contract outcomes (e.g., 'BTC').
|
|
65
|
+
* @param {Value} startOutcomeValue - The starting outcome value for the contract.
|
|
66
|
+
* @param {Value} endOutcomeValue - The ending outcome value for the contract.
|
|
67
|
+
* @returns {CsoInfoParams} An object containing the calculated CSO parameters:
|
|
68
|
+
* - normalizedMaxGain: Maximum gain relative to a 1 BTC contract.
|
|
69
|
+
* - normalizedMaxLoss: Maximum loss relative to a 1 BTC contract.
|
|
70
|
+
* - maxGainForContractSize: Maximum gain for the actual contract size.
|
|
71
|
+
* - maxLossForContractSize: Maximum loss for the actual contract size.
|
|
72
|
+
* - offerCollateral: The offer collateral value after adjustments.
|
|
73
|
+
*
|
|
74
|
+
* Note: This function calculates the adjusted fees incorrectly, using collateral instead of contract size. Use v1 where possible.
|
|
75
|
+
*/
|
|
76
|
+
export const getCsoInfoParamsFromContractInfoV0 = (
|
|
77
|
+
contractSize: Value,
|
|
78
|
+
collateral: Value,
|
|
79
|
+
shiftForFees: DlcParty,
|
|
80
|
+
fees: Value,
|
|
81
|
+
unit: string,
|
|
82
|
+
startOutcomeValue: Value,
|
|
83
|
+
endOutcomeValue: Value,
|
|
84
|
+
): CsoInfoParams => {
|
|
85
|
+
const leverageMultiplier = parseFloat(
|
|
86
|
+
new Decimal(contractSize.bitcoin).dividedBy(collateral.bitcoin).toFixed(1),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const defaultContractSize = Value.fromBitcoin(1);
|
|
90
|
+
|
|
91
|
+
const shiftValue =
|
|
92
|
+
collateral.sats > 0
|
|
93
|
+
? Value.fromSats(
|
|
94
|
+
roundToNearestMultiplier(
|
|
95
|
+
(fees.sats * defaultContractSize.sats) / collateral.sats, // WARNING: this should be contract size not collateral
|
|
96
|
+
UNIT_MULTIPLIER[unit.toLowerCase()], // (use v1 if possible)
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
: Value.zero();
|
|
100
|
+
|
|
101
|
+
if (shiftForFees === 'offeror') {
|
|
102
|
+
startOutcomeValue.sub(shiftValue);
|
|
103
|
+
endOutcomeValue.sub(shiftValue);
|
|
104
|
+
} else if (shiftForFees === 'acceptor') {
|
|
105
|
+
startOutcomeValue.add(shiftValue);
|
|
106
|
+
endOutcomeValue.add(shiftValue);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const normalizedMaxGain = endOutcomeValue.clone();
|
|
110
|
+
normalizedMaxGain.sub(ONE_BTC_CONTRACT);
|
|
111
|
+
|
|
112
|
+
const normalizedMaxLoss = ONE_BTC_CONTRACT.clone();
|
|
113
|
+
normalizedMaxLoss.sub(startOutcomeValue);
|
|
114
|
+
|
|
115
|
+
const maxGainForContractSize = Value.fromBitcoin(
|
|
116
|
+
new Decimal(normalizedMaxGain.bitcoin)
|
|
117
|
+
.times(leverageMultiplier)
|
|
118
|
+
.toDecimalPlaces(5)
|
|
119
|
+
.toNumber(),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const maxLossForContractSize = Value.fromBitcoin(
|
|
123
|
+
new Decimal(normalizedMaxLoss.bitcoin)
|
|
124
|
+
.times(contractSize.bitcoin)
|
|
125
|
+
.toDecimalPlaces(
|
|
126
|
+
8 - Math.log10(Number(UNIT_MULTIPLIER[unit.toLowerCase()])),
|
|
127
|
+
)
|
|
128
|
+
.toNumber(),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const offerCollateral = collateral.clone();
|
|
132
|
+
offerCollateral.sub(
|
|
133
|
+
Value.fromSats(
|
|
134
|
+
(maxGainForContractSize.sats * collateral.sats) / BigInt(1e8),
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
normalizedMaxGain,
|
|
140
|
+
normalizedMaxLoss,
|
|
141
|
+
maxGainForContractSize,
|
|
142
|
+
maxLossForContractSize,
|
|
143
|
+
offerCollateral,
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* getCsoInfoParamsFromContractInfo V1
|
|
149
|
+
*
|
|
150
|
+
* Fixed getCsoInfoParamsFromContractInfo implementation
|
|
151
|
+
*
|
|
152
|
+
* @param {Value} contractSize - The size of the contract in terms of value.
|
|
153
|
+
* @param {Value} collateral - The collateral value put up for the contract.
|
|
154
|
+
* @param {DlcParty} shiftForFees - Specifies which party ('offeror' or 'acceptor') will bear the fees, affecting the outcome values.
|
|
155
|
+
* @param {Value} fees - The fees associated with the contract.
|
|
156
|
+
* @param {string} unit - The unit of measurement for the contract outcomes (e.g., 'BTC').
|
|
157
|
+
* @param {Value} startOutcomeValue - The starting outcome value for the contract.
|
|
158
|
+
* @param {Value} endOutcomeValue - The ending outcome value for the contract.
|
|
159
|
+
* @returns {CsoInfoParams} An object containing the calculated CSO parameters:
|
|
160
|
+
* - normalizedMaxGain: Maximum gain relative to a 1 BTC contract.
|
|
161
|
+
* - normalizedMaxLoss: Maximum loss relative to a 1 BTC contract.
|
|
162
|
+
* - maxGainForContractSize: Maximum gain for the actual contract size.
|
|
163
|
+
* - maxLossForContractSize: Maximum loss for the actual contract size.
|
|
164
|
+
* - offerCollateral: The offer collateral value after adjustments.
|
|
165
|
+
*
|
|
166
|
+
* This version improves upon the previous by correctly adjusting fees based on the contract size, leading to more accurate
|
|
167
|
+
* calculations of CSO parameters.
|
|
168
|
+
*/
|
|
169
|
+
export const getCsoInfoParamsFromContractInfoV1 = (
|
|
170
|
+
contractSize: Value,
|
|
171
|
+
collateral: Value,
|
|
172
|
+
shiftForFees: DlcParty,
|
|
173
|
+
fees: Value,
|
|
174
|
+
unit: string,
|
|
175
|
+
startOutcomeValue: Value,
|
|
176
|
+
endOutcomeValue: Value,
|
|
177
|
+
): CsoInfoParams => {
|
|
178
|
+
const feesAdjusted = Value.fromSats(
|
|
179
|
+
roundToNearestMultiplier(
|
|
180
|
+
(fees.sats * BigInt(1e8)) / contractSize.sats, // NOTE: this is done correctly using contractSize
|
|
181
|
+
BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
|
|
182
|
+
),
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
if (shiftForFees === 'offeror') {
|
|
186
|
+
startOutcomeValue.sub(feesAdjusted);
|
|
187
|
+
endOutcomeValue.sub(feesAdjusted);
|
|
188
|
+
} else if (shiftForFees === 'acceptor') {
|
|
189
|
+
startOutcomeValue.add(feesAdjusted);
|
|
190
|
+
endOutcomeValue.add(feesAdjusted);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const normalizedMaxGain = endOutcomeValue.clone();
|
|
194
|
+
normalizedMaxGain.sub(ONE_BTC_CONTRACT);
|
|
195
|
+
|
|
196
|
+
const normalizedMaxLoss = ONE_BTC_CONTRACT.clone();
|
|
197
|
+
normalizedMaxLoss.sub(startOutcomeValue);
|
|
198
|
+
|
|
199
|
+
const maxGainForContractSize = Value.fromSats(
|
|
200
|
+
roundUpToNearestMultiplier(
|
|
201
|
+
(normalizedMaxGain.sats * contractSize.sats) / BigInt(1e8),
|
|
202
|
+
BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
|
|
203
|
+
),
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
const maxLossForContractSize = Value.fromSats(
|
|
207
|
+
roundUpToNearestMultiplier(
|
|
208
|
+
(normalizedMaxLoss.sats * contractSize.sats) / BigInt(1e8),
|
|
209
|
+
BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
|
|
210
|
+
),
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
const offerCollateral = collateral.clone();
|
|
214
|
+
offerCollateral.sub(maxGainForContractSize);
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
normalizedMaxGain,
|
|
218
|
+
normalizedMaxLoss,
|
|
219
|
+
maxGainForContractSize,
|
|
220
|
+
maxLossForContractSize,
|
|
221
|
+
offerCollateral,
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Decode CsoInfo from a ContractInfo object. Essentially the opposite of buildCustomStrategyOrderOffer
|
|
227
|
+
*
|
|
228
|
+
* @param {_contractInfo} ContractInfo - Contract Info object, containing oracle and descriptor info
|
|
229
|
+
* @param {DlcParty} shiftForFees - Specifies which party ('offeror', 'acceptor', or 'neither') will pay for network fees
|
|
230
|
+
* @param {Value} fees - Network fees associated with the contract. Defaults to 0 sats.
|
|
231
|
+
* @param {_contractSize} Value - Optional. If not provided, it defaults to the total collateral.
|
|
232
|
+
* @param {csoVersion} 'v0' | 'v1' - Specifies the version of the CSO parameter calculation to use. Defaults to 'v1'.
|
|
233
|
+
* @returns {CsoInfo} An object containing the calculated CSO information:
|
|
234
|
+
* - normalizedMaxGain: Maximum gain relative to a 1 BTC contract.
|
|
235
|
+
* - normalizedMaxLoss: Maximum loss relative to a 1 BTC contract.
|
|
236
|
+
* - maxGainForContractSize: Maximum gain for the actual contract size.
|
|
237
|
+
* - maxLossForContractSize: Maximum loss for the actual contract size.
|
|
238
|
+
* - minPayout: Minimum payout as determined by the contract's payout function.
|
|
239
|
+
* - maxPayout: Maximum payout as determined by the contract's payout function.
|
|
240
|
+
* - contractSize: The size of the contract in terms of value.
|
|
241
|
+
* - offerCollateral: The offer collateral value after adjustments.
|
|
242
|
+
* - totalCollateral: The total collateral put up for the contract.
|
|
243
|
+
* - expiry: The expiry date of the contract based on the oracle's event maturity epoch.
|
|
244
|
+
*
|
|
245
|
+
* Note: This function performs several validations to ensure that the contract information and its components are of the
|
|
246
|
+
* expected types and formats.
|
|
247
|
+
* It throws errors if unsupported types or formats are encountered.
|
|
248
|
+
*/
|
|
249
|
+
export const getCsoInfoFromContractInfo = (
|
|
250
|
+
_contractInfo: ContractInfo,
|
|
251
|
+
shiftForFees: DlcParty = 'neither',
|
|
252
|
+
fees: Value = Value.fromSats(0),
|
|
253
|
+
_contractSize?: Value,
|
|
254
|
+
csoVersion: 'v0' | 'v1' = 'v1',
|
|
255
|
+
): CsoInfo => {
|
|
256
|
+
if (_contractInfo.contractInfoType !== ContractInfoType.Single)
|
|
257
|
+
throw Error('Only ContractInfoV0 currently supported');
|
|
258
|
+
|
|
259
|
+
const contractInfo = _contractInfo as SingleContractInfo;
|
|
260
|
+
if (
|
|
261
|
+
contractInfo.contractDescriptor.contractDescriptorType !==
|
|
262
|
+
ContractDescriptorType.NumericOutcome
|
|
263
|
+
)
|
|
264
|
+
throw Error('Only Numeric Descriptor currently supported');
|
|
265
|
+
|
|
266
|
+
const oracleInfo = contractInfo.oracleInfo;
|
|
267
|
+
|
|
268
|
+
// Handle both SingleOracleInfo and MultiOracleInfo
|
|
269
|
+
let eventMaturityEpoch: number;
|
|
270
|
+
let eventDescriptor: DigitDecompositionEventDescriptor;
|
|
271
|
+
|
|
272
|
+
switch (oracleInfo.type) {
|
|
273
|
+
case MessageType.SingleOracleInfo: {
|
|
274
|
+
const singleOracleInfo = oracleInfo as SingleOracleInfo;
|
|
275
|
+
eventMaturityEpoch =
|
|
276
|
+
singleOracleInfo.announcement.oracleEvent.eventMaturityEpoch;
|
|
277
|
+
eventDescriptor = singleOracleInfo.announcement.oracleEvent
|
|
278
|
+
.eventDescriptor as DigitDecompositionEventDescriptor;
|
|
279
|
+
|
|
280
|
+
if (
|
|
281
|
+
singleOracleInfo.announcement.oracleEvent.eventDescriptor.type !==
|
|
282
|
+
MessageType.DigitDecompositionEventDescriptor
|
|
283
|
+
)
|
|
284
|
+
throw Error(
|
|
285
|
+
'Only DigitDecompositionEventDescriptor currently supported',
|
|
286
|
+
);
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
case MessageType.MultiOracleInfo: {
|
|
290
|
+
const multiOracleInfo = oracleInfo as MultiOracleInfo;
|
|
291
|
+
eventMaturityEpoch =
|
|
292
|
+
multiOracleInfo.announcements[0].oracleEvent.eventMaturityEpoch;
|
|
293
|
+
eventDescriptor = multiOracleInfo.announcements[0].oracleEvent
|
|
294
|
+
.eventDescriptor as DigitDecompositionEventDescriptor;
|
|
295
|
+
|
|
296
|
+
if (
|
|
297
|
+
multiOracleInfo.announcements[0].oracleEvent.eventDescriptor.type !==
|
|
298
|
+
MessageType.DigitDecompositionEventDescriptor
|
|
299
|
+
)
|
|
300
|
+
throw Error(
|
|
301
|
+
'Only DigitDecompositionEventDescriptor currently supported',
|
|
302
|
+
);
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
default:
|
|
306
|
+
throw Error('Unknown oracle info type');
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const contractDescriptor = contractInfo.contractDescriptor as NumericalDescriptor;
|
|
310
|
+
if (contractDescriptor.payoutFunction.type !== MessageType.PayoutFunction)
|
|
311
|
+
throw Error('Only PayoutFunction currently supported');
|
|
312
|
+
|
|
313
|
+
const payoutFunction = contractDescriptor.payoutFunction as PayoutFunction;
|
|
314
|
+
|
|
315
|
+
validateCsoPayoutFunction(payoutFunction);
|
|
316
|
+
|
|
317
|
+
const initialPiece = payoutFunction.payoutFunctionPieces[0];
|
|
318
|
+
const midPiece = payoutFunction.payoutFunctionPieces[1];
|
|
319
|
+
|
|
320
|
+
const minPayout = initialPiece.endPoint.outcomePayout;
|
|
321
|
+
const maxPayout = midPiece.endPoint.outcomePayout;
|
|
322
|
+
|
|
323
|
+
const startOutcome = initialPiece.endPoint.eventOutcome;
|
|
324
|
+
const endOutcome = midPiece.endPoint.eventOutcome;
|
|
325
|
+
|
|
326
|
+
const unit = eventDescriptor.unit;
|
|
327
|
+
|
|
328
|
+
const collateral = Value.fromSats(contractInfo.totalCollateral);
|
|
329
|
+
const contractSize =
|
|
330
|
+
_contractSize && _contractSize.sats > 0 ? _contractSize : collateral;
|
|
331
|
+
|
|
332
|
+
const startOutcomeValue = Value.fromSats(
|
|
333
|
+
startOutcome * UNIT_MULTIPLIER[unit.toLowerCase()],
|
|
334
|
+
);
|
|
335
|
+
const endOutcomeValue = Value.fromSats(
|
|
336
|
+
endOutcome * UNIT_MULTIPLIER[unit.toLowerCase()],
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
const getCsoInfoParamsFromContractInfo =
|
|
340
|
+
csoVersion === 'v0'
|
|
341
|
+
? getCsoInfoParamsFromContractInfoV0
|
|
342
|
+
: getCsoInfoParamsFromContractInfoV1;
|
|
343
|
+
|
|
344
|
+
const {
|
|
345
|
+
normalizedMaxGain,
|
|
346
|
+
normalizedMaxLoss,
|
|
347
|
+
maxGainForContractSize,
|
|
348
|
+
maxLossForContractSize,
|
|
349
|
+
offerCollateral,
|
|
350
|
+
} = getCsoInfoParamsFromContractInfo(
|
|
351
|
+
contractSize,
|
|
352
|
+
collateral,
|
|
353
|
+
shiftForFees,
|
|
354
|
+
fees,
|
|
355
|
+
unit,
|
|
356
|
+
startOutcomeValue,
|
|
357
|
+
endOutcomeValue,
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
const expiry = new Date(eventMaturityEpoch * 1000);
|
|
361
|
+
|
|
362
|
+
return {
|
|
363
|
+
normalizedMaxGain,
|
|
364
|
+
normalizedMaxLoss,
|
|
365
|
+
maxGainForContractSize,
|
|
366
|
+
maxLossForContractSize,
|
|
367
|
+
minPayout,
|
|
368
|
+
maxPayout,
|
|
369
|
+
contractSize,
|
|
370
|
+
offerCollateral,
|
|
371
|
+
totalCollateral: collateral,
|
|
372
|
+
expiry,
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Get CsoInfo from OrderOffer or DlcOffer and validate
|
|
378
|
+
*
|
|
379
|
+
* @param {HasContractInfo & HasType} offer
|
|
380
|
+
* @returns {CsoInfo}
|
|
381
|
+
*/
|
|
382
|
+
export const getCsoInfoFromOffer = (
|
|
383
|
+
offer: HasContractInfo & HasType & HasOfferCollateral & MaybeHasPositionInfo,
|
|
384
|
+
csoVersion: 'v0' | 'v1' = 'v1',
|
|
385
|
+
): CsoInfo => {
|
|
386
|
+
if (
|
|
387
|
+
offer.type !== MessageType.DlcOffer &&
|
|
388
|
+
offer.type !== MessageType.OrderOffer
|
|
389
|
+
)
|
|
390
|
+
throw Error('Only DlcOffer and OrderOffer currently supported');
|
|
391
|
+
|
|
392
|
+
let shiftForFees: DlcParty = 'neither';
|
|
393
|
+
const fees = Value.zero();
|
|
394
|
+
const contractSize = Value.zero();
|
|
395
|
+
|
|
396
|
+
if (offer.positionInfo) {
|
|
397
|
+
shiftForFees = (offer.positionInfo as OrderPositionInfo).shiftForFees;
|
|
398
|
+
fees.add(Value.fromSats((offer.positionInfo as OrderPositionInfo).fees));
|
|
399
|
+
contractSize.add(
|
|
400
|
+
Value.fromSats((offer.positionInfo as OrderPositionInfo).contractSize),
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const positionInfo = getCsoInfoFromContractInfo(
|
|
405
|
+
offer.contractInfo,
|
|
406
|
+
shiftForFees,
|
|
407
|
+
fees,
|
|
408
|
+
contractSize,
|
|
409
|
+
csoVersion,
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
if (positionInfo.offerCollateral.sats !== offer.offerCollateral)
|
|
413
|
+
throw Error('Offer was not generated with CSO ContractInfo');
|
|
414
|
+
|
|
415
|
+
return positionInfo;
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Validate Payout Function for proper CSO format
|
|
420
|
+
*
|
|
421
|
+
* It should have 3 PayoutCurvePieces which consist of a flat line (maxLoss),
|
|
422
|
+
* ascending line (maxLoss to maxGain) and finally another flat line (maxGain)
|
|
423
|
+
*
|
|
424
|
+
* All PayoutCurvePieces should be type PolynomialPayoutCurvePieces
|
|
425
|
+
*
|
|
426
|
+
* @param {PayoutFunction} payoutFunction
|
|
427
|
+
*/
|
|
428
|
+
export const validateCsoPayoutFunction = (
|
|
429
|
+
payoutFunction: PayoutFunction,
|
|
430
|
+
): void => {
|
|
431
|
+
assert(
|
|
432
|
+
payoutFunction.payoutFunctionPieces.length === 3,
|
|
433
|
+
'CSO Payout Function must have 3 PayoutFunctionPieces',
|
|
434
|
+
);
|
|
435
|
+
for (const [i, piece] of payoutFunction.payoutFunctionPieces.entries()) {
|
|
436
|
+
assert(
|
|
437
|
+
piece.payoutCurvePiece.payoutCurvePieceType ===
|
|
438
|
+
PayoutCurvePieceType.Polynomial ||
|
|
439
|
+
piece.payoutCurvePiece.type === MessageType.PolynomialPayoutCurvePiece,
|
|
440
|
+
'CSO Payout Function PayoutCurvePieces must be PolynomialCurvePieces',
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
const payoutCurvePiece = piece.payoutCurvePiece as PolynomialPayoutCurvePiece;
|
|
444
|
+
const points = payoutCurvePiece.points;
|
|
445
|
+
|
|
446
|
+
// eventOutcome should always be ascending
|
|
447
|
+
assert(
|
|
448
|
+
points[0].eventOutcome < points[1].eventOutcome,
|
|
449
|
+
'CSO Payout Function PayoutCurvePiece point payout should be an ascending line',
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
// endpoints should always be ascending
|
|
453
|
+
let previousPiece, previousPoints;
|
|
454
|
+
if (i > 0) {
|
|
455
|
+
previousPiece = payoutFunction.payoutFunctionPieces[i - 1];
|
|
456
|
+
previousPoints = previousPiece.payoutCurvePiece.points;
|
|
457
|
+
assert(
|
|
458
|
+
previousPiece.endPoint.eventOutcome < piece.endPoint.eventOutcome,
|
|
459
|
+
'CSO Payout Function point endpoints should be an ascending line',
|
|
460
|
+
);
|
|
461
|
+
assert(
|
|
462
|
+
previousPoints[1].outcomePayout === points[0].outcomePayout,
|
|
463
|
+
'CSO Payout Function point outcome payout should be continuous without gaps',
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
switch (i) {
|
|
468
|
+
case 0:
|
|
469
|
+
// First piece - should start from initial endpoint
|
|
470
|
+
// maxLoss should be a flat line
|
|
471
|
+
assert(
|
|
472
|
+
points[0].outcomePayout === points[1].outcomePayout,
|
|
473
|
+
'CSO Payout Function maxLoss PayoutCurvePiece point should be a flat line',
|
|
474
|
+
);
|
|
475
|
+
break;
|
|
476
|
+
case 1:
|
|
477
|
+
// maxLoss to maxGain should be an ascending line
|
|
478
|
+
assert(
|
|
479
|
+
previousPiece.endPoint.outcomePayout < piece.endPoint.outcomePayout,
|
|
480
|
+
);
|
|
481
|
+
assert(
|
|
482
|
+
points[0].outcomePayout < points[1].outcomePayout,
|
|
483
|
+
'CSO Payout Function maxLoss to maxGain PayoutCurvePiece point should be an ascending line',
|
|
484
|
+
);
|
|
485
|
+
break;
|
|
486
|
+
case 2:
|
|
487
|
+
// maxGain should be a flat line
|
|
488
|
+
assert(
|
|
489
|
+
previousPiece.endPoint.outcomePayout === piece.endPoint.outcomePayout,
|
|
490
|
+
);
|
|
491
|
+
assert(
|
|
492
|
+
points[0].outcomePayout === points[1].outcomePayout,
|
|
493
|
+
'CSO Payout Function maxGain PayoutCurvePiece point should be a flat line',
|
|
494
|
+
);
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { PayoutFunction } from '@node-dlc/messaging';
|
|
2
|
+
import BN from 'bignumber.js';
|
|
3
|
+
|
|
4
|
+
import { PolynomialPayoutCurve } from '../PolynomialPayoutCurve';
|
|
5
|
+
|
|
6
|
+
const buildPayoutFunction = (
|
|
7
|
+
minPayout: bigint,
|
|
8
|
+
maxPayout: bigint,
|
|
9
|
+
startOutcome: bigint,
|
|
10
|
+
endOutcome: bigint,
|
|
11
|
+
oracleBase: number,
|
|
12
|
+
oracleDigits: number,
|
|
13
|
+
): { payoutFunction: PayoutFunction } => {
|
|
14
|
+
// Max outcome limited by the oracle
|
|
15
|
+
const maxOutcome = BigInt(
|
|
16
|
+
new BN(oracleBase).pow(oracleDigits).minus(1).toString(10),
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
// max loss line
|
|
20
|
+
const payoutCurveMaxLoss = new PolynomialPayoutCurve([
|
|
21
|
+
{ outcome: new BN(0), payout: new BN(Number(minPayout)) },
|
|
22
|
+
{
|
|
23
|
+
outcome: new BN(Number(startOutcome)),
|
|
24
|
+
payout: new BN(Number(minPayout)),
|
|
25
|
+
},
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
// payout line
|
|
29
|
+
const payoutCurve = new PolynomialPayoutCurve([
|
|
30
|
+
{
|
|
31
|
+
outcome: new BN(Number(startOutcome)),
|
|
32
|
+
payout: new BN(Number(minPayout)),
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
outcome: new BN(Number(endOutcome)),
|
|
36
|
+
payout: new BN(Number(maxPayout)),
|
|
37
|
+
},
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
// max gain line
|
|
41
|
+
const payoutCurveMaxGain = new PolynomialPayoutCurve([
|
|
42
|
+
{ outcome: new BN(Number(endOutcome)), payout: new BN(Number(maxPayout)) },
|
|
43
|
+
{
|
|
44
|
+
outcome: new BN(Number(maxOutcome)),
|
|
45
|
+
payout: new BN(Number(maxPayout)),
|
|
46
|
+
},
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
const payoutFunction = new PayoutFunction();
|
|
50
|
+
|
|
51
|
+
// Defensive fix: ensure payoutFunctionPieces is initialized as an array
|
|
52
|
+
if (!payoutFunction.payoutFunctionPieces) {
|
|
53
|
+
payoutFunction.payoutFunctionPieces = [];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
payoutFunction.payoutFunctionPieces.push({
|
|
57
|
+
endPoint: {
|
|
58
|
+
eventOutcome: startOutcome,
|
|
59
|
+
outcomePayout: minPayout,
|
|
60
|
+
extraPrecision: 0,
|
|
61
|
+
},
|
|
62
|
+
payoutCurvePiece: payoutCurveMaxLoss.toPayoutCurvePiece(),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
payoutFunction.payoutFunctionPieces.push({
|
|
66
|
+
endPoint: {
|
|
67
|
+
eventOutcome: endOutcome,
|
|
68
|
+
outcomePayout: maxPayout,
|
|
69
|
+
extraPrecision: 0,
|
|
70
|
+
},
|
|
71
|
+
payoutCurvePiece: payoutCurve.toPayoutCurvePiece(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
payoutFunction.payoutFunctionPieces.push({
|
|
75
|
+
endPoint: {
|
|
76
|
+
eventOutcome: maxOutcome,
|
|
77
|
+
outcomePayout: maxPayout,
|
|
78
|
+
extraPrecision: 0,
|
|
79
|
+
},
|
|
80
|
+
payoutCurvePiece: payoutCurveMaxGain.toPayoutCurvePiece(),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
payoutFunction.lastEndpoint = {
|
|
84
|
+
eventOutcome: maxOutcome,
|
|
85
|
+
outcomePayout: maxPayout,
|
|
86
|
+
extraPrecision: 0,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
payoutFunction,
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const LinearPayout = { buildPayoutFunction };
|