@atomiqlabs/sdk 8.1.7 → 8.3.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/bitcoin/wallet/BitcoinWallet.d.ts +41 -5
- package/dist/bitcoin/wallet/BitcoinWallet.js +36 -1
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +52 -2
- package/dist/bitcoin/wallet/IBitcoinWallet.js +2 -1
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +42 -7
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +36 -1
- package/dist/enums/FeeType.d.ts +7 -0
- package/dist/enums/FeeType.js +7 -0
- package/dist/enums/SwapAmountType.d.ts +7 -0
- package/dist/enums/SwapAmountType.js +7 -0
- package/dist/enums/SwapDirection.d.ts +7 -0
- package/dist/enums/SwapDirection.js +7 -0
- package/dist/enums/SwapType.d.ts +62 -1
- package/dist/enums/SwapType.js +62 -1
- package/dist/errors/IntermediaryError.d.ts +4 -0
- package/dist/errors/IntermediaryError.js +1 -0
- package/dist/errors/RequestError.d.ts +15 -1
- package/dist/errors/RequestError.js +8 -0
- package/dist/errors/UserError.d.ts +1 -0
- package/dist/errors/UserError.js +1 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.js +3 -4
- package/dist/intermediaries/Intermediary.d.ts +57 -10
- package/dist/intermediaries/Intermediary.js +37 -10
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +55 -22
- package/dist/intermediaries/IntermediaryDiscovery.js +35 -22
- package/dist/prices/RedundantSwapPrice.d.ts +24 -3
- package/dist/prices/RedundantSwapPrice.js +21 -1
- package/dist/prices/SingleSwapPrice.d.ts +9 -6
- package/dist/prices/SingleSwapPrice.js +10 -7
- package/dist/prices/SwapPriceWithChain.d.ts +54 -16
- package/dist/prices/SwapPriceWithChain.js +58 -20
- package/dist/prices/abstract/ISwapPrice.d.ts +94 -45
- package/dist/prices/abstract/ISwapPrice.js +103 -55
- package/dist/prices/providers/BinancePriceProvider.d.ts +7 -0
- package/dist/prices/providers/BinancePriceProvider.js +7 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.d.ts +6 -0
- package/dist/prices/providers/CoinGeckoPriceProvider.js +6 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.d.ts +6 -0
- package/dist/prices/providers/CoinPaprikaPriceProvider.js +6 -0
- package/dist/prices/providers/CustomPriceProvider.d.ts +11 -0
- package/dist/prices/providers/CustomPriceProvider.js +11 -0
- package/dist/prices/providers/KrakenPriceProvider.d.ts +9 -0
- package/dist/prices/providers/KrakenPriceProvider.js +9 -0
- package/dist/prices/providers/OKXPriceProvider.d.ts +6 -0
- package/dist/prices/providers/OKXPriceProvider.js +6 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.d.ts +3 -0
- package/dist/prices/providers/abstract/ExchangePriceProvider.js +3 -0
- package/dist/storage/IUnifiedStorage.d.ts +19 -7
- package/dist/storage/UnifiedSwapStorage.d.ts +33 -3
- package/dist/storage/UnifiedSwapStorage.js +29 -1
- package/dist/storage-browser/IndexedDBUnifiedStorage.d.ts +31 -7
- package/dist/storage-browser/IndexedDBUnifiedStorage.js +29 -6
- package/dist/storage-browser/LocalStorageManager.d.ts +25 -1
- package/dist/storage-browser/LocalStorageManager.js +25 -1
- package/dist/swapper/Swapper.d.ts +303 -222
- package/dist/swapper/Swapper.js +376 -344
- package/dist/swapper/SwapperFactory.d.ts +41 -17
- package/dist/swapper/SwapperFactory.js +23 -2
- package/dist/swapper/SwapperUtils.d.ts +75 -28
- package/dist/swapper/SwapperUtils.js +107 -60
- package/dist/swapper/SwapperWithChain.d.ts +286 -91
- package/dist/swapper/SwapperWithChain.js +218 -64
- package/dist/swapper/SwapperWithSigner.d.ts +229 -80
- package/dist/swapper/SwapperWithSigner.js +190 -44
- package/dist/swaps/IAddressSwap.d.ts +10 -1
- package/dist/swaps/IAddressSwap.js +2 -1
- package/dist/swaps/IBTCWalletSwap.d.ts +24 -6
- package/dist/swaps/IBTCWalletSwap.js +2 -1
- package/dist/swaps/IClaimableSwap.d.ts +36 -4
- package/dist/swaps/IClaimableSwap.js +2 -1
- package/dist/swaps/IClaimableSwapWrapper.d.ts +11 -1
- package/dist/swaps/IRefundableSwap.d.ts +29 -3
- package/dist/swaps/IRefundableSwap.js +2 -1
- package/dist/swaps/ISwap.d.ts +159 -21
- package/dist/swaps/ISwap.js +90 -33
- package/dist/swaps/ISwapWithGasDrop.d.ts +6 -0
- package/dist/swaps/ISwapWithGasDrop.js +1 -0
- package/dist/swaps/ISwapWrapper.d.ts +157 -48
- package/dist/swaps/ISwapWrapper.js +130 -72
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.d.ts +49 -6
- package/dist/swaps/escrow_swaps/IEscrowSelfInitSwap.js +22 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +65 -12
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +38 -19
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +39 -9
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +30 -21
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +31 -15
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +33 -18
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.d.ts +94 -29
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.js +90 -27
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.d.ts +22 -9
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +24 -11
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +275 -58
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +516 -239
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +76 -25
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +131 -49
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +311 -51
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +542 -193
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +87 -26
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +147 -58
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +209 -53
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +449 -242
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +77 -23
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +116 -46
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +197 -56
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +326 -189
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +30 -5
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +44 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +60 -19
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +74 -31
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +76 -50
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +106 -101
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.d.ts +36 -13
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.js +65 -19
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +46 -17
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +82 -27
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +328 -92
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +460 -219
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +76 -24
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +244 -124
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +146 -18
- package/dist/swaps/trusted/ln/LnForGasSwap.js +173 -43
- package/dist/swaps/trusted/ln/LnForGasWrapper.d.ts +29 -10
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +30 -11
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +200 -47
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +230 -78
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +34 -12
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +33 -14
- package/dist/types/AmountData.d.ts +2 -1
- package/dist/types/CustomPriceFunction.d.ts +7 -1
- package/dist/types/SwapExecutionAction.d.ts +74 -4
- package/dist/types/SwapWithSigner.d.ts +4 -1
- package/dist/types/SwapWithSigner.js +5 -2
- package/dist/types/Token.d.ts +11 -5
- package/dist/types/Token.js +6 -3
- package/dist/types/TokenAmount.d.ts +3 -0
- package/dist/types/TokenAmount.js +2 -0
- package/dist/types/fees/Fee.d.ts +2 -1
- package/dist/types/fees/FeeBreakdown.d.ts +2 -1
- package/dist/types/fees/PercentagePPM.d.ts +2 -0
- package/dist/types/fees/PercentagePPM.js +1 -0
- package/dist/types/lnurl/LNURLPay.d.ts +14 -6
- package/dist/types/lnurl/LNURLPay.js +6 -2
- package/dist/types/lnurl/LNURLWithdraw.d.ts +12 -5
- package/dist/types/lnurl/LNURLWithdraw.js +6 -2
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +20 -0
- package/dist/types/wallets/LightningInvoiceCreateService.js +15 -0
- package/dist/types/wallets/MinimalBitcoinWalletInterface.d.ts +3 -1
- package/dist/types/wallets/MinimalLightningNetworkWalletInterface.d.ts +3 -1
- package/dist/utils/BitcoinUtils.d.ts +1 -0
- package/dist/utils/BitcoinUtils.js +5 -1
- package/dist/utils/SwapUtils.d.ts +56 -1
- package/dist/utils/SwapUtils.js +53 -1
- package/dist/utils/TokenUtils.d.ts +10 -2
- package/dist/utils/TokenUtils.js +12 -4
- package/package.json +3 -3
- package/src/bitcoin/wallet/BitcoinWallet.ts +41 -5
- package/src/bitcoin/wallet/IBitcoinWallet.ts +57 -2
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +42 -6
- package/src/enums/FeeType.ts +7 -0
- package/src/enums/SwapAmountType.ts +7 -0
- package/src/enums/SwapDirection.ts +7 -0
- package/src/enums/SwapType.ts +62 -2
- package/src/errors/IntermediaryError.ts +4 -0
- package/src/errors/RequestError.ts +15 -1
- package/src/errors/UserError.ts +1 -0
- package/src/index.ts +6 -5
- package/src/intermediaries/Intermediary.ts +57 -10
- package/src/intermediaries/IntermediaryDiscovery.ts +60 -27
- package/src/prices/RedundantSwapPrice.ts +24 -4
- package/src/prices/SingleSwapPrice.ts +10 -7
- package/src/prices/SwapPriceWithChain.ts +59 -21
- package/src/prices/abstract/ISwapPrice.ts +114 -65
- package/src/prices/providers/BinancePriceProvider.ts +7 -0
- package/src/prices/providers/CoinGeckoPriceProvider.ts +6 -0
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +6 -0
- package/src/prices/providers/CustomPriceProvider.ts +11 -0
- package/src/prices/providers/KrakenPriceProvider.ts +9 -0
- package/src/prices/providers/OKXPriceProvider.ts +6 -0
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +3 -0
- package/src/storage/IUnifiedStorage.ts +19 -7
- package/src/storage/UnifiedSwapStorage.ts +33 -3
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +31 -8
- package/src/storage-browser/LocalStorageManager.ts +25 -1
- package/src/swapper/Swapper.ts +513 -379
- package/src/swapper/SwapperFactory.ts +44 -21
- package/src/swapper/SwapperUtils.ts +107 -60
- package/src/swapper/SwapperWithChain.ts +320 -81
- package/src/swapper/SwapperWithSigner.ts +263 -56
- package/src/swaps/IAddressSwap.ts +11 -1
- package/src/swaps/IBTCWalletSwap.ts +24 -8
- package/src/swaps/IClaimableSwap.ts +39 -4
- package/src/swaps/IClaimableSwapWrapper.ts +11 -2
- package/src/swaps/IRefundableSwap.ts +32 -3
- package/src/swaps/ISwap.ts +221 -82
- package/src/swaps/ISwapWithGasDrop.ts +6 -0
- package/src/swaps/ISwapWrapper.ts +212 -94
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +62 -18
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +83 -37
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +61 -30
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +37 -19
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +120 -51
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +24 -11
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +559 -256
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +155 -61
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +590 -226
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +177 -74
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +470 -243
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +141 -59
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +352 -193
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +48 -23
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +86 -39
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +110 -110
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +88 -33
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +101 -31
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +534 -263
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +289 -148
- package/src/swaps/trusted/ln/LnForGasSwap.ts +184 -45
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +34 -15
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +260 -86
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +41 -19
- package/src/types/AmountData.ts +2 -1
- package/src/types/CustomPriceFunction.ts +7 -1
- package/src/types/SwapExecutionAction.ts +84 -5
- package/src/types/SwapWithSigner.ts +7 -3
- package/src/types/Token.ts +12 -5
- package/src/types/TokenAmount.ts +3 -0
- package/src/types/fees/Fee.ts +2 -1
- package/src/types/fees/FeeBreakdown.ts +2 -1
- package/src/types/fees/PercentagePPM.ts +2 -0
- package/src/types/lnurl/LNURLPay.ts +14 -6
- package/src/types/lnurl/LNURLWithdraw.ts +12 -5
- package/src/types/wallets/LightningInvoiceCreateService.ts +26 -0
- package/src/types/wallets/MinimalBitcoinWalletInterface.ts +3 -1
- package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +3 -1
- package/src/utils/BitcoinUtils.ts +5 -0
- package/src/utils/SwapUtils.ts +61 -1
- package/src/utils/TokenUtils.ts +12 -4
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.d.ts +0 -68
- package/dist/bitcoin/BitcoinRpcWithAddressIndex.js +0 -2
- package/dist/bitcoin/LightningNetworkApi.d.ts +0 -12
- package/dist/bitcoin/LightningNetworkApi.js +0 -2
- package/dist/bitcoin/mempool/MempoolApi.d.ts +0 -350
- package/dist/bitcoin/mempool/MempoolApi.js +0 -311
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.d.ts +0 -44
- package/dist/bitcoin/mempool/MempoolBitcoinBlock.js +0 -48
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.d.ts +0 -119
- package/dist/bitcoin/mempool/MempoolBitcoinRpc.js +0 -361
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.d.ts +0 -22
- package/dist/bitcoin/mempool/synchronizer/MempoolBtcRelaySynchronizer.js +0 -105
- package/dist/errors/PaymentAuthError.d.ts +0 -11
- package/dist/errors/PaymentAuthError.js +0 -23
- package/src/errors/PaymentAuthError.ts +0 -26
|
@@ -38,24 +38,75 @@ import {
|
|
|
38
38
|
serializePriceInfoType
|
|
39
39
|
} from "../../types/PriceInfoType";
|
|
40
40
|
import {toBitcoinWallet} from "../../utils/BitcoinWalletUtils";
|
|
41
|
+
import {SwapExecutionActionBitcoin} from "../../types/SwapExecutionAction";
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
|
-
* State enum for SPV
|
|
44
|
+
* State enum for SPV vault (UTXO-controlled vault) based swaps
|
|
44
45
|
* @category Swaps
|
|
45
46
|
*/
|
|
46
47
|
export enum SpvFromBTCSwapState {
|
|
48
|
+
/**
|
|
49
|
+
* Catastrophic failure has occurred when processing the swap on the smart chain side,
|
|
50
|
+
* this implies a bug in the smart contract code or the user and intermediary deliberately
|
|
51
|
+
* creating a bitcoin transaction with invalid format unparsable by the smart contract.
|
|
52
|
+
*/
|
|
47
53
|
CLOSED = -5,
|
|
48
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Some of the bitcoin swap transaction inputs were double-spent, this means the swap
|
|
56
|
+
* has failed and no BTC was sent
|
|
57
|
+
*/
|
|
58
|
+
FAILED = -4,
|
|
59
|
+
/**
|
|
60
|
+
* The intermediary (LP) declined to co-sign the submitted PSBT, hence the swap failed
|
|
61
|
+
*/
|
|
49
62
|
DECLINED = -3,
|
|
63
|
+
/**
|
|
64
|
+
* Swap has expired for good and there is no way how it can be executed anymore
|
|
65
|
+
*/
|
|
50
66
|
QUOTE_EXPIRED = -2,
|
|
67
|
+
/**
|
|
68
|
+
* A swap is almost expired, and it should be presented to the user as expired, though
|
|
69
|
+
* there is still a chance that it will be processed
|
|
70
|
+
*/
|
|
51
71
|
QUOTE_SOFT_EXPIRED = -1,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Swap was created, use the {@link SpvFromBTCSwap.getFundedPsbt} or {@link SpvFromBTCSwap.getPsbt} functions
|
|
74
|
+
* to get the bitcoin swap PSBT that should be signed by the user's wallet and then submitted via the
|
|
75
|
+
* {@link SpvFromBTCSwap.submitPsbt} function.
|
|
76
|
+
*/
|
|
77
|
+
CREATED = 0,
|
|
78
|
+
/**
|
|
79
|
+
* Swap bitcoin PSBT was submitted by the client to the SDK
|
|
80
|
+
*/
|
|
81
|
+
SIGNED = 1,
|
|
82
|
+
/**
|
|
83
|
+
* Swap bitcoin PSBT sent to the intermediary (LP), waiting for the intermediary co-sign
|
|
84
|
+
* it and broadcast. You can use the {@link SpvFromBTCSwap.waitTillClaimedOrFronted}
|
|
85
|
+
* function to wait till the intermediary broadcasts the transaction and the transaction
|
|
86
|
+
* confirms.
|
|
87
|
+
*/
|
|
88
|
+
POSTED = 2,
|
|
89
|
+
/**
|
|
90
|
+
* Intermediary (LP) has co-signed and broadcasted the bitcoin transaction. You can use the
|
|
91
|
+
* {@link SpvFromBTCSwap.waitTillClaimedOrFronted} function to wait till the transaction
|
|
92
|
+
* confirms.
|
|
93
|
+
*/
|
|
94
|
+
BROADCASTED = 3,
|
|
95
|
+
/**
|
|
96
|
+
* Settlement on the destination smart chain was fronted and funds were already received
|
|
97
|
+
* by the user, even before the final settlement.
|
|
98
|
+
*/
|
|
99
|
+
FRONTED = 4,
|
|
100
|
+
/**
|
|
101
|
+
* Bitcoin transaction confirmed with necessary amount of confirmations, wait for automatic
|
|
102
|
+
* settlement by the watchtower with the {@link waitTillClaimedOrFronted} function, or settle manually
|
|
103
|
+
* using the {@link FromBTCSwap.claim} or {@link FromBTCSwap.txsClaim} function.
|
|
104
|
+
*/
|
|
105
|
+
BTC_TX_CONFIRMED = 5,
|
|
106
|
+
/**
|
|
107
|
+
* Swap settled on the smart chain and funds received
|
|
108
|
+
*/
|
|
109
|
+
CLAIMED = 6
|
|
59
110
|
}
|
|
60
111
|
|
|
61
112
|
export type SpvFromBTCSwapInit = ISwapInit & {
|
|
@@ -116,52 +167,80 @@ export function isSpvFromBTCSwapInit(obj: any): obj is SpvFromBTCSwapInit {
|
|
|
116
167
|
isISwapInit(obj);
|
|
117
168
|
}
|
|
118
169
|
|
|
170
|
+
/**
|
|
171
|
+
* New spv vault (UTXO-controlled vault) based swaps for Bitcoin -> Smart chain swaps not requiring
|
|
172
|
+
* any initiation on the destination chain, and with the added possibility for the user to receive
|
|
173
|
+
* a native token on the destination chain as part of the swap (a "gas drop" feature).
|
|
174
|
+
*
|
|
175
|
+
* @category Swaps
|
|
176
|
+
*/
|
|
119
177
|
export class SpvFromBTCSwap<T extends ChainType>
|
|
120
178
|
extends ISwap<T, SpvFromBTCTypeDefinition<T>>
|
|
121
179
|
implements IBTCWalletSwap, ISwapWithGasDrop<T>, IClaimableSwap<T, SpvFromBTCTypeDefinition<T>, SpvFromBTCSwapState> {
|
|
122
180
|
|
|
123
|
-
readonly TYPE = SwapType.SPV_VAULT_FROM_BTC;
|
|
181
|
+
readonly TYPE: SwapType.SPV_VAULT_FROM_BTC = SwapType.SPV_VAULT_FROM_BTC;
|
|
182
|
+
/**
|
|
183
|
+
* @inheritDoc
|
|
184
|
+
* @internal
|
|
185
|
+
*/
|
|
124
186
|
protected readonly logger: LoggerType;
|
|
125
187
|
|
|
126
|
-
readonly quoteId: string;
|
|
127
|
-
readonly recipient: string;
|
|
188
|
+
private readonly quoteId: string;
|
|
189
|
+
private readonly recipient: string;
|
|
128
190
|
|
|
129
|
-
readonly vaultOwner: string;
|
|
130
|
-
readonly vaultId: bigint;
|
|
131
|
-
readonly vaultRequiredConfirmations: number;
|
|
132
|
-
readonly vaultTokenMultipliers: bigint[];
|
|
191
|
+
private readonly vaultOwner: string;
|
|
192
|
+
private readonly vaultId: bigint;
|
|
193
|
+
private readonly vaultRequiredConfirmations: number;
|
|
194
|
+
private readonly vaultTokenMultipliers: bigint[];
|
|
133
195
|
|
|
134
|
-
readonly vaultBtcAddress: string;
|
|
135
|
-
readonly vaultUtxo: string;
|
|
136
|
-
readonly vaultUtxoValue: bigint;
|
|
196
|
+
private readonly vaultBtcAddress: string;
|
|
197
|
+
private readonly vaultUtxo: string;
|
|
198
|
+
private readonly vaultUtxoValue: bigint;
|
|
137
199
|
|
|
138
|
-
readonly btcDestinationAddress: string;
|
|
139
|
-
readonly btcAmount: bigint;
|
|
140
|
-
readonly btcAmountSwap: bigint;
|
|
141
|
-
readonly btcAmountGas: bigint;
|
|
142
|
-
readonly minimumBtcFeeRate: number;
|
|
200
|
+
private readonly btcDestinationAddress: string;
|
|
201
|
+
private readonly btcAmount: bigint;
|
|
202
|
+
private readonly btcAmountSwap: bigint;
|
|
203
|
+
private readonly btcAmountGas: bigint;
|
|
143
204
|
|
|
144
|
-
readonly outputTotalSwap: bigint;
|
|
145
|
-
readonly outputSwapToken: string;
|
|
146
|
-
readonly outputTotalGas: bigint;
|
|
147
|
-
readonly outputGasToken: string;
|
|
205
|
+
private readonly outputTotalSwap: bigint;
|
|
206
|
+
private readonly outputSwapToken: string;
|
|
207
|
+
private readonly outputTotalGas: bigint;
|
|
208
|
+
private readonly outputGasToken: string;
|
|
148
209
|
|
|
149
|
-
readonly gasSwapFeeBtc: bigint;
|
|
150
|
-
readonly gasSwapFee: bigint;
|
|
210
|
+
private readonly gasSwapFeeBtc: bigint;
|
|
211
|
+
private readonly gasSwapFee: bigint;
|
|
151
212
|
|
|
152
|
-
readonly callerFeeShare: bigint;
|
|
153
|
-
readonly frontingFeeShare: bigint;
|
|
154
|
-
readonly executionFeeShare: bigint;
|
|
213
|
+
private readonly callerFeeShare: bigint;
|
|
214
|
+
private readonly frontingFeeShare: bigint;
|
|
215
|
+
private readonly executionFeeShare: bigint;
|
|
155
216
|
|
|
156
|
-
readonly
|
|
157
|
-
|
|
158
|
-
gasPricingInfo?: PriceInfoType;
|
|
217
|
+
private readonly gasPricingInfo?: PriceInfoType;
|
|
159
218
|
|
|
160
|
-
|
|
219
|
+
/**
|
|
220
|
+
* @internal
|
|
221
|
+
*/
|
|
222
|
+
readonly _genesisSmartChainBlockHeight: number;
|
|
223
|
+
/**
|
|
224
|
+
* @internal
|
|
225
|
+
*/
|
|
226
|
+
_senderAddress?: string;
|
|
227
|
+
/**
|
|
228
|
+
* @internal
|
|
229
|
+
*/
|
|
230
|
+
_claimTxId?: string;
|
|
231
|
+
/**
|
|
232
|
+
* @internal
|
|
233
|
+
*/
|
|
234
|
+
_frontTxId?: string;
|
|
235
|
+
/**
|
|
236
|
+
* @internal
|
|
237
|
+
*/
|
|
238
|
+
_data?: T["SpvVaultWithdrawalData"];
|
|
161
239
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
240
|
+
/**
|
|
241
|
+
* Minimum fee rate in sats/vB that the input bitcoin transaction needs to pay
|
|
242
|
+
*/
|
|
243
|
+
readonly minimumBtcFeeRate: number;
|
|
165
244
|
|
|
166
245
|
constructor(wrapper: SpvFromBTCWrapper<T>, init: SpvFromBTCSwapInit);
|
|
167
246
|
constructor(wrapper: SpvFromBTCWrapper<T>, obj: any);
|
|
@@ -169,7 +248,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
169
248
|
if(isSpvFromBTCSwapInit(initOrObject) && initOrObject.url!=null) initOrObject.url += "/frombtc_spv";
|
|
170
249
|
super(wrapper, initOrObject);
|
|
171
250
|
if(isSpvFromBTCSwapInit(initOrObject)) {
|
|
172
|
-
this.
|
|
251
|
+
this._state = SpvFromBTCSwapState.CREATED;
|
|
173
252
|
this.quoteId = initOrObject.quoteId;
|
|
174
253
|
this.recipient = initOrObject.recipient;
|
|
175
254
|
this.vaultOwner = initOrObject.vaultOwner;
|
|
@@ -193,9 +272,9 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
193
272
|
this.callerFeeShare = initOrObject.callerFeeShare;
|
|
194
273
|
this.frontingFeeShare = initOrObject.frontingFeeShare;
|
|
195
274
|
this.executionFeeShare = initOrObject.executionFeeShare;
|
|
196
|
-
this.
|
|
275
|
+
this._genesisSmartChainBlockHeight = initOrObject.genesisSmartChainBlockHeight;
|
|
197
276
|
this.gasPricingInfo = initOrObject.gasPricingInfo;
|
|
198
|
-
const vaultAddressType = toCoinselectAddressType(toOutputScript(this.wrapper.
|
|
277
|
+
const vaultAddressType = toCoinselectAddressType(toOutputScript(this.wrapper._options.bitcoinNetwork, this.vaultBtcAddress));
|
|
199
278
|
if(vaultAddressType!=="p2tr" && vaultAddressType!=="p2wpkh" && vaultAddressType!=="p2wsh")
|
|
200
279
|
throw new Error("Vault address type must be of witness type: p2tr, p2wpkh, p2wsh");
|
|
201
280
|
} else {
|
|
@@ -222,22 +301,26 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
222
301
|
this.callerFeeShare = BigInt(initOrObject.callerFeeShare);
|
|
223
302
|
this.frontingFeeShare = BigInt(initOrObject.frontingFeeShare);
|
|
224
303
|
this.executionFeeShare = BigInt(initOrObject.executionFeeShare);
|
|
225
|
-
this.
|
|
226
|
-
this.
|
|
227
|
-
this.
|
|
228
|
-
this.
|
|
304
|
+
this._genesisSmartChainBlockHeight = initOrObject.genesisSmartChainBlockHeight;
|
|
305
|
+
this._senderAddress = initOrObject.senderAddress;
|
|
306
|
+
this._claimTxId = initOrObject.claimTxId;
|
|
307
|
+
this._frontTxId = initOrObject.frontTxId;
|
|
229
308
|
this.gasPricingInfo = deserializePriceInfoType(initOrObject.gasPricingInfo);
|
|
230
|
-
if(initOrObject.data!=null) this.
|
|
309
|
+
if(initOrObject.data!=null) this._data = new this.wrapper._spvWithdrawalDataDeserializer(initOrObject.data);
|
|
231
310
|
}
|
|
232
311
|
this.tryCalculateSwapFee();
|
|
233
312
|
this.logger = getLogger("SPVFromBTC("+this.getId()+"): ");
|
|
234
313
|
}
|
|
235
314
|
|
|
315
|
+
/**
|
|
316
|
+
* @inheritDoc
|
|
317
|
+
* @internal
|
|
318
|
+
*/
|
|
236
319
|
protected upgradeVersion() { /*NOOP*/ }
|
|
237
320
|
|
|
238
321
|
/**
|
|
239
|
-
*
|
|
240
|
-
* @
|
|
322
|
+
* @inheritDoc
|
|
323
|
+
* @internal
|
|
241
324
|
*/
|
|
242
325
|
protected tryCalculateSwapFee() {
|
|
243
326
|
if(this.swapFeeBtc==null && this.swapFee!=null) {
|
|
@@ -246,7 +329,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
246
329
|
|
|
247
330
|
if(this.pricingInfo!=null && this.pricingInfo.swapPriceUSatPerToken==null) {
|
|
248
331
|
const priceUsdPerBtc = this.pricingInfo.realPriceUsdPerBitcoin;
|
|
249
|
-
this.pricingInfo = this.wrapper.
|
|
332
|
+
this.pricingInfo = this.wrapper._prices.recomputePriceInfoReceive(
|
|
250
333
|
this.chainIdentifier,
|
|
251
334
|
this.btcAmountSwap,
|
|
252
335
|
this.pricingInfo.satsBaseFee,
|
|
@@ -262,10 +345,13 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
262
345
|
//////////////////////////////
|
|
263
346
|
//// Pricing
|
|
264
347
|
|
|
348
|
+
/**
|
|
349
|
+
* @inheritDoc
|
|
350
|
+
*/
|
|
265
351
|
async refreshPriceData(): Promise<void> {
|
|
266
352
|
if(this.pricingInfo==null) return;
|
|
267
353
|
const usdPricePerBtc = this.pricingInfo.realPriceUsdPerBitcoin;
|
|
268
|
-
this.pricingInfo = await this.wrapper.
|
|
354
|
+
this.pricingInfo = await this.wrapper._prices.isValidAmountReceive(
|
|
269
355
|
this.chainIdentifier,
|
|
270
356
|
this.btcAmountSwap,
|
|
271
357
|
this.pricingInfo.satsBaseFee,
|
|
@@ -280,97 +366,198 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
280
366
|
//////////////////////////////
|
|
281
367
|
//// Getters & utils
|
|
282
368
|
|
|
369
|
+
/**
|
|
370
|
+
* @inheritDoc
|
|
371
|
+
* @internal
|
|
372
|
+
*/
|
|
283
373
|
_getInitiator(): string {
|
|
284
374
|
return this.recipient;
|
|
285
375
|
}
|
|
286
376
|
|
|
377
|
+
/**
|
|
378
|
+
* @inheritDoc
|
|
379
|
+
* @internal
|
|
380
|
+
*/
|
|
287
381
|
_getEscrowHash(): string | null {
|
|
288
|
-
return this.
|
|
382
|
+
return this._data?.btcTx?.txid ?? null;
|
|
289
383
|
}
|
|
290
384
|
|
|
385
|
+
/**
|
|
386
|
+
* @inheritDoc
|
|
387
|
+
*/
|
|
291
388
|
getId(): string {
|
|
292
|
-
return this.quoteId+this.
|
|
389
|
+
return this.quoteId+this._randomNonce;
|
|
293
390
|
}
|
|
294
391
|
|
|
392
|
+
/**
|
|
393
|
+
* @inheritDoc
|
|
394
|
+
*/
|
|
295
395
|
getQuoteExpiry(): number {
|
|
296
396
|
return this.expiry - 20*1000;
|
|
297
397
|
}
|
|
298
398
|
|
|
299
|
-
|
|
300
|
-
|
|
399
|
+
/**
|
|
400
|
+
* @inheritDoc
|
|
401
|
+
* @internal
|
|
402
|
+
*/
|
|
403
|
+
_verifyQuoteDefinitelyExpired(): Promise<boolean> {
|
|
404
|
+
return Promise.resolve(this.expiry<Date.now());
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* @inheritDoc
|
|
409
|
+
* @internal
|
|
410
|
+
*/
|
|
411
|
+
_verifyQuoteValid(): Promise<boolean> {
|
|
412
|
+
return Promise.resolve(this.expiry>Date.now() && (this._state===SpvFromBTCSwapState.CREATED || this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED));
|
|
301
413
|
}
|
|
302
414
|
|
|
415
|
+
/**
|
|
416
|
+
* @inheritDoc
|
|
417
|
+
*/
|
|
303
418
|
getOutputAddress(): string | null {
|
|
304
419
|
return this.recipient;
|
|
305
420
|
}
|
|
306
421
|
|
|
422
|
+
/**
|
|
423
|
+
* @inheritDoc
|
|
424
|
+
*/
|
|
307
425
|
getOutputTxId(): string | null {
|
|
308
|
-
return this.
|
|
426
|
+
return this._frontTxId ?? this._claimTxId ?? null;
|
|
309
427
|
}
|
|
310
428
|
|
|
429
|
+
/**
|
|
430
|
+
* @inheritDoc
|
|
431
|
+
*/
|
|
311
432
|
getInputAddress(): string | null {
|
|
312
|
-
return this.
|
|
433
|
+
return this._senderAddress ?? null;
|
|
313
434
|
}
|
|
314
435
|
|
|
436
|
+
/**
|
|
437
|
+
* @inheritDoc
|
|
438
|
+
*/
|
|
315
439
|
getInputTxId(): string | null {
|
|
316
|
-
return this.
|
|
440
|
+
return this._data?.btcTx?.txid ?? null;
|
|
317
441
|
}
|
|
318
442
|
|
|
443
|
+
/**
|
|
444
|
+
* @inheritDoc
|
|
445
|
+
*/
|
|
319
446
|
requiresAction(): boolean {
|
|
320
|
-
return this.
|
|
447
|
+
return this._state===SpvFromBTCSwapState.BTC_TX_CONFIRMED;
|
|
321
448
|
}
|
|
322
449
|
|
|
450
|
+
/**
|
|
451
|
+
* @inheritDoc
|
|
452
|
+
*/
|
|
323
453
|
isFinished(): boolean {
|
|
324
|
-
return this.
|
|
454
|
+
return this._state===SpvFromBTCSwapState.CLAIMED || this._state===SpvFromBTCSwapState.QUOTE_EXPIRED || this._state===SpvFromBTCSwapState.FAILED;
|
|
325
455
|
}
|
|
326
456
|
|
|
457
|
+
/**
|
|
458
|
+
* @inheritDoc
|
|
459
|
+
*/
|
|
327
460
|
isClaimable(): boolean {
|
|
328
|
-
return this.
|
|
461
|
+
return this._state===SpvFromBTCSwapState.BTC_TX_CONFIRMED;
|
|
329
462
|
}
|
|
330
463
|
|
|
464
|
+
/**
|
|
465
|
+
* @inheritDoc
|
|
466
|
+
*/
|
|
331
467
|
isSuccessful(): boolean {
|
|
332
|
-
return this.
|
|
468
|
+
return this._state===SpvFromBTCSwapState.FRONTED || this._state===SpvFromBTCSwapState.CLAIMED;
|
|
333
469
|
}
|
|
334
470
|
|
|
471
|
+
/**
|
|
472
|
+
* @inheritDoc
|
|
473
|
+
*/
|
|
335
474
|
isFailed(): boolean {
|
|
336
|
-
return this.
|
|
475
|
+
return this._state===SpvFromBTCSwapState.FAILED || this._state===SpvFromBTCSwapState.DECLINED || this._state===SpvFromBTCSwapState.CLOSED;
|
|
337
476
|
}
|
|
338
477
|
|
|
478
|
+
/**
|
|
479
|
+
* @inheritDoc
|
|
480
|
+
*/
|
|
339
481
|
isQuoteExpired(): boolean {
|
|
340
|
-
return this.
|
|
482
|
+
return this._state===SpvFromBTCSwapState.QUOTE_EXPIRED;
|
|
341
483
|
}
|
|
342
484
|
|
|
485
|
+
/**
|
|
486
|
+
* @inheritDoc
|
|
487
|
+
*/
|
|
343
488
|
isQuoteSoftExpired(): boolean {
|
|
344
|
-
return this.
|
|
489
|
+
return this._state===SpvFromBTCSwapState.QUOTE_EXPIRED || this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Returns the data about used spv vault (UTXO-controlled vault) to perform the swap
|
|
494
|
+
*/
|
|
495
|
+
getSpvVaultData(): {
|
|
496
|
+
owner: string,
|
|
497
|
+
vaultId: bigint,
|
|
498
|
+
utxo: string
|
|
499
|
+
} {
|
|
500
|
+
return {
|
|
501
|
+
owner: this.vaultOwner,
|
|
502
|
+
vaultId: this.vaultId,
|
|
503
|
+
utxo: this.vaultUtxo
|
|
504
|
+
}
|
|
345
505
|
}
|
|
346
506
|
|
|
347
507
|
|
|
348
508
|
//////////////////////////////
|
|
349
509
|
//// Amounts & fees
|
|
350
510
|
|
|
511
|
+
/**
|
|
512
|
+
* Returns the input BTC amount in sats without any fees
|
|
513
|
+
*
|
|
514
|
+
* @internal
|
|
515
|
+
*/
|
|
351
516
|
protected getInputSwapAmountWithoutFee(): bigint {
|
|
352
517
|
return (this.btcAmountSwap - this.swapFeeBtc) * 100_000n / (100_000n + this.callerFeeShare + this.frontingFeeShare + this.executionFeeShare);
|
|
353
518
|
}
|
|
354
519
|
|
|
520
|
+
/**
|
|
521
|
+
* Returns the input gas BTC amount in sats without any fees
|
|
522
|
+
*
|
|
523
|
+
* @internal
|
|
524
|
+
*/
|
|
355
525
|
protected getInputGasAmountWithoutFee(): bigint {
|
|
356
526
|
return (this.btcAmountGas - this.gasSwapFeeBtc) * 100_000n / (100_000n + this.callerFeeShare + this.frontingFeeShare);
|
|
357
527
|
}
|
|
358
528
|
|
|
529
|
+
/**
|
|
530
|
+
* Returns to total input BTC amount in sats without any fees (this is BTC amount for the swap + BTC amount
|
|
531
|
+
* for the gas drop).
|
|
532
|
+
*
|
|
533
|
+
* @internal
|
|
534
|
+
*/
|
|
359
535
|
protected getInputAmountWithoutFee(): bigint {
|
|
360
536
|
return this.getInputSwapAmountWithoutFee() + this.getInputGasAmountWithoutFee();
|
|
361
537
|
}
|
|
362
538
|
|
|
539
|
+
/**
|
|
540
|
+
* Returns the swap output amount without any fees, this value is therefore always higher than
|
|
541
|
+
* the actual received output.
|
|
542
|
+
*
|
|
543
|
+
* @internal
|
|
544
|
+
*/
|
|
363
545
|
protected getOutputWithoutFee(): TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true> {
|
|
364
546
|
return toTokenAmount(
|
|
365
547
|
(this.outputTotalSwap * (100_000n + this.callerFeeShare + this.frontingFeeShare + this.executionFeeShare) / 100_000n) + (this.swapFee ?? 0n),
|
|
366
|
-
this.wrapper.
|
|
548
|
+
this.wrapper._tokens[this.outputSwapToken], this.wrapper._prices, this.pricingInfo
|
|
367
549
|
);
|
|
368
550
|
}
|
|
369
551
|
|
|
552
|
+
/**
|
|
553
|
+
* Returns the swap fee charged by the intermediary (LP) on this swap
|
|
554
|
+
*
|
|
555
|
+
* @internal
|
|
556
|
+
*/
|
|
370
557
|
protected getSwapFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
371
558
|
if(this.pricingInfo==null) throw new Error("No pricing info known, cannot estimate fee!");
|
|
372
559
|
|
|
373
|
-
const outputToken = this.wrapper.
|
|
560
|
+
const outputToken = this.wrapper._tokens[this.outputSwapToken];
|
|
374
561
|
const gasSwapFeeInOutputToken = this.gasSwapFeeBtc
|
|
375
562
|
* (10n ** BigInt(outputToken.decimals))
|
|
376
563
|
* 1_000_000n
|
|
@@ -380,38 +567,44 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
380
567
|
const swapFeePPM = feeWithoutBaseFee * 1000000n / (this.btcAmount - this.swapFeeBtc - this.gasSwapFeeBtc);
|
|
381
568
|
|
|
382
569
|
const amountInSrcToken = toTokenAmount(
|
|
383
|
-
this.swapFeeBtc + this.gasSwapFeeBtc, BitcoinTokens.BTC, this.wrapper.
|
|
570
|
+
this.swapFeeBtc + this.gasSwapFeeBtc, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo
|
|
384
571
|
);
|
|
385
572
|
return {
|
|
386
573
|
amountInSrcToken,
|
|
387
|
-
amountInDstToken: toTokenAmount(this.swapFee + gasSwapFeeInOutputToken, outputToken, this.wrapper.
|
|
574
|
+
amountInDstToken: toTokenAmount(this.swapFee + gasSwapFeeInOutputToken, outputToken, this.wrapper._prices, this.pricingInfo),
|
|
388
575
|
currentUsdValue: amountInSrcToken.currentUsdValue,
|
|
389
576
|
usdValue: amountInSrcToken.usdValue,
|
|
390
577
|
pastUsdValue: amountInSrcToken.pastUsdValue,
|
|
391
578
|
composition: {
|
|
392
|
-
base: toTokenAmount(this.pricingInfo.satsBaseFee, BitcoinTokens.BTC, this.wrapper.
|
|
579
|
+
base: toTokenAmount(this.pricingInfo.satsBaseFee, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo),
|
|
393
580
|
percentage: ppmToPercentage(swapFeePPM)
|
|
394
581
|
}
|
|
395
582
|
};
|
|
396
583
|
}
|
|
397
584
|
|
|
585
|
+
/**
|
|
586
|
+
* Returns the fee to be paid to watchtowers on the destination chain to automatically
|
|
587
|
+
* process and settle this swap without requiring any user interaction
|
|
588
|
+
*
|
|
589
|
+
* @internal
|
|
590
|
+
*/
|
|
398
591
|
protected getWatchtowerFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
399
592
|
if(this.pricingInfo==null) throw new Error("No pricing info known, cannot estimate fee!");
|
|
400
593
|
|
|
401
594
|
const totalFeeShare = this.callerFeeShare + this.frontingFeeShare;
|
|
402
|
-
const outputToken = this.wrapper.
|
|
595
|
+
const outputToken = this.wrapper._tokens[this.outputSwapToken];
|
|
403
596
|
const watchtowerFeeInOutputToken = this.getInputGasAmountWithoutFee() * totalFeeShare
|
|
404
597
|
* (10n ** BigInt(outputToken.decimals))
|
|
405
598
|
* 1_000_000n
|
|
406
599
|
/ this.pricingInfo.swapPriceUSatPerToken
|
|
407
600
|
/ 100_000n;
|
|
408
601
|
const feeBtc = this.getInputAmountWithoutFee() * (totalFeeShare + this.executionFeeShare) / 100_000n;
|
|
409
|
-
const amountInSrcToken = toTokenAmount(feeBtc, BitcoinTokens.BTC, this.wrapper.
|
|
602
|
+
const amountInSrcToken = toTokenAmount(feeBtc, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo);
|
|
410
603
|
return {
|
|
411
604
|
amountInSrcToken,
|
|
412
605
|
amountInDstToken: toTokenAmount(
|
|
413
606
|
(this.outputTotalSwap * (totalFeeShare + this.executionFeeShare) / 100_000n) + watchtowerFeeInOutputToken,
|
|
414
|
-
outputToken, this.wrapper.
|
|
607
|
+
outputToken, this.wrapper._prices, this.pricingInfo
|
|
415
608
|
),
|
|
416
609
|
currentUsdValue: amountInSrcToken.currentUsdValue,
|
|
417
610
|
usdValue: amountInSrcToken.usdValue,
|
|
@@ -419,19 +612,22 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
419
612
|
};
|
|
420
613
|
}
|
|
421
614
|
|
|
615
|
+
/**
|
|
616
|
+
* @inheritDoc
|
|
617
|
+
*/
|
|
422
618
|
getFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
423
619
|
const swapFee = this.getSwapFee();
|
|
424
620
|
const watchtowerFee = this.getWatchtowerFee();
|
|
425
621
|
|
|
426
622
|
const amountInSrcToken = toTokenAmount(
|
|
427
623
|
swapFee.amountInSrcToken.rawAmount + watchtowerFee.amountInSrcToken.rawAmount,
|
|
428
|
-
BitcoinTokens.BTC, this.wrapper.
|
|
624
|
+
BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo
|
|
429
625
|
);
|
|
430
626
|
return {
|
|
431
627
|
amountInSrcToken,
|
|
432
628
|
amountInDstToken: toTokenAmount(
|
|
433
629
|
swapFee.amountInDstToken.rawAmount + watchtowerFee.amountInDstToken.rawAmount,
|
|
434
|
-
this.wrapper.
|
|
630
|
+
this.wrapper._tokens[this.outputSwapToken], this.wrapper._prices, this.pricingInfo
|
|
435
631
|
),
|
|
436
632
|
currentUsdValue: amountInSrcToken.currentUsdValue,
|
|
437
633
|
usdValue: amountInSrcToken.usdValue,
|
|
@@ -439,6 +635,9 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
439
635
|
};
|
|
440
636
|
}
|
|
441
637
|
|
|
638
|
+
/**
|
|
639
|
+
* @inheritDoc
|
|
640
|
+
*/
|
|
442
641
|
getFeeBreakdown(): [
|
|
443
642
|
{type: FeeType.SWAP, fee: Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>>},
|
|
444
643
|
{type: FeeType.NETWORK_OUTPUT, fee: Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>>}
|
|
@@ -455,38 +654,63 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
455
654
|
];
|
|
456
655
|
}
|
|
457
656
|
|
|
657
|
+
/**
|
|
658
|
+
* @inheritDoc
|
|
659
|
+
*/
|
|
458
660
|
getOutputToken(): SCToken<T["ChainId"]> {
|
|
459
|
-
return this.wrapper.
|
|
661
|
+
return this.wrapper._tokens[this.outputSwapToken];
|
|
460
662
|
}
|
|
461
663
|
|
|
664
|
+
/**
|
|
665
|
+
* @inheritDoc
|
|
666
|
+
*/
|
|
462
667
|
getOutput(): TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true> {
|
|
463
|
-
return toTokenAmount(this.outputTotalSwap, this.wrapper.
|
|
668
|
+
return toTokenAmount(this.outputTotalSwap, this.wrapper._tokens[this.outputSwapToken], this.wrapper._prices, this.pricingInfo);
|
|
464
669
|
}
|
|
465
670
|
|
|
671
|
+
/**
|
|
672
|
+
* @inheritDoc
|
|
673
|
+
*/
|
|
466
674
|
getGasDropOutput(): TokenAmount<T["ChainId"], SCToken<T["ChainId"]>, true> {
|
|
467
|
-
return toTokenAmount(this.outputTotalGas, this.wrapper.
|
|
675
|
+
return toTokenAmount(this.outputTotalGas, this.wrapper._tokens[this.outputGasToken], this.wrapper._prices, this.gasPricingInfo);
|
|
468
676
|
}
|
|
469
677
|
|
|
678
|
+
/**
|
|
679
|
+
* @inheritDoc
|
|
680
|
+
*/
|
|
470
681
|
getInputWithoutFee(): TokenAmount<T["ChainId"], BtcToken<false>, true> {
|
|
471
|
-
return toTokenAmount(this.getInputAmountWithoutFee(), BitcoinTokens.BTC, this.wrapper.
|
|
682
|
+
return toTokenAmount(this.getInputAmountWithoutFee(), BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo);
|
|
472
683
|
}
|
|
473
684
|
|
|
685
|
+
/**
|
|
686
|
+
* @inheritDoc
|
|
687
|
+
*/
|
|
474
688
|
getInputToken(): BtcToken<false> {
|
|
475
689
|
return BitcoinTokens.BTC;
|
|
476
690
|
}
|
|
477
691
|
|
|
692
|
+
/**
|
|
693
|
+
* @inheritDoc
|
|
694
|
+
*/
|
|
478
695
|
getInput(): TokenAmount<T["ChainId"], BtcToken<false>, true> {
|
|
479
|
-
return toTokenAmount(this.btcAmount, BitcoinTokens.BTC, this.wrapper.
|
|
696
|
+
return toTokenAmount(this.btcAmount, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo);
|
|
480
697
|
}
|
|
481
698
|
|
|
482
699
|
|
|
483
700
|
//////////////////////////////
|
|
484
701
|
//// Bitcoin tx
|
|
485
702
|
|
|
703
|
+
/**
|
|
704
|
+
* @inheritDoc
|
|
705
|
+
*/
|
|
486
706
|
getRequiredConfirmationsCount(): number {
|
|
487
707
|
return this.vaultRequiredConfirmations;
|
|
488
708
|
}
|
|
489
709
|
|
|
710
|
+
/**
|
|
711
|
+
* Returns raw transaction details that can be used to manually create a swap PSBT. It is better to use
|
|
712
|
+
* the {@link getPsbt} or {@link getFundedPsbt} function retrieve an already prepared PSBT.
|
|
713
|
+
*/
|
|
490
714
|
async getTransactionDetails(): Promise<{
|
|
491
715
|
in0txid: string,
|
|
492
716
|
in0vout: number,
|
|
@@ -501,11 +725,11 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
501
725
|
}> {
|
|
502
726
|
const [txId, voutStr] = this.vaultUtxo.split(":");
|
|
503
727
|
|
|
504
|
-
const vaultScript = toOutputScript(this.wrapper.
|
|
728
|
+
const vaultScript = toOutputScript(this.wrapper._options.bitcoinNetwork, this.vaultBtcAddress);
|
|
505
729
|
|
|
506
|
-
const out2script = toOutputScript(this.wrapper.
|
|
730
|
+
const out2script = toOutputScript(this.wrapper._options.bitcoinNetwork, this.btcDestinationAddress);
|
|
507
731
|
|
|
508
|
-
const opReturnData = this.wrapper.
|
|
732
|
+
const opReturnData = this.wrapper._contract.toOpReturnData(
|
|
509
733
|
this.recipient,
|
|
510
734
|
[
|
|
511
735
|
this.outputTotalSwap / this.vaultTokenMultipliers[0],
|
|
@@ -539,9 +763,9 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
539
763
|
}
|
|
540
764
|
|
|
541
765
|
/**
|
|
542
|
-
* Returns the raw PSBT (not funded), the wallet should fund the PSBT (add its inputs)
|
|
543
|
-
* 2nd input (input 1 - indexing from 0) to the value returned in `in1sequence`, sign the PSBT and then pass
|
|
544
|
-
* it back to the
|
|
766
|
+
* Returns the raw PSBT (not funded), the wallet should fund the PSBT (add its inputs) and importantly **set the nSequence field of the
|
|
767
|
+
* 2nd input** (input 1 - indexing from 0) to the value returned in `in1sequence`, sign the PSBT and then pass
|
|
768
|
+
* it back to the swap with {@link submitPsbt} function.
|
|
545
769
|
*/
|
|
546
770
|
async getPsbt(): Promise<{
|
|
547
771
|
psbt: Transaction,
|
|
@@ -586,13 +810,9 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
586
810
|
}
|
|
587
811
|
|
|
588
812
|
/**
|
|
589
|
-
*
|
|
590
|
-
* also returns inputs indices that need to be signed by the wallet before submitting the PSBT back to the SDK with
|
|
591
|
-
* `swap.submitPsbt()`
|
|
813
|
+
* Note that when passing the `feeRate` argument, the fee must be at least {@link minimumBtcFeeRate} sats/vB.
|
|
592
814
|
*
|
|
593
|
-
* @
|
|
594
|
-
* @param feeRate Optional fee rate for the transaction, needs to be at least as big as {minimumBtcFeeRate} field
|
|
595
|
-
* @param additionalOutputs additional outputs to add to the PSBT - can be used to collect fees from users
|
|
815
|
+
* @inheritDoc
|
|
596
816
|
*/
|
|
597
817
|
async getFundedPsbt(
|
|
598
818
|
_bitcoinWallet: IBitcoinWallet | MinimalBitcoinWalletInterface,
|
|
@@ -604,7 +824,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
604
824
|
psbtBase64: string,
|
|
605
825
|
signInputs: number[]
|
|
606
826
|
}> {
|
|
607
|
-
const bitcoinWallet: IBitcoinWallet = toBitcoinWallet(_bitcoinWallet, this.wrapper.
|
|
827
|
+
const bitcoinWallet: IBitcoinWallet = toBitcoinWallet(_bitcoinWallet, this.wrapper._btcRpc, this.wrapper._options.bitcoinNetwork);
|
|
608
828
|
if(feeRate!=null) {
|
|
609
829
|
if(feeRate<this.minimumBtcFeeRate) throw new Error("Bitcoin tx fee needs to be at least "+this.minimumBtcFeeRate+" sats/vB");
|
|
610
830
|
} else {
|
|
@@ -614,7 +834,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
614
834
|
if(additionalOutputs!=null) additionalOutputs.forEach(output => {
|
|
615
835
|
psbt.addOutput({
|
|
616
836
|
amount: output.amount,
|
|
617
|
-
script: (output as {outputScript: Uint8Array}).outputScript ?? toOutputScript(this.wrapper.
|
|
837
|
+
script: (output as {outputScript: Uint8Array}).outputScript ?? toOutputScript(this.wrapper._options.bitcoinNetwork, (output as {address: string}).address)
|
|
618
838
|
});
|
|
619
839
|
});
|
|
620
840
|
psbt = await bitcoinWallet.fundPsbt(psbt, feeRate);
|
|
@@ -634,9 +854,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
634
854
|
}
|
|
635
855
|
|
|
636
856
|
/**
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
* @param _psbt A psbt - either a Transaction object or a hex or base64 encoded PSBT string
|
|
857
|
+
* @inheritDoc
|
|
640
858
|
*/
|
|
641
859
|
async submitPsbt(_psbt: Transaction | string): Promise<string> {
|
|
642
860
|
const psbt = parsePsbtTransaction(_psbt);
|
|
@@ -647,7 +865,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
647
865
|
}
|
|
648
866
|
|
|
649
867
|
//Ensure valid state
|
|
650
|
-
if(this.
|
|
868
|
+
if(this._state!==SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && this._state!==SpvFromBTCSwapState.CREATED) {
|
|
651
869
|
throw new Error("Invalid swap state!");
|
|
652
870
|
}
|
|
653
871
|
if(this.url==null) throw new Error("LP URL not known, cannot submit PSBT!");
|
|
@@ -658,8 +876,8 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
658
876
|
throw new Error("Legacy (non-segwit) inputs are not allowed in the transaction!");
|
|
659
877
|
psbt.finalizeIdx(i);
|
|
660
878
|
}
|
|
661
|
-
const btcTx = await this.wrapper.
|
|
662
|
-
const data = await this.wrapper.
|
|
879
|
+
const btcTx = await this.wrapper._btcRpc.parseTransaction(Buffer.from(psbt.toBytes(true)).toString("hex"));
|
|
880
|
+
const data = await this.wrapper._contract.getWithdrawalData(btcTx);
|
|
663
881
|
|
|
664
882
|
this.logger.debug("submitPsbt(): parsed withdrawal data: ", data);
|
|
665
883
|
|
|
@@ -673,7 +891,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
673
891
|
data.executionFeeRate!==this.executionFeeShare ||
|
|
674
892
|
data.getSpentVaultUtxo()!==this.vaultUtxo ||
|
|
675
893
|
BigInt(data.getNewVaultBtcAmount())!==this.vaultUtxoValue ||
|
|
676
|
-
!data.getNewVaultScript().equals(toOutputScript(this.wrapper.
|
|
894
|
+
!data.getNewVaultScript().equals(toOutputScript(this.wrapper._options.bitcoinNetwork, this.vaultBtcAddress)) ||
|
|
677
895
|
data.getExecutionData()!=null
|
|
678
896
|
) {
|
|
679
897
|
throw new Error("Invalid withdrawal tx data submitted!");
|
|
@@ -684,19 +902,19 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
684
902
|
if(
|
|
685
903
|
lpOutput.script==null ||
|
|
686
904
|
lpOutput.amount!==this.btcAmount ||
|
|
687
|
-
!toOutputScript(this.wrapper.
|
|
905
|
+
!toOutputScript(this.wrapper._options.bitcoinNetwork, this.btcDestinationAddress).equals(Buffer.from(lpOutput.script))
|
|
688
906
|
) {
|
|
689
907
|
throw new Error("Invalid LP bitcoin output in transaction!");
|
|
690
908
|
}
|
|
691
909
|
|
|
692
910
|
//Verify vault utxo not spent yet
|
|
693
|
-
if(await this.wrapper.
|
|
911
|
+
if(await this.wrapper._btcRpc.isSpent(this.vaultUtxo)) {
|
|
694
912
|
throw new Error("Vault UTXO already spent, please create new swap!");
|
|
695
913
|
}
|
|
696
914
|
|
|
697
915
|
//Verify tx is parsable by the contract
|
|
698
916
|
try {
|
|
699
|
-
await this.wrapper.
|
|
917
|
+
await this.wrapper._contract.checkWithdrawalTx(data);
|
|
700
918
|
} catch (e: any) {
|
|
701
919
|
throw new Error("Transaction not parsable by the contract: "+(e.message ?? e.toString()));
|
|
702
920
|
}
|
|
@@ -706,7 +924,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
706
924
|
throw new Error("Quote expired!");
|
|
707
925
|
}
|
|
708
926
|
|
|
709
|
-
this.
|
|
927
|
+
this._data = data;
|
|
710
928
|
this.initiated = true;
|
|
711
929
|
await this._saveAndEmit(SpvFromBTCSwapState.SIGNED);
|
|
712
930
|
|
|
@@ -725,16 +943,22 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
725
943
|
throw e;
|
|
726
944
|
}
|
|
727
945
|
|
|
728
|
-
return this.
|
|
946
|
+
return this._data.getTxId();
|
|
729
947
|
}
|
|
730
948
|
|
|
949
|
+
/**
|
|
950
|
+
* @inheritDoc
|
|
951
|
+
*/
|
|
731
952
|
async estimateBitcoinFee(_bitcoinWallet: IBitcoinWallet | MinimalBitcoinWalletInterface, feeRate?: number): Promise<TokenAmount<any, BtcToken<false>, true> | null> {
|
|
732
|
-
const bitcoinWallet: IBitcoinWallet = toBitcoinWallet(_bitcoinWallet, this.wrapper.
|
|
953
|
+
const bitcoinWallet: IBitcoinWallet = toBitcoinWallet(_bitcoinWallet, this.wrapper._btcRpc, this.wrapper._options.bitcoinNetwork);
|
|
733
954
|
const txFee = await bitcoinWallet.getFundedPsbtFee((await this.getPsbt()).psbt, feeRate);
|
|
734
955
|
if(txFee==null) return null;
|
|
735
|
-
return toTokenAmount(BigInt(txFee), BitcoinTokens.BTC, this.wrapper.
|
|
956
|
+
return toTokenAmount(BigInt(txFee), BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo);
|
|
736
957
|
}
|
|
737
958
|
|
|
959
|
+
/**
|
|
960
|
+
* @inheritDoc
|
|
961
|
+
*/
|
|
738
962
|
async sendBitcoinTransaction(wallet: IBitcoinWallet | MinimalBitcoinWalletInterfaceWithSigner, feeRate?: number): Promise<string> {
|
|
739
963
|
const {psbt, psbtBase64, psbtHex, signInputs} = await this.getFundedPsbt(wallet, feeRate);
|
|
740
964
|
let signedPsbt: Transaction | string;
|
|
@@ -749,14 +973,15 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
749
973
|
}
|
|
750
974
|
|
|
751
975
|
/**
|
|
752
|
-
* Executes the swap with the provided bitcoin wallet
|
|
976
|
+
* Executes the swap with the provided bitcoin wallet
|
|
753
977
|
*
|
|
754
978
|
* @param wallet Bitcoin wallet to use to sign the bitcoin transaction
|
|
755
979
|
* @param callbacks Callbacks to track the progress of the swap
|
|
756
980
|
* @param options Optional options for the swap like feeRate, AbortSignal, and timeouts/intervals
|
|
757
981
|
*
|
|
758
982
|
* @returns {boolean} Whether a swap was settled automatically by swap watchtowers or requires manual claim by the
|
|
759
|
-
* user, in case `false` is returned the user should call
|
|
983
|
+
* user, in case `false` is returned the user should call the {@link claim} function to settle the swap on the
|
|
984
|
+
* destination manually
|
|
760
985
|
*/
|
|
761
986
|
async execute(
|
|
762
987
|
wallet: IBitcoinWallet | MinimalBitcoinWalletInterfaceWithSigner,
|
|
@@ -773,23 +998,23 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
773
998
|
maxWaitTillAutomaticSettlementSeconds?: number
|
|
774
999
|
}
|
|
775
1000
|
): Promise<boolean> {
|
|
776
|
-
if(this.
|
|
777
|
-
if(this.
|
|
778
|
-
if(this.
|
|
779
|
-
if(this.
|
|
780
|
-
if(this.
|
|
1001
|
+
if(this._state===SpvFromBTCSwapState.CLOSED) throw new Error("Swap encountered a catastrophic failure!");
|
|
1002
|
+
if(this._state===SpvFromBTCSwapState.FAILED) throw new Error("Swap failed!");
|
|
1003
|
+
if(this._state===SpvFromBTCSwapState.DECLINED) throw new Error("Swap execution already declined by the LP!");
|
|
1004
|
+
if(this._state===SpvFromBTCSwapState.QUOTE_EXPIRED) throw new Error("Swap quote expired!");
|
|
1005
|
+
if(this._state===SpvFromBTCSwapState.CLAIMED || this._state===SpvFromBTCSwapState.FRONTED) throw new Error("Swap already settled or fronted!");
|
|
781
1006
|
|
|
782
|
-
if(this.
|
|
1007
|
+
if(this._state===SpvFromBTCSwapState.CREATED) {
|
|
783
1008
|
const txId = await this.sendBitcoinTransaction(wallet, options?.feeRate);
|
|
784
1009
|
if(callbacks?.onSourceTransactionSent!=null) callbacks.onSourceTransactionSent(txId);
|
|
785
1010
|
}
|
|
786
|
-
if(this.
|
|
1011
|
+
if(this._state===SpvFromBTCSwapState.POSTED || this._state===SpvFromBTCSwapState.BROADCASTED) {
|
|
787
1012
|
const txId = await this.waitForBitcoinTransaction(callbacks?.onSourceTransactionConfirmationStatus, options?.btcTxCheckIntervalSeconds, options?.abortSignal);
|
|
788
1013
|
if (callbacks?.onSourceTransactionConfirmed != null) callbacks.onSourceTransactionConfirmed(txId);
|
|
789
1014
|
}
|
|
790
1015
|
// @ts-ignore
|
|
791
|
-
if(this.
|
|
792
|
-
if(this.
|
|
1016
|
+
if(this._state===SpvFromBTCSwapState.CLAIMED || this._state===SpvFromBTCSwapState.FRONTED) return true;
|
|
1017
|
+
if(this._state===SpvFromBTCSwapState.BTC_TX_CONFIRMED) {
|
|
793
1018
|
const success = await this.waitTillClaimedOrFronted(options?.maxWaitTillAutomaticSettlementSeconds ?? 60, options?.abortSignal);
|
|
794
1019
|
if(success && callbacks?.onSwapSettled!=null) callbacks.onSwapSettled(this.getOutputTxId()!);
|
|
795
1020
|
return success;
|
|
@@ -798,14 +1023,24 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
798
1023
|
throw new Error("Unexpected state reached!");
|
|
799
1024
|
}
|
|
800
1025
|
|
|
1026
|
+
/**
|
|
1027
|
+
* @inheritDoc
|
|
1028
|
+
*
|
|
1029
|
+
* @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
|
|
1030
|
+
* if not provided a raw PSBT is returned instead which necessitates the implementor to manually add
|
|
1031
|
+
* inputs to the bitcoin transaction and **set the nSequence field of the 2nd input** (input 1 -
|
|
1032
|
+
* indexing from 0) to the value returned in `in1sequence`
|
|
1033
|
+
*/
|
|
801
1034
|
async txsExecute(options?: {
|
|
802
1035
|
bitcoinWallet?: MinimalBitcoinWalletInterface,
|
|
803
|
-
})
|
|
804
|
-
|
|
805
|
-
|
|
1036
|
+
}): Promise<[
|
|
1037
|
+
SwapExecutionActionBitcoin<"RAW_PSBT" | "FUNDED_PSBT">
|
|
1038
|
+
]> {
|
|
1039
|
+
if(this._state===SpvFromBTCSwapState.CREATED) {
|
|
1040
|
+
if (!await this._verifyQuoteValid()) throw new Error("Quote already expired or close to expiry!");
|
|
806
1041
|
return [
|
|
807
1042
|
{
|
|
808
|
-
name: "Payment"
|
|
1043
|
+
name: "Payment",
|
|
809
1044
|
description: "Send funds to the bitcoin swap address",
|
|
810
1045
|
chain: "BITCOIN",
|
|
811
1046
|
txs: [
|
|
@@ -826,6 +1061,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
826
1061
|
|
|
827
1062
|
/**
|
|
828
1063
|
* Checks whether a bitcoin payment was already made, returns the payment or null when no payment has been made.
|
|
1064
|
+
* @internal
|
|
829
1065
|
*/
|
|
830
1066
|
protected async getBitcoinPayment(): Promise<{
|
|
831
1067
|
txId: string,
|
|
@@ -833,9 +1069,9 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
833
1069
|
targetConfirmations: number,
|
|
834
1070
|
inputAddresses?: string[]
|
|
835
1071
|
} | null> {
|
|
836
|
-
if(this.
|
|
1072
|
+
if(this._data?.btcTx?.txid==null) return null;
|
|
837
1073
|
|
|
838
|
-
const result = await this.wrapper.
|
|
1074
|
+
const result = await this.wrapper._btcRpc.getTransaction(this._data?.btcTx?.txid);
|
|
839
1075
|
if(result==null) return null;
|
|
840
1076
|
|
|
841
1077
|
return {
|
|
@@ -847,12 +1083,9 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
847
1083
|
}
|
|
848
1084
|
|
|
849
1085
|
/**
|
|
850
|
-
*
|
|
851
|
-
*
|
|
852
|
-
*
|
|
853
|
-
* @param checkIntervalSeconds How often to check the bitcoin transaction
|
|
854
|
-
* @param abortSignal Abort signal
|
|
855
|
-
* @throws {Error} if in invalid state (must be CLAIM_COMMITED)
|
|
1086
|
+
* @inheritDoc
|
|
1087
|
+
* @throws {Error} if in invalid state (must be {@link SpvFromBTCSwapState.POSTED} or
|
|
1088
|
+
* {@link SpvFromBTCSwapState.BROADCASTED} states)
|
|
856
1089
|
*/
|
|
857
1090
|
async waitForBitcoinTransaction(
|
|
858
1091
|
updateCallback?: (txId?: string, confirmations?: number, targetConfirmations?: number, txEtaMs?: number) => void,
|
|
@@ -860,25 +1093,25 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
860
1093
|
abortSignal?: AbortSignal
|
|
861
1094
|
): Promise<string> {
|
|
862
1095
|
if(
|
|
863
|
-
this.
|
|
864
|
-
this.
|
|
865
|
-
!(this.
|
|
1096
|
+
this._state!==SpvFromBTCSwapState.POSTED &&
|
|
1097
|
+
this._state!==SpvFromBTCSwapState.BROADCASTED &&
|
|
1098
|
+
!(this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && this.initiated)
|
|
866
1099
|
) throw new Error("Must be in POSTED or BROADCASTED state!");
|
|
867
|
-
if(this.
|
|
1100
|
+
if(this._data==null) throw new Error("Expected swap to have withdrawal data filled!");
|
|
868
1101
|
|
|
869
|
-
const result = await this.wrapper.
|
|
870
|
-
this.
|
|
1102
|
+
const result = await this.wrapper._btcRpc.waitForTransaction(
|
|
1103
|
+
this._data.btcTx.txid,
|
|
871
1104
|
this.vaultRequiredConfirmations,
|
|
872
1105
|
(btcTx?: BtcTxWithBlockheight, txEtaMs?: number) => {
|
|
873
1106
|
if(updateCallback!=null) updateCallback(btcTx?.txid, btcTx?.confirmations, this.vaultRequiredConfirmations, txEtaMs);
|
|
874
1107
|
if(btcTx==null) return;
|
|
875
1108
|
let save = false;
|
|
876
|
-
if(btcTx.inputAddresses!=null && this.
|
|
877
|
-
this.
|
|
1109
|
+
if(btcTx.inputAddresses!=null && this._senderAddress==null) {
|
|
1110
|
+
this._senderAddress = btcTx.inputAddresses[1];
|
|
878
1111
|
save = true;
|
|
879
1112
|
}
|
|
880
|
-
if(this.
|
|
881
|
-
this.
|
|
1113
|
+
if(this._state===SpvFromBTCSwapState.POSTED || this._state==SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED) {
|
|
1114
|
+
this._state = SpvFromBTCSwapState.BROADCASTED;
|
|
882
1115
|
save = true;
|
|
883
1116
|
}
|
|
884
1117
|
if(save) this._saveAndEmit();
|
|
@@ -890,15 +1123,15 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
890
1123
|
if(abortSignal!=null) abortSignal.throwIfAborted();
|
|
891
1124
|
|
|
892
1125
|
let save = false;
|
|
893
|
-
if(result.inputAddresses!=null && this.
|
|
894
|
-
this.
|
|
1126
|
+
if(result.inputAddresses!=null && this._senderAddress==null) {
|
|
1127
|
+
this._senderAddress = result.inputAddresses[1];
|
|
895
1128
|
save = true;
|
|
896
1129
|
}
|
|
897
1130
|
if(
|
|
898
|
-
(this.
|
|
899
|
-
(this.
|
|
1131
|
+
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.FRONTED &&
|
|
1132
|
+
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.CLAIMED
|
|
900
1133
|
) {
|
|
901
|
-
this.
|
|
1134
|
+
this._state = SpvFromBTCSwapState.BTC_TX_CONFIRMED;
|
|
902
1135
|
save = true;
|
|
903
1136
|
}
|
|
904
1137
|
if(save) await this._saveAndEmit();
|
|
@@ -911,10 +1144,11 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
911
1144
|
//// Claim
|
|
912
1145
|
|
|
913
1146
|
/**
|
|
914
|
-
*
|
|
915
|
-
*
|
|
1147
|
+
* Also possibly also syncs the bitcoin light client if necessary
|
|
1148
|
+
*
|
|
1149
|
+
* @inheritDoc
|
|
916
1150
|
*
|
|
917
|
-
* @throws {Error} If the swap is in invalid state (must be BTC_TX_CONFIRMED)
|
|
1151
|
+
* @throws {Error} If the swap is in invalid state (must be {@link SpvFromBTCSwapState.BTC_TX_CONFIRMED})
|
|
918
1152
|
*/
|
|
919
1153
|
async txsClaim(_signer?: string | T["Signer"] | T["NativeSigner"]): Promise<T["TX"][]> {
|
|
920
1154
|
let address: string | undefined = undefined;
|
|
@@ -924,77 +1158,84 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
924
1158
|
} else if (isAbstractSigner(_signer)) {
|
|
925
1159
|
address = _signer.getAddress();
|
|
926
1160
|
} else {
|
|
927
|
-
address = (await this.wrapper.
|
|
1161
|
+
address = (await this.wrapper._chain.wrapSigner(_signer)).getAddress();
|
|
928
1162
|
}
|
|
929
1163
|
}
|
|
930
1164
|
|
|
931
1165
|
if(!this.isClaimable()) throw new Error("Must be in BTC_TX_CONFIRMED state!");
|
|
932
|
-
if(this.
|
|
1166
|
+
if(this._data==null) throw new Error("Expected swap to have withdrawal data filled!");
|
|
933
1167
|
|
|
934
|
-
const vaultData = await this.wrapper.
|
|
1168
|
+
const vaultData = await this.wrapper._contract.getVaultData(this.vaultOwner, this.vaultId);
|
|
935
1169
|
if(vaultData==null) throw new Error(`Vault data for ${this.vaultOwner}:${this.vaultId.toString(10)} not found (already closed???)!`);
|
|
936
1170
|
|
|
937
|
-
const btcTx = await this.wrapper.
|
|
938
|
-
if(btcTx==null) throw new Error(`Bitcoin transaction ${this.
|
|
1171
|
+
const btcTx = await this.wrapper._btcRpc.getTransaction(this._data.btcTx.txid);
|
|
1172
|
+
if(btcTx==null) throw new Error(`Bitcoin transaction ${this._data.btcTx.txid} not found!`);
|
|
939
1173
|
const txs = [btcTx];
|
|
940
1174
|
|
|
941
1175
|
//Trace back from current tx to the vaultData-specified UTXO
|
|
942
1176
|
const vaultUtxo = vaultData.getUtxo();
|
|
943
1177
|
while(txs[0].ins[0].txid+":"+txs[0].ins[0].vout!==vaultUtxo) {
|
|
944
|
-
const btcTx = await this.wrapper.
|
|
945
|
-
if(btcTx==null) throw new Error(`Prior withdrawal bitcoin transaction ${this.
|
|
1178
|
+
const btcTx = await this.wrapper._btcRpc.getTransaction(txs[0].ins[0].txid);
|
|
1179
|
+
if(btcTx==null) throw new Error(`Prior withdrawal bitcoin transaction ${this._data.btcTx.txid} not found!`);
|
|
946
1180
|
txs.unshift(btcTx);
|
|
947
1181
|
}
|
|
948
1182
|
|
|
949
1183
|
//Parse transactions to withdrawal data
|
|
950
1184
|
const withdrawalData: T["SpvVaultWithdrawalData"][] = [];
|
|
951
1185
|
for(let tx of txs) {
|
|
952
|
-
withdrawalData.push(await this.wrapper.
|
|
1186
|
+
withdrawalData.push(await this.wrapper._contract.getWithdrawalData(tx));
|
|
953
1187
|
}
|
|
954
1188
|
|
|
955
|
-
return await this.wrapper.
|
|
1189
|
+
return await this.wrapper._contract.txsClaim(
|
|
956
1190
|
address ?? this._getInitiator(), vaultData,
|
|
957
1191
|
withdrawalData.map(tx => {return {tx}}),
|
|
958
|
-
this.wrapper.
|
|
1192
|
+
this.wrapper._synchronizer, true
|
|
959
1193
|
);
|
|
960
1194
|
}
|
|
961
1195
|
|
|
962
1196
|
/**
|
|
963
|
-
*
|
|
1197
|
+
* Also possibly also syncs the bitcoin light client if necessary
|
|
964
1198
|
*
|
|
965
|
-
* @
|
|
966
|
-
*
|
|
1199
|
+
* @inheritDoc
|
|
1200
|
+
*
|
|
1201
|
+
* @throws {Error} If the swap is in invalid state (must be {@link SpvFromBTCSwapState.BTC_TX_CONFIRMED})
|
|
967
1202
|
*/
|
|
968
|
-
async claim(_signer: T["Signer"] | T["NativeSigner"], abortSignal?: AbortSignal): Promise<string> {
|
|
969
|
-
const signer = isAbstractSigner(_signer) ? _signer : await this.wrapper.
|
|
1203
|
+
async claim(_signer: T["Signer"] | T["NativeSigner"], abortSignal?: AbortSignal, onBeforeTxSent?: (txId: string) => void): Promise<string> {
|
|
1204
|
+
const signer = isAbstractSigner(_signer) ? _signer : await this.wrapper._chain.wrapSigner(_signer);
|
|
970
1205
|
let txIds: string[];
|
|
971
1206
|
try {
|
|
972
|
-
|
|
973
|
-
|
|
1207
|
+
let txCount = 0;
|
|
1208
|
+
const txs = await this.txsClaim(signer);
|
|
1209
|
+
txIds = await this.wrapper._chain.sendAndConfirm(
|
|
1210
|
+
signer, txs, true, abortSignal, undefined, (txId: string) => {
|
|
1211
|
+
txCount++;
|
|
1212
|
+
if(onBeforeTxSent!=null && txCount===txs.length) onBeforeTxSent(txId);
|
|
1213
|
+
return Promise.resolve();
|
|
1214
|
+
}
|
|
974
1215
|
);
|
|
975
1216
|
} catch (e) {
|
|
976
|
-
if(this.
|
|
1217
|
+
if(this._data==null) throw e;
|
|
977
1218
|
|
|
978
1219
|
this.logger.info("claim(): Failed to claim ourselves, checking swap claim state...");
|
|
979
|
-
if(this.
|
|
1220
|
+
if(this._state===SpvFromBTCSwapState.CLAIMED) {
|
|
980
1221
|
this.logger.info("claim(): Transaction state is CLAIMED, swap was successfully claimed by the watchtower");
|
|
981
|
-
return this.
|
|
1222
|
+
return this._claimTxId!;
|
|
982
1223
|
}
|
|
983
|
-
const withdrawalState = await this.wrapper.
|
|
1224
|
+
const withdrawalState = await this.wrapper._contract.getWithdrawalState(this._data, this._genesisSmartChainBlockHeight);
|
|
984
1225
|
if(withdrawalState!=null && withdrawalState.type===SpvWithdrawalStateType.CLAIMED) {
|
|
985
1226
|
this.logger.info("claim(): Transaction status is CLAIMED, swap was successfully claimed by the watchtower");
|
|
986
|
-
this.
|
|
1227
|
+
this._claimTxId = withdrawalState.txId;
|
|
987
1228
|
await this._saveAndEmit(SpvFromBTCSwapState.CLAIMED);
|
|
988
1229
|
return withdrawalState.txId;
|
|
989
1230
|
}
|
|
990
1231
|
throw e;
|
|
991
1232
|
}
|
|
992
1233
|
|
|
993
|
-
this.
|
|
1234
|
+
this._claimTxId = txIds[0];
|
|
994
1235
|
if(
|
|
995
|
-
this.
|
|
996
|
-
this.
|
|
997
|
-
this.
|
|
1236
|
+
this._state===SpvFromBTCSwapState.POSTED || this._state===SpvFromBTCSwapState.BROADCASTED ||
|
|
1237
|
+
this._state===SpvFromBTCSwapState.BTC_TX_CONFIRMED || this._state===SpvFromBTCSwapState.FAILED ||
|
|
1238
|
+
this._state===SpvFromBTCSwapState.FRONTED
|
|
998
1239
|
) {
|
|
999
1240
|
await this._saveAndEmit(SpvFromBTCSwapState.CLAIMED);
|
|
1000
1241
|
}
|
|
@@ -1006,12 +1247,12 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1006
1247
|
*
|
|
1007
1248
|
* @param abortSignal
|
|
1008
1249
|
* @param interval How often to check (in seconds), default to 5s
|
|
1009
|
-
* @
|
|
1250
|
+
* @internal
|
|
1010
1251
|
*/
|
|
1011
1252
|
protected async watchdogWaitTillResult(abortSignal?: AbortSignal, interval: number = 5): Promise<
|
|
1012
1253
|
SpvWithdrawalClaimedState | SpvWithdrawalFrontedState | SpvWithdrawalClosedState
|
|
1013
1254
|
> {
|
|
1014
|
-
if(this.
|
|
1255
|
+
if(this._data==null) throw new Error("Cannot await the result before the btc transaction is sent!");
|
|
1015
1256
|
|
|
1016
1257
|
let status: SpvWithdrawalState = {type: SpvWithdrawalStateType.NOT_FOUND};
|
|
1017
1258
|
while(status.type===SpvWithdrawalStateType.NOT_FOUND) {
|
|
@@ -1019,8 +1260,8 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1019
1260
|
try {
|
|
1020
1261
|
//Be smart about checking withdrawal state
|
|
1021
1262
|
if(await this._shouldCheckWithdrawalState()) {
|
|
1022
|
-
status = await this.wrapper.
|
|
1023
|
-
this.
|
|
1263
|
+
status = await this.wrapper._contract.getWithdrawalState(
|
|
1264
|
+
this._data, this._genesisSmartChainBlockHeight
|
|
1024
1265
|
) ?? {type: SpvWithdrawalStateType.NOT_FOUND};
|
|
1025
1266
|
}
|
|
1026
1267
|
} catch (e) {
|
|
@@ -1032,17 +1273,27 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1032
1273
|
}
|
|
1033
1274
|
|
|
1034
1275
|
/**
|
|
1035
|
-
*
|
|
1276
|
+
* This is an alias for the {@link waitTillClaimedOrFronted} function and will
|
|
1277
|
+
* also resolve if the swap has been fronted (not necessarily claimed)
|
|
1278
|
+
*
|
|
1279
|
+
* @inheritDoc
|
|
1280
|
+
*/
|
|
1281
|
+
waitTillClaimed(maxWaitTimeSeconds?: number, abortSignal?: AbortSignal): Promise<boolean> {
|
|
1282
|
+
return this.waitTillClaimedOrFronted(maxWaitTimeSeconds, abortSignal);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* Waits till the swap is successfully fronted or settled on the destination chain
|
|
1287
|
+
*
|
|
1288
|
+
* @param maxWaitTimeSeconds Maximum time in seconds to wait for the swap to be settled (by default
|
|
1289
|
+
* it waits indefinitely)
|
|
1290
|
+
* @param abortSignal Abort signal
|
|
1036
1291
|
*
|
|
1037
|
-
* @param maxWaitTimeSeconds Maximum time in seconds to wait for the swap to be settled
|
|
1038
|
-
* @param abortSignal
|
|
1039
|
-
* @throws {Error} If swap is in invalid state (must be BTC_TX_CONFIRMED)
|
|
1040
|
-
* @throws {Error} If the LP refunded sooner than we were able to claim
|
|
1041
1292
|
* @returns {boolean} whether the swap was claimed or fronted automatically or not, if the swap was not claimed
|
|
1042
|
-
* the user can claim manually through
|
|
1293
|
+
* the user can claim manually through the {@link claim} function
|
|
1043
1294
|
*/
|
|
1044
1295
|
async waitTillClaimedOrFronted(maxWaitTimeSeconds?: number, abortSignal?: AbortSignal): Promise<boolean> {
|
|
1045
|
-
if(this.
|
|
1296
|
+
if(this._state===SpvFromBTCSwapState.CLAIMED || this._state===SpvFromBTCSwapState.FRONTED) return Promise.resolve(true);
|
|
1046
1297
|
|
|
1047
1298
|
const abortController = extendAbortController(abortSignal);
|
|
1048
1299
|
|
|
@@ -1089,24 +1340,24 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1089
1340
|
|
|
1090
1341
|
if(res.type===SpvWithdrawalStateType.FRONTED) {
|
|
1091
1342
|
if(
|
|
1092
|
-
(this.
|
|
1093
|
-
(this.
|
|
1343
|
+
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.FRONTED ||
|
|
1344
|
+
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.CLAIMED
|
|
1094
1345
|
) {
|
|
1095
|
-
this.
|
|
1346
|
+
this._frontTxId = res.txId;
|
|
1096
1347
|
await this._saveAndEmit(SpvFromBTCSwapState.FRONTED);
|
|
1097
1348
|
}
|
|
1098
1349
|
}
|
|
1099
1350
|
if(res.type===SpvWithdrawalStateType.CLAIMED) {
|
|
1100
1351
|
if(
|
|
1101
|
-
(this.
|
|
1352
|
+
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.CLAIMED
|
|
1102
1353
|
) {
|
|
1103
|
-
this.
|
|
1354
|
+
this._claimTxId = res.txId;
|
|
1104
1355
|
await this._saveAndEmit(SpvFromBTCSwapState.FRONTED);
|
|
1105
1356
|
}
|
|
1106
1357
|
}
|
|
1107
1358
|
if(res.type===SpvWithdrawalStateType.CLOSED) {
|
|
1108
1359
|
if(
|
|
1109
|
-
(this.
|
|
1360
|
+
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.CLOSED
|
|
1110
1361
|
) await this._saveAndEmit(SpvFromBTCSwapState.CLOSED);
|
|
1111
1362
|
throw new Error("Swap failed with catastrophic error!");
|
|
1112
1363
|
}
|
|
@@ -1115,12 +1366,14 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1115
1366
|
}
|
|
1116
1367
|
|
|
1117
1368
|
/**
|
|
1118
|
-
* Waits till the bitcoin transaction confirms and swap
|
|
1369
|
+
* Waits till the bitcoin transaction confirms and swap settled on the destination chain
|
|
1119
1370
|
*
|
|
1120
|
-
* @param abortSignal Abort signal
|
|
1121
|
-
* @param checkIntervalSeconds How often to check the bitcoin transaction
|
|
1122
1371
|
* @param updateCallback Callback called when txId is found, and also called with subsequent confirmations
|
|
1123
|
-
* @
|
|
1372
|
+
* @param checkIntervalSeconds How often to check the bitcoin transaction (5 seconds by default)
|
|
1373
|
+
* @param abortSignal Abort signal
|
|
1374
|
+
*
|
|
1375
|
+
* @throws {Error} if in invalid state (must be {@link SpvFromBTCSwapState.POSTED} or
|
|
1376
|
+
* {@link SpvFromBTCSwapState.BROADCASTED} states)
|
|
1124
1377
|
*/
|
|
1125
1378
|
async waitTillExecuted(
|
|
1126
1379
|
updateCallback?: (txId?: string, confirmations?: number, targetConfirmations?: number, txEtaMs?: number) => void,
|
|
@@ -1135,6 +1388,9 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1135
1388
|
//////////////////////////////
|
|
1136
1389
|
//// Storage
|
|
1137
1390
|
|
|
1391
|
+
/**
|
|
1392
|
+
* @inheritDoc
|
|
1393
|
+
*/
|
|
1138
1394
|
serialize(): any {
|
|
1139
1395
|
return {
|
|
1140
1396
|
...super.serialize(),
|
|
@@ -1161,13 +1417,13 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1161
1417
|
callerFeeShare: this.callerFeeShare.toString(10),
|
|
1162
1418
|
frontingFeeShare: this.frontingFeeShare.toString(10),
|
|
1163
1419
|
executionFeeShare: this.executionFeeShare.toString(10),
|
|
1164
|
-
genesisSmartChainBlockHeight: this.
|
|
1420
|
+
genesisSmartChainBlockHeight: this._genesisSmartChainBlockHeight,
|
|
1165
1421
|
gasPricingInfo: serializePriceInfoType(this.gasPricingInfo),
|
|
1166
1422
|
|
|
1167
|
-
senderAddress: this.
|
|
1168
|
-
claimTxId: this.
|
|
1169
|
-
frontTxId: this.
|
|
1170
|
-
data: this.
|
|
1423
|
+
senderAddress: this._senderAddress,
|
|
1424
|
+
claimTxId: this._claimTxId,
|
|
1425
|
+
frontTxId: this._frontTxId,
|
|
1426
|
+
data: this._data?.serialize()
|
|
1171
1427
|
};
|
|
1172
1428
|
}
|
|
1173
1429
|
|
|
@@ -1176,41 +1432,45 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1176
1432
|
//// Swap ticks & sync
|
|
1177
1433
|
|
|
1178
1434
|
/**
|
|
1179
|
-
*
|
|
1435
|
+
* Used to set the txId of the bitcoin payment from the on-chain events listener
|
|
1180
1436
|
*
|
|
1181
1437
|
* @param txId
|
|
1438
|
+
* @internal
|
|
1182
1439
|
*/
|
|
1183
1440
|
async _setBitcoinTxId(txId: string) {
|
|
1184
|
-
if(this.
|
|
1185
|
-
if(txId!=this.
|
|
1441
|
+
if(this._data==null) return;
|
|
1442
|
+
if(txId!=this._data.btcTx.txid) return;
|
|
1186
1443
|
|
|
1187
|
-
if(this.
|
|
1188
|
-
const btcTx = await this.wrapper.
|
|
1444
|
+
if(this._senderAddress!=null) return;
|
|
1445
|
+
const btcTx = await this.wrapper._btcRpc.getTransaction(txId);
|
|
1189
1446
|
if(btcTx==null || btcTx.inputAddresses==null) return;
|
|
1190
1447
|
|
|
1191
|
-
this.
|
|
1448
|
+
this._senderAddress = btcTx.inputAddresses[1];
|
|
1192
1449
|
}
|
|
1193
1450
|
|
|
1451
|
+
/**
|
|
1452
|
+
* @internal
|
|
1453
|
+
*/
|
|
1194
1454
|
async _syncStateFromBitcoin(save?: boolean) {
|
|
1195
|
-
if(this.
|
|
1455
|
+
if(this._data?.btcTx==null) return false;
|
|
1196
1456
|
|
|
1197
1457
|
//Check if bitcoin payment was confirmed
|
|
1198
1458
|
const res = await this.getBitcoinPayment();
|
|
1199
1459
|
if(res==null) {
|
|
1200
1460
|
//Check inputs double-spent
|
|
1201
|
-
for(let input of this.
|
|
1202
|
-
if(await this.wrapper.
|
|
1461
|
+
for(let input of this._data.btcTx.ins) {
|
|
1462
|
+
if(await this.wrapper._btcRpc.isSpent(input.txid+":"+input.vout, true)) {
|
|
1203
1463
|
if(
|
|
1204
|
-
this.
|
|
1205
|
-
this.
|
|
1206
|
-
this.
|
|
1207
|
-
this.
|
|
1464
|
+
this._state===SpvFromBTCSwapState.SIGNED ||
|
|
1465
|
+
this._state===SpvFromBTCSwapState.POSTED ||
|
|
1466
|
+
this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
1467
|
+
this._state===SpvFromBTCSwapState.DECLINED
|
|
1208
1468
|
) {
|
|
1209
1469
|
//One of the inputs was double-spent
|
|
1210
|
-
this.
|
|
1470
|
+
this._state = SpvFromBTCSwapState.QUOTE_EXPIRED;
|
|
1211
1471
|
} else {
|
|
1212
1472
|
//One of the inputs was double-spent
|
|
1213
|
-
this.
|
|
1473
|
+
this._state = SpvFromBTCSwapState.FAILED;
|
|
1214
1474
|
}
|
|
1215
1475
|
if(save) await this._saveAndEmit();
|
|
1216
1476
|
return true;
|
|
@@ -1218,26 +1478,26 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1218
1478
|
}
|
|
1219
1479
|
} else {
|
|
1220
1480
|
let needsSave = false;
|
|
1221
|
-
if(res.inputAddresses!=null && this.
|
|
1222
|
-
this.
|
|
1481
|
+
if(res.inputAddresses!=null && this._senderAddress==null) {
|
|
1482
|
+
this._senderAddress = res.inputAddresses[1];
|
|
1223
1483
|
needsSave = true;
|
|
1224
1484
|
}
|
|
1225
1485
|
if(res.confirmations>=this.vaultRequiredConfirmations) {
|
|
1226
1486
|
if(
|
|
1227
|
-
this.
|
|
1228
|
-
this.
|
|
1229
|
-
this.
|
|
1487
|
+
this._state!==SpvFromBTCSwapState.BTC_TX_CONFIRMED &&
|
|
1488
|
+
this._state!==SpvFromBTCSwapState.FRONTED &&
|
|
1489
|
+
this._state!==SpvFromBTCSwapState.CLAIMED
|
|
1230
1490
|
) {
|
|
1231
|
-
this.
|
|
1491
|
+
this._state = SpvFromBTCSwapState.BTC_TX_CONFIRMED;
|
|
1232
1492
|
needsSave = true;
|
|
1233
1493
|
}
|
|
1234
1494
|
} else if(
|
|
1235
|
-
this.
|
|
1236
|
-
this.
|
|
1237
|
-
this.
|
|
1238
|
-
this.
|
|
1495
|
+
this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
1496
|
+
this._state===SpvFromBTCSwapState.POSTED ||
|
|
1497
|
+
this._state===SpvFromBTCSwapState.SIGNED ||
|
|
1498
|
+
this._state===SpvFromBTCSwapState.DECLINED
|
|
1239
1499
|
) {
|
|
1240
|
-
this.
|
|
1500
|
+
this._state = SpvFromBTCSwapState.BROADCASTED;
|
|
1241
1501
|
needsSave = true;
|
|
1242
1502
|
}
|
|
1243
1503
|
if(needsSave && save) await this._saveAndEmit();
|
|
@@ -1249,41 +1509,39 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1249
1509
|
/**
|
|
1250
1510
|
* Checks the swap's state on-chain and compares it to its internal state, updates/changes it according to on-chain
|
|
1251
1511
|
* data
|
|
1252
|
-
*
|
|
1253
|
-
* @private
|
|
1254
1512
|
*/
|
|
1255
1513
|
private async syncStateFromChain(): Promise<boolean> {
|
|
1256
1514
|
let changed: boolean = false;
|
|
1257
1515
|
|
|
1258
1516
|
if(
|
|
1259
|
-
this.
|
|
1260
|
-
this.
|
|
1261
|
-
this.
|
|
1262
|
-
this.
|
|
1263
|
-
this.
|
|
1264
|
-
this.
|
|
1517
|
+
this._state===SpvFromBTCSwapState.SIGNED ||
|
|
1518
|
+
this._state===SpvFromBTCSwapState.POSTED ||
|
|
1519
|
+
this._state===SpvFromBTCSwapState.BROADCASTED ||
|
|
1520
|
+
this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED ||
|
|
1521
|
+
this._state===SpvFromBTCSwapState.DECLINED ||
|
|
1522
|
+
this._state===SpvFromBTCSwapState.BTC_TX_CONFIRMED
|
|
1265
1523
|
) {
|
|
1266
1524
|
//Check BTC transaction
|
|
1267
1525
|
if(await this._syncStateFromBitcoin(false)) changed ||= true;
|
|
1268
1526
|
}
|
|
1269
1527
|
|
|
1270
|
-
if(this.
|
|
1528
|
+
if(this._state===SpvFromBTCSwapState.BROADCASTED || this._state===SpvFromBTCSwapState.BTC_TX_CONFIRMED) {
|
|
1271
1529
|
if(await this._shouldCheckWithdrawalState()) {
|
|
1272
|
-
const status = await this.wrapper.
|
|
1273
|
-
this.logger.debug("syncStateFromChain(): status of "+this.
|
|
1530
|
+
const status = await this.wrapper._contract.getWithdrawalState(this._data!, this._genesisSmartChainBlockHeight);
|
|
1531
|
+
this.logger.debug("syncStateFromChain(): status of "+this._data!.btcTx.txid, status);
|
|
1274
1532
|
switch(status?.type) {
|
|
1275
1533
|
case SpvWithdrawalStateType.FRONTED:
|
|
1276
|
-
this.
|
|
1277
|
-
this.
|
|
1534
|
+
this._frontTxId = status.txId;
|
|
1535
|
+
this._state = SpvFromBTCSwapState.FRONTED;
|
|
1278
1536
|
changed ||= true;
|
|
1279
1537
|
break;
|
|
1280
1538
|
case SpvWithdrawalStateType.CLAIMED:
|
|
1281
|
-
this.
|
|
1282
|
-
this.
|
|
1539
|
+
this._claimTxId = status.txId;
|
|
1540
|
+
this._state = SpvFromBTCSwapState.CLAIMED;
|
|
1283
1541
|
changed ||= true;
|
|
1284
1542
|
break;
|
|
1285
1543
|
case SpvWithdrawalStateType.CLOSED:
|
|
1286
|
-
this.
|
|
1544
|
+
this._state = SpvFromBTCSwapState.CLOSED;
|
|
1287
1545
|
changed ||= true;
|
|
1288
1546
|
break;
|
|
1289
1547
|
}
|
|
@@ -1291,15 +1549,15 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1291
1549
|
}
|
|
1292
1550
|
|
|
1293
1551
|
if(
|
|
1294
|
-
this.
|
|
1295
|
-
this.
|
|
1296
|
-
this.
|
|
1552
|
+
this._state===SpvFromBTCSwapState.CREATED ||
|
|
1553
|
+
this._state===SpvFromBTCSwapState.SIGNED ||
|
|
1554
|
+
this._state===SpvFromBTCSwapState.POSTED
|
|
1297
1555
|
) {
|
|
1298
1556
|
if(this.expiry<Date.now()) {
|
|
1299
|
-
if(this.
|
|
1300
|
-
this.
|
|
1557
|
+
if(this._state===SpvFromBTCSwapState.CREATED) {
|
|
1558
|
+
this._state = SpvFromBTCSwapState.QUOTE_EXPIRED;
|
|
1301
1559
|
} else {
|
|
1302
|
-
this.
|
|
1560
|
+
this._state = SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
1303
1561
|
}
|
|
1304
1562
|
changed ||= true;
|
|
1305
1563
|
}
|
|
@@ -1308,27 +1566,35 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1308
1566
|
return changed;
|
|
1309
1567
|
}
|
|
1310
1568
|
|
|
1569
|
+
/**
|
|
1570
|
+
* @inheritDoc
|
|
1571
|
+
* @internal
|
|
1572
|
+
*/
|
|
1311
1573
|
async _sync(save?: boolean): Promise<boolean> {
|
|
1312
1574
|
const changed = await this.syncStateFromChain();
|
|
1313
1575
|
if(changed && save) await this._saveAndEmit();
|
|
1314
1576
|
return changed;
|
|
1315
1577
|
}
|
|
1316
1578
|
|
|
1579
|
+
/**
|
|
1580
|
+
* @inheritDoc
|
|
1581
|
+
* @internal
|
|
1582
|
+
*/
|
|
1317
1583
|
async _tick(save?: boolean): Promise<boolean> {
|
|
1318
1584
|
if(
|
|
1319
|
-
this.
|
|
1320
|
-
this.
|
|
1585
|
+
this._state===SpvFromBTCSwapState.CREATED ||
|
|
1586
|
+
this._state===SpvFromBTCSwapState.SIGNED
|
|
1321
1587
|
) {
|
|
1322
1588
|
if(this.getQuoteExpiry()<Date.now()) {
|
|
1323
|
-
this.
|
|
1589
|
+
this._state = SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED;
|
|
1324
1590
|
if(save) await this._saveAndEmit();
|
|
1325
1591
|
return true;
|
|
1326
1592
|
}
|
|
1327
1593
|
}
|
|
1328
1594
|
|
|
1329
|
-
if(this.
|
|
1595
|
+
if(this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && !this.initiated) {
|
|
1330
1596
|
if(this.expiry<Date.now()) {
|
|
1331
|
-
this.
|
|
1597
|
+
this._state = SpvFromBTCSwapState.QUOTE_EXPIRED;
|
|
1332
1598
|
if(save) await this._saveAndEmit();
|
|
1333
1599
|
return true;
|
|
1334
1600
|
}
|
|
@@ -1336,8 +1602,8 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1336
1602
|
|
|
1337
1603
|
if(Math.floor(Date.now()/1000)%120===0) {
|
|
1338
1604
|
if (
|
|
1339
|
-
this.
|
|
1340
|
-
this.
|
|
1605
|
+
this._state === SpvFromBTCSwapState.POSTED ||
|
|
1606
|
+
this._state === SpvFromBTCSwapState.BROADCASTED
|
|
1341
1607
|
) {
|
|
1342
1608
|
try {
|
|
1343
1609
|
//Check if bitcoin payment was confirmed
|
|
@@ -1351,19 +1617,24 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1351
1617
|
return false;
|
|
1352
1618
|
}
|
|
1353
1619
|
|
|
1620
|
+
/**
|
|
1621
|
+
* Checks whether an on-chain withdrawal state should be fetched for this specific swap
|
|
1622
|
+
*
|
|
1623
|
+
* @internal
|
|
1624
|
+
*/
|
|
1354
1625
|
async _shouldCheckWithdrawalState(frontingAddress?: string | null, vaultDataUtxo?: string | null) {
|
|
1355
|
-
if(frontingAddress===undefined) frontingAddress = await this.wrapper.
|
|
1356
|
-
if(vaultDataUtxo===undefined) vaultDataUtxo = await this.wrapper.
|
|
1626
|
+
if(frontingAddress===undefined) frontingAddress = await this.wrapper._contract.getFronterAddress(this.vaultOwner, this.vaultId, this._data!);
|
|
1627
|
+
if(vaultDataUtxo===undefined) vaultDataUtxo = await this.wrapper._contract.getVaultLatestUtxo(this.vaultOwner, this.vaultId);
|
|
1357
1628
|
|
|
1358
1629
|
if(frontingAddress != null) return true; //In case the swap is fronted there will for sure be a fronted event
|
|
1359
1630
|
if(vaultDataUtxo == null) return true; //Vault UTXO is null (the vault closed)
|
|
1360
1631
|
|
|
1361
1632
|
const [txId, _] = vaultDataUtxo.split(":");
|
|
1362
1633
|
//Don't check both txns if their txId is equal
|
|
1363
|
-
if(this.
|
|
1634
|
+
if(this._data!.btcTx.txid===txId) return true;
|
|
1364
1635
|
const [btcTx, latestVaultTx] = await Promise.all([
|
|
1365
|
-
this.wrapper.
|
|
1366
|
-
this.wrapper.
|
|
1636
|
+
this.wrapper._btcRpc.getTransaction(this._data!.btcTx.txid),
|
|
1637
|
+
this.wrapper._btcRpc.getTransaction(txId)
|
|
1367
1638
|
]);
|
|
1368
1639
|
|
|
1369
1640
|
if(latestVaultTx==null || latestVaultTx.blockheight==null) {
|
|
@@ -1384,7 +1655,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1384
1655
|
}
|
|
1385
1656
|
} else {
|
|
1386
1657
|
//Definitely not claimed because the transaction was probably double-spent (or evicted from mempool)
|
|
1387
|
-
this.logger.debug(`_shouldCheckWithdrawalState(): Skipped checking withdrawal state, btc tx probably replaced or evicted: ${this.
|
|
1658
|
+
this.logger.debug(`_shouldCheckWithdrawalState(): Skipped checking withdrawal state, btc tx probably replaced or evicted: ${this._data!.btcTx.txid} and not fronted`);
|
|
1388
1659
|
return false;
|
|
1389
1660
|
}
|
|
1390
1661
|
|