@atomiqlabs/lp-lib 12.1.0 → 13.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/dist/index.d.ts +18 -13
  2. package/dist/index.js +18 -13
  3. package/dist/plugins/IPlugin.d.ts +35 -12
  4. package/dist/plugins/PluginManager.d.ts +38 -15
  5. package/dist/plugins/PluginManager.js +33 -9
  6. package/dist/prices/BinanceSwapPrice.d.ts +1 -1
  7. package/dist/prices/BinanceSwapPrice.js +1 -1
  8. package/dist/prices/CoinGeckoSwapPrice.d.ts +1 -1
  9. package/dist/prices/CoinGeckoSwapPrice.js +1 -1
  10. package/dist/{swaps → prices}/ISwapPrice.js +4 -0
  11. package/dist/prices/OKXSwapPrice.d.ts +1 -1
  12. package/dist/prices/OKXSwapPrice.js +1 -1
  13. package/dist/swaps/SwapHandler.d.ts +20 -58
  14. package/dist/swaps/SwapHandler.js +17 -186
  15. package/dist/swaps/SwapHandlerSwap.d.ts +8 -23
  16. package/dist/swaps/SwapHandlerSwap.js +7 -39
  17. package/dist/swaps/assertions/AmountAssertions.d.ts +28 -0
  18. package/dist/swaps/assertions/AmountAssertions.js +72 -0
  19. package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -0
  20. package/dist/swaps/assertions/FromBtcAmountAssertions.js +162 -0
  21. package/dist/swaps/assertions/LightningAssertions.d.ts +44 -0
  22. package/dist/swaps/assertions/LightningAssertions.js +86 -0
  23. package/dist/swaps/assertions/ToBtcAmountAssertions.d.ts +53 -0
  24. package/dist/swaps/{ToBtcBaseSwapHandler.js → assertions/ToBtcAmountAssertions.js} +20 -94
  25. package/dist/swaps/escrow/EscrowHandler.d.ts +51 -0
  26. package/dist/swaps/escrow/EscrowHandler.js +158 -0
  27. package/dist/swaps/escrow/EscrowHandlerSwap.d.ts +35 -0
  28. package/dist/swaps/escrow/EscrowHandlerSwap.js +69 -0
  29. package/dist/swaps/{FromBtcBaseSwap.d.ts → escrow/FromBtcBaseSwap.d.ts} +2 -3
  30. package/dist/swaps/{FromBtcBaseSwap.js → escrow/FromBtcBaseSwap.js} +4 -7
  31. package/dist/swaps/{FromBtcBaseSwapHandler.d.ts → escrow/FromBtcBaseSwapHandler.d.ts} +10 -49
  32. package/dist/swaps/{FromBtcBaseSwapHandler.js → escrow/FromBtcBaseSwapHandler.js} +16 -137
  33. package/dist/swaps/{ToBtcBaseSwap.d.ts → escrow/ToBtcBaseSwap.d.ts} +2 -2
  34. package/dist/swaps/{ToBtcBaseSwap.js → escrow/ToBtcBaseSwap.js} +4 -4
  35. package/dist/swaps/escrow/ToBtcBaseSwapHandler.d.ts +53 -0
  36. package/dist/swaps/escrow/ToBtcBaseSwapHandler.js +81 -0
  37. package/dist/swaps/{frombtc_abstract → escrow/frombtc_abstract}/FromBtcAbs.d.ts +4 -4
  38. package/dist/swaps/{frombtc_abstract → escrow/frombtc_abstract}/FromBtcAbs.js +15 -15
  39. package/dist/swaps/{frombtc_abstract → escrow/frombtc_abstract}/FromBtcSwapAbs.js +1 -1
  40. package/dist/swaps/{frombtcln_abstract → escrow/frombtcln_abstract}/FromBtcLnAbs.d.ts +9 -7
  41. package/dist/swaps/{frombtcln_abstract → escrow/frombtcln_abstract}/FromBtcLnAbs.js +22 -19
  42. package/dist/swaps/{frombtcln_abstract → escrow/frombtcln_abstract}/FromBtcLnSwapAbs.js +3 -3
  43. package/dist/swaps/{tobtc_abstract → escrow/tobtc_abstract}/ToBtcAbs.d.ts +4 -4
  44. package/dist/swaps/{tobtc_abstract → escrow/tobtc_abstract}/ToBtcAbs.js +14 -13
  45. package/dist/swaps/{tobtc_abstract → escrow/tobtc_abstract}/ToBtcSwapAbs.js +3 -3
  46. package/dist/swaps/{tobtcln_abstract → escrow/tobtcln_abstract}/ToBtcLnAbs.d.ts +6 -26
  47. package/dist/swaps/{tobtcln_abstract → escrow/tobtcln_abstract}/ToBtcLnAbs.js +20 -57
  48. package/dist/swaps/{tobtcln_abstract → escrow/tobtcln_abstract}/ToBtcLnSwapAbs.js +3 -3
  49. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +41 -0
  50. package/dist/swaps/spv_vault_swap/SpvVault.js +111 -0
  51. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +63 -0
  52. package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +145 -0
  53. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -0
  54. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +469 -0
  55. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +57 -0
  56. package/dist/swaps/spv_vault_swap/SpvVaults.js +369 -0
  57. package/dist/swaps/{frombtc_trusted → trusted/frombtc_trusted}/FromBtcTrusted.d.ts +10 -13
  58. package/dist/swaps/{frombtc_trusted → trusted/frombtc_trusted}/FromBtcTrusted.js +25 -30
  59. package/dist/swaps/{frombtc_trusted → trusted/frombtc_trusted}/FromBtcTrustedSwap.d.ts +9 -4
  60. package/dist/swaps/{frombtc_trusted → trusted/frombtc_trusted}/FromBtcTrustedSwap.js +15 -7
  61. package/dist/swaps/{frombtcln_trusted → trusted/frombtcln_trusted}/FromBtcLnTrusted.d.ts +12 -14
  62. package/dist/swaps/{frombtcln_trusted → trusted/frombtcln_trusted}/FromBtcLnTrusted.js +33 -35
  63. package/dist/swaps/{frombtcln_trusted → trusted/frombtcln_trusted}/FromBtcLnTrustedSwap.d.ts +9 -4
  64. package/dist/swaps/{frombtcln_trusted → trusted/frombtcln_trusted}/FromBtcLnTrustedSwap.js +17 -7
  65. package/dist/utils/Utils.d.ts +13 -5
  66. package/dist/utils/Utils.js +23 -1
  67. package/dist/wallets/IBitcoinWallet.d.ts +6 -0
  68. package/dist/wallets/ISpvVaultSigner.d.ts +7 -0
  69. package/dist/wallets/ISpvVaultSigner.js +2 -0
  70. package/dist/wallets/ISpvVaultWallet.d.ts +42 -0
  71. package/dist/wallets/ISpvVaultWallet.js +2 -0
  72. package/package.json +2 -2
  73. package/src/index.ts +21 -15
  74. package/src/plugins/IPlugin.ts +27 -19
  75. package/src/plugins/PluginManager.ts +51 -26
  76. package/src/prices/BinanceSwapPrice.ts +1 -1
  77. package/src/prices/CoinGeckoSwapPrice.ts +1 -1
  78. package/src/{swaps → prices}/ISwapPrice.ts +4 -0
  79. package/src/prices/OKXSwapPrice.ts +1 -1
  80. package/src/swaps/SwapHandler.ts +22 -205
  81. package/src/swaps/SwapHandlerSwap.ts +10 -46
  82. package/src/swaps/assertions/AmountAssertions.ts +77 -0
  83. package/src/swaps/assertions/FromBtcAmountAssertions.ts +228 -0
  84. package/src/swaps/assertions/LightningAssertions.ts +103 -0
  85. package/src/swaps/{ToBtcBaseSwapHandler.ts → assertions/ToBtcAmountAssertions.ts} +27 -142
  86. package/src/swaps/escrow/EscrowHandler.ts +179 -0
  87. package/src/swaps/escrow/EscrowHandlerSwap.ts +87 -0
  88. package/src/swaps/{FromBtcBaseSwap.ts → escrow/FromBtcBaseSwap.ts} +4 -8
  89. package/src/swaps/{FromBtcBaseSwapHandler.ts → escrow/FromBtcBaseSwapHandler.ts} +30 -190
  90. package/src/swaps/{ToBtcBaseSwap.ts → escrow/ToBtcBaseSwap.ts} +4 -5
  91. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +130 -0
  92. package/src/swaps/{frombtc_abstract → escrow/frombtc_abstract}/FromBtcAbs.ts +20 -20
  93. package/src/swaps/{frombtc_abstract → escrow/frombtc_abstract}/FromBtcSwapAbs.ts +1 -1
  94. package/src/swaps/{frombtcln_abstract → escrow/frombtcln_abstract}/FromBtcLnAbs.ts +29 -25
  95. package/src/swaps/{frombtcln_abstract → escrow/frombtcln_abstract}/FromBtcLnSwapAbs.ts +2 -2
  96. package/src/swaps/{tobtc_abstract → escrow/tobtc_abstract}/ToBtcAbs.ts +19 -18
  97. package/src/swaps/{tobtc_abstract → escrow/tobtc_abstract}/ToBtcSwapAbs.ts +2 -2
  98. package/src/swaps/{tobtcln_abstract → escrow/tobtcln_abstract}/ToBtcLnAbs.ts +26 -66
  99. package/src/swaps/{tobtcln_abstract → escrow/tobtcln_abstract}/ToBtcLnSwapAbs.ts +2 -2
  100. package/src/swaps/spv_vault_swap/SpvVault.ts +143 -0
  101. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +207 -0
  102. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +606 -0
  103. package/src/swaps/spv_vault_swap/SpvVaults.ts +441 -0
  104. package/src/swaps/{frombtc_trusted → trusted/frombtc_trusted}/FromBtcTrusted.ts +36 -51
  105. package/src/swaps/{frombtc_trusted → trusted/frombtc_trusted}/FromBtcTrustedSwap.ts +18 -8
  106. package/src/swaps/{frombtcln_trusted → trusted/frombtcln_trusted}/FromBtcLnTrusted.ts +43 -52
  107. package/src/swaps/{frombtcln_trusted → trusted/frombtcln_trusted}/FromBtcLnTrustedSwap.ts +20 -8
  108. package/src/utils/Utils.ts +27 -1
  109. package/src/wallets/IBitcoinWallet.ts +4 -0
  110. package/src/wallets/ISpvVaultSigner.ts +11 -0
  111. package/dist/swaps/FromBtcLnBaseSwapHandler.d.ts +0 -26
  112. package/dist/swaps/FromBtcLnBaseSwapHandler.js +0 -46
  113. package/dist/swaps/ToBtcBaseSwapHandler.d.ts +0 -95
  114. package/src/swaps/FromBtcLnBaseSwapHandler.ts +0 -63
  115. /package/dist/{swaps → prices}/ISwapPrice.d.ts +0 -0
  116. /package/dist/swaps/{frombtc_abstract → escrow/frombtc_abstract}/FromBtcSwapAbs.d.ts +0 -0
  117. /package/dist/swaps/{frombtcln_abstract → escrow/frombtcln_abstract}/FromBtcLnSwapAbs.d.ts +0 -0
  118. /package/dist/swaps/{tobtc_abstract → escrow/tobtc_abstract}/ToBtcSwapAbs.d.ts +0 -0
  119. /package/dist/swaps/{tobtcln_abstract → escrow/tobtcln_abstract}/ToBtcLnSwapAbs.d.ts +0 -0
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SwapHandler = exports.SwapHandlerType = void 0;
4
- const base_1 = require("@atomiqlabs/base");
5
- const SwapHandlerSwap_1 = require("./SwapHandlerSwap");
6
4
  const PluginManager_1 = require("../plugins/PluginManager");
7
- const IPlugin_1 = require("../plugins/IPlugin");
8
5
  var SwapHandlerType;
9
6
  (function (SwapHandlerType) {
10
7
  SwapHandlerType["TO_BTC"] = "TO_BTC";
@@ -13,13 +10,13 @@ var SwapHandlerType;
13
10
  SwapHandlerType["FROM_BTCLN"] = "FROM_BTCLN";
14
11
  SwapHandlerType["FROM_BTCLN_TRUSTED"] = "FROM_BTCLN_TRUSTED";
15
12
  SwapHandlerType["FROM_BTC_TRUSTED"] = "FROM_BTC_TRUSTED";
13
+ SwapHandlerType["FROM_BTC_SPV"] = "FROM_BTC_SPV";
16
14
  })(SwapHandlerType = exports.SwapHandlerType || (exports.SwapHandlerType = {}));
17
15
  /**
18
16
  * An abstract class defining a singular swap service
19
17
  */
20
18
  class SwapHandler {
21
19
  constructor(storageDirectory, path, chainsData, swapPricing) {
22
- this.escrowHashMap = new Map();
23
20
  this.logger = {
24
21
  debug: (msg, ...args) => console.debug("SwapHandler(" + this.type + "): " + msg, ...args),
25
22
  info: (msg, ...args) => console.info("SwapHandler(" + this.type + "): " + msg, ...args),
@@ -27,10 +24,10 @@ class SwapHandler {
27
24
  error: (msg, ...args) => console.error("SwapHandler(" + this.type + "): " + msg, ...args)
28
25
  };
29
26
  this.swapLogger = {
30
- debug: (swap, msg, ...args) => this.logger.debug(this.getIdentifier(swap) + ": " + msg, ...args),
31
- info: (swap, msg, ...args) => this.logger.info(this.getIdentifier(swap) + ": " + msg, ...args),
32
- warn: (swap, msg, ...args) => this.logger.warn(this.getIdentifier(swap) + ": " + msg, ...args),
33
- error: (swap, msg, ...args) => this.logger.error(this.getIdentifier(swap) + ": " + msg, ...args)
27
+ debug: (swap, msg, ...args) => this.logger.debug(swap.getIdentifier() + ": " + msg, ...args),
28
+ info: (swap, msg, ...args) => this.logger.info(swap.getIdentifier() + ": " + msg, ...args),
29
+ warn: (swap, msg, ...args) => this.logger.warn(swap.getIdentifier() + ": " + msg, ...args),
30
+ error: (swap, msg, ...args) => this.logger.error(swap.getIdentifier() + ": " + msg, ...args)
34
31
  };
35
32
  this.storageManager = storageDirectory;
36
33
  this.chains = chainsData;
@@ -65,57 +62,6 @@ class SwapHandler {
65
62
  };
66
63
  await rerun();
67
64
  }
68
- /**
69
- * Chain event processor
70
- *
71
- * @param chainIdentifier
72
- * @param eventData
73
- */
74
- async processEvent(chainIdentifier, eventData) {
75
- if (this.swapType == null)
76
- return true;
77
- for (let event of eventData) {
78
- if (event instanceof base_1.InitializeEvent) {
79
- if (event.swapType !== this.swapType)
80
- continue;
81
- const swap = this.getSwapByEscrowHash(chainIdentifier, event.escrowHash);
82
- if (swap == null)
83
- continue;
84
- swap.txIds.init = event.meta?.txId;
85
- if (swap.metadata != null)
86
- swap.metadata.times.initTxReceived = Date.now();
87
- await this.processInitializeEvent(chainIdentifier, swap, event);
88
- }
89
- else if (event instanceof base_1.ClaimEvent) {
90
- const swap = this.getSwapByEscrowHash(chainIdentifier, event.escrowHash);
91
- if (swap == null)
92
- continue;
93
- swap.txIds.claim = event.meta?.txId;
94
- if (swap.metadata != null)
95
- swap.metadata.times.claimTxReceived = Date.now();
96
- await this.processClaimEvent(chainIdentifier, swap, event);
97
- }
98
- else if (event instanceof base_1.RefundEvent) {
99
- const swap = this.getSwapByEscrowHash(chainIdentifier, event.escrowHash);
100
- if (swap == null)
101
- continue;
102
- swap.txIds.refund = event.meta?.txId;
103
- if (swap.metadata != null)
104
- swap.metadata.times.refundTxReceived = Date.now();
105
- await this.processRefundEvent(chainIdentifier, swap, event);
106
- }
107
- }
108
- return true;
109
- }
110
- /**
111
- * Initializes chain events subscription
112
- */
113
- subscribeToEvents() {
114
- for (let key in this.chains.chains) {
115
- this.chains.chains[key].chainEvents.registerListener((events) => this.processEvent(key, events));
116
- }
117
- this.logger.info("SC: Events: subscribed to smartchain events");
118
- }
119
65
  async loadData(ctor) {
120
66
  await this.storageManager.loadData(ctor);
121
67
  //Check if all swaps contain a valid amount
@@ -127,7 +73,6 @@ class SwapHandler {
127
73
  await this.storageManager.removeData(hash, sequence);
128
74
  await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
129
75
  }
130
- this.saveSwapToEscrowHashMap(swap);
131
76
  }
132
77
  }
133
78
  async removeSwapData(hashOrSwap, sequenceOrUltimateState) {
@@ -145,143 +90,29 @@ class SwapHandler {
145
90
  if (swap != null)
146
91
  await PluginManager_1.PluginManager.swapRemove(swap);
147
92
  this.swapLogger.debug(swap, "removeSwapData(): removing swap final state: " + swap.state);
148
- this.removeSwapFromEscrowHashMap(swap);
149
93
  await this.storageManager.removeData(swap.getIdentifierHash(), swap.getSequence());
150
94
  }
151
95
  async saveSwapData(swap) {
152
- this.saveSwapToEscrowHashMap(swap);
153
96
  await this.storageManager.saveData(swap.getIdentifierHash(), swap.getSequence(), swap);
154
97
  }
155
- saveSwapToEscrowHashMap(swap) {
156
- if (swap.data != null)
157
- this.escrowHashMap.set(swap.chainIdentifier + "_" + swap.getEscrowHash(), swap);
158
- }
159
- removeSwapFromEscrowHashMap(swap) {
160
- if (swap.data != null)
161
- this.escrowHashMap.delete(swap.chainIdentifier + "_" + swap.data.getEscrowHash());
162
- }
163
- getSwapByEscrowHash(chainIdentifier, escrowHash) {
164
- return this.escrowHashMap.get(chainIdentifier + "_" + escrowHash);
165
- }
166
98
  /**
167
- * Checks whether the bitcoin amount is within specified min/max bounds
99
+ * Checks if we have enough balance of the token in the swap vault
168
100
  *
169
- * @param amount
170
- * @protected
171
- * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
101
+ * @param totalInToken
102
+ * @param balancePrefetch
103
+ * @param signal
104
+ * @throws {DefinedRuntimeError} will throw an error if there are not enough funds in the vault
172
105
  */
173
- checkBtcAmountInBounds(amount) {
174
- if (amount < this.config.min) {
106
+ async checkBalance(totalInToken, balancePrefetch, signal) {
107
+ const balance = await balancePrefetch;
108
+ if (signal != null)
109
+ signal.throwIfAborted();
110
+ if (balance == null || balance < totalInToken) {
175
111
  throw {
176
- code: 20003,
177
- msg: "Amount too low!",
178
- data: {
179
- min: this.config.min.toString(10),
180
- max: this.config.max.toString(10)
181
- }
112
+ code: 20002,
113
+ msg: "Not enough liquidity"
182
114
  };
183
115
  }
184
- if (amount > this.config.max) {
185
- throw {
186
- code: 20004,
187
- msg: "Amount too high!",
188
- data: {
189
- min: this.config.min.toString(10),
190
- max: this.config.max.toString(10)
191
- }
192
- };
193
- }
194
- }
195
- /**
196
- * Handles and throws plugin errors
197
- *
198
- * @param res Response as returned from the PluginManager.onHandlePost{To,From}BtcQuote
199
- * @protected
200
- * @throws {DefinedRuntimeError} will throw an error if the response is an error
201
- */
202
- handlePluginErrorResponses(res) {
203
- if ((0, IPlugin_1.isQuoteThrow)(res))
204
- throw {
205
- code: 29999,
206
- msg: res.message
207
- };
208
- if ((0, IPlugin_1.isQuoteAmountTooHigh)(res))
209
- throw {
210
- code: 20004,
211
- msg: "Amount too high!",
212
- data: {
213
- min: res.data.min.toString(10),
214
- max: res.data.max.toString(10)
215
- }
216
- };
217
- if ((0, IPlugin_1.isQuoteAmountTooLow)(res))
218
- throw {
219
- code: 20003,
220
- msg: "Amount too low!",
221
- data: {
222
- min: res.data.min.toString(10),
223
- max: res.data.max.toString(10)
224
- }
225
- };
226
- }
227
- /**
228
- * Creates an abort controller that extends the responseStream's abort signal
229
- *
230
- * @param responseStream
231
- */
232
- getAbortController(responseStream) {
233
- const abortController = new AbortController();
234
- if (responseStream == null || responseStream.getAbortSignal == null)
235
- return abortController;
236
- const responseStreamAbortController = responseStream.getAbortSignal();
237
- responseStreamAbortController.addEventListener("abort", () => abortController.abort(responseStreamAbortController.reason));
238
- return abortController;
239
- }
240
- /**
241
- * Starts a pre-fetch for signature data
242
- *
243
- * @param chainIdentifier
244
- * @param abortController
245
- * @param responseStream
246
- */
247
- getSignDataPrefetch(chainIdentifier, abortController, responseStream) {
248
- const { swapContract } = this.getChain(chainIdentifier);
249
- let signDataPrefetchPromise = swapContract.preFetchBlockDataForSignatures != null ? swapContract.preFetchBlockDataForSignatures().catch(e => {
250
- this.logger.error("getSignDataPrefetch(): signDataPrefetch: ", e);
251
- abortController.abort(e);
252
- return null;
253
- }) : null;
254
- if (signDataPrefetchPromise != null && responseStream != null) {
255
- signDataPrefetchPromise = signDataPrefetchPromise.then(val => val == null || abortController.signal.aborted ? null : responseStream.writeParams({
256
- signDataPrefetch: val
257
- }).then(() => val).catch(e => {
258
- this.logger.error("getSignDataPrefetch(): signDataPreFetch: error when sending sign data to the client: ", e);
259
- abortController.abort(e);
260
- return null;
261
- }));
262
- }
263
- return signDataPrefetchPromise;
264
- }
265
- getIdentifierFromEvent(event) {
266
- const foundSwap = this.escrowHashMap.get(event.escrowHash);
267
- if (foundSwap != null) {
268
- return foundSwap.getIdentifier();
269
- }
270
- return "UNKNOWN_" + event.escrowHash;
271
- }
272
- getIdentifierFromSwapData(swapData) {
273
- if (swapData.getSequence == null)
274
- return swapData.getClaimHash();
275
- return swapData.getClaimHash() + "_" + swapData.getSequence().toString(16);
276
- }
277
- getIdentifier(swap) {
278
- if (swap instanceof SwapHandlerSwap_1.SwapHandlerSwap) {
279
- return swap.getIdentifier();
280
- }
281
- if (swap instanceof base_1.SwapEvent) {
282
- return this.getIdentifierFromEvent(swap);
283
- }
284
- return this.getIdentifierFromSwapData(swap);
285
116
  }
286
117
  /**
287
118
  * Checks if the sequence number is between 0-2^64
@@ -1,10 +1,9 @@
1
- import { Lockable, StorageObject, SwapData } from "@atomiqlabs/base";
1
+ import { Lockable, StorageObject } from "@atomiqlabs/base";
2
2
  import { SwapHandlerType } from "./SwapHandler";
3
- export declare abstract class SwapHandlerSwap<T extends SwapData = SwapData, S = any> extends Lockable implements StorageObject {
3
+ export declare abstract class SwapHandlerSwap<S = any> extends Lockable implements StorageObject {
4
+ type: SwapHandlerType;
4
5
  chainIdentifier: string;
5
6
  state: S;
6
- type: SwapHandlerType;
7
- data: T;
8
7
  metadata: {
9
8
  request: any;
10
9
  times: {
@@ -13,16 +12,10 @@ export declare abstract class SwapHandlerSwap<T extends SwapData = SwapData, S =
13
12
  [key: string]: any;
14
13
  };
15
14
  txIds: {
16
- init?: string;
17
- claim?: string;
18
- refund?: string;
15
+ [key: string]: string;
19
16
  };
20
17
  readonly swapFee: bigint;
21
18
  readonly swapFeeInToken: bigint;
22
- prefix: string;
23
- timeout: string;
24
- signature: string;
25
- feeRate: string;
26
19
  protected constructor(chainIdentifier: string, swapFee: bigint, swapFeeInToken: bigint);
27
20
  protected constructor(obj: any);
28
21
  serialize(): any;
@@ -32,20 +25,12 @@ export declare abstract class SwapHandlerSwap<T extends SwapData = SwapData, S =
32
25
  * @param newState
33
26
  */
34
27
  setState(newState: S): Promise<void>;
35
- /**
36
- * Returns the escrow hash - i.e. hash of the escrow data
37
- */
38
- getEscrowHash(): string;
39
- /**
40
- * Returns the claim data hash - i.e. hash passed to the claim handler
41
- */
42
- getClaimHash(): string;
43
28
  /**
44
29
  * Returns the identification hash of the swap, usually claim data hash, but can be overriden, e.g. for
45
30
  * lightning swaps the identifier hash is used instead of claim data hash
46
31
  */
47
- getIdentifierHash(): string;
48
- getSequence(): bigint | null;
32
+ abstract getIdentifierHash(): string;
33
+ abstract getSequence(): bigint | null;
49
34
  /**
50
35
  * Returns unique identifier of the swap in the form <hash>_<sequence> or just <hash> if the swap type doesn't
51
36
  * use sequence number
@@ -54,7 +39,7 @@ export declare abstract class SwapHandlerSwap<T extends SwapData = SwapData, S =
54
39
  /**
55
40
  * Returns the smart chain token used for the swap
56
41
  */
57
- getToken(): string;
42
+ abstract getToken(): string;
58
43
  /**
59
44
  * Checks whether the swap is finished, such that it is final and either successful or failed
60
45
  */
@@ -74,7 +59,7 @@ export declare abstract class SwapHandlerSwap<T extends SwapData = SwapData, S =
74
59
  /**
75
60
  * Returns the input amount paid by the user (excluding fees)
76
61
  */
77
- abstract getInputAmount(): bigint;
62
+ getInputAmount(): bigint;
78
63
  /**
79
64
  * Returns the total input amount paid by the user (including all fees)
80
65
  */
@@ -24,32 +24,22 @@ class SwapHandlerSwap extends base_1.Lockable {
24
24
  return;
25
25
  }
26
26
  else {
27
- this.data = obj.data == null ? null : base_1.SwapData.deserialize(obj.data);
28
27
  this.metadata = obj.metadata;
29
28
  this.chainIdentifier = obj.chainIdentifier;
30
29
  this.txIds = obj.txIds || {};
31
30
  this.state = obj.state;
32
31
  this.swapFee = (0, Utils_1.deserializeBN)(obj.swapFee);
33
32
  this.swapFeeInToken = (0, Utils_1.deserializeBN)(obj.swapFeeInToken);
34
- this.prefix = obj.prefix;
35
- this.timeout = obj.timeout;
36
- this.signature = obj.signature;
37
- this.feeRate = obj.feeRate;
38
33
  }
39
34
  }
40
35
  serialize() {
41
36
  return {
42
37
  state: this.state,
43
- data: this.data == null ? null : this.data.serialize(),
44
38
  chainIdentifier: this.chainIdentifier,
45
39
  metadata: objectBigIntsToString(this.metadata),
46
40
  txIds: this.txIds,
47
41
  swapFee: (0, Utils_1.serializeBN)(this.swapFee),
48
- swapFeeInToken: (0, Utils_1.serializeBN)(this.swapFeeInToken),
49
- prefix: this.prefix,
50
- timeout: this.timeout,
51
- signature: this.signature,
52
- feeRate: this.feeRate
42
+ swapFeeInToken: (0, Utils_1.serializeBN)(this.swapFeeInToken)
53
43
  };
54
44
  }
55
45
  /**
@@ -62,28 +52,6 @@ class SwapHandlerSwap extends base_1.Lockable {
62
52
  this.state = newState;
63
53
  return PluginManager_1.PluginManager.swapStateChange(this, oldState);
64
54
  }
65
- /**
66
- * Returns the escrow hash - i.e. hash of the escrow data
67
- */
68
- getEscrowHash() {
69
- return this.data.getEscrowHash();
70
- }
71
- /**
72
- * Returns the claim data hash - i.e. hash passed to the claim handler
73
- */
74
- getClaimHash() {
75
- return this.data.getClaimHash();
76
- }
77
- /**
78
- * Returns the identification hash of the swap, usually claim data hash, but can be overriden, e.g. for
79
- * lightning swaps the identifier hash is used instead of claim data hash
80
- */
81
- getIdentifierHash() {
82
- return this.getClaimHash();
83
- }
84
- getSequence() {
85
- return this.data?.getSequence == null ? null : this.data.getSequence();
86
- }
87
55
  /**
88
56
  * Returns unique identifier of the swap in the form <hash>_<sequence> or just <hash> if the swap type doesn't
89
57
  * use sequence number
@@ -94,17 +62,17 @@ class SwapHandlerSwap extends base_1.Lockable {
94
62
  }
95
63
  return this.chainIdentifier + "_" + this.getIdentifierHash();
96
64
  }
97
- /**
98
- * Returns the smart chain token used for the swap
99
- */
100
- getToken() {
101
- return this.data?.getToken();
102
- }
103
65
  /**
104
66
  * Checks whether the swap is finished, such that it is final and either successful or failed
105
67
  */
106
68
  isFinished() {
107
69
  return this.isSuccess() || this.isFailed();
108
70
  }
71
+ /**
72
+ * Returns the input amount paid by the user (excluding fees)
73
+ */
74
+ getInputAmount() {
75
+ return this.getTotalInputAmount() - this.getSwapFee().inInputToken;
76
+ }
109
77
  }
110
78
  exports.SwapHandlerSwap = SwapHandlerSwap;
@@ -0,0 +1,28 @@
1
+ import { ISwapPrice } from "../../prices/ISwapPrice";
2
+ export type AmountAssertionsConfig = {
3
+ min: bigint;
4
+ max: bigint;
5
+ baseFee: bigint;
6
+ feePPM: bigint;
7
+ };
8
+ export declare abstract class AmountAssertions {
9
+ readonly config: AmountAssertionsConfig;
10
+ readonly swapPricing: ISwapPrice;
11
+ constructor(config: AmountAssertionsConfig, swapPricing: ISwapPrice);
12
+ /**
13
+ * Checks whether the bitcoin amount is within specified min/max bounds
14
+ *
15
+ * @param amount
16
+ * @protected
17
+ * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
18
+ */
19
+ protected checkBtcAmountInBounds(amount: bigint): void;
20
+ /**
21
+ * Handles and throws plugin errors
22
+ *
23
+ * @param res Response as returned from the PluginManager.onHandlePost{To,From}BtcQuote
24
+ * @protected
25
+ * @throws {DefinedRuntimeError} will throw an error if the response is an error
26
+ */
27
+ static handlePluginErrorResponses(res: any): void;
28
+ }
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AmountAssertions = void 0;
4
+ const IPlugin_1 = require("../../plugins/IPlugin");
5
+ class AmountAssertions {
6
+ constructor(config, swapPricing) {
7
+ this.config = config;
8
+ this.swapPricing = swapPricing;
9
+ }
10
+ /**
11
+ * Checks whether the bitcoin amount is within specified min/max bounds
12
+ *
13
+ * @param amount
14
+ * @protected
15
+ * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
16
+ */
17
+ checkBtcAmountInBounds(amount) {
18
+ if (amount < this.config.min) {
19
+ throw {
20
+ code: 20003,
21
+ msg: "Amount too low!",
22
+ data: {
23
+ min: this.config.min.toString(10),
24
+ max: this.config.max.toString(10)
25
+ }
26
+ };
27
+ }
28
+ if (amount > this.config.max) {
29
+ throw {
30
+ code: 20004,
31
+ msg: "Amount too high!",
32
+ data: {
33
+ min: this.config.min.toString(10),
34
+ max: this.config.max.toString(10)
35
+ }
36
+ };
37
+ }
38
+ }
39
+ /**
40
+ * Handles and throws plugin errors
41
+ *
42
+ * @param res Response as returned from the PluginManager.onHandlePost{To,From}BtcQuote
43
+ * @protected
44
+ * @throws {DefinedRuntimeError} will throw an error if the response is an error
45
+ */
46
+ static handlePluginErrorResponses(res) {
47
+ if ((0, IPlugin_1.isQuoteThrow)(res))
48
+ throw {
49
+ code: 29999,
50
+ msg: res.message
51
+ };
52
+ if ((0, IPlugin_1.isQuoteAmountTooHigh)(res))
53
+ throw {
54
+ code: 20004,
55
+ msg: "Amount too high!",
56
+ data: {
57
+ min: res.data.min.toString(10),
58
+ max: res.data.max.toString(10)
59
+ }
60
+ };
61
+ if ((0, IPlugin_1.isQuoteAmountTooLow)(res))
62
+ throw {
63
+ code: 20003,
64
+ msg: "Amount too low!",
65
+ data: {
66
+ min: res.data.min.toString(10),
67
+ max: res.data.max.toString(10)
68
+ }
69
+ };
70
+ }
71
+ }
72
+ exports.AmountAssertions = AmountAssertions;
@@ -0,0 +1,76 @@
1
+ import { FromBtcLnRequestType } from "../escrow/frombtcln_abstract/FromBtcLnAbs";
2
+ import { FromBtcRequestType } from "../escrow/frombtc_abstract/FromBtcAbs";
3
+ import { FromBtcLnTrustedRequestType } from "../trusted/frombtcln_trusted/FromBtcLnTrusted";
4
+ import { RequestData, SwapHandlerType } from "../SwapHandler";
5
+ import { AmountAssertions, AmountAssertionsConfig } from "./AmountAssertions";
6
+ import { ISwapPrice } from "../../prices/ISwapPrice";
7
+ import { FromBtcTrustedRequestType } from "../trusted/frombtc_trusted/FromBtcTrusted";
8
+ import { SpvVaultSwapRequestType } from "../spv_vault_swap/SpvVaultSwapHandler";
9
+ export type FromBtcAmountAssertionsConfig = AmountAssertionsConfig & {
10
+ gasTokenMax?: {
11
+ [chainId: string]: bigint;
12
+ };
13
+ };
14
+ export declare class FromBtcAmountAssertions extends AmountAssertions {
15
+ readonly config: FromBtcAmountAssertionsConfig;
16
+ constructor(config: FromBtcAmountAssertionsConfig, swapPricing: ISwapPrice);
17
+ /**
18
+ * Checks minimums/maximums, calculates the fee & total amount
19
+ *
20
+ * @param swapType
21
+ * @param request
22
+ * @param requestedAmount
23
+ * @param gasAmount
24
+ * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
25
+ */
26
+ preCheckFromBtcAmounts(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
27
+ input: boolean;
28
+ amount: bigint;
29
+ token: string;
30
+ }, gasAmount?: {
31
+ input: false;
32
+ amount: bigint;
33
+ token: string;
34
+ }): Promise<{
35
+ baseFee: bigint;
36
+ feePPM: bigint;
37
+ securityDepositApyPPM?: bigint;
38
+ securityDepositBaseMultiplierPPM?: bigint;
39
+ }>;
40
+ /**
41
+ * Checks minimums/maximums, calculates the fee & total amount
42
+ *
43
+ * @param swapType
44
+ * @param request
45
+ * @param requestedAmount
46
+ * @param fees
47
+ * @param signal
48
+ * @param gasTokenAmount
49
+ * @throws {DefinedRuntimeError} will throw an error if the amount is outside minimum/maximum bounds
50
+ */
51
+ checkFromBtcAmount(swapType: SwapHandlerType.FROM_BTCLN | SwapHandlerType.FROM_BTC | SwapHandlerType.FROM_BTCLN_TRUSTED | SwapHandlerType.FROM_BTC_TRUSTED | SwapHandlerType.FROM_BTC_SPV, request: RequestData<FromBtcLnRequestType | FromBtcRequestType | FromBtcLnTrustedRequestType | FromBtcTrustedRequestType | SpvVaultSwapRequestType>, requestedAmount: {
52
+ input: boolean;
53
+ amount: bigint;
54
+ token: string;
55
+ pricePrefetch?: Promise<bigint>;
56
+ }, fees: {
57
+ baseFee: bigint;
58
+ feePPM: bigint;
59
+ }, signal: AbortSignal, gasTokenAmount?: {
60
+ input: false;
61
+ amount: bigint;
62
+ token: string;
63
+ pricePrefetch?: Promise<bigint>;
64
+ }): Promise<{
65
+ amountBD: bigint;
66
+ swapFee: bigint;
67
+ swapFeeInToken: bigint;
68
+ totalInToken: bigint;
69
+ amountBDgas?: bigint;
70
+ gasSwapFee?: bigint;
71
+ gasSwapFeeInToken?: bigint;
72
+ totalInGasToken?: bigint;
73
+ securityDepositApyPPM?: bigint;
74
+ securityDepositBaseMultiplierPPM?: bigint;
75
+ }>;
76
+ }