@atomiqlabs/sdk 8.6.2 → 8.7.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 (85) hide show
  1. package/dist/events/UnifiedSwapEventListener.js +4 -2
  2. package/dist/http/paramcoders/ParamDecoder.js +9 -4
  3. package/dist/http/paramcoders/ParamEncoder.js +6 -1
  4. package/dist/intermediaries/Intermediary.d.ts +21 -0
  5. package/dist/intermediaries/Intermediary.js +25 -1
  6. package/dist/intermediaries/IntermediaryDiscovery.d.ts +15 -3
  7. package/dist/intermediaries/IntermediaryDiscovery.js +25 -6
  8. package/dist/intermediaries/apis/IntermediaryAPI.d.ts +1 -0
  9. package/dist/swapper/Swapper.d.ts +9 -4
  10. package/dist/swapper/Swapper.js +94 -42
  11. package/dist/swapper/SwapperUtils.js +2 -1
  12. package/dist/swaps/ISwap.d.ts +5 -0
  13. package/dist/swaps/ISwap.js +4 -1
  14. package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +5 -5
  15. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +4 -0
  16. package/dist/swaps/escrow_swaps/IEscrowSwap.js +4 -3
  17. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +19 -6
  18. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +54 -21
  19. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +7 -3
  20. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +3 -4
  21. package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +3 -3
  22. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +8 -2
  23. package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +12 -8
  24. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +18 -18
  25. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +12 -6
  26. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +38 -24
  27. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +9 -9
  28. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +14 -7
  29. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +54 -38
  30. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +5 -5
  31. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +18 -7
  32. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +61 -33
  33. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +12 -12
  34. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +8 -2
  35. package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +13 -8
  36. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +1 -1
  37. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +13 -4
  38. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +44 -28
  39. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +2 -2
  40. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +8 -4
  41. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +29 -21
  42. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +1 -0
  43. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +13 -12
  44. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +21 -10
  45. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +136 -73
  46. package/dist/swaps/trusted/ln/LnForGasWrapper.js +2 -1
  47. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +2 -1
  48. package/dist/utils/RetryUtils.d.ts +2 -1
  49. package/dist/utils/RetryUtils.js +3 -2
  50. package/dist/utils/Utils.d.ts +9 -0
  51. package/dist/utils/Utils.js +15 -1
  52. package/package.json +2 -2
  53. package/src/events/UnifiedSwapEventListener.ts +4 -2
  54. package/src/http/paramcoders/ParamDecoder.ts +8 -4
  55. package/src/http/paramcoders/ParamEncoder.ts +5 -1
  56. package/src/intermediaries/Intermediary.ts +31 -1
  57. package/src/intermediaries/IntermediaryDiscovery.ts +33 -12
  58. package/src/intermediaries/apis/IntermediaryAPI.ts +2 -1
  59. package/src/swapper/Swapper.ts +141 -62
  60. package/src/swapper/SwapperUtils.ts +3 -1
  61. package/src/swaps/ISwap.ts +10 -2
  62. package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +5 -5
  63. package/src/swaps/escrow_swaps/IEscrowSwap.ts +10 -3
  64. package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +64 -26
  65. package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +8 -5
  66. package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +3 -3
  67. package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +22 -12
  68. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +18 -18
  69. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +52 -31
  70. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +9 -9
  71. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +76 -52
  72. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +5 -5
  73. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +82 -38
  74. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +12 -12
  75. package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +21 -9
  76. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +1 -1
  77. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +56 -33
  78. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +2 -2
  79. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +40 -22
  80. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +17 -13
  81. package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +149 -83
  82. package/src/swaps/trusted/ln/LnForGasWrapper.ts +2 -1
  83. package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +2 -1
  84. package/src/utils/RetryUtils.ts +11 -4
  85. package/src/utils/Utils.ts +14 -0
@@ -4,6 +4,7 @@ exports.FromBTCWrapper = void 0;
4
4
  const IFromBTCWrapper_1 = require("../IFromBTCWrapper");
5
5
  const FromBTCSwap_1 = require("./FromBTCSwap");
6
6
  const base_1 = require("@atomiqlabs/base");
7
+ const Intermediary_1 = require("../../../../intermediaries/Intermediary");
7
8
  const buffer_1 = require("buffer");
8
9
  const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
9
10
  const SwapType_1 = require("../../../../enums/SwapType");
@@ -25,18 +26,16 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
25
26
  * @param unifiedStorage Storage interface for the current environment
26
27
  * @param unifiedChainEvents On-chain event listener
27
28
  * @param chain
28
- * @param contract Underlying contract handling the swaps
29
29
  * @param prices Pricing to use
30
30
  * @param tokens
31
- * @param swapDataDeserializer Deserializer for SwapData
32
- * @param btcRelay
33
- * @param synchronizer Btc relay synchronizer
31
+ * @param versionedContracts
32
+ * @param versionedSynchronizer
34
33
  * @param btcRpc Bitcoin RPC which also supports getting transactions by txoHash
35
34
  * @param options
36
35
  * @param events Instance to use for emitting events
37
36
  */
38
- constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, btcRelay, synchronizer, btcRpc, options, events) {
39
- super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, {
37
+ constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, versionedContracts, versionedSynchronizer, btcRpc, options, events) {
38
+ super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, {
40
39
  ...options,
41
40
  bitcoinNetwork: options?.bitcoinNetwork ?? utils_1.TEST_NETWORK,
42
41
  safetyFactor: options?.safetyFactor ?? 2,
@@ -44,7 +43,7 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
44
43
  maxConfirmations: options?.maxConfirmations ?? 6,
45
44
  minSendWindow: options?.minSendWindow ?? 30 * 60,
46
45
  bitcoinBlocktime: options?.bitcoinBlocktime ?? 10 * 60
47
- }, events);
46
+ }, versionedContracts, events);
48
47
  this.TYPE = SwapType_1.SwapType.FROM_BTC;
49
48
  /**
50
49
  * @internal
@@ -68,9 +67,28 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
68
67
  * @internal
69
68
  */
70
69
  this._swapDeserializer = FromBTCSwap_1.FromBTCSwap;
71
- this.btcRelay = btcRelay;
72
- this._synchronizer = synchronizer;
70
+ /**
71
+ * @internal
72
+ */
73
+ this._synchronizer = (version) => {
74
+ const _version = version ?? "v1";
75
+ const data = this.versionedSynchronizer[_version];
76
+ if (data == null)
77
+ throw new Error(`Invalid contract version ${_version} requested`);
78
+ return data.synchronizer;
79
+ };
80
+ this.btcRelay = (version) => {
81
+ const _version = version ?? "v1";
82
+ const data = this.versionedBtcRelay[_version];
83
+ if (data == null)
84
+ throw new Error(`Invalid contract version ${_version} requested`);
85
+ return data.btcRelay;
86
+ };
87
+ this.versionedBtcRelay = {};
88
+ this.versionedSynchronizer = {};
73
89
  this._btcRpc = btcRpc;
90
+ this.versionedBtcRelay = versionedContracts;
91
+ this.versionedSynchronizer = versionedSynchronizer;
74
92
  }
75
93
  /**
76
94
  * @inheritDoc
@@ -128,10 +146,11 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
128
146
  * @param amountData
129
147
  * @param options Options as passed to the swap creation function
130
148
  * @param abortController
149
+ * @param contractVersion
131
150
  *
132
151
  * @private
133
152
  */
134
- async preFetchClaimerBounty(signer, amountData, options, abortController) {
153
+ async preFetchClaimerBounty(signer, amountData, options, abortController, contractVersion) {
135
154
  const startTimestamp = BigInt(Math.floor(Date.now() / 1000));
136
155
  if (options.unsafeZeroWatchtowerFee) {
137
156
  return {
@@ -143,13 +162,13 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
143
162
  };
144
163
  }
145
164
  const dummyAmount = BigInt(Math.floor(Math.random() * 0x1000000));
146
- const dummySwapData = await this._contract.createSwapData(base_1.ChainSwapType.CHAIN, signer, signer, amountData.token, dummyAmount, this._contract.getHashForOnchain((0, Utils_1.randomBytes)(20), dummyAmount, 3).toString("hex"), this.getRandomSequence(), startTimestamp, false, true, BigInt(Math.floor(Math.random() * 0x10000)), BigInt(Math.floor(Math.random() * 0x10000)));
165
+ const dummySwapData = await this._contract(contractVersion).createSwapData(base_1.ChainSwapType.CHAIN, signer, signer, amountData.token, dummyAmount, this._contract(contractVersion).getHashForOnchain((0, Utils_1.randomBytes)(20), dummyAmount, 3).toString("hex"), this.getRandomSequence(), startTimestamp, false, true, BigInt(Math.floor(Math.random() * 0x10000)), BigInt(Math.floor(Math.random() * 0x10000)));
147
166
  try {
148
167
  const [feePerBlock, btcRelayData, currentBtcBlock, claimFeeRate] = await Promise.all([
149
- this.btcRelay.getFeePerBlock(),
150
- this.btcRelay.getTipData(),
168
+ this.btcRelay(contractVersion).getFeePerBlock(),
169
+ this.btcRelay(contractVersion).getTipData(),
151
170
  this._btcRpc.getTipHeight(),
152
- this._contract.getClaimFee(signer, dummySwapData)
171
+ this._contract(contractVersion).getClaimFee(signer, dummySwapData)
153
172
  ]);
154
173
  if (btcRelayData == null)
155
174
  throw new Error("Btc relay not initialized!");
@@ -231,9 +250,10 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
231
250
  if ((expiry - currentTimestamp) < BigInt(this._options.minSendWindow)) {
232
251
  throw new IntermediaryError_1.IntermediaryError("Send window too low");
233
252
  }
253
+ const version = lp.getContractVersion(this.chainIdentifier);
234
254
  const lockingScript = (0, BitcoinUtils_1.toOutputScript)(this._options.bitcoinNetwork, resp.btcAddress);
235
- const desiredExtraData = this._contract.getExtraData(lockingScript, resp.amount, requiredConfirmations);
236
- const desiredClaimHash = this._contract.getHashForOnchain(lockingScript, resp.amount, requiredConfirmations);
255
+ const desiredExtraData = this._contract(version).getExtraData(lockingScript, resp.amount, requiredConfirmations);
256
+ const desiredClaimHash = this._contract(version).getHashForOnchain(lockingScript, resp.amount, requiredConfirmations);
237
257
  if (!desiredClaimHash.equals(buffer_1.Buffer.from(data.getClaimHash(), "hex"))) {
238
258
  throw new IntermediaryError_1.IntermediaryError("Invalid claim hash returned!");
239
259
  }
@@ -261,6 +281,7 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
261
281
  else if (typeof (options?.feeSafetyFactor) === "number") {
262
282
  feeSafetyFactorPPM = BigInt(Math.floor(options.feeSafetyFactor * 1000000));
263
283
  }
284
+ const lpVersions = Intermediary_1.Intermediary.getContractVersionsForLps(this.chainIdentifier, lps);
264
285
  const _options = {
265
286
  blockSafetyFactor: options?.blockSafetyFactor != null ? BigInt(options.blockSafetyFactor) : 1n,
266
287
  feeSafetyFactorPPM,
@@ -270,20 +291,25 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
270
291
  const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
271
292
  const pricePrefetchPromise = this.preFetchPrice(amountData, _abortController.signal);
272
293
  const usdPricePrefetchPromise = this.preFetchUsdPrice(_abortController.signal);
273
- const claimerBountyPrefetchPromise = this.preFetchClaimerBounty(recipient, amountData, _options, _abortController);
294
+ const claimerBountyPrefetchPromise = (0, Utils_1.mapArrayToObject)(lpVersions, (contractVersion) => {
295
+ return this.preFetchClaimerBounty(recipient, amountData, _options, _abortController, contractVersion);
296
+ });
274
297
  const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
275
- const feeRatePromise = this.preFetchFeeRate(recipient, amountData, undefined, _abortController);
276
- const _signDataPromise = this._contract.preFetchBlockDataForSignatures == null ?
277
- this.preFetchSignData(Promise.resolve(true)) :
278
- undefined;
298
+ const feeRatePromise = this.preFetchFeeRate(recipient, amountData, undefined, _abortController, lpVersions);
299
+ const _signDataPromise = (0, Utils_1.mapArrayToObject)(lpVersions, (contractVersion) => {
300
+ return this._contract(contractVersion).preFetchBlockDataForSignatures == null ?
301
+ this.preFetchSignData(Promise.resolve(true), contractVersion) :
302
+ undefined;
303
+ });
279
304
  return lps.map(lp => {
280
305
  return {
281
306
  intermediary: lp,
282
307
  quote: (async () => {
283
308
  if (lp.services[SwapType_1.SwapType.FROM_BTC] == null)
284
309
  throw new Error("LP service for processing from btc swaps not found!");
310
+ const version = lp.getContractVersion(this.chainIdentifier);
285
311
  const abortController = (0, Utils_1.extendAbortController)(_abortController.signal);
286
- const liquidityPromise = this.preFetchIntermediaryLiquidity(amountData, lp, abortController);
312
+ const liquidityPromise = this.preFetchIntermediaryLiquidity(amountData, lp, abortController, version);
287
313
  try {
288
314
  const { signDataPromise, resp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
289
315
  const { signDataPrefetch, response } = IntermediaryAPI_1.IntermediaryAPI.initFromBTC(this.chainIdentifier, lp.url, nativeTokenAddress, {
@@ -292,13 +318,13 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
292
318
  token: amountData.token.toString(),
293
319
  exactOut: !amountData.exactIn,
294
320
  sequence,
295
- claimerBounty: (0, Utils_1.throwIfUndefined)(claimerBountyPrefetchPromise),
296
- feeRate: (0, Utils_1.throwIfUndefined)(feeRatePromise),
321
+ claimerBounty: (0, Utils_1.throwIfUndefined)(claimerBountyPrefetchPromise[version]),
322
+ feeRate: (0, Utils_1.throwIfUndefined)(feeRatePromise[version]),
297
323
  additionalParams
298
324
  }, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
299
- let signDataPromise = _signDataPromise;
325
+ let signDataPromise = _signDataPromise[version];
300
326
  if (signDataPromise == null) {
301
- signDataPromise = this.preFetchSignData(signDataPrefetch);
327
+ signDataPromise = this.preFetchSignData(signDataPrefetch, version);
302
328
  }
303
329
  else
304
330
  signDataPrefetch.catch(() => { });
@@ -307,13 +333,13 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
307
333
  resp: await response
308
334
  };
309
335
  }, undefined, e => e instanceof RequestError_1.RequestError, abortController.signal);
310
- const data = new this._swapDataDeserializer(resp.data);
336
+ const data = new (this._swapDataDeserializer(version))(resp.data);
311
337
  data.setClaimer(recipient);
312
- this.verifyReturnedData(recipient, resp, amountData, lp, _options, data, sequence, (await claimerBountyPrefetchPromise), nativeTokenAddress);
338
+ this.verifyReturnedData(recipient, resp, amountData, lp, _options, data, sequence, (await claimerBountyPrefetchPromise[version]), nativeTokenAddress);
313
339
  const [pricingInfo, signatureExpiry] = await Promise.all([
314
340
  //Get intermediary's liquidity
315
341
  this.verifyReturnedPrice(lp.services[SwapType_1.SwapType.FROM_BTC], false, resp.amount, resp.total, amountData.token, {}, pricePrefetchPromise, usdPricePrefetchPromise, abortController.signal),
316
- this.verifyReturnedSignature(recipient, data, resp, feeRatePromise, signDataPromise, abortController.signal),
342
+ this.verifyReturnedSignature(recipient, data, resp, feeRatePromise[version], signDataPromise, version, abortController.signal),
317
343
  this.verifyIntermediaryLiquidity(data.getAmount(), (0, Utils_1.throwIfUndefined)(liquidityPromise)),
318
344
  ]);
319
345
  const quote = new FromBTCSwap_1.FromBTCSwap(this, {
@@ -322,13 +348,14 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
322
348
  expiry: signatureExpiry,
323
349
  swapFee: resp.swapFee,
324
350
  swapFeeBtc: resp.swapFee * resp.amount / (data.getAmount() - resp.swapFee),
325
- feeRate: (await feeRatePromise),
351
+ feeRate: (await feeRatePromise[version]),
326
352
  signatureData: resp,
327
353
  data,
328
354
  address: resp.btcAddress,
329
355
  amount: resp.amount,
330
356
  exactIn: amountData.exactIn ?? true,
331
- requiredConfirmations: resp.confirmations
357
+ requiredConfirmations: resp.confirmations,
358
+ contractVersion: version
332
359
  });
333
360
  return quote;
334
361
  }
@@ -343,7 +370,7 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
343
370
  /**
344
371
  * @inheritDoc
345
372
  */
346
- async recoverFromSwapDataAndState(init, state, lp) {
373
+ async recoverFromSwapDataAndState(init, state, contractVersion, lp) {
347
374
  const data = init.data;
348
375
  const swapInit = {
349
376
  pricingInfo: {
@@ -361,7 +388,8 @@ class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
361
388
  feeRate: "",
362
389
  signatureData: undefined,
363
390
  data,
364
- exactIn: false
391
+ exactIn: false,
392
+ contractVersion
365
393
  };
366
394
  const swap = new FromBTCSwap_1.FromBTCSwap(this, swapInit);
367
395
  swap._commitTxId = await init.getInitTxId();
@@ -333,7 +333,7 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
333
333
  */
334
334
  async hasEnoughBalance() {
335
335
  const [balance, commitFee] = await Promise.all([
336
- this.wrapper._contract.getBalance(this._getInitiator(), this._data.getToken(), false),
336
+ this._contract.getBalance(this._getInitiator(), this._data.getToken(), false),
337
337
  this._data.getToken() === this.wrapper._chain.getNativeCurrencyAddress() ? this.getCommitFee() : Promise.resolve(null)
338
338
  ]);
339
339
  let required = this._data.getAmount();
@@ -351,7 +351,7 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
351
351
  */
352
352
  async hasEnoughForTxFees() {
353
353
  const [balance, commitFee] = await Promise.all([
354
- this.wrapper._contract.getBalance(this._getInitiator(), this.wrapper._chain.getNativeCurrencyAddress(), false),
354
+ this._contract.getBalance(this._getInitiator(), this.wrapper._chain.getNativeCurrencyAddress(), false),
355
355
  this.getCommitFee()
356
356
  ]);
357
357
  return {
@@ -461,7 +461,7 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
461
461
  this.initiated = true;
462
462
  await this._saveAndEmit();
463
463
  }
464
- return await this.wrapper._contract.txsInit(this._getInitiator(), this._data, this.signatureData, skipChecks, this.feeRate).catch(e => Promise.reject(e instanceof base_1.SignatureVerificationError ? new Error("Request timed out") : e));
464
+ return await this._contract.txsInit(this._getInitiator(), this._data, this.signatureData, skipChecks, this.feeRate).catch(e => Promise.reject(e instanceof base_1.SignatureVerificationError ? new Error("Request timed out") : e));
465
465
  }
466
466
  /**
467
467
  * @inheritDoc
@@ -583,7 +583,7 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
583
583
  }
584
584
  return processed;
585
585
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA:
586
- await this.wrapper._contract.isValidRefundAuthorization(this._data, resp.data);
586
+ await this._contract.isValidRefundAuthorization(this._data, resp.data);
587
587
  this._state = ToBTCSwapState.REFUNDABLE;
588
588
  if (save)
589
589
  await this._saveAndEmit();
@@ -649,11 +649,11 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
649
649
  return true;
650
650
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA:
651
651
  const resultData = result.data;
652
- await this.wrapper._contract.isValidRefundAuthorization(this._data, resultData);
652
+ await this._contract.isValidRefundAuthorization(this._data, resultData);
653
653
  await this._saveAndEmit(ToBTCSwapState.REFUNDABLE);
654
654
  return false;
655
655
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.EXPIRED:
656
- if (await this.wrapper._contract.isExpired(this._getInitiator(), this._data))
656
+ if (await this._contract.isExpired(this._getInitiator(), this._data))
657
657
  throw new Error("Swap expired");
658
658
  throw new IntermediaryError_1.IntermediaryError("Swap expired");
659
659
  case IntermediaryAPI_1.RefundAuthorizationResponseCodes.NOT_FOUND:
@@ -669,7 +669,7 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
669
669
  * Get the estimated smart chain transaction fee of the refund transaction
670
670
  */
671
671
  async getRefundNetworkFee() {
672
- const swapContract = this.wrapper._contract;
672
+ const swapContract = this._contract;
673
673
  return (0, TokenAmount_1.toTokenAmount)(await swapContract.getRefundFee(this._getInitiator(), this._data), this.wrapper._getNativeToken(), this.wrapper._prices);
674
674
  }
675
675
  /**
@@ -697,15 +697,15 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
697
697
  else {
698
698
  signer = this._getInitiator();
699
699
  }
700
- if (await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) {
701
- return await this.wrapper._contract.txsRefund(signer, this._data, true, true);
700
+ if (await this._contract.isExpired(this._getInitiator(), this._data)) {
701
+ return await this._contract.txsRefund(signer, this._data, true, true);
702
702
  }
703
703
  else {
704
704
  if (this.url == null)
705
705
  throw new Error("LP URL not known, cannot get cooperative refund message, wait till expiry to refund!");
706
706
  const res = await IntermediaryAPI_1.IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
707
707
  if (res.code === IntermediaryAPI_1.RefundAuthorizationResponseCodes.REFUND_DATA) {
708
- return await this.wrapper._contract.txsRefundWithAuthorization(signer, this._data, res.data, true, true);
708
+ return await this._contract.txsRefundWithAuthorization(signer, this._data, res.data, true, true);
709
709
  }
710
710
  throw new IntermediaryError_1.IntermediaryError("Invalid intermediary cooperative message returned");
711
711
  }
@@ -800,7 +800,7 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
800
800
  //Check if quote is still valid
801
801
  quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
802
802
  }
803
- commitStatus ??= await this.wrapper._contract.getCommitStatus(this._getInitiator(), this._data);
803
+ commitStatus ??= await this._contract.getCommitStatus(this._getInitiator(), this._data);
804
804
  if (commitStatus != null && await this._forciblySetOnchainState(commitStatus))
805
805
  return true;
806
806
  if ((this._state === ToBTCSwapState.CREATED || this._state === ToBTCSwapState.QUOTE_SOFT_EXPIRED)) {
@@ -909,7 +909,7 @@ class IToBTCSwap extends IEscrowSelfInitSwap_1.IEscrowSelfInitSwap {
909
909
  break;
910
910
  case ToBTCSwapState.COMMITED:
911
911
  case ToBTCSwapState.SOFT_CLAIMED:
912
- const expired = await this.wrapper._contract.isExpired(this._getInitiator(), this._data);
912
+ const expired = await this._contract.isExpired(this._getInitiator(), this._data);
913
913
  if (expired) {
914
914
  this._state = ToBTCSwapState.REFUNDABLE;
915
915
  if (save)
@@ -29,12 +29,13 @@ export declare abstract class IToBTCWrapper<T extends ChainType, D extends IToBT
29
29
  * @param amountData
30
30
  * @param lp Intermediary
31
31
  * @param abortController
32
+ * @param contractVersion
32
33
  * @returns Intermediary's reputation or null if failed
33
34
  * @throws {IntermediaryError} If the intermediary vault doesn't exist
34
35
  *
35
36
  * @internal
36
37
  */
37
- protected preFetchIntermediaryReputation(amountData: Omit<AmountData, "amount">, lp: Intermediary, abortController: AbortController): Promise<SingleChainReputationType | undefined>;
38
+ protected preFetchIntermediaryReputation(amountData: Omit<AmountData, "amount">, lp: Intermediary, abortController: AbortController, contractVersion: string): Promise<SingleChainReputationType | undefined>;
38
39
  /**
39
40
  * Pre-fetches feeRate for a given swap
40
41
  *
@@ -42,11 +43,16 @@ export declare abstract class IToBTCWrapper<T extends ChainType, D extends IToBT
42
43
  * @param amountData
43
44
  * @param claimHash optional hash of the swap or null
44
45
  * @param abortController
46
+ * @param contractVersions
45
47
  * @returns Fee rate
46
48
  *
47
49
  * @internal
48
50
  */
49
- protected preFetchFeeRate(signer: string, amountData: Omit<AmountData, "amount">, claimHash: string | undefined, abortController: AbortController): Promise<string | undefined>;
51
+ protected preFetchFeeRate(signer: string, amountData: Omit<AmountData, "amount">, claimHash: {
52
+ [contractVersion: string]: string;
53
+ } | undefined, abortController: AbortController, contractVersions: string[]): {
54
+ [contractVersion: string]: Promise<string | undefined>;
55
+ };
50
56
  /**
51
57
  * @internal
52
58
  */
@@ -4,6 +4,7 @@ exports.IToBTCWrapper = void 0;
4
4
  const IToBTCSwap_1 = require("./IToBTCSwap");
5
5
  const IntermediaryError_1 = require("../../../errors/IntermediaryError");
6
6
  const IEscrowSwapWrapper_1 = require("../IEscrowSwapWrapper");
7
+ const Utils_1 = require("../../../utils/Utils");
7
8
  /**
8
9
  * Base class for wrappers of escrow-based Smart chain -> Bitcoin (on-chain & lightning) swaps
9
10
  *
@@ -37,13 +38,14 @@ class IToBTCWrapper extends IEscrowSwapWrapper_1.IEscrowSwapWrapper {
37
38
  * @param amountData
38
39
  * @param lp Intermediary
39
40
  * @param abortController
41
+ * @param contractVersion
40
42
  * @returns Intermediary's reputation or null if failed
41
43
  * @throws {IntermediaryError} If the intermediary vault doesn't exist
42
44
  *
43
45
  * @internal
44
46
  */
45
- preFetchIntermediaryReputation(amountData, lp, abortController) {
46
- return lp.getReputation(this.chainIdentifier, this._contract, [amountData.token.toString()], abortController.signal).then(res => {
47
+ preFetchIntermediaryReputation(amountData, lp, abortController, contractVersion) {
48
+ return lp.getReputation(this.chainIdentifier, this._contract(contractVersion), [amountData.token.toString()], abortController.signal).then(res => {
47
49
  if (res == null)
48
50
  throw new IntermediaryError_1.IntermediaryError("Invalid data returned - invalid LP vault");
49
51
  return res;
@@ -60,16 +62,19 @@ class IToBTCWrapper extends IEscrowSwapWrapper_1.IEscrowSwapWrapper {
60
62
  * @param amountData
61
63
  * @param claimHash optional hash of the swap or null
62
64
  * @param abortController
65
+ * @param contractVersions
63
66
  * @returns Fee rate
64
67
  *
65
68
  * @internal
66
69
  */
67
- preFetchFeeRate(signer, amountData, claimHash, abortController) {
68
- return this._contract.getInitPayInFeeRate(signer, this._chain.randomAddress(), amountData.token, claimHash)
69
- .catch(e => {
70
- this.logger.warn("preFetchFeeRate(): Error: ", e);
71
- abortController.abort(e);
72
- return undefined;
70
+ preFetchFeeRate(signer, amountData, claimHash, abortController, contractVersions) {
71
+ return (0, Utils_1.mapArrayToObject)(contractVersions, (contractVersion) => {
72
+ return this._contract(contractVersion).getInitPayInFeeRate(signer, this._chain.randomAddress(), amountData.token, claimHash?.[contractVersion])
73
+ .catch(e => {
74
+ this.logger.warn("preFetchFeeRate(): Error: ", e);
75
+ abortController.abort(e);
76
+ return undefined;
77
+ });
73
78
  });
74
79
  }
75
80
  /**
@@ -78,7 +78,7 @@ class ToBTCLNSwap extends IToBTCSwap_1.IToBTCSwap {
78
78
  const secretBuffer = buffer_1.Buffer.from(result.secret, "hex");
79
79
  const hash = buffer_1.Buffer.from((0, sha2_1.sha256)(secretBuffer));
80
80
  if (check) {
81
- const claimHash = this.wrapper._contract.getHashForHtlc(hash);
81
+ const claimHash = this._contract.getHashForHtlc(hash);
82
82
  const expectedClaimHash = buffer_1.Buffer.from(this.getClaimHash(), "hex");
83
83
  if (!claimHash.equals(expectedClaimHash))
84
84
  throw new IntermediaryError_1.IntermediaryError("Invalid payment secret returned");
@@ -82,7 +82,12 @@ export declare class ToBTCLNWrapper<T extends ChainType> extends IToBTCWrapper<T
82
82
  * @internal
83
83
  */
84
84
  readonly _swapDeserializer: typeof ToBTCLNSwap;
85
- constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], contract: T["Contract"], prices: ISwapPrice, tokens: WrapperCtorTokens, swapDataDeserializer: new (data: any) => T["Data"], options?: AllOptional<ToBTCLNWrapperOptions>, events?: EventEmitter<{
85
+ constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], prices: ISwapPrice, tokens: WrapperCtorTokens, versionedContracts: {
86
+ [version: string]: {
87
+ swapContract: T["Contract"];
88
+ swapDataConstructor: new (data: any) => T["Data"];
89
+ };
90
+ }, options?: AllOptional<ToBTCLNWrapperOptions>, events?: EventEmitter<{
86
91
  swapState: [ISwap];
87
92
  }>);
88
93
  private toRequiredSwapOptions;
@@ -157,10 +162,14 @@ export declare class ToBTCLNWrapper<T extends ChainType> extends IToBTCWrapper<T
157
162
  create(signer: string, recipient: string, amountData: Omit<AmountData, "amount"> & {
158
163
  exactIn: false;
159
164
  }, lps: Intermediary[], options?: ToBTCLNOptions, additionalParams?: Record<string, any>, abortSignal?: AbortSignal, preFetches?: {
160
- feeRatePromise: Promise<string | undefined>;
165
+ feeRatePromise: {
166
+ [contractVersion: string]: Promise<string | undefined>;
167
+ };
161
168
  pricePreFetchPromise: Promise<bigint | undefined>;
162
169
  usdPricePrefetchPromise: Promise<number | undefined>;
163
- signDataPrefetchPromise?: Promise<T["PreFetchVerification"] | undefined>;
170
+ signDataPrefetchPromise?: {
171
+ [contractVersion: string]: Promise<T["PreFetchVerification"] | undefined> | undefined;
172
+ };
164
173
  }): Promise<{
165
174
  quote: Promise<ToBTCLNSwap<T>>;
166
175
  intermediary: Intermediary;
@@ -238,5 +247,5 @@ export declare class ToBTCLNWrapper<T extends ChainType> extends IToBTCWrapper<T
238
247
  blockTime: number;
239
248
  blockHeight: number;
240
249
  }>;
241
- }, state: SwapCommitState, lp?: Intermediary): Promise<ToBTCLNSwap<T> | null>;
250
+ }, state: SwapCommitState, contractVersion: string, lp?: Intermediary): Promise<ToBTCLNSwap<T> | null>;
242
251
  }