@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,332 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.roundPayout = exports.mergePayouts = exports.splitIntoRanges = exports.groupByIgnoringDigits = exports.middleGroupings = exports.backGroupings = exports.frontGroupings = exports.separatePrefix = exports.decompose = exports.dropUntil = exports.zipWithIndex = void 0;
7
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
8
+ const BigIntUtils_1 = require("../utils/BigIntUtils");
9
+ function zipWithIndex(arr) {
10
+ return arr.map((a, i) => [a, i]);
11
+ }
12
+ exports.zipWithIndex = zipWithIndex;
13
+ function dropUntil(data, check) {
14
+ for (let i = 0; i < data.length; i++) {
15
+ if (check(data[i])) {
16
+ return data.slice(i);
17
+ }
18
+ }
19
+ return [];
20
+ }
21
+ exports.dropUntil = dropUntil;
22
+ function decompose(num, base, numDigits) {
23
+ let currentNumber = num;
24
+ const digits = [];
25
+ while (numDigits > 0) {
26
+ digits.push(Number(currentNumber % BigInt(base)));
27
+ currentNumber = currentNumber / BigInt(base);
28
+ numDigits--;
29
+ }
30
+ return digits.reverse();
31
+ }
32
+ exports.decompose = decompose;
33
+ function separatePrefix(start, end, base, numDigits) {
34
+ const startDigits = decompose(start, base, numDigits);
35
+ const endDigits = decompose(end, base, numDigits);
36
+ const prefixDigits = [];
37
+ for (let i = 0; i < startDigits.length; i++) {
38
+ if (startDigits[i] === endDigits[i]) {
39
+ prefixDigits.push(startDigits[i]);
40
+ }
41
+ else
42
+ break;
43
+ }
44
+ return {
45
+ prefixDigits,
46
+ startDigits: startDigits.splice(prefixDigits.length),
47
+ endDigits: endDigits.splice(prefixDigits.length),
48
+ };
49
+ }
50
+ exports.separatePrefix = separatePrefix;
51
+ function frontGroupings(digits, base) {
52
+ const digitsReversed = Array.from(digits).reverse();
53
+ const nonZeroDigits = dropUntil(zipWithIndex(digitsReversed), (a) => a[0] !== 0);
54
+ if (nonZeroDigits.length === 0) {
55
+ return [[0]];
56
+ }
57
+ const fromFront = nonZeroDigits
58
+ .filter((_, i) => i !== nonZeroDigits.length - 1)
59
+ .flatMap(([d, i]) => {
60
+ const fixedDigits = Array.from(digits);
61
+ fixedDigits.length = fixedDigits.length - (i + 1);
62
+ const result = [];
63
+ for (let n = d + 1; n < base; n++) {
64
+ result.push([...Array.from(fixedDigits), n]);
65
+ }
66
+ return result;
67
+ });
68
+ return [nonZeroDigits.map((a) => a[0]).reverse(), ...fromFront];
69
+ }
70
+ exports.frontGroupings = frontGroupings;
71
+ function backGroupings(digits, base) {
72
+ const digitsReversed = Array.from(digits).reverse();
73
+ const nonMaxDigits = dropUntil(zipWithIndex(digitsReversed), (a) => a[0] !== base - 1);
74
+ if (nonMaxDigits.length === 0) {
75
+ return [[base - 1]];
76
+ }
77
+ const fromBack = nonMaxDigits
78
+ .filter((_, i) => i !== nonMaxDigits.length - 1)
79
+ .flatMap(([d, i]) => {
80
+ const fixedDigits = Array.from(digits);
81
+ fixedDigits.length = fixedDigits.length - (i + 1);
82
+ const result = [];
83
+ for (let n = d - 1; n >= 0; n--) {
84
+ result.push([...Array.from(fixedDigits), n]);
85
+ }
86
+ return result;
87
+ });
88
+ return [...fromBack.reverse(), nonMaxDigits.map((a) => a[0]).reverse()];
89
+ }
90
+ exports.backGroupings = backGroupings;
91
+ function middleGroupings(firstDigitStart, firstDigitEnd) {
92
+ const result = [];
93
+ while (++firstDigitStart < firstDigitEnd) {
94
+ result.push([firstDigitStart]);
95
+ }
96
+ return result;
97
+ }
98
+ exports.middleGroupings = middleGroupings;
99
+ function groupByIgnoringDigits(start, end, base, numDigits) {
100
+ const { prefixDigits, startDigits, endDigits } = separatePrefix(start, end, base, numDigits);
101
+ if (start === end ||
102
+ (startDigits.every((n) => n === 0) &&
103
+ endDigits.every((n) => n === base - 1) &&
104
+ prefixDigits.length !== 0)) {
105
+ return [prefixDigits];
106
+ }
107
+ else if (prefixDigits.length === numDigits - 1) {
108
+ const result = [];
109
+ for (let i = startDigits[startDigits.length - 1]; i <= endDigits[endDigits.length - 1]; i++) {
110
+ result.push([...prefixDigits, i]);
111
+ }
112
+ return result;
113
+ }
114
+ else {
115
+ const front = frontGroupings(startDigits, base);
116
+ const middle = middleGroupings(startDigits[0], endDigits[0]);
117
+ const back = backGroupings(endDigits, base);
118
+ const groupings = [...front, ...middle, ...back];
119
+ return groupings.map((g) => [...prefixDigits, ...g]);
120
+ }
121
+ }
122
+ exports.groupByIgnoringDigits = groupByIgnoringDigits;
123
+ /**
124
+ * Performs optimized evaluation and rounding for strictly monotonic hyperbolas on intervals (from, to)
125
+ * e.g. hyperbolas with the form b = c = 0
126
+ *
127
+ * The next start of a payout range is determined by finding the outcome at the next mid-rounding payout.
128
+ * Uses an inverse function of the hyperbola to find the outcome.
129
+ *
130
+ * Optimizes rounding from O(to - from) to O(totalCollateral / rounding)
131
+ *
132
+ *
133
+ * Evaluates and rounds a payout_function equivalent to:
134
+ *
135
+ * payout_function_v0
136
+ * num_pieces: 1
137
+ * endpoint_0: from
138
+ * endpoint_payout_0: fromPayout
139
+ * extra_precision_0: 0
140
+ * payout_curve_piece: HyperbolaPayoutCurve
141
+ * endpoint_1: to
142
+ * endpoint_payout_1: toPayout
143
+ */
144
+ function splitIntoRanges(from, to, fromPayout, // endpoint_payout
145
+ toPayout, // endpoint_payout
146
+ totalCollateral, curve, roundingIntervals) {
147
+ if (to - from <= 0) {
148
+ throw new Error('`to` must be strictly greater than `from`');
149
+ }
150
+ const reversedIntervals = [...roundingIntervals].reverse();
151
+ const getRoundingForOutcome = (outcome) => {
152
+ const roundingIndex = reversedIntervals.findIndex((interval) => interval.beginInterval <= outcome);
153
+ return [
154
+ roundingIndex !== -1
155
+ ? reversedIntervals[roundingIndex].roundingMod
156
+ : BigInt(1),
157
+ roundingIndex,
158
+ ];
159
+ };
160
+ const clamp = (val) => BigIntUtils_1.BigIntMath.clamp(BigInt(0), val, totalCollateral);
161
+ const totalCollateralBN = new bignumber_js_1.default(totalCollateral.toString());
162
+ const clampBN = (val) => bignumber_js_1.default.max(0, bignumber_js_1.default.min(val, totalCollateralBN));
163
+ const result = [];
164
+ // outcome = endpoint_0
165
+ result.push({
166
+ payout: fromPayout,
167
+ indexFrom: from,
168
+ indexTo: from,
169
+ });
170
+ // In the case of a constant payout curve (fromPayout === toPayout), we can skip the range evaluation
171
+ if (fromPayout === toPayout) {
172
+ result.push({
173
+ payout: toPayout,
174
+ indexFrom: from,
175
+ indexTo: to,
176
+ });
177
+ // outcome = endpoint_1
178
+ result.push({
179
+ payout: toPayout,
180
+ indexFrom: to,
181
+ indexTo: to,
182
+ });
183
+ // merge neighbouring ranges with same payout
184
+ return (0, exports.mergePayouts)(result);
185
+ }
186
+ let currentOutcome = from + BigInt(1);
187
+ // iterate over entire range of outcomes from [from, to]
188
+ while (currentOutcome < to) {
189
+ const [rounding, roundingIndex] = getRoundingForOutcome(currentOutcome);
190
+ // either the next rounding interval, or the end of the range
191
+ const nextFirstRoundingOutcome = reversedIntervals[roundingIndex - 1]?.beginInterval || to;
192
+ // temporary variable to hold the current payout
193
+ let currentPayout = new bignumber_js_1.default(roundPayout(clampBN(curve.getPayout(currentOutcome)), rounding).toString());
194
+ let currentMidRoundedOutcome = currentOutcome;
195
+ const isAscending = curve
196
+ .getPayout(nextFirstRoundingOutcome)
197
+ .gt(currentPayout);
198
+ // Add loop counter to prevent infinite loops
199
+ let loopCounter = 0;
200
+ const maxIterations = Number(to - from) + 1000; // Allow some extra iterations
201
+ while (currentMidRoundedOutcome < nextFirstRoundingOutcome) {
202
+ // Prevent infinite loops
203
+ if (++loopCounter > maxIterations) {
204
+ // Breaking out of potential infinite loop - add remaining range and exit
205
+ result.push({
206
+ payout: clamp((0, BigIntUtils_1.toBigInt)(currentPayout)),
207
+ indexFrom: currentMidRoundedOutcome,
208
+ indexTo: to - BigInt(1),
209
+ });
210
+ currentOutcome = to;
211
+ break;
212
+ }
213
+ const nextRoundedPayout = currentPayout
214
+ .integerValue()
215
+ .plus(isAscending ? Number(rounding) : -Number(rounding));
216
+ const nextRoundedPayoutBigInt = (0, BigIntUtils_1.toBigInt)(nextRoundedPayout);
217
+ const nextMidRoundedPayout = currentPayout.plus(isAscending ? Number(rounding) / 2 : -Number(rounding) / 2);
218
+ let nextMidRoundedOutcome = curve.getOutcomeForPayout(nextMidRoundedPayout);
219
+ // Handle invalid outcomes from getOutcomeForPayout
220
+ if (nextMidRoundedOutcome < 0) {
221
+ // If getOutcomeForPayout returns invalid value, advance manually
222
+ nextMidRoundedOutcome = currentMidRoundedOutcome + BigInt(1);
223
+ }
224
+ if ((!isAscending &&
225
+ nextMidRoundedOutcome >= 0 &&
226
+ curve.getPayout(nextMidRoundedOutcome).lt(nextMidRoundedPayout)) ||
227
+ (isAscending &&
228
+ nextMidRoundedOutcome >= 0 &&
229
+ curve.getPayout(nextMidRoundedOutcome).gte(nextMidRoundedPayout))) {
230
+ nextMidRoundedOutcome = nextMidRoundedOutcome - BigInt(1);
231
+ // Ensure we don't go negative
232
+ if (nextMidRoundedOutcome < 0) {
233
+ nextMidRoundedOutcome = currentMidRoundedOutcome;
234
+ }
235
+ }
236
+ const nextOutcome = curve.getOutcomeForPayout(nextRoundedPayout);
237
+ if (nextMidRoundedOutcome >= nextFirstRoundingOutcome) {
238
+ result.push({
239
+ payout: clamp((0, BigIntUtils_1.toBigInt)(currentPayout)),
240
+ indexFrom: currentMidRoundedOutcome,
241
+ indexTo: nextFirstRoundingOutcome - BigInt(1),
242
+ });
243
+ currentOutcome = nextFirstRoundingOutcome;
244
+ break;
245
+ }
246
+ if (nextRoundedPayoutBigInt > totalCollateral ||
247
+ nextRoundedPayoutBigInt < 0 ||
248
+ nextOutcome >= to ||
249
+ nextOutcome < 0 // undefined on curve
250
+ ) {
251
+ if (nextMidRoundedOutcome < from || nextMidRoundedOutcome > to) {
252
+ result.push({
253
+ payout: clamp((0, BigIntUtils_1.toBigInt)(currentPayout)),
254
+ indexFrom: currentMidRoundedOutcome,
255
+ indexTo: to,
256
+ });
257
+ }
258
+ else {
259
+ result.push({
260
+ payout: clamp((0, BigIntUtils_1.toBigInt)(currentPayout)),
261
+ indexFrom: currentMidRoundedOutcome,
262
+ indexTo: nextMidRoundedOutcome,
263
+ }, {
264
+ payout: clamp(nextRoundedPayoutBigInt),
265
+ indexFrom: nextMidRoundedOutcome + BigInt(1),
266
+ indexTo: to - BigInt(1),
267
+ });
268
+ }
269
+ currentOutcome = to;
270
+ break;
271
+ }
272
+ result.push({
273
+ payout: clamp((0, BigIntUtils_1.toBigInt)(currentPayout)),
274
+ indexFrom: currentMidRoundedOutcome,
275
+ indexTo: nextMidRoundedOutcome,
276
+ });
277
+ // Handle case where nextOutcome is invalid
278
+ if (nextOutcome < 0) {
279
+ // Advance manually if getOutcomeForPayout returns invalid value
280
+ currentOutcome = currentMidRoundedOutcome + BigInt(1);
281
+ }
282
+ else {
283
+ currentOutcome = nextOutcome + BigInt(1);
284
+ }
285
+ currentPayout = nextRoundedPayout;
286
+ // Additional safety check: ensure we're making progress
287
+ const previousMidRoundedOutcome = currentMidRoundedOutcome;
288
+ currentMidRoundedOutcome = nextMidRoundedOutcome + BigInt(1);
289
+ if (currentMidRoundedOutcome <= previousMidRoundedOutcome) {
290
+ // No progress detected in splitIntoRanges loop, breaking
291
+ currentOutcome = nextFirstRoundingOutcome;
292
+ break;
293
+ }
294
+ }
295
+ }
296
+ // outcome = endpoint_1
297
+ result.push({
298
+ payout: toPayout,
299
+ indexFrom: to,
300
+ indexTo: to,
301
+ });
302
+ // merge neighbouring ranges with same payout
303
+ return (0, exports.mergePayouts)(result);
304
+ }
305
+ exports.splitIntoRanges = splitIntoRanges;
306
+ const mergePayouts = (payouts) => {
307
+ return payouts.reduce((acc, range) => {
308
+ const prev = acc[acc.length - 1];
309
+ if (prev) {
310
+ if ((prev.indexTo === range.indexFrom ||
311
+ prev.indexTo + BigInt(1) === range.indexFrom) &&
312
+ prev.payout === range.payout) {
313
+ prev.indexTo = range.indexTo;
314
+ return acc;
315
+ }
316
+ }
317
+ return [...acc, range];
318
+ }, []);
319
+ };
320
+ exports.mergePayouts = mergePayouts;
321
+ function roundPayout(payout, rounding) {
322
+ const roundingBN = new bignumber_js_1.default(rounding.toString());
323
+ const mod = payout.gte(0)
324
+ ? payout.mod(roundingBN)
325
+ : payout.mod(roundingBN).plus(roundingBN);
326
+ const roundedPayout = mod.gte(roundingBN.dividedBy(2))
327
+ ? payout.plus(roundingBN).minus(mod)
328
+ : payout.minus(mod);
329
+ return (0, BigIntUtils_1.toBigInt)(roundedPayout);
330
+ }
331
+ exports.roundPayout = roundPayout;
332
+ //# sourceMappingURL=CETCalculator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CETCalculator.js","sourceRoot":"","sources":["../../lib/dlc/CETCalculator.ts"],"names":[],"mappings":";;;;;;AAAA,gEAAqC;AAErC,sDAA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,UAAU,GAAG,IAAI,sBAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC;IACtD,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACvB,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC;QACxB,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAE5C,MAAM,aAAa,GAAG,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACpD,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC;QACpC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAEtB,OAAO,IAAA,sBAAQ,EAAC,aAAa,CAAC,CAAC;AACjC,CAAC;AAXD,kCAWC"}
@@ -0,0 +1,23 @@
1
+ export interface UTXO {
2
+ txid: string;
3
+ vout: number;
4
+ value: number;
5
+ address: string;
6
+ derivationPath?: string;
7
+ }
8
+ export declare const dustThreshold: (feeRate: bigint) => bigint;
9
+ export declare const dualFees: (feeRate: bigint, numInputs: number, numContracts: number) => bigint;
10
+ /**
11
+ * Selects UTXOs for dual funding
12
+ * @param utxos - UTXOs to select from
13
+ * @param collaterals - Collaterals to fund (just one for non-batch tx)
14
+ * @param feeRate - Fee rate in satoshis per byte
15
+ * @returns Inputs and fee
16
+ * @description
17
+ * Add inputs until we reach or surpass the target value (or deplete)
18
+ * Worst-case: O(n)
19
+ */
20
+ export declare const dualFundingCoinSelect: (utxos: UTXO[], collaterals: bigint[], feeRate: bigint) => {
21
+ inputs: UTXO[];
22
+ fee: bigint;
23
+ };
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dualFundingCoinSelect = exports.dualFees = exports.dustThreshold = void 0;
4
+ const messaging_1 = require("@node-dlc/messaging");
5
+ const TxFinalizer_1 = require("./TxFinalizer");
6
+ const TX_INPUT_SIZE = {
7
+ LEGACY: 148,
8
+ P2SH: 92,
9
+ BECH32: 69,
10
+ };
11
+ const inputBytes = () => {
12
+ return BigInt(TX_INPUT_SIZE.BECH32);
13
+ };
14
+ const dustThreshold = (feeRate) => {
15
+ return BigInt(inputBytes()) * feeRate;
16
+ };
17
+ exports.dustThreshold = dustThreshold;
18
+ // order by descending value, minus the inputs approximate fee
19
+ const utxoScore = (x, feeRate) => {
20
+ return BigInt(x.value) - feeRate * inputBytes();
21
+ };
22
+ const dualFees = (feeRate, numInputs, numContracts) => {
23
+ const input = new messaging_1.FundingInput();
24
+ input.maxWitnessLen = 108;
25
+ input.redeemScript = Buffer.from('', 'hex');
26
+ const fakeSPK = Buffer.from('0014663117d27e78eb432505180654e603acb30e8a4a', 'hex');
27
+ const offerInputs = Array.from({ length: numInputs }, () => input);
28
+ const acceptInputs = Array.from({ length: 1 }, () => input);
29
+ return new TxFinalizer_1.DualFundingTxFinalizer(offerInputs, fakeSPK, fakeSPK, acceptInputs, fakeSPK, fakeSPK, feeRate, numContracts).offerFees;
30
+ };
31
+ exports.dualFees = dualFees;
32
+ /**
33
+ * Selects UTXOs for dual funding
34
+ * @param utxos - UTXOs to select from
35
+ * @param collaterals - Collaterals to fund (just one for non-batch tx)
36
+ * @param feeRate - Fee rate in satoshis per byte
37
+ * @returns Inputs and fee
38
+ * @description
39
+ * Add inputs until we reach or surpass the target value (or deplete)
40
+ * Worst-case: O(n)
41
+ */
42
+ const dualFundingCoinSelect = (utxos, collaterals, // in satoshis
43
+ feeRate) => {
44
+ utxos = [...utxos].sort((a, b) => Number(utxoScore(b, feeRate) - utxoScore(a, feeRate)));
45
+ let inAccum = 0;
46
+ const inputs = [];
47
+ const outAccum = collaterals.reduce((acc, val) => acc + val, BigInt(0)) +
48
+ (0, exports.dustThreshold)(feeRate); // sum of collaterals
49
+ for (let i = 0; i < utxos.length; ++i) {
50
+ const utxo = utxos[i];
51
+ const utxoBytes = inputBytes();
52
+ const utxoFee = feeRate * utxoBytes;
53
+ const utxoValue = utxo.value;
54
+ // skip detrimental input
55
+ if (utxoFee > utxo.value) {
56
+ if (i === utxos.length - 1)
57
+ return {
58
+ fee: (0, exports.dualFees)(feeRate, 1, collaterals.length),
59
+ inputs: [],
60
+ };
61
+ continue;
62
+ }
63
+ inAccum += utxoValue;
64
+ inputs.push(utxo);
65
+ const fee = (0, exports.dualFees)(feeRate, inputs.length, collaterals.length);
66
+ // go again?
67
+ if (inAccum < outAccum + fee)
68
+ continue;
69
+ return { inputs, fee };
70
+ }
71
+ return {
72
+ fee: (0, exports.dualFees)(feeRate, 1, collaterals.length),
73
+ inputs: [],
74
+ };
75
+ };
76
+ exports.dualFundingCoinSelect = dualFundingCoinSelect;
77
+ //# sourceMappingURL=CoinSelect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CoinSelect.js","sourceRoot":"","sources":["../../lib/dlc/CoinSelect.ts"],"names":[],"mappings":";;;AAAA,mDAAmD;AAEnD,+CAAuD;AAUvD,MAAM,aAAa,GAAG;IACpB,MAAM,EAAE,GAAG;IACX,IAAI,EAAE,EAAE;IACR,MAAM,EAAE,EAAE;CACX,CAAC;AAEF,MAAM,UAAU,GAAG,GAAG,EAAE;IACtB,OAAO,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC,CAAC;AAEK,MAAM,aAAa,GAAG,CAAC,OAAe,EAAU,EAAE;IACvD,OAAO,MAAM,CAAC,UAAU,EAAE,CAAC,GAAG,OAAO,CAAC;AACxC,CAAC,CAAC;AAFW,QAAA,aAAa,iBAExB;AAEF,8DAA8D;AAC9D,MAAM,SAAS,GAAG,CAAC,CAAO,EAAE,OAAe,EAAU,EAAE;IACrD,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,OAAO,GAAG,UAAU,EAAE,CAAC;AAClD,CAAC,CAAC;AAEK,MAAM,Q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ingIntervals } from '@node-dlc/messaging';
2
+ import BigNumber from 'bignumber.js';
3
+ import { CETPayout } from '..';
4
+ import PayoutCurve from './PayoutCurve';
5
+ export declare class HyperbolaPayoutCurve implements PayoutCurve {
6
+ private a;
7
+ private b;
8
+ private c;
9
+ private d;
10
+ private translateOutcome;
11
+ private translatePayout;
12
+ private positive;
13
+ constructor(a: BigNumber, b: BigNumber, c: BigNumber, d: BigNumber, translateOutcome: BigNumber, translatePayout: BigNumber, positive?: boolean);
14
+ getPayout(_x: bigint): BigNumber;
15
+ getOutcomeForPayout(payout: BigNumber): bigint;
16
+ toPayoutCurvePiece(): HyperbolaPayoutCurvePiece;
17
+ equals(curve: HyperbolaPayoutCurve): boolean;
18
+ static fromPayoutCurvePiece(piece: HyperbolaPayoutCurvePiece): HyperbolaPayoutCurve;
19
+ static computePayouts(payoutFunction: PayoutFunctionV0, totalCollateral: bigint, roundingIntervals: RoundingIntervals): CETPayout[];
20
+ }
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.HyperbolaPayoutCurve = void 0;
7
+ const messaging_1 = require("@node-dlc/messaging");
8
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
9
+ const CETCalculator_1 = require("./CETCalculator");
10
+ class HyperbolaPayoutCurve {
11
+ constructor(a, b, c, d, translateOutcome, translatePayout, positive = true) {
12
+ this.a = a;
13
+ this.b = b;
14
+ this.c = c;
15
+ this.d = d;
16
+ this.translateOutcome = translateOutcome;
17
+ this.translatePayout = translatePayout;
18
+ this.positive = positive;
19
+ }
20
+ getPayout(_x) {
21
+ const { a, b, c, d, translateOutcome, translatePayout } = this;
22
+ const x = new bignumber_js_1.default(Number(_x));
23
+ const payout = c
24
+ .times(x
25
+ .minus(translateOutcome)
26
+ .plus(x
27
+ .minus(translateOutcome)
28
+ .exponentiatedBy(2)
29
+ .minus(a.times(b).times(4))
30
+ .squareRoot())
31
+ .div(a.times(2)))
32
+ .plus(a
33
+ .times(d)
34
+ .times(2)
35
+ .div(x
36
+ .minus(translateOutcome)
37
+ .plus(x
38
+ .minus(translateOutcome)
39
+ .exponentiatedBy(2)
40
+ .minus(a.times(b).times(4))
41
+ .squareRoot())))
42
+ .plus(translatePayout);
43
+ return payout;
44
+ }
45
+ getOutcomeForPayout(payout) {
46
+ const { a, b, c, d, translateOutcome, translatePayout } = this;
47
+ // Inverse function
48
+ // y=(-ad^{2}-bf_{2}^{2}+2bf_{2}x-bx^{2}+df_{1}f_{2}-df_{1}x)/(d(f_{2}-x))
49
+ if (c.eq(0)) {
50
+ const denominator = d.times(translatePayout.minus(payout));
51
+ if (denominator.eq(0)) {
52
+ return BigInt(-1);
53
+ }
54
+ const outcome = a
55
+ .negated()
56
+ .times(d.exponentiatedBy(2))
57
+ .minus(b.times(translatePayout.exponentiatedBy(2)))
58
+ .plus(b.times(translatePayout).times(payout).times(2))
59
+ .minus(b.times(payout.exponentiatedBy(2)))
60
+ .plus(d.times(translateOutcome).times(translatePayout))
61
+ .minus(d.times(translateOutcome).times(payout))
62
+ .dividedBy(denominator)
63
+ .integerValue();
64
+ if (outcome.isFinite())
65
+ return BigInt(outcome.toString());
66
+ return BigInt(-1);
67
+ }
68
+ else {
69
+ // 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)
70
+ throw new Error('Not supported');
71
+ }
72
+ }
73
+ toPayoutCurvePiece() {
74
+ const { a, b, c, d, translateOutcome, translatePayout, positive } = this;
75
+ // Use the constructor with string values to avoid F64 dependency issues
76
+ const piece = new messaging_1.HyperbolaPayoutCurvePiece(positive, translateOutcome.toString(), translatePayout.toString(), a.toString(), b.toString(), c.toString(), d.toString());
77
+ return piece;
78
+ }
79
+ equals(curve) {
80
+ return (this.a.eq(curve.a) &&
81
+ this.b.eq(curve.b) &&
82
+ this.c.eq(curve.c) &&
83
+ this.d.eq(curve.d) &&
84
+ this.translateOutcome.eq(curve.translateOutcome) &&
85
+ this.translatePayout.eq(curve.translatePayout) &&
86
+ this.positive === curve.positive);
87
+ }
88
+ static fromPayoutCurvePiece(piece) {
89
+ // Convert F64 values to BigNumber using toDecimal().toString() to preserve precision
90
+ const a = new bignumber_js_1.default(piece.a.toDecimal().toString());
91
+ const b = new bignumber_js_1.default(piece.b.toDecimal().toString());
92
+ const c = new bignumber_js_1.default(piece.c.toDecimal().toString());
93
+ const d = new bignumber_js_1.default(piece.d.toDecimal().toString());
94
+ const translateOutcome = new bignumber_js_1.default(piece.translateOutcome.toDecimal().toString());
95
+ const translatePayout = new bignumber_js_1.default(piece.translatePayout.toDecimal().toString());
96
+ return new HyperbolaPayoutCurve(a, b, c, d, translateOutcome, translatePayout, piece.usePositivePiece);
97
+ }
98
+ static computePayouts(payoutFunction, totalCollateral, roundingIntervals) {
99
+ if (payoutFunction.payoutFunctionPieces.length !== 1)
100
+ throw new Error('Must have at least one piece');
101
+ const { endPoint, payoutCurvePiece, } = payoutFunction.payoutFunctionPieces[0];
102
+ if (payoutCurvePiece.payoutCurvePieceType !==
103
+ messaging_1.PayoutCurvePieceType.Hyperbola &&
104
+ payoutCurvePiece.type !== messaging_1.MessageType.HyperbolaPayoutCurvePiece &&
105
+ payoutCurvePiece.type !== messaging_1.MessageType.OldHyperbolaPayoutCurvePiece)
106
+ throw new Error('Payout curve piece must be a hyperbola');
107
+ const _payoutCurvePiece = payoutCurvePiece;
108
+ const curve = this.fromPayoutCurvePiece(_payoutCurvePiece);
109
+ // For the new PayoutFunction structure, get the starting point from the hyperbola piece's leftEndPoint
110
+ // This matches the rust-dlc implementation where get_first_outcome() returns left_end_point.event_outcome
111
+ const initialEventOutcome = _payoutCurvePiece.leftEndPoint?.eventOutcome || BigInt(0);
112
+ const initialOutcomePayout = _payoutCurvePiece.leftEndPoint?.outcomePayout || BigInt(0);
113
+ return (0, CETCalculator_1.splitIntoRanges)(initialEventOutcome, endPoint.eventOutcome, initialOutcomePayout, endPoint.outcomePayout, totalCollateral, curve, roundingIntervals.intervals);
114
+ }
115
+ }
116
+ exports.HyperbolaPayoutCurve = HyperbolaPayoutCurve;
117
+ //# sourceMappingURL=HyperbolaPayoutCurve.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HyperbolaPayoutCurve.js","sourceRoot":"","sources":["../../lib/dlc/HyperbolaPayoutCurve.ts"],"names":[],"mappings":";;;;;;AAAA,mDAM6B;AAC7B,gEAAqC;AAGrC,mDAAkD;AAGlD,MAAa,oBAAoB;IAC/B,YACU,CAAY,EACZ,CAAY,EACZ,CAAY,EACZ,CAAY,EACZ,gBAA2B,EAC3B,eAA0B,EAC1B,WAAoB,IAAI;QANxB,MAAC,GAAD,CAAC,CAAW;QACZ,MAAC,GAAD,CAAC,CAAW;QACZ,MAAC,GAAD,CAAC,CAAW;QACZ,MAAC,GAAD,CAAC,CAAW;QACZ,qBAAgB,GAAhB,gBAAgB,CAAW;QAC3B,oBAAe,GAAf,eAAe,CAAW;QAC1B,aAAQ,GAAR,QAAQ,CAAgB;IAC/B,CAAC;IAEJ,SAAS,CAAC,EAAU;QAClB,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,gBAAgB,EAAE,eAAe,EAAE,GAAG,IAAI,CAAC;QAC/D,MAAM,CAAC,GAAG,IAAI,sBAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC;QAEpC,MAAM,MAAM,GAAG,CAAC;aACb,KAAK,CACJ,CAAC;aACE,KAAK,CAAC,gBAAgB,CAAC;aACvB,IAAI,CACH,CA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umber;
4
+ getOutcomeForPayout(y: BigNumber): bigint;
5
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=PayoutCurve.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PayoutCurve.js","sourceRoot":"","sources":["../../lib/dlc/PayoutCurve.ts"],"names":[],"mappings":""}