@atomicfinance/bitcoin-wallet-provider 4.2.8 → 4.3.1

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.
Files changed (226) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-lint$colon$fix.log +4 -0
  3. package/.turbo/turbo-lint.log +5 -0
  4. package/.turbo/turbo-test.log +0 -0
  5. package/.yalc/@node-dlc/core/.nyc_output/687a3c14-9765-45fd-a1fe-f7db3d56fee4.json +1 -0
  6. package/.yalc/@node-dlc/core/.nyc_output/processinfo/687a3c14-9765-45fd-a1fe-f7db3d56fee4.json +1 -0
  7. package/.yalc/@node-dlc/core/.nyc_output/processinfo/index.json +1 -0
  8. package/.yalc/@node-dlc/core/README.md +11 -0
  9. package/.yalc/@node-dlc/core/__tests__/core.spec.ts +7 -0
  10. package/.yalc/@node-dlc/core/__tests__/dlc/CETCalculator.spec.ts +1029 -0
  11. package/.yalc/@node-dlc/core/__tests__/dlc/CoinSelect.spec.ts +179 -0
  12. package/.yalc/@node-dlc/core/__tests__/dlc/PolynomialPayoutCurve.spec.ts +345 -0
  13. package/.yalc/@node-dlc/core/__tests__/dlc/TxBuilder.spec.ts +424 -0
  14. package/.yalc/@node-dlc/core/__tests__/dlc/finance/Builder.spec.ts +492 -0
  15. package/.yalc/@node-dlc/core/__tests__/dlc/finance/CoveredCall.spec.ts +112 -0
  16. package/.yalc/@node-dlc/core/__tests__/dlc/finance/CsoInfo.spec.ts +1124 -0
  17. package/.yalc/@node-dlc/core/__tests__/dlc/finance/LongCall.spec.ts +55 -0
  18. package/.yalc/@node-dlc/core/__tests__/dlc/finance/LongPut.spec.ts +55 -0
  19. package/.yalc/@node-dlc/core/__tests__/dlc/finance/OptionInfo.spec.ts +226 -0
  20. package/.yalc/@node-dlc/core/__tests__/dlc/finance/ShortPut.spec.ts +62 -0
  21. package/.yalc/@node-dlc/core/__tests__/tsconfig.json +8 -0
  22. package/.yalc/@node-dlc/core/__tests__/utils/precision.spec.ts +40 -0
  23. package/.yalc/@node-dlc/core/coverage/lcov-report/base.css +224 -0
  24. package/.yalc/@node-dlc/core/coverage/lcov-report/block-navigation.js +79 -0
  25. package/.yalc/@node-dlc/core/coverage/lcov-report/favicon.png +0 -0
  26. package/.yalc/@node-dlc/core/coverage/lcov-report/index.html +171 -0
  27. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/AsyncProcessingQueue.ts.html +269 -0
  28. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/Base32.ts.html +86 -0
  29. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/BigIntUtils.ts.html +86 -0
  30. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/BitField.ts.html +86 -0
  31. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ChannelId.ts.html +86 -0
  32. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/LinkedList.ts.html +413 -0
  33. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/LinkedListNode.ts.html +122 -0
  34. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/Queue.ts.html +188 -0
  35. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ShortChannelId.ts.html +86 -0
  36. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ShortChannelIdUtils.ts.html +107 -0
  37. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/CETCalculator.ts.html +1430 -0
  38. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/CoinSelect.ts.html +431 -0
  39. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/HyperbolaPayoutCurve.ts.html +644 -0
  40. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/PolynomialPayoutCurve.ts.html +878 -0
  41. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/TxBuilder.ts.html +1088 -0
  42. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/TxFinalizer.ts.html +731 -0
  43. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/Builder.ts.html +2270 -0
  44. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/CoveredCall.ts.html +410 -0
  45. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/CsoInfo.ts.html +1574 -0
  46. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LinearPayout.ts.html +362 -0
  47. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LongCall.ts.html +302 -0
  48. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LongPut.ts.html +302 -0
  49. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/OptionInfo.ts.html +626 -0
  50. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/ShortPut.ts.html +305 -0
  51. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/index.html +216 -0
  52. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/index.html +186 -0
  53. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/index.html +261 -0
  54. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/index.ts.html +278 -0
  55. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ChannelId.ts.html +242 -0
  56. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ChannelKeys.ts.html +407 -0
  57. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentNumber.ts.html +362 -0
  58. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentSecret.ts.html +203 -0
  59. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentSecretStore.ts.html +332 -0
  60. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/Htlc.ts.html +236 -0
  61. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/HtlcDirection.ts.html +131 -0
  62. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ScriptFactory.ts.html +530 -0
  63. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/TxFactory.ts.html +1094 -0
  64. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/index.html +231 -0
  65. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/BigIntUtils.ts.html +272 -0
  66. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/Precision.ts.html +149 -0
  67. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/index.html +126 -0
  68. package/.yalc/@node-dlc/core/coverage/lcov-report/prettify.css +1 -0
  69. package/.yalc/@node-dlc/core/coverage/lcov-report/prettify.js +2 -0
  70. package/.yalc/@node-dlc/core/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  71. package/.yalc/@node-dlc/core/coverage/lcov-report/sorter.js +170 -0
  72. package/.yalc/@node-dlc/core/coverage/lcov.info +2426 -0
  73. package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.d.ts +22 -0
  74. package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.js +55 -0
  75. package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.js.map +1 -0
  76. package/.yalc/@node-dlc/core/dist/Base32.d.ts +1 -0
  77. package/.yalc/@node-dlc/core/dist/Base32.js +7 -0
  78. package/.yalc/@node-dlc/core/dist/Base32.js.map +1 -0
  79. package/.yalc/@node-dlc/core/dist/BigIntUtils.d.ts +1 -0
  80. package/.yalc/@node-dlc/core/dist/BigIntUtils.js +8 -0
  81. package/.yalc/@node-dlc/core/dist/BigIntUtils.js.map +1 -0
  82. package/.yalc/@node-dlc/core/dist/BitField.d.ts +1 -0
  83. package/.yalc/@node-dlc/core/dist/BitField.js +7 -0
  84. package/.yalc/@node-dlc/core/dist/BitField.js.map +1 -0
  85. package/.yalc/@node-dlc/core/dist/ChannelId.d.ts +1 -0
  86. package/.yalc/@node-dlc/core/dist/ChannelId.js +7 -0
  87. package/.yalc/@node-dlc/core/dist/ChannelId.js.map +1 -0
  88. package/.yalc/@node-dlc/core/dist/LinkedList.d.ts +30 -0
  89. package/.yalc/@node-dlc/core/dist/LinkedList.js +104 -0
  90. package/.yalc/@node-dlc/core/dist/LinkedList.js.map +1 -0
  91. package/.yalc/@node-dlc/core/dist/LinkedListNode.d.ts +9 -0
  92. package/.yalc/@node-dlc/core/dist/LinkedListNode.js +15 -0
  93. package/.yalc/@node-dlc/core/dist/LinkedListNode.js.map +1 -0
  94. package/.yalc/@node-dlc/core/dist/Queue.d.ts +22 -0
  95. package/.yalc/@node-dlc/core/dist/Queue.js +38 -0
  96. package/.yalc/@node-dlc/core/dist/Queue.js.map +1 -0
  97. package/.yalc/@node-dlc/core/dist/ShortChannelId.d.ts +1 -0
  98. package/.yalc/@node-dlc/core/dist/ShortChannelId.js +7 -0
  99. package/.yalc/@node-dlc/core/dist/ShortChannelId.js.map +1 -0
  100. package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.d.ts +1 -0
  101. package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.js +12 -0
  102. package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.js.map +1 -0
  103. package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.d.ts +50 -0
  104. package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.js +332 -0
  105. package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.js.map +1 -0
  106. package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.d.ts +23 -0
  107. package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.js +77 -0
  108. package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.js.map +1 -0
  109. package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.d.ts +20 -0
  110. package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.js +117 -0
  111. package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.js.map +1 -0
  112. package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.d.ts +5 -0
  113. package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.js +3 -0
  114. package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.js.map +1 -0
  115. package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.d.ts +52 -0
  116. package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.js +173 -0
  117. package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.js.map +1 -0
  118. package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.d.ts +55 -0
  119. package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.js +217 -0
  120. package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.js.map +1 -0
  121. package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.d.ts +40 -0
  122. package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.js +146 -0
  123. package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.js.map +1 -0
  124. package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.d.ts +193 -0
  125. package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.js +422 -0
  126. package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.js.map +1 -0
  127. package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.d.ts +13 -0
  128. package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.js +67 -0
  129. package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.js.map +1 -0
  130. package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.d.ts +114 -0
  131. package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.js +290 -0
  132. package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.js.map +1 -0
  133. package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.d.ts +6 -0
  134. package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.js +79 -0
  135. package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.js.map +1 -0
  136. package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.d.ts +13 -0
  137. package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.js +47 -0
  138. package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.js.map +1 -0
  139. package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.d.ts +13 -0
  140. package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.js +47 -0
  141. package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.js.map +1 -0
  142. package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.d.ts +20 -0
  143. package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.js +101 -0
  144. package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.js.map +1 -0
  145. package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.d.ts +12 -0
  146. package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.js +46 -0
  147. package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.js.map +1 -0
  148. package/.yalc/@node-dlc/core/dist/index.d.ts +39 -0
  149. package/.yalc/@node-dlc/core/dist/index.js +78 -0
  150. package/.yalc/@node-dlc/core/dist/index.js.map +1 -0
  151. package/.yalc/@node-dlc/core/dist/lightning/ChannelId.d.ts +31 -0
  152. package/.yalc/@node-dlc/core/dist/lightning/ChannelId.js +52 -0
  153. package/.yalc/@node-dlc/core/dist/lightning/ChannelId.js.map +1 -0
  154. package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.d.ts +55 -0
  155. package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.js +105 -0
  156. package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.js.map +1 -0
  157. package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.d.ts +55 -0
  158. package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.js +76 -0
  159. package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.js.map +1 -0
  160. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.d.ts +26 -0
  161. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.js +44 -0
  162. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.js.map +1 -0
  163. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.d.ts +41 -0
  164. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.js +79 -0
  165. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.js.map +1 -0
  166. package/.yalc/@node-dlc/core/dist/lightning/Htlc.d.ts +46 -0
  167. package/.yalc/@node-dlc/core/dist/lightning/Htlc.js +46 -0
  168. package/.yalc/@node-dlc/core/dist/lightning/Htlc.js.map +1 -0
  169. package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.d.ts +16 -0
  170. package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.js +21 -0
  171. package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.js.map +1 -0
  172. package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.d.ts +76 -0
  173. package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.js +102 -0
  174. package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.js.map +1 -0
  175. package/.yalc/@node-dlc/core/dist/lightning/TxFactory.d.ts +83 -0
  176. package/.yalc/@node-dlc/core/dist/lightning/TxFactory.js +222 -0
  177. package/.yalc/@node-dlc/core/dist/lightning/TxFactory.js.map +1 -0
  178. package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.d.ts +9 -0
  179. package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.js +59 -0
  180. package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.js.map +1 -0
  181. package/.yalc/@node-dlc/core/dist/utils/Precision.d.ts +13 -0
  182. package/.yalc/@node-dlc/core/dist/utils/Precision.js +28 -0
  183. package/.yalc/@node-dlc/core/dist/utils/Precision.js.map +1 -0
  184. package/.yalc/@node-dlc/core/lib/AsyncProcessingQueue.ts +63 -0
  185. package/.yalc/@node-dlc/core/lib/Base32.ts +2 -0
  186. package/.yalc/@node-dlc/core/lib/BigIntUtils.ts +2 -0
  187. package/.yalc/@node-dlc/core/lib/BitField.ts +2 -0
  188. package/.yalc/@node-dlc/core/lib/ChannelId.ts +2 -0
  189. package/.yalc/@node-dlc/core/lib/LinkedList.ts +111 -0
  190. package/.yalc/@node-dlc/core/lib/LinkedListNode.ts +14 -0
  191. package/.yalc/@node-dlc/core/lib/Queue.ts +36 -0
  192. package/.yalc/@node-dlc/core/lib/ShortChannelId.ts +2 -0
  193. package/.yalc/@node-dlc/core/lib/ShortChannelIdUtils.ts +9 -0
  194. package/.yalc/@node-dlc/core/lib/dlc/CETCalculator.ts +450 -0
  195. package/.yalc/@node-dlc/core/lib/dlc/CoinSelect.ts +117 -0
  196. package/.yalc/@node-dlc/core/lib/dlc/HyperbolaPayoutCurve.ts +188 -0
  197. package/.yalc/@node-dlc/core/lib/dlc/PayoutCurve.ts +6 -0
  198. package/.yalc/@node-dlc/core/lib/dlc/PolynomialPayoutCurve.ts +266 -0
  199. package/.yalc/@node-dlc/core/lib/dlc/TxBuilder.ts +336 -0
  200. package/.yalc/@node-dlc/core/lib/dlc/TxFinalizer.ts +217 -0
  201. package/.yalc/@node-dlc/core/lib/dlc/finance/Builder.ts +730 -0
  202. package/.yalc/@node-dlc/core/lib/dlc/finance/CoveredCall.ts +110 -0
  203. package/.yalc/@node-dlc/core/lib/dlc/finance/CsoInfo.ts +498 -0
  204. package/.yalc/@node-dlc/core/lib/dlc/finance/LinearPayout.ts +94 -0
  205. package/.yalc/@node-dlc/core/lib/dlc/finance/LongCall.ts +74 -0
  206. package/.yalc/@node-dlc/core/lib/dlc/finance/LongPut.ts +74 -0
  207. package/.yalc/@node-dlc/core/lib/dlc/finance/OptionInfo.ts +182 -0
  208. package/.yalc/@node-dlc/core/lib/dlc/finance/ShortPut.ts +75 -0
  209. package/.yalc/@node-dlc/core/lib/index.ts +66 -0
  210. package/.yalc/@node-dlc/core/lib/lightning/ChannelId.ts +54 -0
  211. package/.yalc/@node-dlc/core/lib/lightning/ChannelKeys.ts +109 -0
  212. package/.yalc/@node-dlc/core/lib/lightning/CommitmentNumber.ts +94 -0
  213. package/.yalc/@node-dlc/core/lib/lightning/CommitmentSecret.ts +41 -0
  214. package/.yalc/@node-dlc/core/lib/lightning/CommitmentSecretStore.ts +84 -0
  215. package/.yalc/@node-dlc/core/lib/lightning/Htlc.ts +52 -0
  216. package/.yalc/@node-dlc/core/lib/lightning/HtlcDirection.ts +17 -0
  217. package/.yalc/@node-dlc/core/lib/lightning/ScriptFactory.ts +150 -0
  218. package/.yalc/@node-dlc/core/lib/lightning/TxFactory.ts +338 -0
  219. package/.yalc/@node-dlc/core/lib/utils/BigIntUtils.ts +64 -0
  220. package/.yalc/@node-dlc/core/lib/utils/Precision.ts +23 -0
  221. package/.yalc/@node-dlc/core/package.json +38 -0
  222. package/.yalc/@node-dlc/core/tsconfig.json +8 -0
  223. package/.yalc/@node-dlc/core/yalc.sig +1 -0
  224. package/CHANGELOG.md +23 -0
  225. package/package.json +7 -7
  226. package/yalc.lock +10 -0
@@ -0,0 +1,188 @@
1
+ import {
2
+ HyperbolaPayoutCurvePiece,
3
+ MessageType,
4
+ PayoutCurvePieceType,
5
+ PayoutFunctionV0,
6
+ RoundingIntervals,
7
+ } from '@node-dlc/messaging';
8
+ import BigNumber from 'bignumber.js';
9
+
10
+ import { CETPayout } from '..';
11
+ import { splitIntoRanges } from './CETCalculator';
12
+ import PayoutCurve from './PayoutCurve';
13
+
14
+ export class HyperbolaPayoutCurve implements PayoutCurve {
15
+ constructor(
16
+ private a: BigNumber,
17
+ private b: BigNumber,
18
+ private c: BigNumber,
19
+ private d: BigNumber,
20
+ private translateOutcome: BigNumber,
21
+ private translatePayout: BigNumber,
22
+ private positive: boolean = true, // TODO: support negative pieces
23
+ ) {}
24
+
25
+ getPayout(_x: bigint): BigNumber {
26
+ const { a, b, c, d, translateOutcome, translatePayout } = this;
27
+ const x = new BigNumber(Number(_x));
28
+
29
+ const payout = c
30
+ .times(
31
+ x
32
+ .minus(translateOutcome)
33
+ .plus(
34
+ x
35
+ .minus(translateOutcome)
36
+ .exponentiatedBy(2)
37
+ .minus(a.times(b).times(4))
38
+ .squareRoot(),
39
+ )
40
+ .div(a.times(2)),
41
+ )
42
+ .plus(
43
+ a
44
+ .times(d)
45
+ .times(2)
46
+ .div(
47
+ x
48
+ .minus(translateOutcome)
49
+ .plus(
50
+ x
51
+ .minus(translateOutcome)
52
+ .exponentiatedBy(2)
53
+ .minus(a.times(b).times(4))
54
+ .squareRoot(),
55
+ ),
56
+ ),
57
+ )
58
+ .plus(translatePayout);
59
+
60
+ return payout;
61
+ }
62
+
63
+ getOutcomeForPayout(payout: BigNumber): bigint {
64
+ const { a, b, c, d, translateOutcome, translatePayout } = this;
65
+
66
+ // Inverse function
67
+ // y=(-ad^{2}-bf_{2}^{2}+2bf_{2}x-bx^{2}+df_{1}f_{2}-df_{1}x)/(d(f_{2}-x))
68
+ if (c.eq(0)) {
69
+ const denominator = d.times(translatePayout.minus(payout));
70
+
71
+ if (denominator.eq(0)) {
72
+ return BigInt(-1);
73
+ }
74
+
75
+ const outcome = a
76
+ .negated()
77
+ .times(d.exponentiatedBy(2))
78
+ .minus(b.times(translatePayout.exponentiatedBy(2)))
79
+ .plus(b.times(translatePayout).times(payout).times(2))
80
+ .minus(b.times(payout.exponentiatedBy(2)))
81
+ .plus(d.times(translateOutcome).times(translatePayout))
82
+ .minus(d.times(translateOutcome).times(payout))
83
+ .dividedBy(denominator)
84
+ .integerValue();
85
+
86
+ if (outcome.isFinite()) return BigInt(outcome.toString());
87
+ return BigInt(-1);
88
+ } else {
89
+ // y=\left((\sqrt{((adf_{2}-adx+bcf_{2}-bcx-2c\cdot d\cdot f_{1})^{2}-4cd(a^{2}d^{2}-2abcd+abf_{2}^{2}-2abf_{2}x+abx^{2}-adf_{1}f_{2}+adf_{1}x+b^{2}c^{2}-bcf_{1}f_{2}+bcf_{1}x+c\cdot d\cdot f_{1}^{2}))}-adf_{2}+adx-bcf_{2}+bcx+2c\cdot d\cdot f_{1})\right)/(2cd)
90
+ throw new Error('Not supported');
91
+ }
92
+ }
93
+
94
+ toPayoutCurvePiece(): HyperbolaPayoutCurvePiece {
95
+ const { a, b, c, d, translateOutcome, translatePayout, positive } = this;
96
+
97
+ // Use the constructor with string values to avoid F64 dependency issues
98
+ const piece = new HyperbolaPayoutCurvePiece(
99
+ positive,
100
+ translateOutcome.toString(),
101
+ translatePayout.toString(),
102
+ a.toString(),
103
+ b.toString(),
104
+ c.toString(),
105
+ d.toString(),
106
+ );
107
+
108
+ return piece;
109
+ }
110
+
111
+ equals(curve: HyperbolaPayoutCurve): boolean {
112
+ return (
113
+ this.a.eq(curve.a) &&
114
+ this.b.eq(curve.b) &&
115
+ this.c.eq(curve.c) &&
116
+ this.d.eq(curve.d) &&
117
+ this.translateOutcome.eq(curve.translateOutcome) &&
118
+ this.translatePayout.eq(curve.translatePayout) &&
119
+ this.positive === curve.positive
120
+ );
121
+ }
122
+
123
+ static fromPayoutCurvePiece(
124
+ piece: HyperbolaPayoutCurvePiece,
125
+ ): HyperbolaPayoutCurve {
126
+ // Convert F64 values to BigNumber using toDecimal().toString() to preserve precision
127
+ const a = new BigNumber(piece.a.toDecimal().toString());
128
+ const b = new BigNumber(piece.b.toDecimal().toString());
129
+ const c = new BigNumber(piece.c.toDecimal().toString());
130
+ const d = new BigNumber(piece.d.toDecimal().toString());
131
+ const translateOutcome = new BigNumber(
132
+ piece.translateOutcome.toDecimal().toString(),
133
+ );
134
+ const translatePayout = new BigNumber(
135
+ piece.translatePayout.toDecimal().toString(),
136
+ );
137
+
138
+ return new HyperbolaPayoutCurve(
139
+ a,
140
+ b,
141
+ c,
142
+ d,
143
+ translateOutcome,
144
+ translatePayout,
145
+ piece.usePositivePiece,
146
+ );
147
+ }
148
+
149
+ static computePayouts(
150
+ payoutFunction: PayoutFunctionV0,
151
+ totalCollateral: bigint,
152
+ roundingIntervals: RoundingIntervals,
153
+ ): CETPayout[] {
154
+ if (payoutFunction.payoutFunctionPieces.length !== 1)
155
+ throw new Error('Must have at least one piece');
156
+ const {
157
+ endPoint,
158
+ payoutCurvePiece,
159
+ } = payoutFunction.payoutFunctionPieces[0];
160
+
161
+ if (
162
+ payoutCurvePiece.payoutCurvePieceType !==
163
+ PayoutCurvePieceType.Hyperbola &&
164
+ payoutCurvePiece.type !== MessageType.HyperbolaPayoutCurvePiece &&
165
+ payoutCurvePiece.type !== MessageType.OldHyperbolaPayoutCurvePiece
166
+ )
167
+ throw new Error('Payout curve piece must be a hyperbola');
168
+
169
+ const _payoutCurvePiece = payoutCurvePiece as HyperbolaPayoutCurvePiece;
170
+ const curve = this.fromPayoutCurvePiece(_payoutCurvePiece);
171
+
172
+ // For the new PayoutFunction structure, get the starting point from the hyperbola piece's leftEndPoint
173
+ // This matches the rust-dlc implementation where get_first_outcome() returns left_end_point.event_outcome
174
+ const initialEventOutcome =
175
+ _payoutCurvePiece.leftEndPoint?.eventOutcome || BigInt(0);
176
+ const initialOutcomePayout =
177
+ _payoutCurvePiece.leftEndPoint?.outcomePayout || BigInt(0);
178
+ return splitIntoRanges(
179
+ initialEventOutcome,
180
+ endPoint.eventOutcome,
181
+ initialOutcomePayout,
182
+ endPoint.outcomePayout,
183
+ totalCollateral,
184
+ curve,
185
+ roundingIntervals.intervals,
186
+ );
187
+ }
188
+ }
@@ -0,0 +1,6 @@
1
+ import BigNumber from 'bignumber.js';
2
+
3
+ export default interface PayoutCurve {
4
+ getPayout(x: bigint): BigNumber;
5
+ getOutcomeForPayout(y: BigNumber): bigint;
6
+ }
@@ -0,0 +1,266 @@
1
+ import {
2
+ MessageType,
3
+ PayoutCurvePieceType,
4
+ PayoutFunction,
5
+ PolynomialPayoutCurvePiece,
6
+ RoundingIntervals,
7
+ } from '@node-dlc/messaging';
8
+ import BigNumber from 'bignumber.js';
9
+
10
+ import { fromPrecision, getPrecision } from '../utils/Precision';
11
+ import { CETPayout, mergePayouts, splitIntoRanges } from './CETCalculator';
12
+
13
+ interface DlcPoint {
14
+ outcome: BigNumber;
15
+ payout: BigNumber;
16
+ }
17
+
18
+ export class PolynomialPayoutCurve {
19
+ private readonly points: DlcPoint[];
20
+ private readonly slope: BigNumber;
21
+ private readonly left: DlcPoint;
22
+ private readonly right: DlcPoint;
23
+
24
+ constructor(points: DlcPoint[]) {
25
+ if (points.length !== 2) throw new Error('Must have two points');
26
+ this.points = points;
27
+ this.left = points[0];
28
+ this.right = points[points.length - 1];
29
+
30
+ // m = (y2 - y1) / (x2 - x1)
31
+ this.slope = this.right.payout
32
+ .minus(this.left.payout)
33
+ .dividedBy(this.right.outcome.minus(this.left.outcome));
34
+ }
35
+
36
+ /**
37
+ * Get the payout for a given outcome
38
+ * @param outcome The outcome to get the payout for
39
+ * @returns The payout for the outcome
40
+ */
41
+ getPayout(outcome: bigint): BigNumber {
42
+ const { left, slope } = this;
43
+
44
+ const x = new BigNumber(Number(outcome));
45
+
46
+ // y = mx + b
47
+ const payout = slope.times(x.minus(left.outcome)).plus(left.payout);
48
+
49
+ return payout;
50
+ }
51
+
52
+ /**
53
+ * Get the outcome for a given payout
54
+ * @param payout The payout to get the outcome for
55
+ * @returns The outcome for the payout
56
+ */
57
+ getOutcomeForPayout(payout: BigNumber): bigint {
58
+ const { left, right, slope } = this;
59
+ const y = new BigNumber(Number(payout));
60
+
61
+ // Handle constant payout curve (slope = 0)
62
+ if (slope.isZero()) {
63
+ // For constant curves, if the payout matches, return any outcome in the range
64
+ // If it doesn't match, this is an invalid request
65
+ if (y.eq(left.payout)) {
66
+ return BigInt(left.outcome.toString());
67
+ } else {
68
+ // For constant curves, if payout doesn't match, return left outcome
69
+ // This prevents Infinity errors in calculations
70
+ return BigInt(left.outcome.toString());
71
+ }
72
+ }
73
+
74
+ // Find the x value for the given y
75
+ // slope = (y2 - y1) / (x2 - x1)
76
+ // x1 = (y2 - y1) / slope + x2
77
+ const outcome = y
78
+ .minus(left.payout)
79
+ .dividedBy(slope)
80
+ .plus(left.outcome)
81
+ .integerValue();
82
+
83
+ // Clamp the outcome to the valid range
84
+ const clampedOutcome = BigNumber.max(
85
+ left.outcome,
86
+ BigNumber.min(outcome, right.outcome),
87
+ );
88
+
89
+ return BigInt(clampedOutcome.toString());
90
+ }
91
+
92
+ /**
93
+ * Serializes PolynomialPayoutCurve to a PolynomialPayoutCurvePiece (for transport)
94
+ * @returns A PolynomialPayoutCurvePiece
95
+ */
96
+ toPayoutCurvePiece(): PolynomialPayoutCurvePiece {
97
+ const { points } = this;
98
+
99
+ const piece = new PolynomialPayoutCurvePiece();
100
+
101
+ piece.points = points.map((point) => {
102
+ const eventOutcome = BigInt(point.outcome.toString());
103
+ const outcomePayout = BigInt(point.payout.toString());
104
+ const extraPrecision = getPrecision(point.payout);
105
+ return { eventOutcome, outcomePayout, extraPrecision };
106
+ });
107
+
108
+ return piece;
109
+ }
110
+
111
+ /**
112
+ * Determine if the payout curve is equal to another
113
+ * @param curve A PolynomialPayoutCurve
114
+ * @returns True if the curves are the same
115
+ */
116
+ equals(curve: PolynomialPayoutCurve): boolean {
117
+ return this.points.every((point, i) => {
118
+ const otherPoint = curve.points[i];
119
+ return (
120
+ point.outcome.eq(otherPoint.outcome) &&
121
+ point.payout.eq(otherPoint.payout)
122
+ );
123
+ });
124
+ }
125
+
126
+ /**
127
+ * Creates a PolynomialPayoutCurve from a PolynomialPayoutCurvePiece
128
+ * @param piece
129
+ * @returns A PolynomialPayoutCurve
130
+ */
131
+ static fromPayoutCurvePiece(
132
+ piece: PolynomialPayoutCurvePiece,
133
+ ): PolynomialPayoutCurve {
134
+ const points = piece.points.map((point) => {
135
+ const outcome = new BigNumber(point.eventOutcome.toString());
136
+ const payout = new BigNumber(point.outcomePayout.toString()).plus(
137
+ fromPrecision(point.extraPrecision),
138
+ );
139
+ return { outcome, payout };
140
+ });
141
+ return new PolynomialPayoutCurve(points);
142
+ }
143
+
144
+ /**
145
+ * Computes all CETs for a given payout curve
146
+ * @param payoutFunction The payout function
147
+ * @param totalCollateral The total collateral
148
+ * @param roundingIntervals The rounding intervals
149
+ * @returns A list of CETs
150
+ */
151
+ static computePayouts(
152
+ payoutFunction: PayoutFunction,
153
+ totalCollateral: bigint,
154
+ roundingIntervals: RoundingIntervals,
155
+ ): CETPayout[] {
156
+ if (payoutFunction.payoutFunctionPieces.length < 1)
157
+ throw new Error('Must have at least one piece');
158
+
159
+ payoutFunction.payoutFunctionPieces.forEach((piece) => {
160
+ if (
161
+ piece.payoutCurvePiece.payoutCurvePieceType !==
162
+ PayoutCurvePieceType.Polynomial &&
163
+ piece.payoutCurvePiece.type !== MessageType.PolynomialPayoutCurvePiece
164
+ )
165
+ throw new Error('Payout curve piece must be a polynomial');
166
+ });
167
+
168
+ const CETS: CETPayout[] = [];
169
+
170
+ // 1. Add the first piece to the list
171
+ const { payoutCurvePiece } = payoutFunction.payoutFunctionPieces[0];
172
+
173
+ const curve = this.fromPayoutCurvePiece(
174
+ payoutCurvePiece as PolynomialPayoutCurvePiece,
175
+ );
176
+
177
+ // For the first piece, start from 0 and go to the first endpoint
178
+ const firstPiece = payoutFunction.payoutFunctionPieces[0];
179
+
180
+ // Calculate the start payout by evaluating the curve at outcome 0
181
+ const startPayout = curve.getPayout(BigInt(0));
182
+ const startPayoutBigInt = BigInt(startPayout.integerValue().toString());
183
+
184
+ // Only add ranges if there's actually a range to cover
185
+ if (firstPiece.endPoint.eventOutcome > 0) {
186
+ CETS.push(
187
+ ...splitIntoRanges(
188
+ BigInt(0), // Start from 0
189
+ firstPiece.endPoint.eventOutcome,
190
+ startPayoutBigInt, // Start payout calculated from curve
191
+ firstPiece.endPoint.outcomePayout, // End payout from endpoint
192
+ totalCollateral,
193
+ curve,
194
+ roundingIntervals.intervals,
195
+ ),
196
+ );
197
+ }
198
+
199
+ // 2. If there are subsequent pieces, add them to the list
200
+ for (let i = 1; i < payoutFunction.payoutFunctionPieces.length; i++) {
201
+ const { payoutCurvePiece } = payoutFunction.payoutFunctionPieces[i];
202
+
203
+ const curve = this.fromPayoutCurvePiece(
204
+ payoutCurvePiece as PolynomialPayoutCurvePiece,
205
+ );
206
+
207
+ CETS.push(
208
+ ...splitIntoRanges(
209
+ payoutFunction.payoutFunctionPieces[i - 1].endPoint.eventOutcome,
210
+ payoutFunction.payoutFunctionPieces[i].endPoint.eventOutcome,
211
+ payoutFunction.payoutFunctionPieces[i - 1].endPoint.outcomePayout,
212
+ payoutFunction.payoutFunctionPieces[i].endPoint.outcomePayout,
213
+ totalCollateral,
214
+ curve,
215
+ roundingIntervals.intervals,
216
+ ),
217
+ );
218
+ }
219
+
220
+ // 3. Handle the final range from the last piece to lastEndpoint if it exists
221
+ if (
222
+ payoutFunction.lastEndpoint &&
223
+ payoutFunction.payoutFunctionPieces.length > 0
224
+ ) {
225
+ const lastPieceIndex = payoutFunction.payoutFunctionPieces.length - 1;
226
+ const lastPiece = payoutFunction.payoutFunctionPieces[lastPieceIndex];
227
+
228
+ // Check if there's a range to cover
229
+ if (
230
+ payoutFunction.lastEndpoint.eventOutcome >
231
+ lastPiece.endPoint.eventOutcome
232
+ ) {
233
+ // For the final range, we'll assume a constant payout from the last piece endpoint to lastEndpoint
234
+ // This is a common pattern for DLC payout functions
235
+ const finalCurve = new PolynomialPayoutCurve([
236
+ {
237
+ outcome: new BigNumber(lastPiece.endPoint.eventOutcome.toString()),
238
+ payout: new BigNumber(lastPiece.endPoint.outcomePayout.toString()),
239
+ },
240
+ {
241
+ outcome: new BigNumber(
242
+ payoutFunction.lastEndpoint.eventOutcome.toString(),
243
+ ),
244
+ payout: new BigNumber(
245
+ payoutFunction.lastEndpoint.outcomePayout.toString(),
246
+ ),
247
+ },
248
+ ]);
249
+
250
+ CETS.push(
251
+ ...splitIntoRanges(
252
+ lastPiece.endPoint.eventOutcome,
253
+ payoutFunction.lastEndpoint.eventOutcome,
254
+ lastPiece.endPoint.outcomePayout,
255
+ payoutFunction.lastEndpoint.outcomePayout,
256
+ totalCollateral,
257
+ finalCurve,
258
+ roundingIntervals.intervals,
259
+ ),
260
+ );
261
+ }
262
+ }
263
+
264
+ return mergePayouts(CETS);
265
+ }
266
+ }