@atomiqlabs/sdk 8.6.4 → 8.6.6
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/SmartChainAssets.d.ts +181 -181
- package/dist/SmartChainAssets.js +181 -181
- package/dist/bitcoin/coinselect2/accumulative.d.ts +6 -6
- package/dist/bitcoin/coinselect2/accumulative.js +51 -52
- package/dist/bitcoin/coinselect2/blackjack.d.ts +6 -6
- package/dist/bitcoin/coinselect2/blackjack.js +37 -38
- package/dist/bitcoin/coinselect2/index.d.ts +17 -19
- package/dist/bitcoin/coinselect2/index.js +69 -69
- package/dist/bitcoin/coinselect2/utils.d.ts +75 -77
- package/dist/bitcoin/coinselect2/utils.js +123 -123
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +128 -130
- package/dist/bitcoin/wallet/BitcoinWallet.js +322 -322
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +78 -78
- package/dist/bitcoin/wallet/IBitcoinWallet.js +20 -21
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +99 -101
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +176 -176
- package/dist/enums/FeeType.d.ts +15 -15
- package/dist/enums/FeeType.js +19 -19
- package/dist/enums/SwapAmountType.d.ts +15 -15
- package/dist/enums/SwapAmountType.js +19 -19
- package/dist/enums/SwapDirection.d.ts +15 -15
- package/dist/enums/SwapDirection.js +19 -19
- package/dist/enums/SwapSide.d.ts +15 -15
- package/dist/enums/SwapSide.js +19 -19
- package/dist/enums/SwapType.d.ts +75 -75
- package/dist/enums/SwapType.js +79 -79
- package/dist/errors/IntermediaryError.d.ts +13 -13
- package/dist/errors/IntermediaryError.js +27 -27
- package/dist/errors/RequestError.d.ts +32 -32
- package/dist/errors/RequestError.js +54 -54
- package/dist/errors/UserError.d.ts +8 -8
- package/dist/errors/UserError.js +16 -16
- package/dist/events/UnifiedSwapEventListener.d.ts +23 -23
- package/dist/events/UnifiedSwapEventListener.js +130 -130
- package/dist/http/HttpUtils.d.ts +27 -27
- package/dist/http/HttpUtils.js +90 -91
- package/dist/http/paramcoders/IParamReader.d.ts +8 -8
- package/dist/http/paramcoders/IParamReader.js +2 -2
- package/dist/http/paramcoders/ParamDecoder.d.ts +42 -44
- package/dist/http/paramcoders/ParamDecoder.js +137 -137
- package/dist/http/paramcoders/ParamEncoder.d.ts +18 -20
- package/dist/http/paramcoders/ParamEncoder.js +36 -36
- package/dist/http/paramcoders/SchemaVerifier.d.ts +26 -26
- package/dist/http/paramcoders/SchemaVerifier.js +145 -145
- package/dist/http/paramcoders/client/ResponseParamDecoder.d.ts +11 -11
- package/dist/http/paramcoders/client/ResponseParamDecoder.js +57 -57
- package/dist/http/paramcoders/client/StreamParamEncoder.d.ts +11 -13
- package/dist/http/paramcoders/client/StreamParamEncoder.js +26 -26
- package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +16 -16
- package/dist/http/paramcoders/client/StreamingFetchPromise.js +173 -174
- package/dist/index.d.ts +85 -85
- package/dist/index.js +158 -158
- package/dist/intermediaries/Intermediary.d.ts +157 -157
- package/dist/intermediaries/Intermediary.js +142 -142
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +198 -199
- package/dist/intermediaries/IntermediaryDiscovery.js +406 -406
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +437 -438
- package/dist/intermediaries/apis/IntermediaryAPI.js +603 -602
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.d.ts +155 -155
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.js +137 -137
- package/dist/lnurl/LNURL.d.ts +102 -102
- package/dist/lnurl/LNURL.js +321 -321
- package/dist/prices/RedundantSwapPrice.d.ts +110 -110
- package/dist/prices/RedundantSwapPrice.js +222 -222
- package/dist/prices/SingleSwapPrice.d.ts +34 -34
- package/dist/prices/SingleSwapPrice.js +44 -44
- package/dist/prices/SwapPriceWithChain.d.ts +107 -107
- package/dist/prices/SwapPriceWithChain.js +128 -128
- package/dist/prices/abstract/ICachedSwapPrice.d.ts +28 -28
- package/dist/prices/abstract/ICachedSwapPrice.js +62 -62
- package/dist/prices/abstract/IPriceProvider.d.ts +81 -81
- package/dist/prices/abstract/IPriceProvider.js +74 -74
- package/dist/prices/abstract/ISwapPrice.d.ts +168 -166
- package/dist/prices/abstract/ISwapPrice.js +279 -267
- package/dist/prices/providers/BinancePriceProvider.d.ts +23 -23
- package/dist/prices/providers/BinancePriceProvider.js +30 -30
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +23 -23
- package/dist/prices/providers/CoinGeckoPriceProvider.js +29 -29
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +25 -25
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +29 -29
- package/dist/prices/providers/CustomPriceProvider.d.ts +24 -24
- package/dist/prices/providers/CustomPriceProvider.js +35 -35
- package/dist/prices/providers/KrakenPriceProvider.d.ts +38 -38
- package/dist/prices/providers/KrakenPriceProvider.js +45 -45
- package/dist/prices/providers/OKXPriceProvider.d.ts +34 -34
- package/dist/prices/providers/OKXPriceProvider.js +29 -29
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +17 -17
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +21 -21
- package/dist/prices/providers/abstract/HttpPriceProvider.d.ts +7 -7
- package/dist/prices/providers/abstract/HttpPriceProvider.js +12 -12
- package/dist/storage/IUnifiedStorage.d.ts +85 -85
- package/dist/storage/IUnifiedStorage.js +2 -2
- package/dist/storage/UnifiedSwapStorage.d.ts +114 -114
- package/dist/storage/UnifiedSwapStorage.js +116 -116
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +63 -63
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +298 -298
- package/dist/storage-browser/LocalStorageManager.d.ts +49 -49
- package/dist/storage-browser/LocalStorageManager.js +93 -93
- package/dist/swapper/Swapper.d.ts +686 -687
- package/dist/swapper/Swapper.js +1603 -1603
- package/dist/swapper/SwapperFactory.d.ts +135 -135
- package/dist/swapper/SwapperFactory.js +162 -162
- package/dist/swapper/SwapperUtils.d.ts +200 -200
- package/dist/swapper/SwapperUtils.js +467 -467
- package/dist/swapper/SwapperWithChain.d.ts +404 -404
- package/dist/swapper/SwapperWithChain.js +469 -469
- package/dist/swapper/SwapperWithSigner.d.ts +322 -322
- package/dist/swapper/SwapperWithSigner.js +318 -318
- package/dist/swaps/IAddressSwap.d.ts +22 -22
- package/dist/swaps/IAddressSwap.js +13 -14
- package/dist/swaps/IBTCWalletSwap.d.ts +73 -73
- package/dist/swaps/IBTCWalletSwap.js +17 -18
- package/dist/swaps/IClaimableSwap.d.ts +49 -49
- package/dist/swaps/IClaimableSwap.js +14 -15
- package/dist/swaps/IClaimableSwapWrapper.d.ts +15 -15
- package/dist/swaps/IClaimableSwapWrapper.js +2 -2
- package/dist/swaps/IRefundableSwap.d.ts +43 -43
- package/dist/swaps/IRefundableSwap.js +13 -14
- package/dist/swaps/ISwap.d.ts +386 -387
- package/dist/swaps/ISwap.js +346 -341
- package/dist/swaps/ISwapWithGasDrop.d.ts +21 -21
- package/dist/swaps/ISwapWithGasDrop.js +11 -12
- package/dist/swaps/ISwapWrapper.d.ts +283 -284
- package/dist/swaps/ISwapWrapper.js +353 -353
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +98 -98
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +126 -126
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +133 -135
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +169 -169
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +114 -115
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +134 -134
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +98 -101
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +130 -130
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +162 -162
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +190 -190
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +58 -58
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +78 -78
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +529 -531
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +1285 -1285
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +181 -184
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +418 -417
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +581 -583
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +1371 -1371
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +225 -228
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +506 -506
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +458 -458
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +1126 -1126
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +190 -191
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +378 -377
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +403 -403
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +924 -924
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +62 -62
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +112 -112
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +125 -127
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +256 -256
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +241 -242
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +520 -520
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +73 -73
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +155 -155
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +127 -128
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +278 -278
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +630 -630
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +1443 -1443
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +213 -207
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +756 -755
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +261 -261
- package/dist/swaps/trusted/ln/LnForGasSwap.js +511 -511
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +40 -40
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +82 -82
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +342 -342
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +715 -715
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +68 -69
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +92 -92
- package/dist/types/AmountData.d.ts +10 -10
- package/dist/types/AmountData.js +2 -2
- package/dist/types/CustomPriceFunction.d.ts +11 -11
- package/dist/types/CustomPriceFunction.js +2 -2
- package/dist/types/PriceInfoType.d.ts +28 -28
- package/dist/types/PriceInfoType.js +56 -57
- package/dist/types/SwapExecutionAction.d.ts +88 -88
- package/dist/types/SwapExecutionAction.js +2 -2
- package/dist/types/SwapStateInfo.d.ts +5 -5
- package/dist/types/SwapStateInfo.js +2 -2
- package/dist/types/SwapWithSigner.d.ts +17 -17
- package/dist/types/SwapWithSigner.js +42 -43
- package/dist/types/Token.d.ts +99 -99
- package/dist/types/Token.js +76 -76
- package/dist/types/TokenAmount.d.ts +69 -69
- package/dist/types/TokenAmount.js +59 -60
- package/dist/types/fees/Fee.d.ts +50 -50
- package/dist/types/fees/Fee.js +2 -2
- package/dist/types/fees/FeeBreakdown.d.ts +11 -11
- package/dist/types/fees/FeeBreakdown.js +2 -2
- package/dist/types/fees/PercentagePPM.d.ts +17 -17
- package/dist/types/fees/PercentagePPM.js +17 -18
- package/dist/types/lnurl/LNURLPay.d.ts +61 -61
- package/dist/types/lnurl/LNURLPay.js +30 -31
- package/dist/types/lnurl/LNURLWithdraw.d.ts +48 -48
- package/dist/types/lnurl/LNURLWithdraw.js +26 -27
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -24
- package/dist/types/wallets/LightningInvoiceCreateService.js +14 -15
- package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +23 -23
- package/dist/types/wallets/MinimalBitcoinWalletInterface.js +2 -2
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +9 -9
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.js +2 -2
- package/dist/utils/AutomaticClockDriftCorrection.d.ts +1 -1
- package/dist/utils/AutomaticClockDriftCorrection.js +69 -70
- package/dist/utils/BitcoinUtils.d.ts +12 -14
- package/dist/utils/BitcoinUtils.js +101 -102
- package/dist/utils/BitcoinWalletUtils.d.ts +7 -7
- package/dist/utils/BitcoinWalletUtils.js +13 -14
- package/dist/utils/Logger.d.ts +7 -7
- package/dist/utils/Logger.js +11 -12
- package/dist/utils/RetryUtils.d.ts +22 -22
- package/dist/utils/RetryUtils.js +66 -67
- package/dist/utils/SwapUtils.d.ts +88 -88
- package/dist/utils/SwapUtils.js +72 -72
- package/dist/utils/TimeoutUtils.d.ts +17 -17
- package/dist/utils/TimeoutUtils.js +54 -55
- package/dist/utils/TokenUtils.d.ts +19 -19
- package/dist/utils/TokenUtils.js +36 -37
- package/dist/utils/TypeUtils.d.ts +7 -7
- package/dist/utils/TypeUtils.js +2 -2
- package/dist/utils/Utils.d.ts +56 -58
- package/dist/utils/Utils.js +193 -194
- package/package.json +1 -1
- package/src/intermediaries/apis/IntermediaryAPI.ts +4 -2
- package/src/prices/abstract/ISwapPrice.ts +19 -6
- package/src/swaps/ISwap.ts +17 -6
- package/src/swaps/ISwapWrapper.ts +4 -3
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +4 -2
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +4 -1
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +2 -2
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +4 -2
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +6 -6
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +5 -5
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +4 -1
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +10 -2
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +1 -1
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +1 -1
|
@@ -1,377 +1,378 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FromBTCWrapper = void 0;
|
|
4
|
-
const IFromBTCWrapper_1 = require("../IFromBTCWrapper");
|
|
5
|
-
const FromBTCSwap_1 = require("./FromBTCSwap");
|
|
6
|
-
const base_1 = require("@atomiqlabs/base");
|
|
7
|
-
const buffer_1 = require("buffer");
|
|
8
|
-
const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
|
|
9
|
-
const SwapType_1 = require("../../../../enums/SwapType");
|
|
10
|
-
const Utils_1 = require("../../../../utils/Utils");
|
|
11
|
-
const BitcoinUtils_1 = require("../../../../utils/BitcoinUtils");
|
|
12
|
-
const IntermediaryAPI_1 = require("../../../../intermediaries/apis/IntermediaryAPI");
|
|
13
|
-
const RequestError_1 = require("../../../../errors/RequestError");
|
|
14
|
-
const utils_1 = require("@scure/btc-signer/utils");
|
|
15
|
-
const RetryUtils_1 = require("../../../../utils/RetryUtils");
|
|
16
|
-
/**
|
|
17
|
-
* Legacy escrow (PrTLC) based swap for Bitcoin -> Smart chains, requires manual initiation
|
|
18
|
-
* of the swap escrow on the destination chain.
|
|
19
|
-
*
|
|
20
|
-
* @category Swaps/Legacy/Bitcoin → Smart chain
|
|
21
|
-
*/
|
|
22
|
-
class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
|
|
23
|
-
/**
|
|
24
|
-
* @param chainIdentifier
|
|
25
|
-
* @param unifiedStorage Storage interface for the current environment
|
|
26
|
-
* @param unifiedChainEvents On-chain event listener
|
|
27
|
-
* @param chain
|
|
28
|
-
* @param contract Underlying contract handling the swaps
|
|
29
|
-
* @param prices Pricing to use
|
|
30
|
-
* @param tokens
|
|
31
|
-
* @param swapDataDeserializer Deserializer for SwapData
|
|
32
|
-
* @param btcRelay
|
|
33
|
-
* @param synchronizer Btc relay synchronizer
|
|
34
|
-
* @param btcRpc Bitcoin RPC which also supports getting transactions by txoHash
|
|
35
|
-
* @param options
|
|
36
|
-
* @param events Instance to use for emitting events
|
|
37
|
-
*/
|
|
38
|
-
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, btcRelay, synchronizer, btcRpc, options, events) {
|
|
39
|
-
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, {
|
|
40
|
-
...options,
|
|
41
|
-
bitcoinNetwork: options?.bitcoinNetwork ?? utils_1.TEST_NETWORK,
|
|
42
|
-
safetyFactor: options?.safetyFactor ?? 2,
|
|
43
|
-
blocksTillTxConfirms: options?.blocksTillTxConfirms ?? 12,
|
|
44
|
-
maxConfirmations: options?.maxConfirmations ?? 6,
|
|
45
|
-
minSendWindow: options?.minSendWindow ?? 30 * 60,
|
|
46
|
-
bitcoinBlocktime: options?.bitcoinBlocktime ?? 10 * 60
|
|
47
|
-
}, events);
|
|
48
|
-
this.TYPE = SwapType_1.SwapType.FROM_BTC;
|
|
49
|
-
/**
|
|
50
|
-
* @internal
|
|
51
|
-
*/
|
|
52
|
-
this.tickSwapState = [FromBTCSwap_1.FromBTCSwapState.PR_CREATED, FromBTCSwap_1.FromBTCSwapState.CLAIM_COMMITED, FromBTCSwap_1.FromBTCSwapState.EXPIRED];
|
|
53
|
-
/**
|
|
54
|
-
* @internal
|
|
55
|
-
*/
|
|
56
|
-
this._pendingSwapStates = [
|
|
57
|
-
FromBTCSwap_1.FromBTCSwapState.PR_CREATED,
|
|
58
|
-
FromBTCSwap_1.FromBTCSwapState.QUOTE_SOFT_EXPIRED,
|
|
59
|
-
FromBTCSwap_1.FromBTCSwapState.CLAIM_COMMITED,
|
|
60
|
-
FromBTCSwap_1.FromBTCSwapState.BTC_TX_CONFIRMED,
|
|
61
|
-
FromBTCSwap_1.FromBTCSwapState.EXPIRED
|
|
62
|
-
];
|
|
63
|
-
/**
|
|
64
|
-
* @internal
|
|
65
|
-
*/
|
|
66
|
-
this._claimableSwapStates = [FromBTCSwap_1.FromBTCSwapState.BTC_TX_CONFIRMED];
|
|
67
|
-
/**
|
|
68
|
-
* @internal
|
|
69
|
-
*/
|
|
70
|
-
this._swapDeserializer = FromBTCSwap_1.FromBTCSwap;
|
|
71
|
-
this.btcRelay = btcRelay;
|
|
72
|
-
this._synchronizer = synchronizer;
|
|
73
|
-
this._btcRpc = btcRpc;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* @inheritDoc
|
|
77
|
-
* @internal
|
|
78
|
-
*/
|
|
79
|
-
processEventInitialize(swap, event) {
|
|
80
|
-
if (swap._state === FromBTCSwap_1.FromBTCSwapState.PR_CREATED || swap._state === FromBTCSwap_1.FromBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
81
|
-
swap._state = FromBTCSwap_1.FromBTCSwapState.CLAIM_COMMITED;
|
|
82
|
-
return Promise.resolve(true);
|
|
83
|
-
}
|
|
84
|
-
return Promise.resolve(false);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* @inheritDoc
|
|
88
|
-
* @internal
|
|
89
|
-
*/
|
|
90
|
-
async processEventClaim(swap, event) {
|
|
91
|
-
if (swap._state !== FromBTCSwap_1.FromBTCSwapState.FAILED && swap._state !== FromBTCSwap_1.FromBTCSwapState.CLAIM_CLAIMED) {
|
|
92
|
-
await swap._setBitcoinTxId(buffer_1.Buffer.from(event.result, "hex").reverse().toString("hex")).catch(e => {
|
|
93
|
-
this.logger.warn("processEventClaim(): Error setting bitcoin txId: ", e);
|
|
94
|
-
});
|
|
95
|
-
swap._state = FromBTCSwap_1.FromBTCSwapState.CLAIM_CLAIMED;
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* @inheritDoc
|
|
102
|
-
* @internal
|
|
103
|
-
*/
|
|
104
|
-
processEventRefund(swap, event) {
|
|
105
|
-
if (swap._state !== FromBTCSwap_1.FromBTCSwapState.CLAIM_CLAIMED && swap._state !== FromBTCSwap_1.FromBTCSwapState.FAILED) {
|
|
106
|
-
swap._state = FromBTCSwap_1.FromBTCSwapState.FAILED;
|
|
107
|
-
return Promise.resolve(true);
|
|
108
|
-
}
|
|
109
|
-
return Promise.resolve(false);
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Returns the swap expiry, leaving enough time for the user to send a transaction and for it to confirm
|
|
113
|
-
*
|
|
114
|
-
* @param data Swap data
|
|
115
|
-
* @param requiredConfirmations Confirmations required on the bitcoin side to settle the swap
|
|
116
|
-
*
|
|
117
|
-
* @internal
|
|
118
|
-
*/
|
|
119
|
-
_getOnchainSendTimeout(data, requiredConfirmations) {
|
|
120
|
-
const tsDelta = (this._options.blocksTillTxConfirms + requiredConfirmations) * this._options.bitcoinBlocktime * this._options.safetyFactor;
|
|
121
|
-
return data.getExpiry() - BigInt(tsDelta);
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Pre-fetches claimer (watchtower) bounty data for the swap. Doesn't throw, instead returns null and aborts the
|
|
125
|
-
* provided abortController
|
|
126
|
-
*
|
|
127
|
-
* @param signer Smartchain signer address initiating the swap
|
|
128
|
-
* @param amountData
|
|
129
|
-
* @param options Options as passed to the swap creation function
|
|
130
|
-
* @param abortController
|
|
131
|
-
*
|
|
132
|
-
* @private
|
|
133
|
-
*/
|
|
134
|
-
async preFetchClaimerBounty(signer, amountData, options, abortController) {
|
|
135
|
-
const startTimestamp = BigInt(Math.floor(Date.now() / 1000));
|
|
136
|
-
if (options.unsafeZeroWatchtowerFee) {
|
|
137
|
-
return {
|
|
138
|
-
feePerBlock: 0n,
|
|
139
|
-
safetyFactor: options.blockSafetyFactor,
|
|
140
|
-
startTimestamp: startTimestamp,
|
|
141
|
-
addBlock: 0n,
|
|
142
|
-
addFee: 0n
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
const dummyAmount = BigInt(Math.floor(Math.random() * 0x1000000));
|
|
146
|
-
const dummySwapData = await this._contract.createSwapData(base_1.ChainSwapType.CHAIN, signer, signer, amountData.token, dummyAmount, this._contract.getHashForOnchain((0, Utils_1.randomBytes)(20), dummyAmount, 3).toString("hex"), this.getRandomSequence(), startTimestamp, false, true, BigInt(Math.floor(Math.random() * 0x10000)), BigInt(Math.floor(Math.random() * 0x10000)));
|
|
147
|
-
try {
|
|
148
|
-
const [feePerBlock, btcRelayData, currentBtcBlock, claimFeeRate] = await Promise.all([
|
|
149
|
-
this.btcRelay.getFeePerBlock(),
|
|
150
|
-
this.btcRelay.getTipData(),
|
|
151
|
-
this._btcRpc.getTipHeight(),
|
|
152
|
-
this._contract.getClaimFee(signer, dummySwapData)
|
|
153
|
-
]);
|
|
154
|
-
if (btcRelayData == null)
|
|
155
|
-
throw new Error("Btc relay not initialized!");
|
|
156
|
-
const currentBtcRelayBlock = btcRelayData.blockheight;
|
|
157
|
-
const addBlock = Math.max(currentBtcBlock - currentBtcRelayBlock, 0);
|
|
158
|
-
return {
|
|
159
|
-
feePerBlock: feePerBlock * options.feeSafetyFactorPPM / 1000000n,
|
|
160
|
-
safetyFactor: options.blockSafetyFactor,
|
|
161
|
-
startTimestamp: startTimestamp,
|
|
162
|
-
addBlock: BigInt(addBlock),
|
|
163
|
-
addFee: claimFeeRate * options.feeSafetyFactorPPM / 1000000n
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
catch (e) {
|
|
167
|
-
abortController.abort(e);
|
|
168
|
-
return undefined;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Returns calculated claimer bounty calculated from the claimer bounty data as fetched from preFetchClaimerBounty()
|
|
173
|
-
*
|
|
174
|
-
* @param data Parsed swap data returned from the intermediary
|
|
175
|
-
* @param options Options as passed to the swap creation function
|
|
176
|
-
* @param claimerBounty Claimer bounty data as fetched from {@link preFetchClaimerBounty} function
|
|
177
|
-
*
|
|
178
|
-
* @private
|
|
179
|
-
*/
|
|
180
|
-
getClaimerBounty(data, options, claimerBounty) {
|
|
181
|
-
const tsDelta = data.getExpiry() - claimerBounty.startTimestamp;
|
|
182
|
-
const blocksDelta = tsDelta / BigInt(this._options.bitcoinBlocktime) * options.blockSafetyFactor;
|
|
183
|
-
const totalBlock = blocksDelta + claimerBounty.addBlock;
|
|
184
|
-
return claimerBounty.addFee + (totalBlock * claimerBounty.feePerBlock);
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Verifies response returned from intermediary
|
|
188
|
-
*
|
|
189
|
-
* @param signer
|
|
190
|
-
* @param resp Response as returned by the intermediary
|
|
191
|
-
* @param amountData
|
|
192
|
-
* @param lp Intermediary
|
|
193
|
-
* @param options Options as passed to the swap creation function
|
|
194
|
-
* @param data Parsed swap data returned by the intermediary
|
|
195
|
-
* @param sequence Required swap sequence
|
|
196
|
-
* @param claimerBounty Claimer bount data as returned from the preFetchClaimerBounty() pre-fetch promise
|
|
197
|
-
* @param depositToken
|
|
198
|
-
*
|
|
199
|
-
* @throws {IntermediaryError} in case the response is invalid
|
|
200
|
-
*
|
|
201
|
-
* @private
|
|
202
|
-
*/
|
|
203
|
-
verifyReturnedData(signer, resp, amountData, lp, options, data, sequence, claimerBounty, depositToken) {
|
|
204
|
-
if (amountData.exactIn) {
|
|
205
|
-
if (resp.amount !== amountData.amount)
|
|
206
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid amount returned");
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
if (resp.total !== amountData.amount)
|
|
210
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid total returned");
|
|
211
|
-
}
|
|
212
|
-
const requiredConfirmations = resp.confirmations;
|
|
213
|
-
if (requiredConfirmations > this._options.maxConfirmations)
|
|
214
|
-
throw new IntermediaryError_1.IntermediaryError("Requires too many confirmations");
|
|
215
|
-
const totalClaimerBounty = this.getClaimerBounty(data, options, claimerBounty);
|
|
216
|
-
if (data.getClaimerBounty() !== totalClaimerBounty ||
|
|
217
|
-
data.getType() != base_1.ChainSwapType.CHAIN ||
|
|
218
|
-
data.getSequence() !== sequence ||
|
|
219
|
-
data.getAmount() !== resp.total ||
|
|
220
|
-
data.isPayIn() ||
|
|
221
|
-
!data.isToken(amountData.token) ||
|
|
222
|
-
!data.isOfferer(lp.getAddress(this.chainIdentifier)) ||
|
|
223
|
-
!data.isClaimer(signer) ||
|
|
224
|
-
!data.isDepositToken(depositToken) ||
|
|
225
|
-
data.hasSuccessAction()) {
|
|
226
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid data returned");
|
|
227
|
-
}
|
|
228
|
-
//Check that we have enough time to send the TX and for it to confirm
|
|
229
|
-
const expiry = this._getOnchainSendTimeout(data, requiredConfirmations);
|
|
230
|
-
const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
|
|
231
|
-
if ((expiry - currentTimestamp) < BigInt(this._options.minSendWindow)) {
|
|
232
|
-
throw new IntermediaryError_1.IntermediaryError("Send window too low");
|
|
233
|
-
}
|
|
234
|
-
const lockingScript = (0, BitcoinUtils_1.toOutputScript)(this._options.bitcoinNetwork, resp.btcAddress);
|
|
235
|
-
const desiredExtraData = this._contract.getExtraData(lockingScript, resp.amount, requiredConfirmations);
|
|
236
|
-
const desiredClaimHash = this._contract.getHashForOnchain(lockingScript, resp.amount, requiredConfirmations);
|
|
237
|
-
if (!desiredClaimHash.equals(buffer_1.Buffer.from(data.getClaimHash(), "hex"))) {
|
|
238
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid claim hash returned!");
|
|
239
|
-
}
|
|
240
|
-
const extraData = data.getExtraData();
|
|
241
|
-
if (extraData == null || !desiredExtraData.equals(buffer_1.Buffer.from(extraData, "hex"))) {
|
|
242
|
-
throw new IntermediaryError_1.IntermediaryError("Invalid extra data returned!");
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Returns a newly created legacy Bitcoin -> Smart chain swap using the PrTLC based escrow swap protocol,
|
|
247
|
-
* with the passed amount.
|
|
248
|
-
*
|
|
249
|
-
* @param recipient Smart chain signer's address on the destination chain
|
|
250
|
-
* @param amountData Amount, token and exact input/output data for to swap
|
|
251
|
-
* @param lps An array of intermediaries (LPs) to get the quotes from
|
|
252
|
-
* @param options Optional additional quote options
|
|
253
|
-
* @param additionalParams Optional additional parameters sent to the LP when creating the swap
|
|
254
|
-
* @param abortSignal Abort signal
|
|
255
|
-
*/
|
|
256
|
-
create(recipient, amountData, lps, options, additionalParams, abortSignal) {
|
|
257
|
-
let feeSafetyFactorPPM = 1500000n;
|
|
258
|
-
if (typeof (options?.feeSafetyFactor) === "bigint") {
|
|
259
|
-
feeSafetyFactorPPM = options.feeSafetyFactor * 1000000n;
|
|
260
|
-
}
|
|
261
|
-
else if (typeof (options?.feeSafetyFactor) === "number") {
|
|
262
|
-
feeSafetyFactorPPM = BigInt(Math.floor(options.feeSafetyFactor *
|
|
263
|
-
}
|
|
264
|
-
const _options = {
|
|
265
|
-
blockSafetyFactor: options?.blockSafetyFactor != null ? BigInt(options.blockSafetyFactor) : 1n,
|
|
266
|
-
feeSafetyFactorPPM,
|
|
267
|
-
unsafeZeroWatchtowerFee: options?.unsafeZeroWatchtowerFee ?? false
|
|
268
|
-
};
|
|
269
|
-
const sequence = this.getRandomSequence();
|
|
270
|
-
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
271
|
-
const pricePrefetchPromise = this.preFetchPrice(amountData, _abortController.signal);
|
|
272
|
-
const usdPricePrefetchPromise = this.preFetchUsdPrice(_abortController.signal);
|
|
273
|
-
const claimerBountyPrefetchPromise = this.preFetchClaimerBounty(recipient, amountData, _options, _abortController);
|
|
274
|
-
const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
|
|
275
|
-
const feeRatePromise = this.preFetchFeeRate(recipient, amountData, undefined, _abortController);
|
|
276
|
-
const _signDataPromise = this._contract.preFetchBlockDataForSignatures == null ?
|
|
277
|
-
this.preFetchSignData(Promise.resolve(true)) :
|
|
278
|
-
undefined;
|
|
279
|
-
return lps.map(lp => {
|
|
280
|
-
return {
|
|
281
|
-
intermediary: lp,
|
|
282
|
-
quote: (async () => {
|
|
283
|
-
if (lp.services[SwapType_1.SwapType.FROM_BTC] == null)
|
|
284
|
-
throw new Error("LP service for processing from btc swaps not found!");
|
|
285
|
-
const abortController = (0, Utils_1.extendAbortController)(_abortController.signal);
|
|
286
|
-
const liquidityPromise = this.preFetchIntermediaryLiquidity(amountData, lp, abortController);
|
|
287
|
-
try {
|
|
288
|
-
const { signDataPromise, resp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
289
|
-
const { signDataPrefetch, response } = IntermediaryAPI_1.IntermediaryAPI.initFromBTC(this.chainIdentifier, lp.url, nativeTokenAddress, {
|
|
290
|
-
claimer: recipient,
|
|
291
|
-
amount: amountData.amount,
|
|
292
|
-
token: amountData.token.toString(),
|
|
293
|
-
exactOut: !amountData.exactIn,
|
|
294
|
-
sequence,
|
|
295
|
-
claimerBounty: (0, Utils_1.throwIfUndefined)(claimerBountyPrefetchPromise),
|
|
296
|
-
feeRate: (0, Utils_1.throwIfUndefined)(feeRatePromise),
|
|
297
|
-
additionalParams
|
|
298
|
-
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
299
|
-
let signDataPromise = _signDataPromise;
|
|
300
|
-
if (signDataPromise == null) {
|
|
301
|
-
signDataPromise = this.preFetchSignData(signDataPrefetch);
|
|
302
|
-
}
|
|
303
|
-
else
|
|
304
|
-
signDataPrefetch.catch(() => { });
|
|
305
|
-
return {
|
|
306
|
-
signDataPromise,
|
|
307
|
-
resp: await response
|
|
308
|
-
};
|
|
309
|
-
}, undefined, e => e instanceof RequestError_1.RequestError, abortController.signal);
|
|
310
|
-
const data = new this._swapDataDeserializer(resp.data);
|
|
311
|
-
data.setClaimer(recipient);
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
this.
|
|
317
|
-
this.
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
swap
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
swap.
|
|
371
|
-
swap.
|
|
372
|
-
|
|
373
|
-
await swap.
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
}
|
|
377
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FromBTCWrapper = void 0;
|
|
4
|
+
const IFromBTCWrapper_1 = require("../IFromBTCWrapper");
|
|
5
|
+
const FromBTCSwap_1 = require("./FromBTCSwap");
|
|
6
|
+
const base_1 = require("@atomiqlabs/base");
|
|
7
|
+
const buffer_1 = require("buffer");
|
|
8
|
+
const IntermediaryError_1 = require("../../../../errors/IntermediaryError");
|
|
9
|
+
const SwapType_1 = require("../../../../enums/SwapType");
|
|
10
|
+
const Utils_1 = require("../../../../utils/Utils");
|
|
11
|
+
const BitcoinUtils_1 = require("../../../../utils/BitcoinUtils");
|
|
12
|
+
const IntermediaryAPI_1 = require("../../../../intermediaries/apis/IntermediaryAPI");
|
|
13
|
+
const RequestError_1 = require("../../../../errors/RequestError");
|
|
14
|
+
const utils_1 = require("@scure/btc-signer/utils");
|
|
15
|
+
const RetryUtils_1 = require("../../../../utils/RetryUtils");
|
|
16
|
+
/**
|
|
17
|
+
* Legacy escrow (PrTLC) based swap for Bitcoin -> Smart chains, requires manual initiation
|
|
18
|
+
* of the swap escrow on the destination chain.
|
|
19
|
+
*
|
|
20
|
+
* @category Swaps/Legacy/Bitcoin → Smart chain
|
|
21
|
+
*/
|
|
22
|
+
class FromBTCWrapper extends IFromBTCWrapper_1.IFromBTCWrapper {
|
|
23
|
+
/**
|
|
24
|
+
* @param chainIdentifier
|
|
25
|
+
* @param unifiedStorage Storage interface for the current environment
|
|
26
|
+
* @param unifiedChainEvents On-chain event listener
|
|
27
|
+
* @param chain
|
|
28
|
+
* @param contract Underlying contract handling the swaps
|
|
29
|
+
* @param prices Pricing to use
|
|
30
|
+
* @param tokens
|
|
31
|
+
* @param swapDataDeserializer Deserializer for SwapData
|
|
32
|
+
* @param btcRelay
|
|
33
|
+
* @param synchronizer Btc relay synchronizer
|
|
34
|
+
* @param btcRpc Bitcoin RPC which also supports getting transactions by txoHash
|
|
35
|
+
* @param options
|
|
36
|
+
* @param events Instance to use for emitting events
|
|
37
|
+
*/
|
|
38
|
+
constructor(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, btcRelay, synchronizer, btcRpc, options, events) {
|
|
39
|
+
super(chainIdentifier, unifiedStorage, unifiedChainEvents, chain, contract, prices, tokens, swapDataDeserializer, {
|
|
40
|
+
...options,
|
|
41
|
+
bitcoinNetwork: options?.bitcoinNetwork ?? utils_1.TEST_NETWORK,
|
|
42
|
+
safetyFactor: options?.safetyFactor ?? 2,
|
|
43
|
+
blocksTillTxConfirms: options?.blocksTillTxConfirms ?? 12,
|
|
44
|
+
maxConfirmations: options?.maxConfirmations ?? 6,
|
|
45
|
+
minSendWindow: options?.minSendWindow ?? 30 * 60, //Minimum time window for user to send in the on-chain funds for From BTC swap
|
|
46
|
+
bitcoinBlocktime: options?.bitcoinBlocktime ?? 10 * 60
|
|
47
|
+
}, events);
|
|
48
|
+
this.TYPE = SwapType_1.SwapType.FROM_BTC;
|
|
49
|
+
/**
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
this.tickSwapState = [FromBTCSwap_1.FromBTCSwapState.PR_CREATED, FromBTCSwap_1.FromBTCSwapState.CLAIM_COMMITED, FromBTCSwap_1.FromBTCSwapState.EXPIRED];
|
|
53
|
+
/**
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
this._pendingSwapStates = [
|
|
57
|
+
FromBTCSwap_1.FromBTCSwapState.PR_CREATED,
|
|
58
|
+
FromBTCSwap_1.FromBTCSwapState.QUOTE_SOFT_EXPIRED,
|
|
59
|
+
FromBTCSwap_1.FromBTCSwapState.CLAIM_COMMITED,
|
|
60
|
+
FromBTCSwap_1.FromBTCSwapState.BTC_TX_CONFIRMED,
|
|
61
|
+
FromBTCSwap_1.FromBTCSwapState.EXPIRED
|
|
62
|
+
];
|
|
63
|
+
/**
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
this._claimableSwapStates = [FromBTCSwap_1.FromBTCSwapState.BTC_TX_CONFIRMED];
|
|
67
|
+
/**
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
this._swapDeserializer = FromBTCSwap_1.FromBTCSwap;
|
|
71
|
+
this.btcRelay = btcRelay;
|
|
72
|
+
this._synchronizer = synchronizer;
|
|
73
|
+
this._btcRpc = btcRpc;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* @inheritDoc
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
processEventInitialize(swap, event) {
|
|
80
|
+
if (swap._state === FromBTCSwap_1.FromBTCSwapState.PR_CREATED || swap._state === FromBTCSwap_1.FromBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
81
|
+
swap._state = FromBTCSwap_1.FromBTCSwapState.CLAIM_COMMITED;
|
|
82
|
+
return Promise.resolve(true);
|
|
83
|
+
}
|
|
84
|
+
return Promise.resolve(false);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @inheritDoc
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
async processEventClaim(swap, event) {
|
|
91
|
+
if (swap._state !== FromBTCSwap_1.FromBTCSwapState.FAILED && swap._state !== FromBTCSwap_1.FromBTCSwapState.CLAIM_CLAIMED) {
|
|
92
|
+
await swap._setBitcoinTxId(buffer_1.Buffer.from(event.result, "hex").reverse().toString("hex")).catch(e => {
|
|
93
|
+
this.logger.warn("processEventClaim(): Error setting bitcoin txId: ", e);
|
|
94
|
+
});
|
|
95
|
+
swap._state = FromBTCSwap_1.FromBTCSwapState.CLAIM_CLAIMED;
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @inheritDoc
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
processEventRefund(swap, event) {
|
|
105
|
+
if (swap._state !== FromBTCSwap_1.FromBTCSwapState.CLAIM_CLAIMED && swap._state !== FromBTCSwap_1.FromBTCSwapState.FAILED) {
|
|
106
|
+
swap._state = FromBTCSwap_1.FromBTCSwapState.FAILED;
|
|
107
|
+
return Promise.resolve(true);
|
|
108
|
+
}
|
|
109
|
+
return Promise.resolve(false);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Returns the swap expiry, leaving enough time for the user to send a transaction and for it to confirm
|
|
113
|
+
*
|
|
114
|
+
* @param data Swap data
|
|
115
|
+
* @param requiredConfirmations Confirmations required on the bitcoin side to settle the swap
|
|
116
|
+
*
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
_getOnchainSendTimeout(data, requiredConfirmations) {
|
|
120
|
+
const tsDelta = (this._options.blocksTillTxConfirms + requiredConfirmations) * this._options.bitcoinBlocktime * this._options.safetyFactor;
|
|
121
|
+
return data.getExpiry() - BigInt(tsDelta);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Pre-fetches claimer (watchtower) bounty data for the swap. Doesn't throw, instead returns null and aborts the
|
|
125
|
+
* provided abortController
|
|
126
|
+
*
|
|
127
|
+
* @param signer Smartchain signer address initiating the swap
|
|
128
|
+
* @param amountData
|
|
129
|
+
* @param options Options as passed to the swap creation function
|
|
130
|
+
* @param abortController
|
|
131
|
+
*
|
|
132
|
+
* @private
|
|
133
|
+
*/
|
|
134
|
+
async preFetchClaimerBounty(signer, amountData, options, abortController) {
|
|
135
|
+
const startTimestamp = BigInt(Math.floor(Date.now() / 1000));
|
|
136
|
+
if (options.unsafeZeroWatchtowerFee) {
|
|
137
|
+
return {
|
|
138
|
+
feePerBlock: 0n,
|
|
139
|
+
safetyFactor: options.blockSafetyFactor,
|
|
140
|
+
startTimestamp: startTimestamp,
|
|
141
|
+
addBlock: 0n,
|
|
142
|
+
addFee: 0n
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const dummyAmount = BigInt(Math.floor(Math.random() * 0x1000000));
|
|
146
|
+
const dummySwapData = await this._contract.createSwapData(base_1.ChainSwapType.CHAIN, signer, signer, amountData.token, dummyAmount, this._contract.getHashForOnchain((0, Utils_1.randomBytes)(20), dummyAmount, 3).toString("hex"), this.getRandomSequence(), startTimestamp, false, true, BigInt(Math.floor(Math.random() * 0x10000)), BigInt(Math.floor(Math.random() * 0x10000)));
|
|
147
|
+
try {
|
|
148
|
+
const [feePerBlock, btcRelayData, currentBtcBlock, claimFeeRate] = await Promise.all([
|
|
149
|
+
this.btcRelay.getFeePerBlock(),
|
|
150
|
+
this.btcRelay.getTipData(),
|
|
151
|
+
this._btcRpc.getTipHeight(),
|
|
152
|
+
this._contract.getClaimFee(signer, dummySwapData)
|
|
153
|
+
]);
|
|
154
|
+
if (btcRelayData == null)
|
|
155
|
+
throw new Error("Btc relay not initialized!");
|
|
156
|
+
const currentBtcRelayBlock = btcRelayData.blockheight;
|
|
157
|
+
const addBlock = Math.max(currentBtcBlock - currentBtcRelayBlock, 0);
|
|
158
|
+
return {
|
|
159
|
+
feePerBlock: feePerBlock * options.feeSafetyFactorPPM / 1000000n,
|
|
160
|
+
safetyFactor: options.blockSafetyFactor,
|
|
161
|
+
startTimestamp: startTimestamp,
|
|
162
|
+
addBlock: BigInt(addBlock),
|
|
163
|
+
addFee: claimFeeRate * options.feeSafetyFactorPPM / 1000000n
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
catch (e) {
|
|
167
|
+
abortController.abort(e);
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Returns calculated claimer bounty calculated from the claimer bounty data as fetched from preFetchClaimerBounty()
|
|
173
|
+
*
|
|
174
|
+
* @param data Parsed swap data returned from the intermediary
|
|
175
|
+
* @param options Options as passed to the swap creation function
|
|
176
|
+
* @param claimerBounty Claimer bounty data as fetched from {@link preFetchClaimerBounty} function
|
|
177
|
+
*
|
|
178
|
+
* @private
|
|
179
|
+
*/
|
|
180
|
+
getClaimerBounty(data, options, claimerBounty) {
|
|
181
|
+
const tsDelta = data.getExpiry() - claimerBounty.startTimestamp;
|
|
182
|
+
const blocksDelta = tsDelta / BigInt(this._options.bitcoinBlocktime) * options.blockSafetyFactor;
|
|
183
|
+
const totalBlock = blocksDelta + claimerBounty.addBlock;
|
|
184
|
+
return claimerBounty.addFee + (totalBlock * claimerBounty.feePerBlock);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Verifies response returned from intermediary
|
|
188
|
+
*
|
|
189
|
+
* @param signer
|
|
190
|
+
* @param resp Response as returned by the intermediary
|
|
191
|
+
* @param amountData
|
|
192
|
+
* @param lp Intermediary
|
|
193
|
+
* @param options Options as passed to the swap creation function
|
|
194
|
+
* @param data Parsed swap data returned by the intermediary
|
|
195
|
+
* @param sequence Required swap sequence
|
|
196
|
+
* @param claimerBounty Claimer bount data as returned from the preFetchClaimerBounty() pre-fetch promise
|
|
197
|
+
* @param depositToken
|
|
198
|
+
*
|
|
199
|
+
* @throws {IntermediaryError} in case the response is invalid
|
|
200
|
+
*
|
|
201
|
+
* @private
|
|
202
|
+
*/
|
|
203
|
+
verifyReturnedData(signer, resp, amountData, lp, options, data, sequence, claimerBounty, depositToken) {
|
|
204
|
+
if (amountData.exactIn) {
|
|
205
|
+
if (resp.amount !== amountData.amount)
|
|
206
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid amount returned");
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
if (resp.total !== amountData.amount)
|
|
210
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid total returned");
|
|
211
|
+
}
|
|
212
|
+
const requiredConfirmations = resp.confirmations;
|
|
213
|
+
if (requiredConfirmations > this._options.maxConfirmations)
|
|
214
|
+
throw new IntermediaryError_1.IntermediaryError("Requires too many confirmations");
|
|
215
|
+
const totalClaimerBounty = this.getClaimerBounty(data, options, claimerBounty);
|
|
216
|
+
if (data.getClaimerBounty() !== totalClaimerBounty ||
|
|
217
|
+
data.getType() != base_1.ChainSwapType.CHAIN ||
|
|
218
|
+
data.getSequence() !== sequence ||
|
|
219
|
+
data.getAmount() !== resp.total ||
|
|
220
|
+
data.isPayIn() ||
|
|
221
|
+
!data.isToken(amountData.token) ||
|
|
222
|
+
!data.isOfferer(lp.getAddress(this.chainIdentifier)) ||
|
|
223
|
+
!data.isClaimer(signer) ||
|
|
224
|
+
!data.isDepositToken(depositToken) ||
|
|
225
|
+
data.hasSuccessAction()) {
|
|
226
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid data returned");
|
|
227
|
+
}
|
|
228
|
+
//Check that we have enough time to send the TX and for it to confirm
|
|
229
|
+
const expiry = this._getOnchainSendTimeout(data, requiredConfirmations);
|
|
230
|
+
const currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
|
|
231
|
+
if ((expiry - currentTimestamp) < BigInt(this._options.minSendWindow)) {
|
|
232
|
+
throw new IntermediaryError_1.IntermediaryError("Send window too low");
|
|
233
|
+
}
|
|
234
|
+
const lockingScript = (0, BitcoinUtils_1.toOutputScript)(this._options.bitcoinNetwork, resp.btcAddress);
|
|
235
|
+
const desiredExtraData = this._contract.getExtraData(lockingScript, resp.amount, requiredConfirmations);
|
|
236
|
+
const desiredClaimHash = this._contract.getHashForOnchain(lockingScript, resp.amount, requiredConfirmations);
|
|
237
|
+
if (!desiredClaimHash.equals(buffer_1.Buffer.from(data.getClaimHash(), "hex"))) {
|
|
238
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid claim hash returned!");
|
|
239
|
+
}
|
|
240
|
+
const extraData = data.getExtraData();
|
|
241
|
+
if (extraData == null || !desiredExtraData.equals(buffer_1.Buffer.from(extraData, "hex"))) {
|
|
242
|
+
throw new IntermediaryError_1.IntermediaryError("Invalid extra data returned!");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Returns a newly created legacy Bitcoin -> Smart chain swap using the PrTLC based escrow swap protocol,
|
|
247
|
+
* with the passed amount.
|
|
248
|
+
*
|
|
249
|
+
* @param recipient Smart chain signer's address on the destination chain
|
|
250
|
+
* @param amountData Amount, token and exact input/output data for to swap
|
|
251
|
+
* @param lps An array of intermediaries (LPs) to get the quotes from
|
|
252
|
+
* @param options Optional additional quote options
|
|
253
|
+
* @param additionalParams Optional additional parameters sent to the LP when creating the swap
|
|
254
|
+
* @param abortSignal Abort signal
|
|
255
|
+
*/
|
|
256
|
+
create(recipient, amountData, lps, options, additionalParams, abortSignal) {
|
|
257
|
+
let feeSafetyFactorPPM = 1500000n;
|
|
258
|
+
if (typeof (options?.feeSafetyFactor) === "bigint") {
|
|
259
|
+
feeSafetyFactorPPM = options.feeSafetyFactor * 1000000n;
|
|
260
|
+
}
|
|
261
|
+
else if (typeof (options?.feeSafetyFactor) === "number") {
|
|
262
|
+
feeSafetyFactorPPM = BigInt(Math.floor(options.feeSafetyFactor * 1_000_000));
|
|
263
|
+
}
|
|
264
|
+
const _options = {
|
|
265
|
+
blockSafetyFactor: options?.blockSafetyFactor != null ? BigInt(options.blockSafetyFactor) : 1n,
|
|
266
|
+
feeSafetyFactorPPM,
|
|
267
|
+
unsafeZeroWatchtowerFee: options?.unsafeZeroWatchtowerFee ?? false
|
|
268
|
+
};
|
|
269
|
+
const sequence = this.getRandomSequence();
|
|
270
|
+
const _abortController = (0, Utils_1.extendAbortController)(abortSignal);
|
|
271
|
+
const pricePrefetchPromise = this.preFetchPrice(amountData, _abortController.signal);
|
|
272
|
+
const usdPricePrefetchPromise = this.preFetchUsdPrice(_abortController.signal);
|
|
273
|
+
const claimerBountyPrefetchPromise = this.preFetchClaimerBounty(recipient, amountData, _options, _abortController);
|
|
274
|
+
const nativeTokenAddress = this._chain.getNativeCurrencyAddress();
|
|
275
|
+
const feeRatePromise = this.preFetchFeeRate(recipient, amountData, undefined, _abortController);
|
|
276
|
+
const _signDataPromise = this._contract.preFetchBlockDataForSignatures == null ?
|
|
277
|
+
this.preFetchSignData(Promise.resolve(true)) :
|
|
278
|
+
undefined;
|
|
279
|
+
return lps.map(lp => {
|
|
280
|
+
return {
|
|
281
|
+
intermediary: lp,
|
|
282
|
+
quote: (async () => {
|
|
283
|
+
if (lp.services[SwapType_1.SwapType.FROM_BTC] == null)
|
|
284
|
+
throw new Error("LP service for processing from btc swaps not found!");
|
|
285
|
+
const abortController = (0, Utils_1.extendAbortController)(_abortController.signal);
|
|
286
|
+
const liquidityPromise = this.preFetchIntermediaryLiquidity(amountData, lp, abortController);
|
|
287
|
+
try {
|
|
288
|
+
const { signDataPromise, resp } = await (0, RetryUtils_1.tryWithRetries)(async (retryCount) => {
|
|
289
|
+
const { signDataPrefetch, response } = IntermediaryAPI_1.IntermediaryAPI.initFromBTC(this.chainIdentifier, lp.url, nativeTokenAddress, {
|
|
290
|
+
claimer: recipient,
|
|
291
|
+
amount: amountData.amount,
|
|
292
|
+
token: amountData.token.toString(),
|
|
293
|
+
exactOut: !amountData.exactIn,
|
|
294
|
+
sequence,
|
|
295
|
+
claimerBounty: (0, Utils_1.throwIfUndefined)(claimerBountyPrefetchPromise),
|
|
296
|
+
feeRate: (0, Utils_1.throwIfUndefined)(feeRatePromise),
|
|
297
|
+
additionalParams
|
|
298
|
+
}, this._options.postRequestTimeout, abortController.signal, retryCount > 0 ? false : undefined);
|
|
299
|
+
let signDataPromise = _signDataPromise;
|
|
300
|
+
if (signDataPromise == null) {
|
|
301
|
+
signDataPromise = this.preFetchSignData(signDataPrefetch);
|
|
302
|
+
}
|
|
303
|
+
else
|
|
304
|
+
signDataPrefetch.catch(() => { });
|
|
305
|
+
return {
|
|
306
|
+
signDataPromise,
|
|
307
|
+
resp: await response
|
|
308
|
+
};
|
|
309
|
+
}, undefined, e => e instanceof RequestError_1.RequestError, abortController.signal);
|
|
310
|
+
const data = new this._swapDataDeserializer(resp.data);
|
|
311
|
+
data.setClaimer(recipient);
|
|
312
|
+
const swapFeeBtc = resp.swapFee * resp.amount / (data.getAmount() - resp.swapFee);
|
|
313
|
+
this.verifyReturnedData(recipient, resp, amountData, lp, _options, data, sequence, (await claimerBountyPrefetchPromise), nativeTokenAddress);
|
|
314
|
+
const [pricingInfo, signatureExpiry] = await Promise.all([
|
|
315
|
+
//Get intermediary's liquidity
|
|
316
|
+
this.verifyReturnedPrice(lp.services[SwapType_1.SwapType.FROM_BTC], false, resp.amount, resp.total, amountData.token, { swapFeeBtc }, pricePrefetchPromise, usdPricePrefetchPromise, abortController.signal),
|
|
317
|
+
this.verifyReturnedSignature(recipient, data, resp, feeRatePromise, signDataPromise, abortController.signal),
|
|
318
|
+
this.verifyIntermediaryLiquidity(data.getAmount(), (0, Utils_1.throwIfUndefined)(liquidityPromise)),
|
|
319
|
+
]);
|
|
320
|
+
const quote = new FromBTCSwap_1.FromBTCSwap(this, {
|
|
321
|
+
pricingInfo,
|
|
322
|
+
url: lp.url,
|
|
323
|
+
expiry: signatureExpiry,
|
|
324
|
+
swapFee: resp.swapFee,
|
|
325
|
+
swapFeeBtc,
|
|
326
|
+
feeRate: (await feeRatePromise),
|
|
327
|
+
signatureData: resp,
|
|
328
|
+
data,
|
|
329
|
+
address: resp.btcAddress,
|
|
330
|
+
amount: resp.amount,
|
|
331
|
+
exactIn: amountData.exactIn ?? true,
|
|
332
|
+
requiredConfirmations: resp.confirmations
|
|
333
|
+
});
|
|
334
|
+
return quote;
|
|
335
|
+
}
|
|
336
|
+
catch (e) {
|
|
337
|
+
abortController.abort(e);
|
|
338
|
+
throw e;
|
|
339
|
+
}
|
|
340
|
+
})()
|
|
341
|
+
};
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* @inheritDoc
|
|
346
|
+
*/
|
|
347
|
+
async recoverFromSwapDataAndState(init, state, lp) {
|
|
348
|
+
const data = init.data;
|
|
349
|
+
const swapInit = {
|
|
350
|
+
pricingInfo: {
|
|
351
|
+
isValid: true,
|
|
352
|
+
satsBaseFee: 0n,
|
|
353
|
+
swapPriceUSatPerToken: 100000000000000n,
|
|
354
|
+
realPriceUSatPerToken: 100000000000000n,
|
|
355
|
+
differencePPM: 0n,
|
|
356
|
+
feePPM: 0n,
|
|
357
|
+
},
|
|
358
|
+
url: lp?.url,
|
|
359
|
+
expiry: 0,
|
|
360
|
+
swapFee: 0n,
|
|
361
|
+
swapFeeBtc: 0n,
|
|
362
|
+
feeRate: "",
|
|
363
|
+
signatureData: undefined,
|
|
364
|
+
data,
|
|
365
|
+
exactIn: false
|
|
366
|
+
};
|
|
367
|
+
const swap = new FromBTCSwap_1.FromBTCSwap(this, swapInit);
|
|
368
|
+
swap._commitTxId = await init.getInitTxId();
|
|
369
|
+
const blockData = await init.getTxBlock();
|
|
370
|
+
swap.createdAt = blockData.blockTime * 1000;
|
|
371
|
+
swap._setInitiated();
|
|
372
|
+
swap._state = FromBTCSwap_1.FromBTCSwapState.CLAIM_COMMITED;
|
|
373
|
+
await swap._sync(false, false, state);
|
|
374
|
+
await swap._save();
|
|
375
|
+
return swap;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
exports.FromBTCWrapper = FromBTCWrapper;
|