@atomiqlabs/sdk 8.6.3 → 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/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 +21 -3
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +1 -0
- package/dist/swapper/Swapper.d.ts +9 -4
- package/dist/swapper/Swapper.js +89 -41
- 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/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/intermediaries/Intermediary.ts +31 -1
- package/src/intermediaries/IntermediaryDiscovery.ts +27 -8
- package/src/intermediaries/apis/IntermediaryAPI.ts +2 -1
- package/src/swapper/Swapper.ts +133 -61
- 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/Utils.ts +14 -0
|
@@ -18,7 +18,7 @@ import {Buffer} from "buffer";
|
|
|
18
18
|
import {IntermediaryError} from "../../../../errors/IntermediaryError";
|
|
19
19
|
import {SwapType} from "../../../../enums/SwapType";
|
|
20
20
|
import {
|
|
21
|
-
extendAbortController,
|
|
21
|
+
extendAbortController, mapArrayToObject,
|
|
22
22
|
randomBytes,
|
|
23
23
|
throwIfUndefined
|
|
24
24
|
} from "../../../../utils/Utils";
|
|
@@ -109,25 +109,46 @@ export class FromBTCWrapper<
|
|
|
109
109
|
/**
|
|
110
110
|
* @internal
|
|
111
111
|
*/
|
|
112
|
-
readonly _synchronizer: RelaySynchronizer<any, T["TX"], any
|
|
112
|
+
readonly _synchronizer: (version?: string) => RelaySynchronizer<any, T["TX"], any> = (version?: string) => {
|
|
113
|
+
const _version = version ?? "v1";
|
|
114
|
+
const data = this.versionedSynchronizer[_version];
|
|
115
|
+
if(data==null) throw new Error(`Invalid contract version ${_version} requested`);
|
|
116
|
+
return data.synchronizer;
|
|
117
|
+
};
|
|
118
|
+
|
|
113
119
|
/**
|
|
114
120
|
* @internal
|
|
115
121
|
*/
|
|
116
122
|
readonly _btcRpc: BitcoinRpcWithAddressIndex<any>;
|
|
117
123
|
|
|
118
|
-
private readonly btcRelay: BtcRelay<any, T["TX"], any
|
|
124
|
+
private readonly btcRelay: (version?: string) => BtcRelay<any, T["TX"], any> = (version?: string) => {
|
|
125
|
+
const _version = version ?? "v1";
|
|
126
|
+
const data = this.versionedBtcRelay[_version];
|
|
127
|
+
if(data==null) throw new Error(`Invalid contract version ${_version} requested`);
|
|
128
|
+
return data.btcRelay;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
private readonly versionedBtcRelay: {
|
|
132
|
+
[version: string]: {
|
|
133
|
+
btcRelay: BtcRelay<any, T["TX"], any>
|
|
134
|
+
}
|
|
135
|
+
}= {};
|
|
136
|
+
|
|
137
|
+
private readonly versionedSynchronizer: {
|
|
138
|
+
[version: string]: {
|
|
139
|
+
synchronizer: RelaySynchronizer<any, T["TX"], any>
|
|
140
|
+
}
|
|
141
|
+
}= {};
|
|
119
142
|
|
|
120
143
|
/**
|
|
121
144
|
* @param chainIdentifier
|
|
122
145
|
* @param unifiedStorage Storage interface for the current environment
|
|
123
146
|
* @param unifiedChainEvents On-chain event listener
|
|
124
147
|
* @param chain
|
|
125
|
-
* @param contract Underlying contract handling the swaps
|
|
126
148
|
* @param prices Pricing to use
|
|
127
149
|
* @param tokens
|
|
128
|
-
* @param
|
|
129
|
-
* @param
|
|
130
|
-
* @param synchronizer Btc relay synchronizer
|
|
150
|
+
* @param versionedContracts
|
|
151
|
+
* @param versionedSynchronizer
|
|
131
152
|
* @param btcRpc Bitcoin RPC which also supports getting transactions by txoHash
|
|
132
153
|
* @param options
|
|
133
154
|
* @param events Instance to use for emitting events
|
|
@@ -137,18 +158,26 @@ export class FromBTCWrapper<
|
|
|
137
158
|
unifiedStorage: UnifiedSwapStorage<T>,
|
|
138
159
|
unifiedChainEvents: UnifiedSwapEventListener<T>,
|
|
139
160
|
chain: T["ChainInterface"],
|
|
140
|
-
contract: T["Contract"],
|
|
141
161
|
prices: ISwapPrice,
|
|
142
162
|
tokens: WrapperCtorTokens,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
163
|
+
versionedContracts: {
|
|
164
|
+
[version: string]: {
|
|
165
|
+
swapContract: T["Contract"],
|
|
166
|
+
swapDataConstructor: new (data: any) => T["Data"],
|
|
167
|
+
btcRelay: BtcRelay<any, T["TX"], any>
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
versionedSynchronizer: {
|
|
171
|
+
[version: string]: {
|
|
172
|
+
synchronizer: RelaySynchronizer<any, T["TX"], any>
|
|
173
|
+
}
|
|
174
|
+
},
|
|
146
175
|
btcRpc: BitcoinRpcWithAddressIndex<any>,
|
|
147
176
|
options?: AllOptional<FromBTCWrapperOptions>,
|
|
148
177
|
events?: EventEmitter<{swapState: [ISwap]}>
|
|
149
178
|
) {
|
|
150
179
|
super(
|
|
151
|
-
chainIdentifier, unifiedStorage, unifiedChainEvents, chain,
|
|
180
|
+
chainIdentifier, unifiedStorage, unifiedChainEvents, chain, prices, tokens,
|
|
152
181
|
{
|
|
153
182
|
...options,
|
|
154
183
|
bitcoinNetwork: options?.bitcoinNetwork ?? TEST_NETWORK,
|
|
@@ -158,11 +187,12 @@ export class FromBTCWrapper<
|
|
|
158
187
|
minSendWindow: options?.minSendWindow ?? 30*60, //Minimum time window for user to send in the on-chain funds for From BTC swap
|
|
159
188
|
bitcoinBlocktime: options?.bitcoinBlocktime ?? 10*60
|
|
160
189
|
},
|
|
190
|
+
versionedContracts,
|
|
161
191
|
events
|
|
162
192
|
);
|
|
163
|
-
this.btcRelay = btcRelay;
|
|
164
|
-
this._synchronizer = synchronizer;
|
|
165
193
|
this._btcRpc = btcRpc;
|
|
194
|
+
this.versionedBtcRelay = versionedContracts;
|
|
195
|
+
this.versionedSynchronizer = versionedSynchronizer;
|
|
166
196
|
}
|
|
167
197
|
|
|
168
198
|
/**
|
|
@@ -225,6 +255,7 @@ export class FromBTCWrapper<
|
|
|
225
255
|
* @param amountData
|
|
226
256
|
* @param options Options as passed to the swap creation function
|
|
227
257
|
* @param abortController
|
|
258
|
+
* @param contractVersion
|
|
228
259
|
*
|
|
229
260
|
* @private
|
|
230
261
|
*/
|
|
@@ -236,7 +267,8 @@ export class FromBTCWrapper<
|
|
|
236
267
|
blockSafetyFactor: bigint,
|
|
237
268
|
unsafeZeroWatchtowerFee: boolean
|
|
238
269
|
},
|
|
239
|
-
abortController: AbortController
|
|
270
|
+
abortController: AbortController,
|
|
271
|
+
contractVersion: string
|
|
240
272
|
): Promise<{
|
|
241
273
|
feePerBlock: bigint,
|
|
242
274
|
safetyFactor: bigint,
|
|
@@ -257,19 +289,19 @@ export class FromBTCWrapper<
|
|
|
257
289
|
}
|
|
258
290
|
|
|
259
291
|
const dummyAmount = BigInt(Math.floor(Math.random()* 0x1000000));
|
|
260
|
-
const dummySwapData = await this._contract.createSwapData(
|
|
292
|
+
const dummySwapData = await this._contract(contractVersion).createSwapData(
|
|
261
293
|
ChainSwapType.CHAIN, signer, signer, amountData.token,
|
|
262
|
-
dummyAmount, this._contract.getHashForOnchain(randomBytes(20), dummyAmount, 3).toString("hex"),
|
|
294
|
+
dummyAmount, this._contract(contractVersion).getHashForOnchain(randomBytes(20), dummyAmount, 3).toString("hex"),
|
|
263
295
|
this.getRandomSequence(), startTimestamp, false, true,
|
|
264
296
|
BigInt(Math.floor(Math.random() * 0x10000)), BigInt(Math.floor(Math.random() * 0x10000))
|
|
265
297
|
);
|
|
266
298
|
|
|
267
299
|
try {
|
|
268
300
|
const [feePerBlock, btcRelayData, currentBtcBlock, claimFeeRate] = await Promise.all([
|
|
269
|
-
this.btcRelay.getFeePerBlock(),
|
|
270
|
-
this.btcRelay.getTipData(),
|
|
301
|
+
this.btcRelay(contractVersion).getFeePerBlock(),
|
|
302
|
+
this.btcRelay(contractVersion).getTipData(),
|
|
271
303
|
this._btcRpc.getTipHeight(),
|
|
272
|
-
this._contract.getClaimFee(signer, dummySwapData)
|
|
304
|
+
this._contract(contractVersion).getClaimFee(signer, dummySwapData)
|
|
273
305
|
]);
|
|
274
306
|
|
|
275
307
|
if(btcRelayData==null) throw new Error("Btc relay not initialized!");
|
|
@@ -386,9 +418,11 @@ export class FromBTCWrapper<
|
|
|
386
418
|
throw new IntermediaryError("Send window too low");
|
|
387
419
|
}
|
|
388
420
|
|
|
421
|
+
const version = lp.getContractVersion(this.chainIdentifier);
|
|
422
|
+
|
|
389
423
|
const lockingScript = toOutputScript(this._options.bitcoinNetwork, resp.btcAddress);
|
|
390
|
-
const desiredExtraData = this._contract.getExtraData(lockingScript, resp.amount, requiredConfirmations);
|
|
391
|
-
const desiredClaimHash = this._contract.getHashForOnchain(lockingScript, resp.amount, requiredConfirmations);
|
|
424
|
+
const desiredExtraData = this._contract(version).getExtraData(lockingScript, resp.amount, requiredConfirmations);
|
|
425
|
+
const desiredClaimHash = this._contract(version).getHashForOnchain(lockingScript, resp.amount, requiredConfirmations);
|
|
392
426
|
if(!desiredClaimHash.equals(Buffer.from(data.getClaimHash(), "hex"))) {
|
|
393
427
|
throw new IntermediaryError("Invalid claim hash returned!");
|
|
394
428
|
}
|
|
@@ -426,6 +460,7 @@ export class FromBTCWrapper<
|
|
|
426
460
|
} else if(typeof(options?.feeSafetyFactor)==="number") {
|
|
427
461
|
feeSafetyFactorPPM = BigInt(Math.floor(options.feeSafetyFactor * 1_000_000));
|
|
428
462
|
}
|
|
463
|
+
const lpVersions = Intermediary.getContractVersionsForLps(this.chainIdentifier, lps);
|
|
429
464
|
|
|
430
465
|
const _options = {
|
|
431
466
|
blockSafetyFactor: options?.blockSafetyFactor!=null ? BigInt(options.blockSafetyFactor) : 1n,
|
|
@@ -438,21 +473,27 @@ export class FromBTCWrapper<
|
|
|
438
473
|
const _abortController = extendAbortController(abortSignal);
|
|
439
474
|
const pricePrefetchPromise: Promise<bigint | undefined> = this.preFetchPrice(amountData, _abortController.signal);
|
|
440
475
|
const usdPricePrefetchPromise: Promise<number | undefined> = this.preFetchUsdPrice(_abortController.signal);
|
|
441
|
-
const claimerBountyPrefetchPromise =
|
|
476
|
+
const claimerBountyPrefetchPromise = mapArrayToObject(lpVersions, (contractVersion: string) => {
|
|
477
|
+
return this.preFetchClaimerBounty(recipient, amountData, _options, _abortController, contractVersion);
|
|
478
|
+
});
|
|
442
479
|
const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
|
|
443
|
-
const feeRatePromise
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
480
|
+
const feeRatePromise = this.preFetchFeeRate(recipient, amountData, undefined, _abortController, lpVersions);
|
|
481
|
+
|
|
482
|
+
const _signDataPromise = mapArrayToObject(lpVersions, (contractVersion: string) => {
|
|
483
|
+
return this._contract(contractVersion).preFetchBlockDataForSignatures == null ?
|
|
484
|
+
this.preFetchSignData(Promise.resolve(true), contractVersion) :
|
|
485
|
+
undefined;
|
|
486
|
+
});
|
|
447
487
|
|
|
448
488
|
return lps.map(lp => {
|
|
449
489
|
return {
|
|
450
490
|
intermediary: lp,
|
|
451
491
|
quote: (async () => {
|
|
452
492
|
if(lp.services[SwapType.FROM_BTC]==null) throw new Error("LP service for processing from btc swaps not found!");
|
|
493
|
+
const version = lp.getContractVersion(this.chainIdentifier);
|
|
453
494
|
|
|
454
495
|
const abortController = extendAbortController(_abortController.signal);
|
|
455
|
-
const liquidityPromise: Promise<bigint | undefined> = this.preFetchIntermediaryLiquidity(amountData, lp, abortController);
|
|
496
|
+
const liquidityPromise: Promise<bigint | undefined> = this.preFetchIntermediaryLiquidity(amountData, lp, abortController, version);
|
|
456
497
|
|
|
457
498
|
try {
|
|
458
499
|
const {signDataPromise, resp} = await tryWithRetries(async(retryCount: number) => {
|
|
@@ -466,16 +507,16 @@ export class FromBTCWrapper<
|
|
|
466
507
|
exactOut: !amountData.exactIn,
|
|
467
508
|
sequence,
|
|
468
509
|
|
|
469
|
-
claimerBounty: throwIfUndefined(claimerBountyPrefetchPromise),
|
|
470
|
-
feeRate: throwIfUndefined(feeRatePromise),
|
|
510
|
+
claimerBounty: throwIfUndefined(claimerBountyPrefetchPromise[version]),
|
|
511
|
+
feeRate: throwIfUndefined(feeRatePromise[version]),
|
|
471
512
|
additionalParams
|
|
472
513
|
},
|
|
473
514
|
this._options.postRequestTimeout, abortController.signal, retryCount>0 ? false : undefined
|
|
474
515
|
);
|
|
475
516
|
|
|
476
|
-
let signDataPromise = _signDataPromise;
|
|
517
|
+
let signDataPromise = _signDataPromise[version];
|
|
477
518
|
if(signDataPromise==null) {
|
|
478
|
-
signDataPromise = this.preFetchSignData(signDataPrefetch);
|
|
519
|
+
signDataPromise = this.preFetchSignData(signDataPrefetch, version);
|
|
479
520
|
} else signDataPrefetch.catch(() => {});
|
|
480
521
|
|
|
481
522
|
return {
|
|
@@ -484,17 +525,17 @@ export class FromBTCWrapper<
|
|
|
484
525
|
};
|
|
485
526
|
}, undefined, e => e instanceof RequestError, abortController.signal);
|
|
486
527
|
|
|
487
|
-
const data: T["Data"] = new this._swapDataDeserializer(resp.data);
|
|
528
|
+
const data: T["Data"] = new (this._swapDataDeserializer(version))(resp.data);
|
|
488
529
|
data.setClaimer(recipient);
|
|
489
530
|
|
|
490
|
-
this.verifyReturnedData(recipient, resp, amountData, lp, _options, data, sequence, (await claimerBountyPrefetchPromise)!, nativeTokenAddress);
|
|
531
|
+
this.verifyReturnedData(recipient, resp, amountData, lp, _options, data, sequence, (await claimerBountyPrefetchPromise[version])!, nativeTokenAddress);
|
|
491
532
|
const [pricingInfo, signatureExpiry] = await Promise.all([
|
|
492
533
|
//Get intermediary's liquidity
|
|
493
534
|
this.verifyReturnedPrice(
|
|
494
535
|
lp.services[SwapType.FROM_BTC], false, resp.amount, resp.total,
|
|
495
536
|
amountData.token, {}, pricePrefetchPromise, usdPricePrefetchPromise, abortController.signal
|
|
496
537
|
),
|
|
497
|
-
this.verifyReturnedSignature(recipient, data, resp, feeRatePromise, signDataPromise, abortController.signal),
|
|
538
|
+
this.verifyReturnedSignature(recipient, data, resp, feeRatePromise[version], signDataPromise, version, abortController.signal),
|
|
498
539
|
this.verifyIntermediaryLiquidity(data.getAmount(), throwIfUndefined(liquidityPromise)),
|
|
499
540
|
]);
|
|
500
541
|
|
|
@@ -504,13 +545,14 @@ export class FromBTCWrapper<
|
|
|
504
545
|
expiry: signatureExpiry,
|
|
505
546
|
swapFee: resp.swapFee,
|
|
506
547
|
swapFeeBtc: resp.swapFee * resp.amount / (data.getAmount() - resp.swapFee),
|
|
507
|
-
feeRate: (await feeRatePromise)!,
|
|
548
|
+
feeRate: (await feeRatePromise[version])!,
|
|
508
549
|
signatureData: resp,
|
|
509
550
|
data,
|
|
510
551
|
address: resp.btcAddress,
|
|
511
552
|
amount: resp.amount,
|
|
512
553
|
exactIn: amountData.exactIn ?? true,
|
|
513
|
-
requiredConfirmations: resp.confirmations
|
|
554
|
+
requiredConfirmations: resp.confirmations,
|
|
555
|
+
contractVersion: version
|
|
514
556
|
} as FromBTCSwapInit<T["Data"]>);
|
|
515
557
|
return quote;
|
|
516
558
|
} catch (e) {
|
|
@@ -528,6 +570,7 @@ export class FromBTCWrapper<
|
|
|
528
570
|
async recoverFromSwapDataAndState(
|
|
529
571
|
init: {data: T["Data"], getInitTxId: () => Promise<string>, getTxBlock: () => Promise<{blockTime: number, blockHeight: number}>},
|
|
530
572
|
state: SwapCommitState,
|
|
573
|
+
contractVersion: string,
|
|
531
574
|
lp?: Intermediary
|
|
532
575
|
): Promise<FromBTCSwap<T> | null> {
|
|
533
576
|
const data = init.data;
|
|
@@ -548,7 +591,8 @@ export class FromBTCWrapper<
|
|
|
548
591
|
feeRate: "",
|
|
549
592
|
signatureData: undefined,
|
|
550
593
|
data,
|
|
551
|
-
exactIn: false
|
|
594
|
+
exactIn: false,
|
|
595
|
+
contractVersion
|
|
552
596
|
}
|
|
553
597
|
const swap = new FromBTCSwap(this, swapInit);
|
|
554
598
|
swap._commitTxId = await init.getInitTxId();
|
|
@@ -449,7 +449,7 @@ export abstract class IToBTCSwap<
|
|
|
449
449
|
required: TokenAmount<SCToken<T["ChainId"]>, true>
|
|
450
450
|
}> {
|
|
451
451
|
const [balance, commitFee] = await Promise.all([
|
|
452
|
-
this.
|
|
452
|
+
this._contract.getBalance(this._getInitiator(), this._data.getToken(), false),
|
|
453
453
|
this._data.getToken()===this.wrapper._chain.getNativeCurrencyAddress() ? this.getCommitFee() : Promise.resolve(null)
|
|
454
454
|
]);
|
|
455
455
|
let required = this._data.getAmount();
|
|
@@ -471,7 +471,7 @@ export abstract class IToBTCSwap<
|
|
|
471
471
|
required: TokenAmount<SCToken<T["ChainId"]>, true>
|
|
472
472
|
}> {
|
|
473
473
|
const [balance, commitFee] = await Promise.all([
|
|
474
|
-
this.
|
|
474
|
+
this._contract.getBalance(this._getInitiator(), this.wrapper._chain.getNativeCurrencyAddress(), false),
|
|
475
475
|
this.getCommitFee()
|
|
476
476
|
]);
|
|
477
477
|
return {
|
|
@@ -601,7 +601,7 @@ export abstract class IToBTCSwap<
|
|
|
601
601
|
await this._saveAndEmit();
|
|
602
602
|
}
|
|
603
603
|
|
|
604
|
-
return await this.
|
|
604
|
+
return await this._contract.txsInit(
|
|
605
605
|
this._getInitiator(), this._data, this.signatureData, skipChecks, this.feeRate
|
|
606
606
|
).catch(e => Promise.reject(e instanceof SignatureVerificationError ? new Error("Request timed out") : e));
|
|
607
607
|
}
|
|
@@ -732,7 +732,7 @@ export abstract class IToBTCSwap<
|
|
|
732
732
|
}
|
|
733
733
|
return processed;
|
|
734
734
|
case RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
735
|
-
await this.
|
|
735
|
+
await this._contract.isValidRefundAuthorization(this._data, resp.data);
|
|
736
736
|
this._state = ToBTCSwapState.REFUNDABLE;
|
|
737
737
|
if(save) await this._saveAndEmit();
|
|
738
738
|
return true;
|
|
@@ -799,14 +799,14 @@ export abstract class IToBTCSwap<
|
|
|
799
799
|
return true;
|
|
800
800
|
case RefundAuthorizationResponseCodes.REFUND_DATA:
|
|
801
801
|
const resultData = result.data;
|
|
802
|
-
await this.
|
|
802
|
+
await this._contract.isValidRefundAuthorization(
|
|
803
803
|
this._data,
|
|
804
804
|
resultData
|
|
805
805
|
);
|
|
806
806
|
await this._saveAndEmit(ToBTCSwapState.REFUNDABLE);
|
|
807
807
|
return false;
|
|
808
808
|
case RefundAuthorizationResponseCodes.EXPIRED:
|
|
809
|
-
if(await this.
|
|
809
|
+
if(await this._contract.isExpired(this._getInitiator(), this._data)) throw new Error("Swap expired");
|
|
810
810
|
throw new IntermediaryError("Swap expired");
|
|
811
811
|
case RefundAuthorizationResponseCodes.NOT_FOUND:
|
|
812
812
|
if((this._state as ToBTCSwapState)===ToBTCSwapState.CLAIMED) return true;
|
|
@@ -824,7 +824,7 @@ export abstract class IToBTCSwap<
|
|
|
824
824
|
* Get the estimated smart chain transaction fee of the refund transaction
|
|
825
825
|
*/
|
|
826
826
|
async getRefundNetworkFee(): Promise<TokenAmount<SCToken<T["ChainId"]>, true>> {
|
|
827
|
-
const swapContract: T["Contract"] = this.
|
|
827
|
+
const swapContract: T["Contract"] = this._contract;
|
|
828
828
|
return toTokenAmount(
|
|
829
829
|
await swapContract.getRefundFee(this._getInitiator(), this._data),
|
|
830
830
|
this.wrapper._getNativeToken(),
|
|
@@ -855,13 +855,13 @@ export abstract class IToBTCSwap<
|
|
|
855
855
|
signer = this._getInitiator();
|
|
856
856
|
}
|
|
857
857
|
|
|
858
|
-
if(await this.
|
|
859
|
-
return await this.
|
|
858
|
+
if(await this._contract.isExpired(this._getInitiator(), this._data)) {
|
|
859
|
+
return await this._contract.txsRefund(signer, this._data, true, true);
|
|
860
860
|
} else {
|
|
861
861
|
if(this.url==null) throw new Error("LP URL not known, cannot get cooperative refund message, wait till expiry to refund!");
|
|
862
862
|
const res = await IntermediaryAPI.getRefundAuthorization(this.url, this.getLpIdentifier(), this._data.getSequence());
|
|
863
863
|
if(res.code===RefundAuthorizationResponseCodes.REFUND_DATA) {
|
|
864
|
-
return await this.
|
|
864
|
+
return await this._contract.txsRefundWithAuthorization(
|
|
865
865
|
signer,
|
|
866
866
|
this._data,
|
|
867
867
|
res.data,
|
|
@@ -974,7 +974,7 @@ export abstract class IToBTCSwap<
|
|
|
974
974
|
quoteExpired = quoteDefinitelyExpired ?? await this._verifyQuoteDefinitelyExpired();
|
|
975
975
|
}
|
|
976
976
|
|
|
977
|
-
commitStatus ??= await this.
|
|
977
|
+
commitStatus ??= await this._contract.getCommitStatus(this._getInitiator(), this._data);
|
|
978
978
|
if(commitStatus!=null && await this._forciblySetOnchainState(commitStatus)) return true;
|
|
979
979
|
|
|
980
980
|
if((this._state===ToBTCSwapState.CREATED || this._state===ToBTCSwapState.QUOTE_SOFT_EXPIRED)) {
|
|
@@ -1083,7 +1083,7 @@ export abstract class IToBTCSwap<
|
|
|
1083
1083
|
break;
|
|
1084
1084
|
case ToBTCSwapState.COMMITED:
|
|
1085
1085
|
case ToBTCSwapState.SOFT_CLAIMED:
|
|
1086
|
-
const expired = await this.
|
|
1086
|
+
const expired = await this._contract.isExpired(this._getInitiator(), this._data);
|
|
1087
1087
|
if(expired) {
|
|
1088
1088
|
this._state = ToBTCSwapState.REFUNDABLE;
|
|
1089
1089
|
if(save) await this._saveAndEmit();
|
|
@@ -5,6 +5,7 @@ import {Intermediary, SingleChainReputationType} from "../../../intermediaries/I
|
|
|
5
5
|
import {IntermediaryError} from "../../../errors/IntermediaryError";
|
|
6
6
|
import {IEscrowSwapWrapper} from "../IEscrowSwapWrapper";
|
|
7
7
|
import {AmountData} from "../../../types/AmountData";
|
|
8
|
+
import {mapArrayToObject} from "../../../utils/Utils";
|
|
8
9
|
|
|
9
10
|
export type IToBTCDefinition<T extends ChainType, W extends IToBTCWrapper<T, any>, S extends IToBTCSwap<T>> = SwapTypeDefinition<T, W, S>;
|
|
10
11
|
|
|
@@ -44,6 +45,7 @@ export abstract class IToBTCWrapper<
|
|
|
44
45
|
* @param amountData
|
|
45
46
|
* @param lp Intermediary
|
|
46
47
|
* @param abortController
|
|
48
|
+
* @param contractVersion
|
|
47
49
|
* @returns Intermediary's reputation or null if failed
|
|
48
50
|
* @throws {IntermediaryError} If the intermediary vault doesn't exist
|
|
49
51
|
*
|
|
@@ -52,9 +54,10 @@ export abstract class IToBTCWrapper<
|
|
|
52
54
|
protected preFetchIntermediaryReputation(
|
|
53
55
|
amountData: Omit<AmountData, "amount">,
|
|
54
56
|
lp: Intermediary,
|
|
55
|
-
abortController: AbortController
|
|
57
|
+
abortController: AbortController,
|
|
58
|
+
contractVersion: string
|
|
56
59
|
): Promise<SingleChainReputationType | undefined> {
|
|
57
|
-
return lp.getReputation(this.chainIdentifier, this._contract, [amountData.token.toString()], abortController.signal).then(res => {
|
|
60
|
+
return lp.getReputation(this.chainIdentifier, this._contract(contractVersion), [amountData.token.toString()], abortController.signal).then(res => {
|
|
58
61
|
if(res==null) throw new IntermediaryError("Invalid data returned - invalid LP vault");
|
|
59
62
|
return res;
|
|
60
63
|
}).catch(e => {
|
|
@@ -71,17 +74,26 @@ export abstract class IToBTCWrapper<
|
|
|
71
74
|
* @param amountData
|
|
72
75
|
* @param claimHash optional hash of the swap or null
|
|
73
76
|
* @param abortController
|
|
77
|
+
* @param contractVersions
|
|
74
78
|
* @returns Fee rate
|
|
75
79
|
*
|
|
76
80
|
* @internal
|
|
77
81
|
*/
|
|
78
|
-
protected preFetchFeeRate(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
protected preFetchFeeRate(
|
|
83
|
+
signer: string,
|
|
84
|
+
amountData: Omit<AmountData, "amount">,
|
|
85
|
+
claimHash: {[contractVersion: string]: string} | undefined,
|
|
86
|
+
abortController: AbortController,
|
|
87
|
+
contractVersions: string[]
|
|
88
|
+
): {[contractVersion: string]: Promise<string | undefined>} {
|
|
89
|
+
return mapArrayToObject(contractVersions, (contractVersion) => {
|
|
90
|
+
return this._contract(contractVersion).getInitPayInFeeRate(signer, this._chain.randomAddress(), amountData.token, claimHash?.[contractVersion])
|
|
91
|
+
.catch(e => {
|
|
92
|
+
this.logger.warn("preFetchFeeRate(): Error: ", e);
|
|
93
|
+
abortController.abort(e);
|
|
94
|
+
return undefined;
|
|
95
|
+
});
|
|
96
|
+
});
|
|
85
97
|
}
|
|
86
98
|
|
|
87
99
|
/**
|
|
@@ -104,7 +104,7 @@ export class ToBTCLNSwap<T extends ChainType = ChainType> extends IToBTCSwap<T,
|
|
|
104
104
|
const hash = Buffer.from(sha256(secretBuffer));
|
|
105
105
|
|
|
106
106
|
if(check) {
|
|
107
|
-
const claimHash = this.
|
|
107
|
+
const claimHash = this._contract.getHashForHtlc(hash);
|
|
108
108
|
|
|
109
109
|
const expectedClaimHash = Buffer.from(this.getClaimHash(), "hex");
|
|
110
110
|
if(!claimHash.equals(expectedClaimHash)) throw new IntermediaryError("Invalid payment secret returned");
|