@atomicfinance/bitcoin-wallet-provider 3.6.1 → 4.0.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 (228) hide show
  1. package/.nvmrc +1 -1
  2. package/.turbo/turbo-lint.log +1 -0
  3. package/.yalc/@node-dlc/core/.nyc_output/687a3c14-9765-45fd-a1fe-f7db3d56fee4.json +1 -0
  4. package/.yalc/@node-dlc/core/.nyc_output/processinfo/687a3c14-9765-45fd-a1fe-f7db3d56fee4.json +1 -0
  5. package/.yalc/@node-dlc/core/.nyc_output/processinfo/index.json +1 -0
  6. package/.yalc/@node-dlc/core/README.md +11 -0
  7. package/.yalc/@node-dlc/core/__tests__/core.spec.ts +7 -0
  8. package/.yalc/@node-dlc/core/__tests__/dlc/CETCalculator.spec.ts +1029 -0
  9. package/.yalc/@node-dlc/core/__tests__/dlc/CoinSelect.spec.ts +179 -0
  10. package/.yalc/@node-dlc/core/__tests__/dlc/PolynomialPayoutCurve.spec.ts +345 -0
  11. package/.yalc/@node-dlc/core/__tests__/dlc/TxBuilder.spec.ts +424 -0
  12. package/.yalc/@node-dlc/core/__tests__/dlc/finance/Builder.spec.ts +492 -0
  13. package/.yalc/@node-dlc/core/__tests__/dlc/finance/CoveredCall.spec.ts +112 -0
  14. package/.yalc/@node-dlc/core/__tests__/dlc/finance/CsoInfo.spec.ts +1124 -0
  15. package/.yalc/@node-dlc/core/__tests__/dlc/finance/LongCall.spec.ts +55 -0
  16. package/.yalc/@node-dlc/core/__tests__/dlc/finance/LongPut.spec.ts +55 -0
  17. package/.yalc/@node-dlc/core/__tests__/dlc/finance/OptionInfo.spec.ts +226 -0
  18. package/.yalc/@node-dlc/core/__tests__/dlc/finance/ShortPut.spec.ts +62 -0
  19. package/.yalc/@node-dlc/core/__tests__/tsconfig.json +8 -0
  20. package/.yalc/@node-dlc/core/__tests__/utils/precision.spec.ts +40 -0
  21. package/.yalc/@node-dlc/core/coverage/lcov-report/base.css +224 -0
  22. package/.yalc/@node-dlc/core/coverage/lcov-report/block-navigation.js +79 -0
  23. package/.yalc/@node-dlc/core/coverage/lcov-report/favicon.png +0 -0
  24. package/.yalc/@node-dlc/core/coverage/lcov-report/index.html +171 -0
  25. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/AsyncProcessingQueue.ts.html +269 -0
  26. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/Base32.ts.html +86 -0
  27. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/BigIntUtils.ts.html +86 -0
  28. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/BitField.ts.html +86 -0
  29. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ChannelId.ts.html +86 -0
  30. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/LinkedList.ts.html +413 -0
  31. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/LinkedListNode.ts.html +122 -0
  32. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/Queue.ts.html +188 -0
  33. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ShortChannelId.ts.html +86 -0
  34. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/ShortChannelIdUtils.ts.html +107 -0
  35. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/CETCalculator.ts.html +1430 -0
  36. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/CoinSelect.ts.html +431 -0
  37. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/HyperbolaPayoutCurve.ts.html +644 -0
  38. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/PolynomialPayoutCurve.ts.html +878 -0
  39. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/TxBuilder.ts.html +1088 -0
  40. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/TxFinalizer.ts.html +731 -0
  41. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/Builder.ts.html +2270 -0
  42. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/CoveredCall.ts.html +410 -0
  43. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/CsoInfo.ts.html +1574 -0
  44. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LinearPayout.ts.html +362 -0
  45. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LongCall.ts.html +302 -0
  46. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/LongPut.ts.html +302 -0
  47. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/OptionInfo.ts.html +626 -0
  48. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/ShortPut.ts.html +305 -0
  49. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/finance/index.html +216 -0
  50. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/dlc/index.html +186 -0
  51. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/index.html +261 -0
  52. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/index.ts.html +278 -0
  53. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ChannelId.ts.html +242 -0
  54. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ChannelKeys.ts.html +407 -0
  55. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentNumber.ts.html +362 -0
  56. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentSecret.ts.html +203 -0
  57. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/CommitmentSecretStore.ts.html +332 -0
  58. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/Htlc.ts.html +236 -0
  59. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/HtlcDirection.ts.html +131 -0
  60. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/ScriptFactory.ts.html +530 -0
  61. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/TxFactory.ts.html +1094 -0
  62. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/lightning/index.html +231 -0
  63. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/BigIntUtils.ts.html +272 -0
  64. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/Precision.ts.html +149 -0
  65. package/.yalc/@node-dlc/core/coverage/lcov-report/lib/utils/index.html +126 -0
  66. package/.yalc/@node-dlc/core/coverage/lcov-report/prettify.css +1 -0
  67. package/.yalc/@node-dlc/core/coverage/lcov-report/prettify.js +2 -0
  68. package/.yalc/@node-dlc/core/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  69. package/.yalc/@node-dlc/core/coverage/lcov-report/sorter.js +170 -0
  70. package/.yalc/@node-dlc/core/coverage/lcov.info +2426 -0
  71. package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.d.ts +22 -0
  72. package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.js +55 -0
  73. package/.yalc/@node-dlc/core/dist/AsyncProcessingQueue.js.map +1 -0
  74. package/.yalc/@node-dlc/core/dist/Base32.d.ts +1 -0
  75. package/.yalc/@node-dlc/core/dist/Base32.js +7 -0
  76. package/.yalc/@node-dlc/core/dist/Base32.js.map +1 -0
  77. package/.yalc/@node-dlc/core/dist/BigIntUtils.d.ts +1 -0
  78. package/.yalc/@node-dlc/core/dist/BigIntUtils.js +8 -0
  79. package/.yalc/@node-dlc/core/dist/BigIntUtils.js.map +1 -0
  80. package/.yalc/@node-dlc/core/dist/BitField.d.ts +1 -0
  81. package/.yalc/@node-dlc/core/dist/BitField.js +7 -0
  82. package/.yalc/@node-dlc/core/dist/BitField.js.map +1 -0
  83. package/.yalc/@node-dlc/core/dist/ChannelId.d.ts +1 -0
  84. package/.yalc/@node-dlc/core/dist/ChannelId.js +7 -0
  85. package/.yalc/@node-dlc/core/dist/ChannelId.js.map +1 -0
  86. package/.yalc/@node-dlc/core/dist/LinkedList.d.ts +30 -0
  87. package/.yalc/@node-dlc/core/dist/LinkedList.js +104 -0
  88. package/.yalc/@node-dlc/core/dist/LinkedList.js.map +1 -0
  89. package/.yalc/@node-dlc/core/dist/LinkedListNode.d.ts +9 -0
  90. package/.yalc/@node-dlc/core/dist/LinkedListNode.js +15 -0
  91. package/.yalc/@node-dlc/core/dist/LinkedListNode.js.map +1 -0
  92. package/.yalc/@node-dlc/core/dist/Queue.d.ts +22 -0
  93. package/.yalc/@node-dlc/core/dist/Queue.js +38 -0
  94. package/.yalc/@node-dlc/core/dist/Queue.js.map +1 -0
  95. package/.yalc/@node-dlc/core/dist/ShortChannelId.d.ts +1 -0
  96. package/.yalc/@node-dlc/core/dist/ShortChannelId.js +7 -0
  97. package/.yalc/@node-dlc/core/dist/ShortChannelId.js.map +1 -0
  98. package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.d.ts +1 -0
  99. package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.js +12 -0
  100. package/.yalc/@node-dlc/core/dist/ShortChannelIdUtils.js.map +1 -0
  101. package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.d.ts +50 -0
  102. package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.js +332 -0
  103. package/.yalc/@node-dlc/core/dist/dlc/CETCalculator.js.map +1 -0
  104. package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.d.ts +23 -0
  105. package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.js +77 -0
  106. package/.yalc/@node-dlc/core/dist/dlc/CoinSelect.js.map +1 -0
  107. package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.d.ts +20 -0
  108. package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.js +117 -0
  109. package/.yalc/@node-dlc/core/dist/dlc/HyperbolaPayoutCurve.js.map +1 -0
  110. package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.d.ts +5 -0
  111. package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.js +3 -0
  112. package/.yalc/@node-dlc/core/dist/dlc/PayoutCurve.js.map +1 -0
  113. package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.d.ts +52 -0
  114. package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.js +173 -0
  115. package/.yalc/@node-dlc/core/dist/dlc/PolynomialPayoutCurve.js.map +1 -0
  116. package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.d.ts +55 -0
  117. package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.js +217 -0
  118. package/.yalc/@node-dlc/core/dist/dlc/TxBuilder.js.map +1 -0
  119. package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.d.ts +40 -0
  120. package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.js +146 -0
  121. package/.yalc/@node-dlc/core/dist/dlc/TxFinalizer.js.map +1 -0
  122. package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.d.ts +193 -0
  123. package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.js +422 -0
  124. package/.yalc/@node-dlc/core/dist/dlc/finance/Builder.js.map +1 -0
  125. package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.d.ts +13 -0
  126. package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.js +67 -0
  127. package/.yalc/@node-dlc/core/dist/dlc/finance/CoveredCall.js.map +1 -0
  128. package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.d.ts +114 -0
  129. package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.js +290 -0
  130. package/.yalc/@node-dlc/core/dist/dlc/finance/CsoInfo.js.map +1 -0
  131. package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.d.ts +6 -0
  132. package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.js +79 -0
  133. package/.yalc/@node-dlc/core/dist/dlc/finance/LinearPayout.js.map +1 -0
  134. package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.d.ts +13 -0
  135. package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.js +47 -0
  136. package/.yalc/@node-dlc/core/dist/dlc/finance/LongCall.js.map +1 -0
  137. package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.d.ts +13 -0
  138. package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.js +47 -0
  139. package/.yalc/@node-dlc/core/dist/dlc/finance/LongPut.js.map +1 -0
  140. package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.d.ts +20 -0
  141. package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.js +101 -0
  142. package/.yalc/@node-dlc/core/dist/dlc/finance/OptionInfo.js.map +1 -0
  143. package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.d.ts +12 -0
  144. package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.js +46 -0
  145. package/.yalc/@node-dlc/core/dist/dlc/finance/ShortPut.js.map +1 -0
  146. package/.yalc/@node-dlc/core/dist/index.d.ts +39 -0
  147. package/.yalc/@node-dlc/core/dist/index.js +78 -0
  148. package/.yalc/@node-dlc/core/dist/index.js.map +1 -0
  149. package/.yalc/@node-dlc/core/dist/lightning/ChannelId.d.ts +31 -0
  150. package/.yalc/@node-dlc/core/dist/lightning/ChannelId.js +52 -0
  151. package/.yalc/@node-dlc/core/dist/lightning/ChannelId.js.map +1 -0
  152. package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.d.ts +55 -0
  153. package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.js +105 -0
  154. package/.yalc/@node-dlc/core/dist/lightning/ChannelKeys.js.map +1 -0
  155. package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.d.ts +55 -0
  156. package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.js +76 -0
  157. package/.yalc/@node-dlc/core/dist/lightning/CommitmentNumber.js.map +1 -0
  158. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.d.ts +26 -0
  159. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.js +44 -0
  160. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecret.js.map +1 -0
  161. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.d.ts +41 -0
  162. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.js +79 -0
  163. package/.yalc/@node-dlc/core/dist/lightning/CommitmentSecretStore.js.map +1 -0
  164. package/.yalc/@node-dlc/core/dist/lightning/Htlc.d.ts +46 -0
  165. package/.yalc/@node-dlc/core/dist/lightning/Htlc.js +46 -0
  166. package/.yalc/@node-dlc/core/dist/lightning/Htlc.js.map +1 -0
  167. package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.d.ts +16 -0
  168. package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.js +21 -0
  169. package/.yalc/@node-dlc/core/dist/lightning/HtlcDirection.js.map +1 -0
  170. package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.d.ts +76 -0
  171. package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.js +102 -0
  172. package/.yalc/@node-dlc/core/dist/lightning/ScriptFactory.js.map +1 -0
  173. package/.yalc/@node-dlc/core/dist/lightning/TxFactory.d.ts +83 -0
  174. package/.yalc/@node-dlc/core/dist/lightning/TxFactory.js +222 -0
  175. package/.yalc/@node-dlc/core/dist/lightning/TxFactory.js.map +1 -0
  176. package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.d.ts +9 -0
  177. package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.js +59 -0
  178. package/.yalc/@node-dlc/core/dist/utils/BigIntUtils.js.map +1 -0
  179. package/.yalc/@node-dlc/core/dist/utils/Precision.d.ts +13 -0
  180. package/.yalc/@node-dlc/core/dist/utils/Precision.js +28 -0
  181. package/.yalc/@node-dlc/core/dist/utils/Precision.js.map +1 -0
  182. package/.yalc/@node-dlc/core/lib/AsyncProcessingQueue.ts +63 -0
  183. package/.yalc/@node-dlc/core/lib/Base32.ts +2 -0
  184. package/.yalc/@node-dlc/core/lib/BigIntUtils.ts +2 -0
  185. package/.yalc/@node-dlc/core/lib/BitField.ts +2 -0
  186. package/.yalc/@node-dlc/core/lib/ChannelId.ts +2 -0
  187. package/.yalc/@node-dlc/core/lib/LinkedList.ts +111 -0
  188. package/.yalc/@node-dlc/core/lib/LinkedListNode.ts +14 -0
  189. package/.yalc/@node-dlc/core/lib/Queue.ts +36 -0
  190. package/.yalc/@node-dlc/core/lib/ShortChannelId.ts +2 -0
  191. package/.yalc/@node-dlc/core/lib/ShortChannelIdUtils.ts +9 -0
  192. package/.yalc/@node-dlc/core/lib/dlc/CETCalculator.ts +450 -0
  193. package/.yalc/@node-dlc/core/lib/dlc/CoinSelect.ts +117 -0
  194. package/.yalc/@node-dlc/core/lib/dlc/HyperbolaPayoutCurve.ts +188 -0
  195. package/.yalc/@node-dlc/core/lib/dlc/PayoutCurve.ts +6 -0
  196. package/.yalc/@node-dlc/core/lib/dlc/PolynomialPayoutCurve.ts +266 -0
  197. package/.yalc/@node-dlc/core/lib/dlc/TxBuilder.ts +336 -0
  198. package/.yalc/@node-dlc/core/lib/dlc/TxFinalizer.ts +217 -0
  199. package/.yalc/@node-dlc/core/lib/dlc/finance/Builder.ts +730 -0
  200. package/.yalc/@node-dlc/core/lib/dlc/finance/CoveredCall.ts +110 -0
  201. package/.yalc/@node-dlc/core/lib/dlc/finance/CsoInfo.ts +498 -0
  202. package/.yalc/@node-dlc/core/lib/dlc/finance/LinearPayout.ts +94 -0
  203. package/.yalc/@node-dlc/core/lib/dlc/finance/LongCall.ts +74 -0
  204. package/.yalc/@node-dlc/core/lib/dlc/finance/LongPut.ts +74 -0
  205. package/.yalc/@node-dlc/core/lib/dlc/finance/OptionInfo.ts +182 -0
  206. package/.yalc/@node-dlc/core/lib/dlc/finance/ShortPut.ts +75 -0
  207. package/.yalc/@node-dlc/core/lib/index.ts +66 -0
  208. package/.yalc/@node-dlc/core/lib/lightning/ChannelId.ts +54 -0
  209. package/.yalc/@node-dlc/core/lib/lightning/ChannelKeys.ts +109 -0
  210. package/.yalc/@node-dlc/core/lib/lightning/CommitmentNumber.ts +94 -0
  211. package/.yalc/@node-dlc/core/lib/lightning/CommitmentSecret.ts +41 -0
  212. package/.yalc/@node-dlc/core/lib/lightning/CommitmentSecretStore.ts +84 -0
  213. package/.yalc/@node-dlc/core/lib/lightning/Htlc.ts +52 -0
  214. package/.yalc/@node-dlc/core/lib/lightning/HtlcDirection.ts +17 -0
  215. package/.yalc/@node-dlc/core/lib/lightning/ScriptFactory.ts +150 -0
  216. package/.yalc/@node-dlc/core/lib/lightning/TxFactory.ts +338 -0
  217. package/.yalc/@node-dlc/core/lib/utils/BigIntUtils.ts +64 -0
  218. package/.yalc/@node-dlc/core/lib/utils/Precision.ts +23 -0
  219. package/.yalc/@node-dlc/core/package.json +38 -0
  220. package/.yalc/@node-dlc/core/tsconfig.json +8 -0
  221. package/.yalc/@node-dlc/core/yalc.sig +1 -0
  222. package/CHANGELOG.md +112 -0
  223. package/dist/BitcoinWalletProvider.d.ts +4 -2
  224. package/dist/BitcoinWalletProvider.js +1 -0
  225. package/dist/BitcoinWalletProvider.js.map +1 -1
  226. package/lib/BitcoinWalletProvider.ts +13 -4
  227. package/package.json +5 -5
  228. package/yalc.lock +10 -0
@@ -0,0 +1,730 @@
1
+ import { Value } from '@node-dlc/bitcoin';
2
+ import {
3
+ DigitDecompositionEventDescriptorV0,
4
+ MessageType,
5
+ NumericalDescriptor,
6
+ OracleAnnouncement,
7
+ OrderOffer,
8
+ OrderPositionInfo,
9
+ PayoutFunction,
10
+ RoundingIntervals,
11
+ SingleContractInfo,
12
+ SingleOracleInfo,
13
+ } from '@node-dlc/messaging';
14
+ import {
15
+ BitcoinNetwork,
16
+ BitcoinNetworks,
17
+ chainHashFromNetwork,
18
+ } from 'bitcoin-networks';
19
+ import Decimal from 'decimal.js';
20
+
21
+ import { dustThreshold } from '../CoinSelect';
22
+ import { getFinalizerByCount } from '../TxFinalizer';
23
+ import { CoveredCall } from './CoveredCall';
24
+ import { LinearPayout } from './LinearPayout';
25
+ import { LongCall } from './LongCall';
26
+ import { LongPut } from './LongPut';
27
+ import { ShortPut } from './ShortPut';
28
+
29
+ export const UNIT_MULTIPLIER = {
30
+ bits: BigInt(1e2),
31
+ sats: BigInt(1),
32
+ };
33
+
34
+ /**
35
+ * Round a number to the nearest multiple of a given multiplier.
36
+ *
37
+ * @param num - The number to be rounded.
38
+ * @param multiplier - The multiplier to round to.
39
+ * @returns The number rounded to the nearest multiple of the multiplier.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * // Example: rounding to nearest 100
44
+ * const number = BigInt(354);
45
+ * const multiplier = BigInt(100);
46
+ * const roundedNumber = roundToNearestMultiplier(number, multiplier);
47
+ * console.log(roundedNumber); // Output: 300
48
+ * ```
49
+ */
50
+ export const roundToNearestMultiplier = (
51
+ num: bigint,
52
+ multiplier: bigint,
53
+ ): bigint => (num / multiplier) * multiplier;
54
+
55
+ /**
56
+ * Round a number up to the nearest multiple of a given multiplier.
57
+ *
58
+ * @param num - The number to be rounded.
59
+ * @param multiplier - The multiplier to round to.
60
+ * @returns The number rounded up to the nearest multiple of the multiplier.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * // Example: rounding up to nearest 100
65
+ * const number = BigInt(354);
66
+ * const multiplier = BigInt(100);
67
+ * const roundedNumber = roundToNearestMultiplier(number, multiplier);
68
+ * console.log(roundedNumber); // Output: 400
69
+ * ```
70
+ */
71
+ export const roundUpToNearestMultiplier = (
72
+ num: bigint,
73
+ multiplier: bigint,
74
+ ): bigint => ((num + multiplier - BigInt(1)) / multiplier) * multiplier;
75
+
76
+ export const roundDownToNearestMultiplier = (
77
+ num: bigint,
78
+ multiplier: bigint,
79
+ ): bigint => num - (num % multiplier);
80
+
81
+ export type DlcParty = 'offeror' | 'acceptor' | 'neither';
82
+
83
+ /**
84
+ * Compute rounding intervals for a linear or hyperbola payout curve
85
+ *
86
+ * @param {number | bigint | Value} rounding rounding interval in sats
87
+ * @param {number | bigint | Value} contractSize contract size in sats
88
+ * @returns rounding mod for contract size
89
+ */
90
+ export const computeRoundingModulus = (
91
+ rounding: number | bigint | Value,
92
+ contractSize: number | bigint | Value,
93
+ ): bigint => {
94
+ const roundingInSats =
95
+ rounding instanceof Value
96
+ ? rounding.sats
97
+ : typeof rounding === 'number'
98
+ ? BigInt(Math.round(rounding * 1e8)) // Convert bitcoin amount to satoshis
99
+ : rounding;
100
+
101
+ const contractSizeInSats =
102
+ contractSize instanceof Value
103
+ ? contractSize.sats
104
+ : typeof contractSize === 'number'
105
+ ? BigInt(Math.round(contractSize * 1e8)) // Convert bitcoin amount to satoshis
106
+ : contractSize;
107
+
108
+ return (roundingInSats * contractSizeInSats) / BigInt(1e8);
109
+ };
110
+
111
+ /**
112
+ * Get digit decomposition event descriptor from oracle announcement
113
+ *
114
+ * @param {OracleAnnouncement} announcement oracle announcement
115
+ * @returns {DigitDecompositionEventDescriptorV0} event descriptor
116
+ */
117
+ export const getDigitDecompositionEventDescriptor = (
118
+ announcement: OracleAnnouncement,
119
+ ): DigitDecompositionEventDescriptorV0 => {
120
+ if (
121
+ announcement.oracleEvent.eventDescriptor.type !==
122
+ MessageType.DigitDecompositionEventDescriptorV0
123
+ )
124
+ throw Error('Only DigitDecompositionEventDescriptorV0 currently supported');
125
+
126
+ const eventDescriptor = announcement.oracleEvent
127
+ .eventDescriptor as DigitDecompositionEventDescriptorV0;
128
+
129
+ return eventDescriptor;
130
+ };
131
+
132
+ /**
133
+ * Build an orderoffer for ContractDescriptorV1
134
+ *
135
+ * @param {OracleAnnouncement} announcement oracle announcement
136
+ * @param {bigint} totalCollateral total collateral in satoshis
137
+ * @param {bigint} offerCollateral offer collateral in satoshis
138
+ * @param {PayoutFunction} payoutFunction
139
+ * @param {RoundingIntervals} roundingIntervals
140
+ * @param {bigint} feePerByte sats/vbyte
141
+ * @param {NetworkName} network
142
+ * @returns {OrderOffer} Returns order offer
143
+ */
144
+ export const buildOrderOffer = (
145
+ announcement: OracleAnnouncement,
146
+ totalCollateral: bigint,
147
+ offerCollateral: bigint,
148
+ payoutFunction: PayoutFunction,
149
+ roundingIntervals: RoundingIntervals,
150
+ feePerByte: bigint,
151
+ network: string,
152
+ ): OrderOffer => {
153
+ const eventDescriptor = getDigitDecompositionEventDescriptor(announcement);
154
+
155
+ const contractDescriptor = new NumericalDescriptor();
156
+ contractDescriptor.numDigits = eventDescriptor.nbDigits;
157
+ contractDescriptor.payoutFunction = payoutFunction;
158
+ contractDescriptor.roundingIntervals = roundingIntervals;
159
+
160
+ const oracleInfo = new SingleOracleInfo();
161
+ oracleInfo.announcement = announcement;
162
+
163
+ const contractInfo = new SingleContractInfo();
164
+ contractInfo.totalCollateral = totalCollateral;
165
+ contractInfo.contractDescriptor = contractDescriptor;
166
+ contractInfo.oracleInfo = oracleInfo;
167
+
168
+ const orderOffer = new OrderOffer();
169
+
170
+ // Set required fields for OrderOffer
171
+ orderOffer.contractFlags = Buffer.from('00', 'hex'); // Default contract flags
172
+ // Generate a random 32-byte temporary contract ID
173
+ orderOffer.temporaryContractId = Buffer.from(
174
+ Array.from({ length: 32 }, () => Math.floor(Math.random() * 256)),
175
+ );
176
+ orderOffer.chainHash = chainHashFromNetwork(BitcoinNetworks[network]);
177
+ orderOffer.contractInfo = contractInfo;
178
+ orderOffer.offerCollateral = offerCollateral; // Use correct property name
179
+ orderOffer.feeRatePerVb = feePerByte;
180
+ orderOffer.cetLocktime = Math.floor(new Date().getTime() / 1000); // set to current time
181
+ orderOffer.refundLocktime =
182
+ announcement.oracleEvent.eventMaturityEpoch + 2419200; // 4 weeks after maturity
183
+
184
+ return orderOffer;
185
+ };
186
+
187
+ /**
188
+ * Builds an order offer for a covered call or short put
189
+ *
190
+ * @param {OracleAnnouncement} announcement oracle announcement
191
+ * @param {number} contractSize contract size in satoshis
192
+ * @param {number} strikePrice strike price of contract
193
+ * @param {number} premium premium of contract in satoshis
194
+ * @param {number | bigint} feePerByte sats/vbyte
195
+ * @param {number} rounding rounding interval
196
+ * @param {string} network bitcoin network type
197
+ * @param {string} type call or put
198
+ * @param {number} _totalCollateral total collateral in satoshis (applicable only for short put)
199
+ * @returns {OrderOffer} Returns order offer
200
+ */
201
+ export const buildOptionOrderOffer = (
202
+ announcement: OracleAnnouncement,
203
+ contractSize: Value,
204
+ strikePrice: number,
205
+ premium: Value,
206
+ feePerByte: number | bigint,
207
+ rounding: number,
208
+ network: string,
209
+ type: 'call' | 'put',
210
+ direction: 'long' | 'short',
211
+ _totalCollateral?: Value,
212
+ ): OrderOffer => {
213
+ const eventDescriptor = getDigitDecompositionEventDescriptor(announcement);
214
+
215
+ let totalCollateral: bigint;
216
+ let payoutFunctionInfo: {
217
+ payoutFunction: PayoutFunction;
218
+ totalCollateral?: bigint;
219
+ };
220
+
221
+ const roundingIntervals = new RoundingIntervals();
222
+ const roundingMod = computeRoundingModulus(rounding, contractSize);
223
+
224
+ if (direction === 'short') {
225
+ if (type === 'call') {
226
+ payoutFunctionInfo = CoveredCall.buildPayoutFunction(
227
+ BigInt(strikePrice),
228
+ contractSize.sats,
229
+ eventDescriptor.base,
230
+ eventDescriptor.nbDigits,
231
+ );
232
+
233
+ totalCollateral = payoutFunctionInfo.totalCollateral;
234
+ roundingIntervals.intervals = [
235
+ {
236
+ beginInterval: BigInt(0),
237
+ roundingMod: BigInt(1),
238
+ },
239
+ {
240
+ beginInterval: BigInt(strikePrice),
241
+ roundingMod,
242
+ },
243
+ ];
244
+ } else {
245
+ payoutFunctionInfo = ShortPut.buildPayoutFunction(
246
+ BigInt(strikePrice),
247
+ contractSize.sats,
248
+ _totalCollateral.sats,
249
+ eventDescriptor.base,
250
+ eventDescriptor.nbDigits,
251
+ );
252
+ totalCollateral = _totalCollateral.sats;
253
+ roundingIntervals.intervals = [
254
+ {
255
+ beginInterval: BigInt(0),
256
+ roundingMod,
257
+ },
258
+ {
259
+ beginInterval: BigInt(strikePrice),
260
+ roundingMod: BigInt(1),
261
+ },
262
+ ];
263
+ }
264
+ } else {
265
+ totalCollateral = _totalCollateral.sats;
266
+
267
+ if (type === 'call') {
268
+ payoutFunctionInfo = LongCall.buildPayoutFunction(
269
+ BigInt(strikePrice),
270
+ contractSize.sats,
271
+ totalCollateral,
272
+ eventDescriptor.base,
273
+ eventDescriptor.nbDigits,
274
+ );
275
+
276
+ roundingIntervals.intervals = [
277
+ {
278
+ beginInterval: BigInt(0),
279
+ roundingMod: BigInt(1),
280
+ },
281
+ {
282
+ beginInterval: BigInt(strikePrice),
283
+ roundingMod,
284
+ },
285
+ ];
286
+ } else {
287
+ payoutFunctionInfo = LongPut.buildPayoutFunction(
288
+ BigInt(strikePrice),
289
+ contractSize.sats,
290
+ totalCollateral,
291
+ eventDescriptor.base,
292
+ eventDescriptor.nbDigits,
293
+ );
294
+
295
+ roundingIntervals.intervals = [
296
+ {
297
+ beginInterval: BigInt(0),
298
+ roundingMod,
299
+ },
300
+ {
301
+ beginInterval: BigInt(strikePrice),
302
+ roundingMod: BigInt(1),
303
+ },
304
+ ];
305
+ }
306
+ }
307
+
308
+ const payoutFunction = payoutFunctionInfo.payoutFunction;
309
+
310
+ const offerCollateral =
311
+ direction === 'short' ? totalCollateral - premium.sats : premium.sats;
312
+
313
+ return buildOrderOffer(
314
+ announcement,
315
+ totalCollateral,
316
+ offerCollateral,
317
+ payoutFunction,
318
+ roundingIntervals,
319
+ BigInt(feePerByte),
320
+ network,
321
+ );
322
+ };
323
+
324
+ /**
325
+ * Builds an order offer for a covered call
326
+ *
327
+ * @param {OracleAnnouncement} announcement oracle announcement
328
+ * @param {number} contractSize contract size in satoshis
329
+ * @param {number} strikePrice strike price of contract
330
+ * @param {number} premium premium of contract in satoshis
331
+ * @param {number} feePerByte sats/vbyte
332
+ * @param {number} rounding rounding interval
333
+ * @param {string} network bitcoin network type
334
+ * @returns {OrderOffer} Returns order offer
335
+ */
336
+ export const buildCoveredCallOrderOffer = (
337
+ announcement: OracleAnnouncement,
338
+ contractSize: Value,
339
+ strikePrice: number,
340
+ premium: Value,
341
+ feePerByte: number,
342
+ rounding: number,
343
+ network: string,
344
+ ): OrderOffer => {
345
+ return buildOptionOrderOffer(
346
+ announcement,
347
+ contractSize,
348
+ strikePrice,
349
+ premium,
350
+ feePerByte,
351
+ rounding,
352
+ network,
353
+ 'call',
354
+ 'short',
355
+ );
356
+ };
357
+
358
+ /**
359
+ * Builds an order offer for a short put
360
+ *
361
+ * @param {OracleAnnouncement} announcement oracle announcement
362
+ * @param {number} contractSize contract size in satoshis
363
+ * @param {number} strikePrice strike price of contract
364
+ * @param {number} totalCollateral total collateral in satoshis
365
+ * @param {number} premium premium of contract in satoshis
366
+ * @param {number} feePerByte sats/vbyte
367
+ * @param {number} rounding rounding interval
368
+ * @param {string} network bitcoin network type
369
+ * @returns {OrderOffer} Returns order offer
370
+ */
371
+ export const buildShortPutOrderOffer = (
372
+ announcement: OracleAnnouncement,
373
+ contractSize: Value,
374
+ strikePrice: number,
375
+ totalCollateral: Value,
376
+ premium: Value,
377
+ feePerByte: number,
378
+ rounding: number,
379
+ network: string,
380
+ ): OrderOffer => {
381
+ return buildOptionOrderOffer(
382
+ announcement,
383
+ contractSize,
384
+ strikePrice,
385
+ premium,
386
+ feePerByte,
387
+ rounding,
388
+ network,
389
+ 'put',
390
+ 'short',
391
+ totalCollateral,
392
+ );
393
+ };
394
+
395
+ /**
396
+ * Builds an order offer for a long call
397
+ *
398
+ * @param {OracleAnnouncement} announcement oracle announcement
399
+ * @param {number} contractSize contract size in satoshis
400
+ * @param {number} strikePrice strike price of contract
401
+ * @param {number} maxGain maximum amount that can be gained (totalCollateral)
402
+ * @param {number} premium premium of contract in satoshis
403
+ * @param {number} feePerByte sats/vbyte
404
+ * @param {number} rounding rounding interval
405
+ * @param {string} network bitcoin network type
406
+ * @returns {OrderOffer} Returns order offer
407
+ */
408
+ export const buildLongCallOrderOffer = (
409
+ announcement: OracleAnnouncement,
410
+ contractSize: Value,
411
+ strikePrice: number,
412
+ maxGain: Value,
413
+ premium: Value,
414
+ feePerByte: number,
415
+ rounding: number,
416
+ network: string,
417
+ ): OrderOffer => {
418
+ return buildOptionOrderOffer(
419
+ announcement,
420
+ contractSize,
421
+ strikePrice,
422
+ premium,
423
+ feePerByte,
424
+ rounding,
425
+ network,
426
+ 'call',
427
+ 'long',
428
+ maxGain,
429
+ );
430
+ };
431
+
432
+ /**
433
+ * Builds an order offer for a long put
434
+ *
435
+ * @param {OracleAnnouncement} announcement oracle announcement
436
+ * @param {number} contractSize contract size in satoshis
437
+ * @param {number} strikePrice strike price of contract
438
+ * @param {number} maxGain maximum amount that can be gained (totalCollateral)
439
+ * @param {number} premium premium of contract in satoshis
440
+ * @param {number} feePerByte sats/vbyte
441
+ * @param {number} rounding rounding interval
442
+ * @param {string} network bitcoin network type
443
+ * @returns {OrderOffer} Returns order offer
444
+ */
445
+ export const buildLongPutOrderOffer = (
446
+ announcement: OracleAnnouncement,
447
+ contractSize: Value,
448
+ strikePrice: number,
449
+ maxGain: Value,
450
+ premium: Value,
451
+ feePerByte: number,
452
+ rounding: number,
453
+ network: string,
454
+ ): OrderOffer => {
455
+ return buildOptionOrderOffer(
456
+ announcement,
457
+ contractSize,
458
+ strikePrice,
459
+ premium,
460
+ feePerByte,
461
+ rounding,
462
+ network,
463
+ 'put',
464
+ 'long',
465
+ maxGain,
466
+ );
467
+ };
468
+
469
+ /**
470
+ * Builds an order offer for a linear curve
471
+ *
472
+ * @param {OracleAnnouncement} announcement oracle announcement
473
+ * @param {Value} offerCollateral offer collateral amount
474
+ * @param {Value} minPayout minimum payout
475
+ * @param {Value} maxPayout maximum payout (also total collateral)
476
+ * @param {bigint} startOutcome oracle outcome (in bits or sats)
477
+ * @param {bigint} endOutcome oracle outcome (in bits or sats)
478
+ * @param {bigint} feePerByte sats/vbyte
479
+ * @param {Value} rounding rounding mod for RoundingInterval
480
+ * @param {BitcoinNetwork} network bitcoin, bitcoin_testnet or bitcoin_regtest
481
+ * @param {DlcParty} [shiftForFees] shift for offerer, acceptor or neither (who should pay fees)
482
+ * @param {Value} [fees] fees to shift
483
+ * @returns {OrderOffer}
484
+ */
485
+ export const buildLinearOrderOffer = (
486
+ announcement: OracleAnnouncement,
487
+ offerCollateral: Value,
488
+ minPayout: Value,
489
+ maxPayout: Value,
490
+ startOutcome: bigint,
491
+ endOutcome: bigint,
492
+ feePerByte: bigint,
493
+ roundingIntervals: RoundingIntervals,
494
+ network: BitcoinNetwork,
495
+ shiftForFees: DlcParty = 'neither',
496
+ fees: Value = Value.fromSats(0),
497
+ ): OrderOffer => {
498
+ if (maxPayout.lt(minPayout))
499
+ throw Error('maxPayout must be greater than minPayout');
500
+ if (endOutcome < startOutcome)
501
+ throw Error('endOutcome must be greater than startOutcome');
502
+
503
+ const eventDescriptor = getDigitDecompositionEventDescriptor(announcement);
504
+
505
+ const totalCollateral = maxPayout.sats;
506
+
507
+ const { payoutFunction } = LinearPayout.buildPayoutFunction(
508
+ minPayout.sats,
509
+ maxPayout.sats,
510
+ startOutcome,
511
+ endOutcome,
512
+ eventDescriptor.base,
513
+ eventDescriptor.nbDigits,
514
+ );
515
+
516
+ const orderOffer = buildOrderOffer(
517
+ announcement,
518
+ totalCollateral,
519
+ offerCollateral.sats,
520
+ payoutFunction,
521
+ roundingIntervals,
522
+ feePerByte,
523
+ network.name,
524
+ );
525
+
526
+ const positionInfo = new OrderPositionInfo();
527
+ positionInfo.shiftForFees = shiftForFees;
528
+ positionInfo.fees = shiftForFees === 'neither' ? BigInt(0) : fees.sats;
529
+ orderOffer.positionInfo = positionInfo;
530
+
531
+ return orderOffer;
532
+ };
533
+
534
+ /**
535
+ * Builds a custom strategy order offer
536
+ *
537
+ * Calculates offer fees
538
+ * calculates the minimum max gain
539
+ * maxLoss and maxGain are normalized to 1 BTC contracts
540
+ *
541
+ * shiftForFees 'offeror' means 'offeror' pays network fees
542
+ * shiftForFees 'acceptor' means 'acceptor' pays network fees
543
+ *
544
+ * numContracts refers to the number of DLCs in the funding transaction
545
+ * if it's not a batch dlc funding transaction, then this is not relevant
546
+ *
547
+ * @param {OracleAnnouncement} announcement oracle announcement
548
+ * @param {Value} contractSize contract size
549
+ * @param {Value} normalizedMaxLoss maximum amount that can be lost based on 1 BTC contract
550
+ * @param {Value} normalizedMaxGain maximum amount that can be gained based on 1 BTC contract
551
+ * @param {bigint} feePerByte sats/vbyte
552
+ * @param {Value} roundingIntervals rounding mod for RoundingInterval
553
+ * @param {BitcoinNetwork} network bitcoin, bitcoin_testnet or bitcoin_regtest
554
+ * @param {DlcParty} [shiftForFees] shift for offerer, acceptor or neither
555
+ * @param {Value} [fees_] fees to shift
556
+ * @param {Value} [collateral] collateral to use
557
+ * @param {number} [numOfferInputs] number of inputs to use
558
+ * @param {number} [numContracts] number of DLCs in the funding transaction
559
+ *
560
+ * @returns {OrderOffer}
561
+ */
562
+ export const buildCustomStrategyOrderOffer = (
563
+ announcement: OracleAnnouncement,
564
+ contractSize: Value,
565
+ normalizedMaxLoss: Value,
566
+ normalizedMaxGain: Value,
567
+ feePerByte: bigint,
568
+ roundingIntervals: RoundingIntervals,
569
+ network: BitcoinNetwork,
570
+ shiftForFees: DlcParty = 'neither',
571
+ fees_: Value = Value.fromSats(0), // NOTE: fees should be divided before doing batch transaction
572
+ collateral: Value = Value.fromSats(0),
573
+ numOfferInputs = 1,
574
+ numContracts = 1,
575
+ skipValidation = false,
576
+ ): OrderOffer => {
577
+ if (contractSize.eq(Value.zero())) {
578
+ throw Error('contractSize must be greater than 0');
579
+ }
580
+
581
+ if (collateral.eq(Value.zero())) {
582
+ collateral = contractSize.clone();
583
+ }
584
+
585
+ const eventDescriptor = getDigitDecompositionEventDescriptor(announcement);
586
+
587
+ const unit = eventDescriptor.unit;
588
+
589
+ const fees = Value.fromSats(
590
+ Number(new Decimal(Number(fees_.sats)).dividedBy(numContracts).toFixed(0)),
591
+ );
592
+
593
+ const finalizer = getFinalizerByCount(
594
+ feePerByte,
595
+ numOfferInputs,
596
+ 1,
597
+ numContracts,
598
+ );
599
+ const offerFees = Value.fromSats(finalizer.offerFees);
600
+
601
+ // Use offerFees + dustThreshold for min max gain, to ensure in the case of
602
+ // 0 PnL the acceptor payout is not dust
603
+ const minMaxGainForContractSize_ = offerFees.addn(
604
+ Value.fromSats(dustThreshold(BigInt(feePerByte))),
605
+ );
606
+ const minMaxGainForContractSize = Value.fromSats(
607
+ roundUpToNearestMultiplier(
608
+ minMaxGainForContractSize_.sats,
609
+ BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
610
+ ),
611
+ );
612
+
613
+ const maxLossForContractSize = Value.fromSats(
614
+ roundUpToNearestMultiplier(
615
+ (normalizedMaxLoss.sats * contractSize.sats) / BigInt(1e8),
616
+ BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
617
+ ),
618
+ );
619
+ const tempMaxGainForContractSize = Value.fromSats(
620
+ roundUpToNearestMultiplier(
621
+ (normalizedMaxGain.sats * contractSize.sats) / BigInt(1e8),
622
+ BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
623
+ ),
624
+ );
625
+
626
+ const maxGainForContractSize = Value.zero();
627
+
628
+ if (tempMaxGainForContractSize.lt(minMaxGainForContractSize)) {
629
+ maxGainForContractSize.add(minMaxGainForContractSize);
630
+ } else {
631
+ maxGainForContractSize.add(tempMaxGainForContractSize);
632
+ }
633
+
634
+ const maxGainAdjusted = Value.fromSats(
635
+ roundToNearestMultiplier(
636
+ (maxGainForContractSize.sats * BigInt(1e8)) / contractSize.sats,
637
+ BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
638
+ ),
639
+ );
640
+
641
+ const feesAdjusted = Value.fromSats(
642
+ roundToNearestMultiplier(
643
+ (fees.sats * BigInt(1e8)) / contractSize.sats,
644
+ BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]),
645
+ ),
646
+ );
647
+
648
+ const minPayout = collateral.clone();
649
+ if (minPayout.lt(maxLossForContractSize.addn(maxGainForContractSize))) {
650
+ throw new Error(
651
+ 'Subtraction would result in a negative value for minPayout.',
652
+ );
653
+ }
654
+ minPayout.sub(maxLossForContractSize);
655
+ minPayout.sub(maxGainForContractSize);
656
+ const maxPayout = collateral.clone();
657
+
658
+ const startOutcomeValue = Value.fromBitcoin(1);
659
+ startOutcomeValue.sub(normalizedMaxLoss);
660
+ const endOutcomeValue = Value.fromBitcoin(1);
661
+ endOutcomeValue.add(maxGainAdjusted);
662
+
663
+ if (shiftForFees === 'offeror') {
664
+ startOutcomeValue.add(feesAdjusted);
665
+ endOutcomeValue.add(feesAdjusted);
666
+ } else if (shiftForFees === 'acceptor') {
667
+ startOutcomeValue.sub(feesAdjusted);
668
+ endOutcomeValue.sub(feesAdjusted);
669
+ }
670
+
671
+ const startOutcome =
672
+ startOutcomeValue.sats / BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]);
673
+ const endOutcome =
674
+ endOutcomeValue.sats / BigInt(UNIT_MULTIPLIER[unit.toLowerCase()]);
675
+
676
+ const offerCollateral = collateral.clone();
677
+ if (offerCollateral.lt(maxGainForContractSize)) {
678
+ throw new Error(
679
+ 'Subtraction would result in a negative value for offerCollateral.',
680
+ );
681
+ }
682
+ offerCollateral.sub(maxGainForContractSize);
683
+
684
+ const orderOffer = buildLinearOrderOffer(
685
+ announcement,
686
+ offerCollateral,
687
+ minPayout,
688
+ maxPayout,
689
+ startOutcome,
690
+ endOutcome,
691
+ feePerByte,
692
+ roundingIntervals,
693
+ network,
694
+ shiftForFees,
695
+ fees,
696
+ );
697
+
698
+ (orderOffer.positionInfo as OrderPositionInfo).contractSize =
699
+ contractSize.sats;
700
+
701
+ (orderOffer.positionInfo as OrderPositionInfo).instrumentName = ((orderOffer.contractInfo as SingleContractInfo)
702
+ .oracleInfo as SingleOracleInfo).announcement.oracleEvent.eventId;
703
+
704
+ if (!skipValidation) orderOffer.validate();
705
+
706
+ return orderOffer;
707
+ };
708
+
709
+ interface IInterval {
710
+ beginInterval: bigint;
711
+ rounding: number | bigint | Value;
712
+ }
713
+
714
+ export const buildRoundingIntervalsFromIntervals = (
715
+ contractSize: Value,
716
+ intervals: IInterval[],
717
+ ): RoundingIntervals => {
718
+ const roundingIntervals = new RoundingIntervals();
719
+
720
+ roundingIntervals.intervals = intervals.map((interval) => {
721
+ const roundingMod = computeRoundingModulus(interval.rounding, contractSize);
722
+
723
+ return {
724
+ beginInterval: interval.beginInterval,
725
+ roundingMod,
726
+ };
727
+ });
728
+
729
+ return roundingIntervals;
730
+ };