@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.
- package/dist/events/UnifiedSwapEventListener.js +4 -2
- package/dist/http/paramcoders/ParamDecoder.js +9 -4
- package/dist/http/paramcoders/ParamEncoder.js +6 -1
- package/dist/intermediaries/Intermediary.d.ts +21 -0
- package/dist/intermediaries/Intermediary.js +25 -1
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +15 -3
- package/dist/intermediaries/IntermediaryDiscovery.js +25 -6
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +1 -0
- package/dist/swapper/Swapper.d.ts +9 -4
- package/dist/swapper/Swapper.js +94 -42
- package/dist/swapper/SwapperUtils.js +2 -1
- package/dist/swaps/ISwap.d.ts +5 -0
- package/dist/swaps/ISwap.js +4 -1
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +5 -5
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +4 -0
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +4 -3
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +19 -6
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +54 -21
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +7 -3
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +3 -4
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +3 -3
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +8 -2
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +12 -8
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +18 -18
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +12 -6
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +38 -24
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +9 -9
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +14 -7
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +54 -38
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +5 -5
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +18 -7
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +61 -33
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +12 -12
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +8 -2
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +13 -8
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +1 -1
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +13 -4
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +44 -28
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +2 -2
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +8 -4
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +29 -21
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +1 -0
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +13 -12
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +21 -10
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +136 -73
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +2 -1
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +2 -1
- package/dist/utils/RetryUtils.d.ts +2 -1
- package/dist/utils/RetryUtils.js +3 -2
- package/dist/utils/Utils.d.ts +9 -0
- package/dist/utils/Utils.js +15 -1
- package/package.json +2 -2
- package/src/events/UnifiedSwapEventListener.ts +4 -2
- package/src/http/paramcoders/ParamDecoder.ts +8 -4
- package/src/http/paramcoders/ParamEncoder.ts +5 -1
- package/src/intermediaries/Intermediary.ts +31 -1
- package/src/intermediaries/IntermediaryDiscovery.ts +33 -12
- package/src/intermediaries/apis/IntermediaryAPI.ts +2 -1
- package/src/swapper/Swapper.ts +141 -62
- package/src/swapper/SwapperUtils.ts +3 -1
- package/src/swaps/ISwap.ts +10 -2
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +5 -5
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +10 -3
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +64 -26
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +8 -5
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +3 -3
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +22 -12
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +18 -18
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +52 -31
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +9 -9
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +76 -52
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +5 -5
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +82 -38
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +12 -12
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +21 -9
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +1 -1
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +56 -33
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +2 -2
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +40 -22
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +17 -13
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +149 -83
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +2 -1
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +2 -1
- package/src/utils/RetryUtils.ts +11 -4
- package/src/utils/Utils.ts +14 -0
|
@@ -4,6 +4,7 @@ exports.FromBTCLNWrapper = void 0;
|
|
|
4
4
|
const FromBTCLNSwap_1 = require("./FromBTCLNSwap");
|
|
5
5
|
const bolt11_1 = require("@atomiqlabs/bolt11");
|
|
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 UserError_1 = require("../../../../errors/UserError");
|
|
9
10
|
const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
|
|
@@ -26,16 +27,15 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
26
27
|
* @param unifiedStorage Storage interface for the current environment
|
|
27
28
|
* @param unifiedChainEvents On-chain event listener
|
|
28
29
|
* @param chain
|
|
29
|
-
* @param contract Underlying contract handling the swaps
|
|
30
30
|
* @param prices Swap pricing handler
|
|
31
31
|
* @param tokens
|
|
32
|
-
* @param
|
|
32
|
+
* @param versionedContracts
|
|
33
33
|
* @param lnApi
|
|
34
34
|
* @param options
|
|
35
35
|
* @param events Instance to use for emitting events
|
|
36
36
|
*/
|
|
37
|
-
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain,
|
|
38
|
-
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain,
|
|
37
|
+
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, versionedContracts, lnApi, options, events) {
|
|
38
|
+
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, versionedContracts, lnApi, {
|
|
39
39
|
...options,
|
|
40
40
|
safetyFactor: options?.safetyFactor ?? 2,
|
|
41
41
|
bitcoinBlocktime: options?.bitcoinBlocktime ?? 10 * 60,
|
|
@@ -164,6 +164,7 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
164
164
|
};
|
|
165
165
|
if (_options.description != null && buffer_1.Buffer.byteLength(_options.description, "utf8") > 500)
|
|
166
166
|
throw new UserError_1.UserError("Invalid description length");
|
|
167
|
+
const lpVersions = Intermediary_1.Intermediary.getContractVersionsForLps(this.chainIdentifier, lps);
|
|
167
168
|
let secret;
|
|
168
169
|
let paymentHash;
|
|
169
170
|
if (_options.paymentHash != null) {
|
|
@@ -172,13 +173,15 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
172
173
|
else {
|
|
173
174
|
({ secret, paymentHash } = this.getSecretAndHash());
|
|
174
175
|
}
|
|
175
|
-
const
|
|
176
|
+
const _hash = (0, Utils_1.mapArrayToObject)(lpVersions, (contractVersion) => {
|
|
177
|
+
return this._contract(contractVersion).getHashForHtlc(paymentHash).toString("hex");
|
|
178
|
+
});
|
|
176
179
|
const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
|
|
177
180
|
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
178
|
-
const _preFetches = {
|
|
179
|
-
pricePrefetchPromise:
|
|
180
|
-
feeRatePromise:
|
|
181
|
-
usdPricePrefetchPromise:
|
|
181
|
+
const _preFetches = preFetches ?? {
|
|
182
|
+
pricePrefetchPromise: this.preFetchPrice(amountData, _abortController.signal),
|
|
183
|
+
feeRatePromise: this.preFetchFeeRate(recipient, amountData, _hash, _abortController, lpVersions),
|
|
184
|
+
usdPricePrefetchPromise: this.preFetchUsdPrice(_abortController.signal),
|
|
182
185
|
};
|
|
183
186
|
return lps.map(lp => {
|
|
184
187
|
return {
|
|
@@ -186,8 +189,9 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
186
189
|
quote: (async () => {
|
|
187
190
|
if (lp.services[SwapType_1.SwapType.FROM_BTCLN] == null)
|
|
188
191
|
throw new Error("LP service for processing from btcln swaps not found!");
|
|
192
|
+
const version = lp.getContractVersion(this.chainIdentifier);
|
|
189
193
|
const abortController = (0, Utils_1.extendAbortController)(_abortController.signal);
|
|
190
|
-
const liquidityPromise = this.preFetchIntermediaryLiquidity(amountData, lp, abortController);
|
|
194
|
+
const liquidityPromise = this.preFetchIntermediaryLiquidity(amountData, lp, abortController, version);
|
|
191
195
|
const { lnCapacityPromise, resp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
192
196
|
const { lnPublicKey, response } = IntermediaryAPI_1.IntermediaryAPI.initFromBTCLN(this.chainIdentifier, lp.url, nativeTokenAddress, {
|
|
193
197
|
paymentHash,
|
|
@@ -197,7 +201,7 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
197
201
|
description: _options.description,
|
|
198
202
|
descriptionHash: _options.descriptionHash,
|
|
199
203
|
exactOut: !amountData.exactIn,
|
|
200
|
-
feeRate: (0, Utils_1.throwIfUndefined)(_preFetches.feeRatePromise),
|
|
204
|
+
feeRate: (0, Utils_1.throwIfUndefined)(_preFetches.feeRatePromise[version]),
|
|
201
205
|
additionalParams
|
|
202
206
|
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
203
207
|
let lnCapacityPromise;
|
|
@@ -230,11 +234,12 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
230
234
|
expiry: decodedPr.timeExpireDate * 1000,
|
|
231
235
|
swapFee: resp.swapFee,
|
|
232
236
|
swapFeeBtc: resp.swapFee * amountIn / (resp.total - resp.swapFee),
|
|
233
|
-
feeRate: (await _preFetches.feeRatePromise),
|
|
234
|
-
initialSwapData: await this._contract.createSwapData(base_1.ChainSwapType.HTLC, lp.getAddress(this.chainIdentifier), recipient, amountData.token, resp.total,
|
|
237
|
+
feeRate: (await _preFetches.feeRatePromise[version]),
|
|
238
|
+
initialSwapData: await this._contract(version).createSwapData(base_1.ChainSwapType.HTLC, lp.getAddress(this.chainIdentifier), recipient, amountData.token, resp.total, _hash[version], this.getRandomSequence(), BigInt(Math.floor(Date.now() / 1000)), false, true, resp.securityDeposit, 0n, nativeTokenAddress),
|
|
235
239
|
pr: resp.pr,
|
|
236
240
|
secret: secret?.toString("hex"),
|
|
237
|
-
exactIn: amountData.exactIn ?? true
|
|
241
|
+
exactIn: amountData.exactIn ?? true,
|
|
242
|
+
contractVersion: version
|
|
238
243
|
});
|
|
239
244
|
return quote;
|
|
240
245
|
}
|
|
@@ -270,11 +275,12 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
270
275
|
descriptionHash: (0, Utils_1.parseHashValueExact32Bytes)(options?.descriptionHash, "description hash"),
|
|
271
276
|
unsafeSkipLnNodeCheck: options?.unsafeSkipLnNodeCheck ?? this._options.unsafeSkipLnNodeCheck
|
|
272
277
|
};
|
|
278
|
+
const lpVersions = Intermediary_1.Intermediary.getContractVersionsForLps(this.chainIdentifier, lps);
|
|
273
279
|
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
274
280
|
const preFetches = {
|
|
275
281
|
pricePrefetchPromise: this.preFetchPrice(amountData, abortController.signal),
|
|
276
282
|
usdPricePrefetchPromise: this.preFetchUsdPrice(abortController.signal),
|
|
277
|
-
feeRatePromise: this.preFetchFeeRate(recipient, amountData, undefined, abortController)
|
|
283
|
+
feeRatePromise: this.preFetchFeeRate(recipient, amountData, undefined, abortController, lpVersions)
|
|
278
284
|
};
|
|
279
285
|
try {
|
|
280
286
|
const exactOutAmountPromise = !amountData.exactIn ? preFetches.pricePrefetchPromise.then(price => this._prices.getToBtcSwapAmount(this.chainIdentifier, amountData.amount, amountData.token, abortController.signal, price)).catch(e => {
|
|
@@ -325,7 +331,7 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
325
331
|
async _checkPastSwaps(pastSwaps) {
|
|
326
332
|
const changedSwapSet = new Set();
|
|
327
333
|
const swapExpiredStatus = {};
|
|
328
|
-
const checkStatusSwaps =
|
|
334
|
+
const checkStatusSwaps = {};
|
|
329
335
|
await Promise.all(pastSwaps.map(async (pastSwap) => {
|
|
330
336
|
if (pastSwap._shouldCheckIntermediary()) {
|
|
331
337
|
try {
|
|
@@ -345,14 +351,21 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
345
351
|
if (pastSwap._shouldFetchOnchainState()) {
|
|
346
352
|
//Add to swaps for which status should be checked
|
|
347
353
|
if (pastSwap._data != null)
|
|
348
|
-
checkStatusSwaps.push(pastSwap);
|
|
354
|
+
(checkStatusSwaps[pastSwap._contractVersion ?? "v1"] ??= []).push(pastSwap);
|
|
349
355
|
}
|
|
350
356
|
}));
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
357
|
+
for (let version in checkStatusSwaps) {
|
|
358
|
+
if (this._versionedContracts[version] == null) {
|
|
359
|
+
this.logger.warn(`_checkPastSwaps(): No contract was found for ${this.chainIdentifier} version ${version}! Skipping these swaps!`);
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
const _checkStatusSwap = checkStatusSwaps[version];
|
|
363
|
+
const swapStatuses = await this._contract(version).getCommitStatuses(_checkStatusSwap.map(val => ({ signer: val._getInitiator(), swapData: val._data })));
|
|
364
|
+
for (let pastSwap of _checkStatusSwap) {
|
|
365
|
+
const shouldSave = await pastSwap._sync(false, swapExpiredStatus[pastSwap.getId()], swapStatuses[pastSwap.getEscrowHash()], true);
|
|
366
|
+
if (shouldSave) {
|
|
367
|
+
changedSwapSet.add(pastSwap);
|
|
368
|
+
}
|
|
356
369
|
}
|
|
357
370
|
}
|
|
358
371
|
const changedSwaps = [];
|
|
@@ -374,7 +387,7 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
374
387
|
* @inheritDoc
|
|
375
388
|
* @internal
|
|
376
389
|
*/
|
|
377
|
-
async recoverFromSwapDataAndState(init, state, lp) {
|
|
390
|
+
async recoverFromSwapDataAndState(init, state, contractVersion, lp) {
|
|
378
391
|
const data = init.data;
|
|
379
392
|
let paymentHash = data.getHTLCHashHint();
|
|
380
393
|
let secret;
|
|
@@ -401,7 +414,8 @@ class FromBTCLNWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
401
414
|
data,
|
|
402
415
|
pr: paymentHash ?? undefined,
|
|
403
416
|
secret,
|
|
404
|
-
exactIn: false
|
|
417
|
+
exactIn: false,
|
|
418
|
+
contractVersion
|
|
405
419
|
};
|
|
406
420
|
const swap = new FromBTCLNSwap_1.FromBTCLNSwap(this, swapInit);
|
|
407
421
|
swap._commitTxId = await init.getInitTxId();
|
|
@@ -576,7 +576,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
576
576
|
}
|
|
577
577
|
isValidSecretPreimage(secret) {
|
|
578
578
|
const paymentHash = buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.from(secret, "hex")));
|
|
579
|
-
const claimHash = this.
|
|
579
|
+
const claimHash = this._contract.getHashForHtlc(paymentHash).toString("hex");
|
|
580
580
|
return this.getSwapData().getClaimHash() === claimHash;
|
|
581
581
|
}
|
|
582
582
|
/**
|
|
@@ -737,7 +737,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
737
737
|
const resp = await IntermediaryAPI_1.IntermediaryAPI.getInvoiceStatus(this.url, paymentHash.toString("hex"));
|
|
738
738
|
switch (resp.code) {
|
|
739
739
|
case IntermediaryAPI_1.InvoiceStatusResponseCodes.PAID:
|
|
740
|
-
const data = new this.wrapper._swapDataDeserializer(resp.data.data);
|
|
740
|
+
const data = new (this.wrapper._swapDataDeserializer(this._contractVersion))(resp.data.data);
|
|
741
741
|
if (this._state === FromBTCLNAutoSwapState.PR_CREATED || this._state === FromBTCLNAutoSwapState.QUOTE_SOFT_EXPIRED)
|
|
742
742
|
try {
|
|
743
743
|
await this._saveRealSwapData(data, save);
|
|
@@ -809,7 +809,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
809
809
|
throw new IntermediaryError_1.IntermediaryError("Invalid deposit token used!");
|
|
810
810
|
if (data.hasSuccessAction())
|
|
811
811
|
throw new IntermediaryError_1.IntermediaryError("Invalid has success action");
|
|
812
|
-
if (await this.wrapper._contract.isExpired(this._getInitiator(), data))
|
|
812
|
+
if (await this.wrapper._contract(this._contractVersion).isExpired(this._getInitiator(), data))
|
|
813
813
|
throw new IntermediaryError_1.IntermediaryError("Not enough time to claim!");
|
|
814
814
|
if (this.wrapper._getHtlcTimeout(data) <= (Date.now() / 1000))
|
|
815
815
|
throw new IntermediaryError_1.IntermediaryError("HTLC expires too soon!");
|
|
@@ -880,7 +880,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
880
880
|
this.lnurlFailSignal.signal.removeEventListener("abort", lnurlFailListener);
|
|
881
881
|
abortController.signal.throwIfAborted();
|
|
882
882
|
if (resp.code === IntermediaryAPI_1.InvoiceStatusResponseCodes.PAID) {
|
|
883
|
-
const swapData = new this.wrapper._swapDataDeserializer(resp.data.data);
|
|
883
|
+
const swapData = new (this.wrapper._swapDataDeserializer(this._contractVersion))(resp.data.data);
|
|
884
884
|
return await this._saveRealSwapData(swapData, true);
|
|
885
885
|
}
|
|
886
886
|
if (this._state === FromBTCLNAutoSwapState.PR_CREATED || this._state === FromBTCLNAutoSwapState.QUOTE_SOFT_EXPIRED) {
|
|
@@ -984,7 +984,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
984
984
|
throw new Error("Swap secret pre-image not known and not provided, please provide the swap secret pre-image as an argument");
|
|
985
985
|
if (!this.isValidSecretPreimage(useSecret))
|
|
986
986
|
throw new Error("Invalid swap secret pre-image provided!");
|
|
987
|
-
return await this.
|
|
987
|
+
return await this._contract.txsClaimWithSecret(address ?? this._getInitiator(), this._data, useSecret, true, true);
|
|
988
988
|
}
|
|
989
989
|
/**
|
|
990
990
|
* @inheritDoc
|
|
@@ -1176,7 +1176,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
1176
1176
|
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
|
|
1177
1177
|
}
|
|
1178
1178
|
//Check if it's already successfully paid
|
|
1179
|
-
commitStatus ??= await this.
|
|
1179
|
+
commitStatus ??= await this._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
1180
1180
|
if (commitStatus != null && await this._forciblySetOnchainState(commitStatus))
|
|
1181
1181
|
return true;
|
|
1182
1182
|
if (this._state === FromBTCLNAutoSwapState.PR_PAID) {
|
|
@@ -1239,7 +1239,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
1239
1239
|
if (await this.syncStateFromChain(quoteDefinitelyExpired, commitStatus))
|
|
1240
1240
|
changed = true;
|
|
1241
1241
|
if (this._state === FromBTCLNAutoSwapState.CLAIM_COMMITED) {
|
|
1242
|
-
const expired = await this.
|
|
1242
|
+
const expired = await this._contract.isExpired(this._getInitiator(), this._data);
|
|
1243
1243
|
if (expired) {
|
|
1244
1244
|
this._state = FromBTCLNAutoSwapState.EXPIRED;
|
|
1245
1245
|
changed = true;
|
|
@@ -1308,7 +1308,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
1308
1308
|
if (!this.isValidSecretPreimage(useSecret))
|
|
1309
1309
|
throw new Error("Invalid swap secret pre-image provided!");
|
|
1310
1310
|
if (!noCheckExpiry) {
|
|
1311
|
-
if (await this.
|
|
1311
|
+
if (await this._contract.isExpired(this._getInitiator(), this._data))
|
|
1312
1312
|
throw new Error("On-chain HTLC already expired!");
|
|
1313
1313
|
}
|
|
1314
1314
|
await this.wrapper._messenger.broadcast(new base_1.SwapClaimWitnessMessage(this._data, useSecret));
|
|
@@ -1337,7 +1337,7 @@ class FromBTCLNAutoSwap extends IEscrowSwap_1.IEscrowSwap {
|
|
|
1337
1337
|
break;
|
|
1338
1338
|
case FromBTCLNAutoSwapState.PR_PAID:
|
|
1339
1339
|
case FromBTCLNAutoSwapState.CLAIM_COMMITED:
|
|
1340
|
-
const expired = await this.
|
|
1340
|
+
const expired = await this._contract.isExpired(this._getInitiator(), this._data);
|
|
1341
1341
|
if (expired) {
|
|
1342
1342
|
this._state = FromBTCLNAutoSwapState.EXPIRED;
|
|
1343
1343
|
if (save)
|
|
@@ -108,16 +108,20 @@ export declare class FromBTCLNAutoWrapper<T extends ChainType> extends IFromBTCL
|
|
|
108
108
|
* @param unifiedStorage Storage interface for the current environment
|
|
109
109
|
* @param unifiedChainEvents On-chain event listener
|
|
110
110
|
* @param chain
|
|
111
|
-
* @param contract Underlying contract handling the swaps
|
|
112
111
|
* @param prices Swap pricing handler
|
|
113
112
|
* @param tokens
|
|
114
|
-
* @param
|
|
113
|
+
* @param versionedContracts
|
|
115
114
|
* @param lnApi
|
|
116
115
|
* @param messenger
|
|
117
116
|
* @param options
|
|
118
117
|
* @param events Instance to use for emitting events
|
|
119
118
|
*/
|
|
120
|
-
constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"],
|
|
119
|
+
constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], prices: ISwapPrice, tokens: WrapperCtorTokens, versionedContracts: {
|
|
120
|
+
[version: string]: {
|
|
121
|
+
swapContract: T["Contract"];
|
|
122
|
+
swapDataConstructor: new (data: any) => T["Data"];
|
|
123
|
+
};
|
|
124
|
+
}, lnApi: LightningNetworkApi, messenger: Messenger, options?: AllOptional<FromBTCLNAutoWrapperOptions>, events?: EventEmitter<{
|
|
121
125
|
swapState: [ISwap];
|
|
122
126
|
}>);
|
|
123
127
|
/**
|
|
@@ -144,6 +148,7 @@ export declare class FromBTCLNAutoWrapper<T extends ChainType> extends IFromBTCL
|
|
|
144
148
|
* @param options Options as passed to the swap creation function
|
|
145
149
|
* @param abortController
|
|
146
150
|
*
|
|
151
|
+
* @param contractVersions
|
|
147
152
|
* @private
|
|
148
153
|
*/
|
|
149
154
|
private preFetchClaimerBounty;
|
|
@@ -178,10 +183,12 @@ export declare class FromBTCLNAutoWrapper<T extends ChainType> extends IFromBTCL
|
|
|
178
183
|
* @param preFetches Optional pre-fetches for speeding up the quoting process (mainly used internally)
|
|
179
184
|
*/
|
|
180
185
|
create(recipient: string, amountData: AmountData, lps: Intermediary[], options?: FromBTCLNAutoOptions, additionalParams?: Record<string, any>, abortSignal?: AbortSignal, preFetches?: {
|
|
181
|
-
pricePrefetchPromise
|
|
182
|
-
usdPricePrefetchPromise
|
|
186
|
+
pricePrefetchPromise: Promise<bigint | undefined>;
|
|
187
|
+
usdPricePrefetchPromise: Promise<number | undefined>;
|
|
188
|
+
claimerBountyPrefetch: {
|
|
189
|
+
[contractVersion: string]: Promise<bigint | undefined>;
|
|
190
|
+
};
|
|
183
191
|
gasTokenPricePrefetchPromise?: Promise<bigint | undefined>;
|
|
184
|
-
claimerBountyPrefetch?: Promise<bigint | undefined>;
|
|
185
192
|
}): {
|
|
186
193
|
quote: Promise<FromBTCLNAutoSwap<T>>;
|
|
187
194
|
intermediary: Intermediary;
|
|
@@ -224,5 +231,5 @@ export declare class FromBTCLNAutoWrapper<T extends ChainType> extends IFromBTCL
|
|
|
224
231
|
blockTime: number;
|
|
225
232
|
blockHeight: number;
|
|
226
233
|
}>;
|
|
227
|
-
}, state: SwapCommitState, lp?: Intermediary): Promise<FromBTCLNAutoSwap<T> | null>;
|
|
234
|
+
}, state: SwapCommitState, contractVersion: string, lp?: Intermediary): Promise<FromBTCLNAutoSwap<T> | null>;
|
|
228
235
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FromBTCLNAutoWrapper = void 0;
|
|
4
4
|
const bolt11_1 = require("@atomiqlabs/bolt11");
|
|
5
5
|
const base_1 = require("@atomiqlabs/base");
|
|
6
|
+
const Intermediary_1 = require("../../../../intermediaries/Intermediary");
|
|
6
7
|
const buffer_1 = require("buffer");
|
|
7
8
|
const UserError_1 = require("../../../../errors/UserError");
|
|
8
9
|
const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
|
|
@@ -28,17 +29,16 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
28
29
|
* @param unifiedStorage Storage interface for the current environment
|
|
29
30
|
* @param unifiedChainEvents On-chain event listener
|
|
30
31
|
* @param chain
|
|
31
|
-
* @param contract Underlying contract handling the swaps
|
|
32
32
|
* @param prices Swap pricing handler
|
|
33
33
|
* @param tokens
|
|
34
|
-
* @param
|
|
34
|
+
* @param versionedContracts
|
|
35
35
|
* @param lnApi
|
|
36
36
|
* @param messenger
|
|
37
37
|
* @param options
|
|
38
38
|
* @param events Instance to use for emitting events
|
|
39
39
|
*/
|
|
40
|
-
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain,
|
|
41
|
-
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain,
|
|
40
|
+
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, versionedContracts, lnApi, messenger, options, events) {
|
|
41
|
+
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, versionedContracts, lnApi, {
|
|
42
42
|
...options,
|
|
43
43
|
safetyFactor: options?.safetyFactor ?? 2,
|
|
44
44
|
bitcoinBlocktime: options?.bitcoinBlocktime ?? 10 * 60,
|
|
@@ -142,20 +142,23 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
142
142
|
* @param options Options as passed to the swap creation function
|
|
143
143
|
* @param abortController
|
|
144
144
|
*
|
|
145
|
+
* @param contractVersions
|
|
145
146
|
* @private
|
|
146
147
|
*/
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
148
|
+
preFetchClaimerBounty(signer, amountData, options, abortController, contractVersions) {
|
|
149
|
+
return (0, Utils_1.mapArrayToObject)(contractVersions, async (contractVersion) => {
|
|
150
|
+
if (options.unsafeZeroWatchtowerFee)
|
|
151
|
+
return 0n;
|
|
152
|
+
const dummyAmount = BigInt(Math.floor(Math.random() * 0x1000000));
|
|
153
|
+
const dummySwapData = await this._contract(contractVersion).createSwapData(base_1.ChainSwapType.HTLC, this._chain.randomAddress(), signer, amountData.token, dummyAmount, this._contract(contractVersion).getHashForHtlc((0, Utils_1.randomBytes)(32)).toString("hex"), this.getRandomSequence(), BigInt(Math.floor(Date.now() / 1000)), false, true, BigInt(Math.floor(Math.random() * 0x10000)), BigInt(Math.floor(Math.random() * 0x10000)));
|
|
154
|
+
try {
|
|
155
|
+
const result = await this._contract(contractVersion).getClaimFee(this._chain.randomAddress(), dummySwapData);
|
|
156
|
+
return result * BigInt(Math.floor(options.feeSafetyFactor * 1000000)) / 1000000n;
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
abortController.abort(e);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
159
162
|
}
|
|
160
163
|
/**
|
|
161
164
|
* Verifies response returned from intermediary
|
|
@@ -232,6 +235,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
232
235
|
throw new UserError_1.UserError("Cannot specify `gasAmount` for swaps to a native token!");
|
|
233
236
|
if (_options.description != null && buffer_1.Buffer.byteLength(_options.description, "utf8") > 500)
|
|
234
237
|
throw new UserError_1.UserError("Invalid description length");
|
|
238
|
+
const lpVersions = Intermediary_1.Intermediary.getContractVersionsForLps(this.chainIdentifier, lps);
|
|
235
239
|
let secret;
|
|
236
240
|
let paymentHash;
|
|
237
241
|
if (_options?.paymentHash != null) {
|
|
@@ -240,16 +244,17 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
240
244
|
else {
|
|
241
245
|
({ secret, paymentHash } = this.getSecretAndHash());
|
|
242
246
|
}
|
|
243
|
-
const
|
|
247
|
+
const _hash = (0, Utils_1.mapArrayToObject)(lpVersions, (contractVersion) => {
|
|
248
|
+
return this._contract(contractVersion).getHashForHtlc(paymentHash).toString("hex");
|
|
249
|
+
});
|
|
244
250
|
const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
|
|
245
251
|
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
246
|
-
preFetches
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
claimerBountyPrefetch: preFetches?.claimerBountyPrefetch ?? this.preFetchClaimerBounty(recipient, amountData, _options, _abortController),
|
|
252
|
+
const _preFetches = preFetches ?? {
|
|
253
|
+
pricePrefetchPromise: this.preFetchPrice(amountData, _abortController.signal),
|
|
254
|
+
usdPricePrefetchPromise: this.preFetchUsdPrice(_abortController.signal),
|
|
255
|
+
claimerBountyPrefetch: this.preFetchClaimerBounty(recipient, amountData, _options, _abortController, lpVersions),
|
|
251
256
|
gasTokenPricePrefetchPromise: _options.gasAmount !== 0n || !_options.unsafeZeroWatchtowerFee ?
|
|
252
|
-
|
|
257
|
+
this.preFetchPrice({ token: nativeTokenAddress }, _abortController.signal) :
|
|
253
258
|
undefined
|
|
254
259
|
};
|
|
255
260
|
return lps.map(lp => {
|
|
@@ -258,8 +263,9 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
258
263
|
quote: (async () => {
|
|
259
264
|
if (lp.services[SwapType_1.SwapType.FROM_BTCLN_AUTO] == null)
|
|
260
265
|
throw new Error("LP service for processing from btcln auto swaps not found!");
|
|
266
|
+
const version = lp.getContractVersion(this.chainIdentifier);
|
|
261
267
|
const abortController = (0, Utils_1.extendAbortController)(_abortController.signal);
|
|
262
|
-
const liquidityPromise = this.preFetchIntermediaryLiquidity(amountData, lp, abortController);
|
|
268
|
+
const liquidityPromise = this.preFetchIntermediaryLiquidity(amountData, lp, abortController, version);
|
|
263
269
|
const { lnCapacityPromise, resp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
264
270
|
const { lnPublicKey, response } = IntermediaryAPI_1.IntermediaryAPI.initFromBTCLNAuto(this.chainIdentifier, lp.url, {
|
|
265
271
|
paymentHash,
|
|
@@ -272,7 +278,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
272
278
|
additionalParams,
|
|
273
279
|
gasToken: this._chain.getNativeCurrencyAddress(),
|
|
274
280
|
gasAmount: _options.gasAmount,
|
|
275
|
-
claimerBounty: (0, Utils_1.throwIfUndefined)(_preFetches.claimerBountyPrefetch)
|
|
281
|
+
claimerBounty: (0, Utils_1.throwIfUndefined)(_preFetches.claimerBountyPrefetch[version])
|
|
276
282
|
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
277
283
|
let lnCapacityPromise;
|
|
278
284
|
if (!_options.unsafeSkipLnNodeCheck) {
|
|
@@ -291,7 +297,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
291
297
|
if (decodedPr.timeExpireDate == null)
|
|
292
298
|
throw new IntermediaryError_1.IntermediaryError("Invalid returned swap invoice, no expiry date field");
|
|
293
299
|
const amountIn = (BigInt(decodedPr.millisatoshis) + 999n) / 1000n;
|
|
294
|
-
const claimerBounty = (await _preFetches.claimerBountyPrefetch);
|
|
300
|
+
const claimerBounty = (await _preFetches.claimerBountyPrefetch[version]);
|
|
295
301
|
try {
|
|
296
302
|
this.verifyReturnedData(resp, amountData, lp, _options, decodedPr, paymentHash, claimerBounty);
|
|
297
303
|
const [pricingInfo, gasPricingInfo] = await Promise.all([
|
|
@@ -312,10 +318,11 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
312
318
|
btcAmountGas: resp.btcAmountGas,
|
|
313
319
|
btcAmountSwap: resp.btcAmountSwap,
|
|
314
320
|
gasPricingInfo,
|
|
315
|
-
initialSwapData: await this._contract.createSwapData(base_1.ChainSwapType.HTLC, lp.getAddress(this.chainIdentifier), recipient, amountData.token, resp.total,
|
|
321
|
+
initialSwapData: await this._contract(version).createSwapData(base_1.ChainSwapType.HTLC, lp.getAddress(this.chainIdentifier), recipient, amountData.token, resp.total, _hash[version], this.getRandomSequence(), BigInt(Math.floor(Date.now() / 1000)), false, true, _options.gasAmount + resp.claimerBounty, resp.claimerBounty, nativeTokenAddress),
|
|
316
322
|
pr: resp.pr,
|
|
317
323
|
secret: secret?.toString("hex"),
|
|
318
|
-
exactIn: amountData.exactIn ?? true
|
|
324
|
+
exactIn: amountData.exactIn ?? true,
|
|
325
|
+
contractVersion: version
|
|
319
326
|
};
|
|
320
327
|
const quote = new FromBTCLNAutoSwap_1.FromBTCLNAutoSwap(this, swapInit);
|
|
321
328
|
return quote;
|
|
@@ -356,6 +363,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
356
363
|
description: options?.description,
|
|
357
364
|
descriptionHash: (0, Utils_1.parseHashValueExact32Bytes)(options?.descriptionHash, "description hash")
|
|
358
365
|
};
|
|
366
|
+
const lpVersions = Intermediary_1.Intermediary.getContractVersionsForLps(this.chainIdentifier, lps);
|
|
359
367
|
const abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
360
368
|
const preFetches = {
|
|
361
369
|
pricePrefetchPromise: this.preFetchPrice(amountData, abortController.signal),
|
|
@@ -363,7 +371,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
363
371
|
gasTokenPricePrefetchPromise: _options.gasAmount !== 0n || !_options.unsafeZeroWatchtowerFee ?
|
|
364
372
|
this.preFetchPrice({ token: this._chain.getNativeCurrencyAddress() }, abortController.signal) :
|
|
365
373
|
undefined,
|
|
366
|
-
claimerBountyPrefetch: this.preFetchClaimerBounty(recipient, amountData, _options, abortController)
|
|
374
|
+
claimerBountyPrefetch: this.preFetchClaimerBounty(recipient, amountData, _options, abortController, lpVersions)
|
|
367
375
|
};
|
|
368
376
|
try {
|
|
369
377
|
const exactOutAmountPromise = !amountData.exactIn ? preFetches.pricePrefetchPromise.then(price => this._prices.getToBtcSwapAmount(this.chainIdentifier, amountData.amount, amountData.token, abortController.signal, price)).catch(e => {
|
|
@@ -414,7 +422,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
414
422
|
async _checkPastSwaps(pastSwaps) {
|
|
415
423
|
const changedSwapSet = new Set();
|
|
416
424
|
const swapExpiredStatus = {};
|
|
417
|
-
const checkStatusSwaps =
|
|
425
|
+
const checkStatusSwaps = {};
|
|
418
426
|
await Promise.all(pastSwaps.map(async (pastSwap) => {
|
|
419
427
|
if (pastSwap._shouldCheckIntermediary()) {
|
|
420
428
|
try {
|
|
@@ -434,14 +442,21 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
434
442
|
if (pastSwap._shouldFetchOnchainState()) {
|
|
435
443
|
//Add to swaps for which status should be checked
|
|
436
444
|
if (pastSwap._data != null)
|
|
437
|
-
checkStatusSwaps.push(pastSwap);
|
|
445
|
+
(checkStatusSwaps[pastSwap._contractVersion ?? "v1"] ??= []).push(pastSwap);
|
|
438
446
|
}
|
|
439
447
|
}));
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
448
|
+
for (let version in checkStatusSwaps) {
|
|
449
|
+
if (this._versionedContracts[version] == null) {
|
|
450
|
+
this.logger.warn(`_checkPastSwaps(): No contract was found for ${this.chainIdentifier} version ${version}! Skipping these swaps!`);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const _checkStatusSwap = checkStatusSwaps[version];
|
|
454
|
+
const swapStatuses = await this._contract(version).getCommitStatuses(_checkStatusSwap.map(val => ({ signer: val._getInitiator(), swapData: val._data })));
|
|
455
|
+
for (let pastSwap of _checkStatusSwap) {
|
|
456
|
+
const shouldSave = await pastSwap._sync(false, swapExpiredStatus[pastSwap.getId()], swapStatuses[pastSwap.getEscrowHash()], true);
|
|
457
|
+
if (shouldSave) {
|
|
458
|
+
changedSwapSet.add(pastSwap);
|
|
459
|
+
}
|
|
445
460
|
}
|
|
446
461
|
}
|
|
447
462
|
const changedSwaps = [];
|
|
@@ -462,7 +477,7 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
462
477
|
/**
|
|
463
478
|
* @inheritDoc
|
|
464
479
|
*/
|
|
465
|
-
async recoverFromSwapDataAndState(init, state, lp) {
|
|
480
|
+
async recoverFromSwapDataAndState(init, state, contractVersion, lp) {
|
|
466
481
|
const data = init.data;
|
|
467
482
|
let paymentHash = data.getHTLCHashHint();
|
|
468
483
|
let secret;
|
|
@@ -489,7 +504,8 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
|
|
|
489
504
|
data,
|
|
490
505
|
pr: paymentHash ?? undefined,
|
|
491
506
|
secret,
|
|
492
|
-
exactIn: false
|
|
507
|
+
exactIn: false,
|
|
508
|
+
contractVersion
|
|
493
509
|
};
|
|
494
510
|
const swap = new FromBTCLNAutoSwap_1.FromBTCLNAutoSwap(this, swapInit);
|
|
495
511
|
swap._commitTxId = await init.getInitTxId();
|
|
@@ -305,7 +305,7 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
305
305
|
inferRequiredConfirmationsCount(btcTx, vout) {
|
|
306
306
|
const txOut = btcTx.outs[vout];
|
|
307
307
|
for (let i = 1; i <= 20; i++) {
|
|
308
|
-
const computedClaimHash = this.
|
|
308
|
+
const computedClaimHash = this._contract.getHashForOnchain(buffer_1.Buffer.from(txOut.scriptPubKey.hex, "hex"), BigInt(txOut.value), i);
|
|
309
309
|
if (computedClaimHash.toString("hex") === this._data.getClaimHash()) {
|
|
310
310
|
return i;
|
|
311
311
|
}
|
|
@@ -815,13 +815,13 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
815
815
|
throw new Error("Cannot create claim transaction, because required confirmations are not known and cannot be infered! This can happen after a swap is recovered.");
|
|
816
816
|
if (tx.blockhash == null || tx.confirmations == null || tx.blockheight == null || tx.confirmations < this.requiredConfirmations)
|
|
817
817
|
throw new Error("Bitcoin transaction not confirmed yet!");
|
|
818
|
-
return await this.
|
|
818
|
+
return await this._contract.txsClaimWithTxData(signer ?? this._getInitiator(), this._data, {
|
|
819
819
|
blockhash: tx.blockhash,
|
|
820
820
|
confirmations: tx.confirmations,
|
|
821
821
|
txid: tx.txid,
|
|
822
822
|
hex: tx.hex,
|
|
823
823
|
height: tx.blockheight
|
|
824
|
-
}, this.requiredConfirmations, this.vout, undefined, this.wrapper._synchronizer, true);
|
|
824
|
+
}, this.requiredConfirmations, this.vout, undefined, this.wrapper._synchronizer(this._contractVersion), true);
|
|
825
825
|
}
|
|
826
826
|
/**
|
|
827
827
|
* Settles the swap by claiming the funds on the destination chain if the swap requires manual settlement, you can
|
|
@@ -855,7 +855,7 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
855
855
|
this.logger.info("claim(): Transaction state is CLAIM_CLAIMED, swap was successfully claimed by the watchtower");
|
|
856
856
|
return this._claimTxId;
|
|
857
857
|
}
|
|
858
|
-
const status = await this.
|
|
858
|
+
const status = await this._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
859
859
|
if (status?.type === base_1.SwapCommitStateType.PAID) {
|
|
860
860
|
this.logger.info("claim(): Transaction commit status is PAID, swap was successfully claimed by the watchtower");
|
|
861
861
|
if (this._claimTxId == null)
|
|
@@ -973,7 +973,7 @@ class FromBTCSwap extends IFromBTCSelfInitSwap_1.IFromBTCSelfInitSwap {
|
|
|
973
973
|
if (this._state === FromBTCSwapState.PR_CREATED || this._state === FromBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
974
974
|
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired(); //Make sure we check for expiry here, to prevent race conditions
|
|
975
975
|
}
|
|
976
|
-
const status = commitStatus ?? await this.
|
|
976
|
+
const status = commitStatus ?? await this._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
977
977
|
if (status != null && await this._forciblySetOnchainState(status))
|
|
978
978
|
return true;
|
|
979
979
|
if (this._state === FromBTCSwapState.PR_CREATED || this._state === FromBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
@@ -74,28 +74,38 @@ export declare class FromBTCWrapper<T extends ChainType> extends IFromBTCWrapper
|
|
|
74
74
|
/**
|
|
75
75
|
* @internal
|
|
76
76
|
*/
|
|
77
|
-
readonly _synchronizer: RelaySynchronizer<any, T["TX"], any>;
|
|
77
|
+
readonly _synchronizer: (version?: string) => RelaySynchronizer<any, T["TX"], any>;
|
|
78
78
|
/**
|
|
79
79
|
* @internal
|
|
80
80
|
*/
|
|
81
81
|
readonly _btcRpc: BitcoinRpcWithAddressIndex<any>;
|
|
82
82
|
private readonly btcRelay;
|
|
83
|
+
private readonly versionedBtcRelay;
|
|
84
|
+
private readonly versionedSynchronizer;
|
|
83
85
|
/**
|
|
84
86
|
* @param chainIdentifier
|
|
85
87
|
* @param unifiedStorage Storage interface for the current environment
|
|
86
88
|
* @param unifiedChainEvents On-chain event listener
|
|
87
89
|
* @param chain
|
|
88
|
-
* @param contract Underlying contract handling the swaps
|
|
89
90
|
* @param prices Pricing to use
|
|
90
91
|
* @param tokens
|
|
91
|
-
* @param
|
|
92
|
-
* @param
|
|
93
|
-
* @param synchronizer Btc relay synchronizer
|
|
92
|
+
* @param versionedContracts
|
|
93
|
+
* @param versionedSynchronizer
|
|
94
94
|
* @param btcRpc Bitcoin RPC which also supports getting transactions by txoHash
|
|
95
95
|
* @param options
|
|
96
96
|
* @param events Instance to use for emitting events
|
|
97
97
|
*/
|
|
98
|
-
constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"],
|
|
98
|
+
constructor(chainIdentifier: string, unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], prices: ISwapPrice, tokens: WrapperCtorTokens, versionedContracts: {
|
|
99
|
+
[version: string]: {
|
|
100
|
+
swapContract: T["Contract"];
|
|
101
|
+
swapDataConstructor: new (data: any) => T["Data"];
|
|
102
|
+
btcRelay: BtcRelay<any, T["TX"], any>;
|
|
103
|
+
};
|
|
104
|
+
}, versionedSynchronizer: {
|
|
105
|
+
[version: string]: {
|
|
106
|
+
synchronizer: RelaySynchronizer<any, T["TX"], any>;
|
|
107
|
+
};
|
|
108
|
+
}, btcRpc: BitcoinRpcWithAddressIndex<any>, options?: AllOptional<FromBTCWrapperOptions>, events?: EventEmitter<{
|
|
99
109
|
swapState: [ISwap];
|
|
100
110
|
}>);
|
|
101
111
|
/**
|
|
@@ -130,6 +140,7 @@ export declare class FromBTCWrapper<T extends ChainType> extends IFromBTCWrapper
|
|
|
130
140
|
* @param amountData
|
|
131
141
|
* @param options Options as passed to the swap creation function
|
|
132
142
|
* @param abortController
|
|
143
|
+
* @param contractVersion
|
|
133
144
|
*
|
|
134
145
|
* @private
|
|
135
146
|
*/
|
|
@@ -187,5 +198,5 @@ export declare class FromBTCWrapper<T extends ChainType> extends IFromBTCWrapper
|
|
|
187
198
|
blockTime: number;
|
|
188
199
|
blockHeight: number;
|
|
189
200
|
}>;
|
|
190
|
-
}, state: SwapCommitState, lp?: Intermediary): Promise<FromBTCSwap<T> | null>;
|
|
201
|
+
}, state: SwapCommitState, contractVersion: string, lp?: Intermediary): Promise<FromBTCSwap<T> | null>;
|
|
191
202
|
}
|