@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
|
@@ -728,7 +728,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
728
728
|
|
|
729
729
|
private isValidSecretPreimage(secret: string) {
|
|
730
730
|
const paymentHash = Buffer.from(sha256(Buffer.from(secret, "hex")));
|
|
731
|
-
const claimHash = this.
|
|
731
|
+
const claimHash = this._contract.getHashForHtlc(paymentHash).toString("hex");
|
|
732
732
|
return this.getSwapData().getClaimHash()===claimHash;
|
|
733
733
|
}
|
|
734
734
|
|
|
@@ -914,7 +914,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
914
914
|
const resp = await IntermediaryAPI.getInvoiceStatus(this.url, paymentHash.toString("hex"));
|
|
915
915
|
switch(resp.code) {
|
|
916
916
|
case InvoiceStatusResponseCodes.PAID:
|
|
917
|
-
const data = new this.wrapper._swapDataDeserializer(resp.data.data);
|
|
917
|
+
const data = new (this.wrapper._swapDataDeserializer(this._contractVersion))(resp.data.data);
|
|
918
918
|
if(this._state===FromBTCLNAutoSwapState.PR_CREATED || this._state===FromBTCLNAutoSwapState.QUOTE_SOFT_EXPIRED) try {
|
|
919
919
|
await this._saveRealSwapData(data, save);
|
|
920
920
|
return true;
|
|
@@ -974,7 +974,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
974
974
|
if (!data.isDepositToken(this.getSwapData().getDepositToken())) throw new IntermediaryError("Invalid deposit token used!");
|
|
975
975
|
if (data.hasSuccessAction()) throw new IntermediaryError("Invalid has success action");
|
|
976
976
|
|
|
977
|
-
if (await this.wrapper._contract.isExpired(this._getInitiator(), data)) throw new IntermediaryError("Not enough time to claim!");
|
|
977
|
+
if (await this.wrapper._contract(this._contractVersion).isExpired(this._getInitiator(), data)) throw new IntermediaryError("Not enough time to claim!");
|
|
978
978
|
if (this.wrapper._getHtlcTimeout(data) <= (Date.now()/1000)) throw new IntermediaryError("HTLC expires too soon!");
|
|
979
979
|
}
|
|
980
980
|
|
|
@@ -1051,7 +1051,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
1051
1051
|
abortController.signal.throwIfAborted();
|
|
1052
1052
|
|
|
1053
1053
|
if(resp.code===InvoiceStatusResponseCodes.PAID) {
|
|
1054
|
-
const swapData = new this.wrapper._swapDataDeserializer(resp.data.data);
|
|
1054
|
+
const swapData = new (this.wrapper._swapDataDeserializer(this._contractVersion))(resp.data.data);
|
|
1055
1055
|
return await this._saveRealSwapData(swapData, true);
|
|
1056
1056
|
}
|
|
1057
1057
|
|
|
@@ -1164,7 +1164,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
1164
1164
|
if(!this.isValidSecretPreimage(useSecret))
|
|
1165
1165
|
throw new Error("Invalid swap secret pre-image provided!");
|
|
1166
1166
|
|
|
1167
|
-
return await this.
|
|
1167
|
+
return await this._contract.txsClaimWithSecret(
|
|
1168
1168
|
address ?? this._getInitiator(),
|
|
1169
1169
|
this._data, useSecret, true, true
|
|
1170
1170
|
);
|
|
@@ -1381,7 +1381,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
1381
1381
|
}
|
|
1382
1382
|
|
|
1383
1383
|
//Check if it's already successfully paid
|
|
1384
|
-
commitStatus ??= await this.
|
|
1384
|
+
commitStatus ??= await this._contract.getCommitStatus(this._getInitiator(), this._data!);
|
|
1385
1385
|
if(commitStatus!=null && await this._forciblySetOnchainState(commitStatus)) return true;
|
|
1386
1386
|
|
|
1387
1387
|
if(this._state===FromBTCLNAutoSwapState.PR_PAID) {
|
|
@@ -1450,7 +1450,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
1450
1450
|
if(await this.syncStateFromChain(quoteDefinitelyExpired, commitStatus)) changed = true;
|
|
1451
1451
|
|
|
1452
1452
|
if(this._state===FromBTCLNAutoSwapState.CLAIM_COMMITED) {
|
|
1453
|
-
const expired = await this.
|
|
1453
|
+
const expired = await this._contract.isExpired(this._getInitiator(), this._data!);
|
|
1454
1454
|
if(expired) {
|
|
1455
1455
|
this._state = FromBTCLNAutoSwapState.EXPIRED;
|
|
1456
1456
|
changed = true;
|
|
@@ -1518,7 +1518,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
1518
1518
|
throw new Error("Invalid swap secret pre-image provided!");
|
|
1519
1519
|
|
|
1520
1520
|
if(!noCheckExpiry) {
|
|
1521
|
-
if(await this.
|
|
1521
|
+
if(await this._contract.isExpired(this._getInitiator(), this._data)) throw new Error("On-chain HTLC already expired!");
|
|
1522
1522
|
}
|
|
1523
1523
|
await this.wrapper._messenger.broadcast(new SwapClaimWitnessMessage(this._data, useSecret));
|
|
1524
1524
|
}
|
|
@@ -1545,7 +1545,7 @@ export class FromBTCLNAutoSwap<T extends ChainType = ChainType>
|
|
|
1545
1545
|
break;
|
|
1546
1546
|
case FromBTCLNAutoSwapState.PR_PAID:
|
|
1547
1547
|
case FromBTCLNAutoSwapState.CLAIM_COMMITED:
|
|
1548
|
-
const expired = await this.
|
|
1548
|
+
const expired = await this._contract.isExpired(this._getInitiator(), this._data!);
|
|
1549
1549
|
if(expired) {
|
|
1550
1550
|
this._state = FromBTCLNAutoSwapState.EXPIRED;
|
|
1551
1551
|
if(save) await this._saveAndEmit();
|
|
@@ -12,7 +12,7 @@ import {UserError} from "../../../../errors/UserError";
|
|
|
12
12
|
import {IntermediaryError} from "../../../../errors/IntermediaryError";
|
|
13
13
|
import {SwapType} from "../../../../enums/SwapType";
|
|
14
14
|
import {
|
|
15
|
-
extendAbortController, parseHashValueExact32Bytes,
|
|
15
|
+
extendAbortController, mapArrayToObject, parseHashValueExact32Bytes,
|
|
16
16
|
randomBytes,
|
|
17
17
|
throwIfUndefined
|
|
18
18
|
} from "../../../../utils/Utils";
|
|
@@ -148,10 +148,9 @@ export class FromBTCLNAutoWrapper<
|
|
|
148
148
|
* @param unifiedStorage Storage interface for the current environment
|
|
149
149
|
* @param unifiedChainEvents On-chain event listener
|
|
150
150
|
* @param chain
|
|
151
|
-
* @param contract Underlying contract handling the swaps
|
|
152
151
|
* @param prices Swap pricing handler
|
|
153
152
|
* @param tokens
|
|
154
|
-
* @param
|
|
153
|
+
* @param versionedContracts
|
|
155
154
|
* @param lnApi
|
|
156
155
|
* @param messenger
|
|
157
156
|
* @param options
|
|
@@ -162,17 +161,21 @@ export class FromBTCLNAutoWrapper<
|
|
|
162
161
|
unifiedStorage: UnifiedSwapStorage<T>,
|
|
163
162
|
unifiedChainEvents: UnifiedSwapEventListener<T>,
|
|
164
163
|
chain: T["ChainInterface"],
|
|
165
|
-
contract: T["Contract"],
|
|
166
164
|
prices: ISwapPrice,
|
|
167
165
|
tokens: WrapperCtorTokens,
|
|
168
|
-
|
|
166
|
+
versionedContracts: {
|
|
167
|
+
[version: string]: {
|
|
168
|
+
swapContract: T["Contract"],
|
|
169
|
+
swapDataConstructor: new (data: any) => T["Data"]
|
|
170
|
+
}
|
|
171
|
+
},
|
|
169
172
|
lnApi: LightningNetworkApi,
|
|
170
173
|
messenger: Messenger,
|
|
171
174
|
options?: AllOptional<FromBTCLNAutoWrapperOptions>,
|
|
172
175
|
events?: EventEmitter<{swapState: [ISwap]}>
|
|
173
176
|
) {
|
|
174
177
|
super(
|
|
175
|
-
chainIdentifier, unifiedStorage, unifiedChainEvents, chain,
|
|
178
|
+
chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens, versionedContracts, lnApi,
|
|
176
179
|
{
|
|
177
180
|
...options,
|
|
178
181
|
safetyFactor: options?.safetyFactor ?? 2,
|
|
@@ -255,30 +258,34 @@ export class FromBTCLNAutoWrapper<
|
|
|
255
258
|
* @param options Options as passed to the swap creation function
|
|
256
259
|
* @param abortController
|
|
257
260
|
*
|
|
261
|
+
* @param contractVersions
|
|
258
262
|
* @private
|
|
259
263
|
*/
|
|
260
|
-
private
|
|
264
|
+
private preFetchClaimerBounty(
|
|
261
265
|
signer: string,
|
|
262
266
|
amountData: AmountData,
|
|
263
267
|
options: {feeSafetyFactor: number, unsafeZeroWatchtowerFee: boolean},
|
|
264
|
-
abortController: AbortController
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
268
|
+
abortController: AbortController,
|
|
269
|
+
contractVersions: string[]
|
|
270
|
+
): {[chainVersion: string]: Promise<bigint | undefined>} {
|
|
271
|
+
return mapArrayToObject(contractVersions, async (contractVersion) => {
|
|
272
|
+
if(options.unsafeZeroWatchtowerFee) return 0n;
|
|
273
|
+
|
|
274
|
+
const dummyAmount = BigInt(Math.floor(Math.random()* 0x1000000));
|
|
275
|
+
const dummySwapData = await this._contract(contractVersion).createSwapData(
|
|
276
|
+
ChainSwapType.HTLC, this._chain.randomAddress(), signer, amountData.token,
|
|
277
|
+
dummyAmount, this._contract(contractVersion).getHashForHtlc(randomBytes(32)).toString("hex"),
|
|
278
|
+
this.getRandomSequence(), BigInt(Math.floor(Date.now()/1000)), false, true,
|
|
279
|
+
BigInt(Math.floor(Math.random() * 0x10000)), BigInt(Math.floor(Math.random() * 0x10000))
|
|
280
|
+
);
|
|
275
281
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
+
try {
|
|
283
|
+
const result = await this._contract(contractVersion).getClaimFee(this._chain.randomAddress(), dummySwapData);
|
|
284
|
+
return result * BigInt(Math.floor(options.feeSafetyFactor*1000000)) / 1_000_000n
|
|
285
|
+
} catch (e) {
|
|
286
|
+
abortController.abort(e);
|
|
287
|
+
}
|
|
288
|
+
});
|
|
282
289
|
}
|
|
283
290
|
|
|
284
291
|
/**
|
|
@@ -354,10 +361,10 @@ export class FromBTCLNAutoWrapper<
|
|
|
354
361
|
additionalParams?: Record<string, any>,
|
|
355
362
|
abortSignal?: AbortSignal,
|
|
356
363
|
preFetches?: {
|
|
357
|
-
pricePrefetchPromise
|
|
358
|
-
usdPricePrefetchPromise
|
|
364
|
+
pricePrefetchPromise: Promise<bigint | undefined>,
|
|
365
|
+
usdPricePrefetchPromise: Promise<number | undefined>,
|
|
366
|
+
claimerBountyPrefetch: {[contractVersion: string]: Promise<bigint | undefined>},
|
|
359
367
|
gasTokenPricePrefetchPromise?: Promise<bigint | undefined>,
|
|
360
|
-
claimerBountyPrefetch?: Promise<bigint | undefined>,
|
|
361
368
|
}
|
|
362
369
|
): {
|
|
363
370
|
quote: Promise<FromBTCLNAutoSwap<T>>,
|
|
@@ -381,6 +388,8 @@ export class FromBTCLNAutoWrapper<
|
|
|
381
388
|
if(_options.description!=null && Buffer.byteLength(_options.description, "utf8") > 500)
|
|
382
389
|
throw new UserError("Invalid description length");
|
|
383
390
|
|
|
391
|
+
const lpVersions = Intermediary.getContractVersionsForLps(this.chainIdentifier, lps);
|
|
392
|
+
|
|
384
393
|
let secret: Buffer | undefined;
|
|
385
394
|
let paymentHash: Buffer;
|
|
386
395
|
if(_options?.paymentHash!=null) {
|
|
@@ -388,19 +397,20 @@ export class FromBTCLNAutoWrapper<
|
|
|
388
397
|
} else {
|
|
389
398
|
({secret, paymentHash} = this.getSecretAndHash());
|
|
390
399
|
}
|
|
391
|
-
const
|
|
400
|
+
const _hash = mapArrayToObject(lpVersions, (contractVersion: string) => {
|
|
401
|
+
return this._contract(contractVersion).getHashForHtlc(paymentHash).toString("hex");
|
|
402
|
+
});
|
|
392
403
|
|
|
393
404
|
const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
|
|
394
405
|
|
|
395
406
|
const _abortController = extendAbortController(abortSignal);
|
|
396
407
|
|
|
397
|
-
preFetches
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
claimerBountyPrefetch: preFetches?.claimerBountyPrefetch ?? this.preFetchClaimerBounty(recipient, amountData, _options, _abortController),
|
|
408
|
+
const _preFetches = preFetches ?? {
|
|
409
|
+
pricePrefetchPromise: this.preFetchPrice(amountData, _abortController.signal),
|
|
410
|
+
usdPricePrefetchPromise: this.preFetchUsdPrice(_abortController.signal),
|
|
411
|
+
claimerBountyPrefetch: this.preFetchClaimerBounty(recipient, amountData, _options, _abortController, lpVersions),
|
|
402
412
|
gasTokenPricePrefetchPromise: _options.gasAmount!==0n || !_options.unsafeZeroWatchtowerFee ?
|
|
403
|
-
|
|
413
|
+
this.preFetchPrice({token: nativeTokenAddress}, _abortController.signal) :
|
|
404
414
|
undefined
|
|
405
415
|
};
|
|
406
416
|
|
|
@@ -409,10 +419,11 @@ export class FromBTCLNAutoWrapper<
|
|
|
409
419
|
intermediary: lp,
|
|
410
420
|
quote: (async () => {
|
|
411
421
|
if(lp.services[SwapType.FROM_BTCLN_AUTO]==null) throw new Error("LP service for processing from btcln auto swaps not found!");
|
|
422
|
+
const version = lp.getContractVersion(this.chainIdentifier);
|
|
412
423
|
|
|
413
424
|
const abortController = extendAbortController(_abortController.signal);
|
|
414
425
|
|
|
415
|
-
const liquidityPromise: Promise<bigint | undefined> = this.preFetchIntermediaryLiquidity(amountData, lp, abortController);
|
|
426
|
+
const liquidityPromise: Promise<bigint | undefined> = this.preFetchIntermediaryLiquidity(amountData, lp, abortController, version);
|
|
416
427
|
|
|
417
428
|
const {lnCapacityPromise, resp} = await tryWithRetries(async(retryCount: number) => {
|
|
418
429
|
const {lnPublicKey, response} = IntermediaryAPI.initFromBTCLNAuto(
|
|
@@ -428,7 +439,7 @@ export class FromBTCLNAutoWrapper<
|
|
|
428
439
|
additionalParams,
|
|
429
440
|
gasToken: this._chain.getNativeCurrencyAddress(),
|
|
430
441
|
gasAmount: _options.gasAmount,
|
|
431
|
-
claimerBounty: throwIfUndefined(_preFetches.claimerBountyPrefetch)
|
|
442
|
+
claimerBounty: throwIfUndefined(_preFetches.claimerBountyPrefetch[version])
|
|
432
443
|
},
|
|
433
444
|
this._options.postRequestTimeout, abortController.signal, retryCount>0 ? false : undefined
|
|
434
445
|
);
|
|
@@ -448,7 +459,7 @@ export class FromBTCLNAutoWrapper<
|
|
|
448
459
|
if(decodedPr.millisatoshis==null) throw new IntermediaryError("Invalid returned swap invoice, no msat amount field");
|
|
449
460
|
if(decodedPr.timeExpireDate==null) throw new IntermediaryError("Invalid returned swap invoice, no expiry date field");
|
|
450
461
|
const amountIn = (BigInt(decodedPr.millisatoshis) + 999n) / 1000n;
|
|
451
|
-
const claimerBounty = (await _preFetches.claimerBountyPrefetch)!;
|
|
462
|
+
const claimerBounty = (await _preFetches.claimerBountyPrefetch[version])!;
|
|
452
463
|
|
|
453
464
|
try {
|
|
454
465
|
this.verifyReturnedData(resp, amountData, lp, _options, decodedPr, paymentHash, claimerBounty);
|
|
@@ -485,15 +496,16 @@ export class FromBTCLNAutoWrapper<
|
|
|
485
496
|
|
|
486
497
|
gasPricingInfo,
|
|
487
498
|
|
|
488
|
-
initialSwapData: await this._contract.createSwapData(
|
|
499
|
+
initialSwapData: await this._contract(version).createSwapData(
|
|
489
500
|
ChainSwapType.HTLC, lp.getAddress(this.chainIdentifier), recipient, amountData.token,
|
|
490
|
-
resp.total,
|
|
501
|
+
resp.total, _hash[version],
|
|
491
502
|
this.getRandomSequence(), BigInt(Math.floor(Date.now()/1000)), false, true,
|
|
492
503
|
_options.gasAmount + resp.claimerBounty, resp.claimerBounty, nativeTokenAddress
|
|
493
504
|
),
|
|
494
505
|
pr: resp.pr,
|
|
495
506
|
secret: secret?.toString("hex"),
|
|
496
|
-
exactIn: amountData.exactIn ?? true
|
|
507
|
+
exactIn: amountData.exactIn ?? true,
|
|
508
|
+
contractVersion: version
|
|
497
509
|
};
|
|
498
510
|
const quote = new FromBTCLNAutoSwap<T>(this, swapInit);
|
|
499
511
|
return quote;
|
|
@@ -546,6 +558,8 @@ export class FromBTCLNAutoWrapper<
|
|
|
546
558
|
descriptionHash: parseHashValueExact32Bytes(options?.descriptionHash, "description hash")
|
|
547
559
|
};
|
|
548
560
|
|
|
561
|
+
const lpVersions = Intermediary.getContractVersionsForLps(this.chainIdentifier, lps);
|
|
562
|
+
|
|
549
563
|
const abortController = extendAbortController(abortSignal);
|
|
550
564
|
const preFetches = {
|
|
551
565
|
pricePrefetchPromise: this.preFetchPrice(amountData, abortController.signal),
|
|
@@ -553,7 +567,7 @@ export class FromBTCLNAutoWrapper<
|
|
|
553
567
|
gasTokenPricePrefetchPromise: _options.gasAmount!==0n || !_options.unsafeZeroWatchtowerFee ?
|
|
554
568
|
this.preFetchPrice({token: this._chain.getNativeCurrencyAddress()}, abortController.signal) :
|
|
555
569
|
undefined,
|
|
556
|
-
claimerBountyPrefetch: this.preFetchClaimerBounty(recipient, amountData, _options, abortController)
|
|
570
|
+
claimerBountyPrefetch: this.preFetchClaimerBounty(recipient, amountData, _options, abortController, lpVersions)
|
|
557
571
|
};
|
|
558
572
|
|
|
559
573
|
try {
|
|
@@ -615,7 +629,7 @@ export class FromBTCLNAutoWrapper<
|
|
|
615
629
|
const changedSwapSet: Set<FromBTCLNAutoSwap<T>> = new Set();
|
|
616
630
|
|
|
617
631
|
const swapExpiredStatus: {[id: string]: boolean} = {};
|
|
618
|
-
const checkStatusSwaps: (FromBTCLNAutoSwap<T> & {_data: T["Data"]})[] =
|
|
632
|
+
const checkStatusSwaps: {[contractVersion: string]: (FromBTCLNAutoSwap<T> & {_data: T["Data"]})[]} = {};
|
|
619
633
|
|
|
620
634
|
await Promise.all(pastSwaps.map(async (pastSwap) => {
|
|
621
635
|
if(pastSwap._shouldCheckIntermediary()) {
|
|
@@ -634,19 +648,27 @@ export class FromBTCLNAutoWrapper<
|
|
|
634
648
|
}
|
|
635
649
|
if(pastSwap._shouldFetchOnchainState()) {
|
|
636
650
|
//Add to swaps for which status should be checked
|
|
637
|
-
if(pastSwap._data!=null) checkStatusSwaps.push(pastSwap as FromBTCLNAutoSwap<T> & {_data: T["Data"]});
|
|
651
|
+
if(pastSwap._data!=null) (checkStatusSwaps[pastSwap._contractVersion ?? "v1"] ??= []).push(pastSwap as FromBTCLNAutoSwap<T> & {_data: T["Data"]});
|
|
638
652
|
}
|
|
639
653
|
}));
|
|
640
654
|
|
|
641
|
-
|
|
655
|
+
for(let version in checkStatusSwaps) {
|
|
656
|
+
if (this._versionedContracts[version] == null) {
|
|
657
|
+
this.logger.warn(`_checkPastSwaps(): No contract was found for ${this.chainIdentifier} version ${version}! Skipping these swaps!`);
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
642
660
|
|
|
643
|
-
|
|
644
|
-
const
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
661
|
+
const _checkStatusSwap = checkStatusSwaps[version];
|
|
662
|
+
const swapStatuses = await this._contract(version).getCommitStatuses(_checkStatusSwap.map(val => ({signer: val._getInitiator(), swapData: val._data})));
|
|
663
|
+
|
|
664
|
+
for(let pastSwap of _checkStatusSwap) {
|
|
665
|
+
const shouldSave = await pastSwap._sync(
|
|
666
|
+
false, swapExpiredStatus[pastSwap.getId()],
|
|
667
|
+
swapStatuses[pastSwap.getEscrowHash()!], true
|
|
668
|
+
);
|
|
669
|
+
if(shouldSave) {
|
|
670
|
+
changedSwapSet.add(pastSwap);
|
|
671
|
+
}
|
|
650
672
|
}
|
|
651
673
|
}
|
|
652
674
|
|
|
@@ -672,6 +694,7 @@ export class FromBTCLNAutoWrapper<
|
|
|
672
694
|
async recoverFromSwapDataAndState(
|
|
673
695
|
init: {data: T["Data"], getInitTxId: () => Promise<string>, getTxBlock: () => Promise<{blockTime: number, blockHeight: number}>},
|
|
674
696
|
state: SwapCommitState,
|
|
697
|
+
contractVersion: string,
|
|
675
698
|
lp?: Intermediary
|
|
676
699
|
): Promise<FromBTCLNAutoSwap<T> | null> {
|
|
677
700
|
const data = init.data;
|
|
@@ -702,7 +725,8 @@ export class FromBTCLNAutoWrapper<
|
|
|
702
725
|
data,
|
|
703
726
|
pr: paymentHash ?? undefined,
|
|
704
727
|
secret,
|
|
705
|
-
exactIn: false
|
|
728
|
+
exactIn: false,
|
|
729
|
+
contractVersion
|
|
706
730
|
}
|
|
707
731
|
const swap = new FromBTCLNAutoSwap(this, swapInit);
|
|
708
732
|
swap._commitTxId = await init.getInitTxId();
|
|
@@ -385,7 +385,7 @@ export class FromBTCSwap<T extends ChainType = ChainType>
|
|
|
385
385
|
private inferRequiredConfirmationsCount(btcTx: Omit<BtcTxWithBlockheight, "hex" | "raw">, vout: number): number | undefined {
|
|
386
386
|
const txOut = btcTx.outs[vout];
|
|
387
387
|
for(let i=1;i<=20;i++) {
|
|
388
|
-
const computedClaimHash = this.
|
|
388
|
+
const computedClaimHash = this._contract.getHashForOnchain(
|
|
389
389
|
Buffer.from(txOut.scriptPubKey.hex, "hex"),
|
|
390
390
|
BigInt(txOut.value),
|
|
391
391
|
i
|
|
@@ -976,13 +976,13 @@ export class FromBTCSwap<T extends ChainType = ChainType>
|
|
|
976
976
|
if(tx.blockhash==null || tx.confirmations==null || tx.blockheight==null || tx.confirmations<this.requiredConfirmations)
|
|
977
977
|
throw new Error("Bitcoin transaction not confirmed yet!");
|
|
978
978
|
|
|
979
|
-
return await this.
|
|
979
|
+
return await this._contract.txsClaimWithTxData(signer ?? this._getInitiator(), this._data, {
|
|
980
980
|
blockhash: tx.blockhash,
|
|
981
981
|
confirmations: tx.confirmations,
|
|
982
982
|
txid: tx.txid,
|
|
983
983
|
hex: tx.hex,
|
|
984
984
|
height: tx.blockheight
|
|
985
|
-
}, this.requiredConfirmations, this.vout, undefined, this.wrapper._synchronizer, true);
|
|
985
|
+
}, this.requiredConfirmations, this.vout, undefined, this.wrapper._synchronizer(this._contractVersion), true);
|
|
986
986
|
}
|
|
987
987
|
|
|
988
988
|
/**
|
|
@@ -1017,7 +1017,7 @@ export class FromBTCSwap<T extends ChainType = ChainType>
|
|
|
1017
1017
|
this.logger.info("claim(): Transaction state is CLAIM_CLAIMED, swap was successfully claimed by the watchtower");
|
|
1018
1018
|
return this._claimTxId!;
|
|
1019
1019
|
}
|
|
1020
|
-
const status = await this.
|
|
1020
|
+
const status = await this._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
1021
1021
|
if(status?.type===SwapCommitStateType.PAID) {
|
|
1022
1022
|
this.logger.info("claim(): Transaction commit status is PAID, swap was successfully claimed by the watchtower");
|
|
1023
1023
|
if(this._claimTxId==null) this._claimTxId = await status.getClaimTxId();
|
|
@@ -1149,7 +1149,7 @@ export class FromBTCSwap<T extends ChainType = ChainType>
|
|
|
1149
1149
|
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired(); //Make sure we check for expiry here, to prevent race conditions
|
|
1150
1150
|
}
|
|
1151
1151
|
|
|
1152
|
-
const status = commitStatus ?? await this.
|
|
1152
|
+
const status = commitStatus ?? await this._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
1153
1153
|
if(status!=null && await this._forciblySetOnchainState(status)) return true;
|
|
1154
1154
|
|
|
1155
1155
|
if(this._state===FromBTCSwapState.PR_CREATED || this._state===FromBTCSwapState.QUOTE_SOFT_EXPIRED) {
|