@atomiqlabs/sdk 8.9.1 → 8.9.3
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/LICENSE +201 -201
- package/README.md +1760 -1760
- package/api/index.d.ts +1 -1
- package/api/index.js +3 -3
- package/dist/ApiList.d.ts +37 -37
- package/dist/ApiList.js +30 -30
- package/dist/SmartChainAssets.d.ts +181 -181
- package/dist/SmartChainAssets.js +181 -181
- package/dist/api/ApiEndpoints.d.ts +393 -393
- package/dist/api/ApiEndpoints.js +2 -2
- package/dist/api/ApiParser.d.ts +10 -10
- package/dist/api/ApiParser.js +134 -134
- package/dist/api/ApiTypes.d.ts +157 -157
- package/dist/api/ApiTypes.js +75 -75
- package/dist/api/SerializedAction.d.ts +40 -40
- package/dist/api/SerializedAction.js +59 -59
- package/dist/api/SwapperApi.d.ts +50 -50
- package/dist/api/SwapperApi.js +431 -431
- package/dist/api/index.d.ts +5 -5
- package/dist/api/index.js +24 -24
- package/dist/bitcoin/coinselect2/accumulative.d.ts +7 -7
- package/dist/bitcoin/coinselect2/accumulative.js +52 -52
- package/dist/bitcoin/coinselect2/blackjack.d.ts +7 -7
- package/dist/bitcoin/coinselect2/blackjack.js +38 -38
- package/dist/bitcoin/coinselect2/index.d.ts +20 -20
- package/dist/bitcoin/coinselect2/index.js +69 -69
- package/dist/bitcoin/coinselect2/utils.d.ts +82 -82
- package/dist/bitcoin/coinselect2/utils.js +158 -158
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +113 -113
- package/dist/bitcoin/wallet/BitcoinWallet.js +335 -335
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +116 -116
- package/dist/bitcoin/wallet/IBitcoinWallet.js +21 -21
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +106 -106
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +196 -196
- 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 +24 -24
- package/dist/events/UnifiedSwapEventListener.js +138 -138
- package/dist/http/HttpUtils.d.ts +29 -29
- package/dist/http/HttpUtils.js +97 -97
- package/dist/http/paramcoders/IParamReader.d.ts +8 -8
- package/dist/http/paramcoders/IParamReader.js +2 -2
- package/dist/http/paramcoders/ParamDecoder.d.ts +44 -44
- package/dist/http/paramcoders/ParamDecoder.js +137 -137
- package/dist/http/paramcoders/ParamEncoder.d.ts +20 -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 +13 -13
- package/dist/http/paramcoders/client/StreamParamEncoder.js +26 -26
- package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +17 -17
- package/dist/http/paramcoders/client/StreamingFetchPromise.js +175 -175
- package/dist/index.d.ts +86 -86
- package/dist/index.js +159 -159
- package/dist/intermediaries/Intermediary.d.ts +178 -178
- package/dist/intermediaries/Intermediary.js +166 -166
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +216 -216
- package/dist/intermediaries/IntermediaryDiscovery.js +424 -424
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +607 -607
- package/dist/intermediaries/apis/IntermediaryAPI.js +764 -764
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.d.ts +155 -155
- package/dist/intermediaries/apis/TrustedIntermediaryAPI.js +137 -137
- package/dist/intermediaries/auth/SignedKeyBasedAuth.d.ts +14 -14
- package/dist/intermediaries/auth/SignedKeyBasedAuth.js +68 -68
- 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 -168
- package/dist/prices/abstract/ISwapPrice.js +279 -279
- 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 +127 -127
- package/dist/storage/IUnifiedStorage.js +2 -2
- package/dist/storage/UnifiedSwapStorage.d.ts +120 -120
- package/dist/storage/UnifiedSwapStorage.js +154 -154
- 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 +770 -770
- package/dist/swapper/Swapper.js +1758 -1758
- package/dist/swapper/SwapperFactory.d.ts +135 -135
- package/dist/swapper/SwapperFactory.js +162 -162
- package/dist/swapper/SwapperUtils.d.ts +222 -222
- package/dist/swapper/SwapperUtils.js +519 -519
- 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 +14 -14
- package/dist/swaps/IBTCWalletSwap.d.ts +73 -73
- package/dist/swaps/IBTCWalletSwap.js +18 -18
- package/dist/swaps/IClaimableSwap.d.ts +49 -49
- package/dist/swaps/IClaimableSwap.js +15 -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 +14 -14
- package/dist/swaps/ISwap.d.ts +453 -453
- package/dist/swaps/ISwap.js +371 -371
- package/dist/swaps/ISwapWithGasDrop.d.ts +21 -21
- package/dist/swaps/ISwapWithGasDrop.js +12 -12
- package/dist/swaps/ISwapWrapper.d.ts +295 -295
- package/dist/swaps/ISwapWrapper.js +373 -373
- 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 +139 -139
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +172 -172
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +129 -129
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +167 -167
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +107 -107
- 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 +64 -64
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCWrapper.js +82 -82
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +547 -547
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +1419 -1419
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +192 -192
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +432 -432
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +650 -650
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +1577 -1577
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +237 -237
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +525 -525
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +491 -491
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +1463 -1463
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +204 -204
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +406 -406
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +446 -446
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +1097 -1097
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.d.ts +68 -68
- package/dist/swaps/escrow_swaps/tobtc/IToBTCWrapper.js +117 -117
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.d.ts +127 -127
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.js +256 -256
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +252 -252
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +535 -535
- 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 +134 -134
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +286 -286
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +694 -694
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +1687 -1687
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +259 -259
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +947 -947
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +302 -302
- package/dist/swaps/trusted/ln/LnForGasSwap.js +625 -625
- 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 +343 -343
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +698 -698
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +71 -71
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +93 -93
- 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 +57 -57
- package/dist/types/SwapExecutionAction.d.ts +195 -195
- package/dist/types/SwapExecutionAction.js +106 -106
- package/dist/types/SwapExecutionStep.d.ts +144 -144
- package/dist/types/SwapExecutionStep.js +87 -87
- 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 +43 -43
- package/dist/types/Token.d.ts +99 -99
- package/dist/types/Token.js +76 -76
- package/dist/types/TokenAmount.d.ts +75 -75
- package/dist/types/TokenAmount.js +85 -85
- 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 +18 -18
- package/dist/types/lnurl/LNURLPay.d.ts +61 -61
- package/dist/types/lnurl/LNURLPay.js +31 -31
- package/dist/types/lnurl/LNURLWithdraw.d.ts +48 -48
- package/dist/types/lnurl/LNURLWithdraw.js +27 -27
- package/dist/types/wallets/LightningInvoiceCreateService.d.ts +24 -24
- package/dist/types/wallets/LightningInvoiceCreateService.js +15 -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 +70 -70
- package/dist/utils/BitcoinUtils.d.ts +18 -18
- package/dist/utils/BitcoinUtils.js +174 -174
- package/dist/utils/BitcoinWalletUtils.d.ts +7 -7
- package/dist/utils/BitcoinWalletUtils.js +14 -14
- package/dist/utils/Logger.d.ts +7 -7
- package/dist/utils/Logger.js +12 -12
- package/dist/utils/RetryUtils.d.ts +22 -22
- package/dist/utils/RetryUtils.js +67 -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 +55 -55
- package/dist/utils/TokenUtils.d.ts +19 -19
- package/dist/utils/TokenUtils.js +37 -37
- package/dist/utils/TypeUtils.d.ts +7 -7
- package/dist/utils/TypeUtils.js +2 -2
- package/dist/utils/Utils.d.ts +69 -69
- package/dist/utils/Utils.js +214 -214
- package/package.json +46 -46
- package/src/SmartChainAssets.ts +186 -186
- package/src/api/ApiEndpoints.ts +427 -427
- package/src/api/ApiParser.ts +138 -138
- package/src/api/ApiTypes.ts +229 -229
- package/src/api/SerializedAction.ts +97 -97
- package/src/api/SwapperApi.ts +545 -545
- package/src/api/index.ts +5 -5
- package/src/bitcoin/coinselect2/accumulative.ts +69 -69
- package/src/bitcoin/coinselect2/blackjack.ts +50 -50
- package/src/bitcoin/coinselect2/index.ts +93 -93
- package/src/bitcoin/coinselect2/utils.ts +236 -236
- package/src/bitcoin/wallet/BitcoinWallet.ts +439 -439
- package/src/bitcoin/wallet/IBitcoinWallet.ts +140 -140
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +225 -225
- package/src/enums/FeeType.ts +15 -15
- package/src/enums/SwapAmountType.ts +16 -16
- package/src/enums/SwapDirection.ts +15 -15
- package/src/enums/SwapSide.ts +16 -16
- package/src/enums/SwapType.ts +75 -75
- package/src/errors/IntermediaryError.ts +28 -28
- package/src/errors/RequestError.ts +64 -64
- package/src/errors/UserError.ts +15 -15
- package/src/events/UnifiedSwapEventListener.ts +181 -181
- package/src/http/HttpUtils.ts +97 -97
- package/src/http/paramcoders/IParamReader.ts +9 -9
- package/src/http/paramcoders/ParamDecoder.ts +145 -145
- package/src/http/paramcoders/ParamEncoder.ts +40 -40
- package/src/http/paramcoders/SchemaVerifier.ts +153 -153
- package/src/http/paramcoders/client/ResponseParamDecoder.ts +57 -57
- package/src/http/paramcoders/client/StreamParamEncoder.ts +28 -28
- package/src/http/paramcoders/client/StreamingFetchPromise.ts +194 -194
- package/src/index.ts +141 -141
- package/src/intermediaries/Intermediary.ts +280 -280
- package/src/intermediaries/IntermediaryDiscovery.ts +548 -548
- package/src/intermediaries/apis/IntermediaryAPI.ts +1247 -1247
- package/src/intermediaries/auth/SignedKeyBasedAuth.ts +69 -69
- package/src/lnurl/LNURL.ts +402 -402
- package/src/prices/RedundantSwapPrice.ts +264 -264
- package/src/prices/SingleSwapPrice.ts +50 -50
- package/src/prices/SwapPriceWithChain.ts +194 -194
- package/src/prices/abstract/ICachedSwapPrice.ts +85 -85
- package/src/prices/abstract/IPriceProvider.ts +127 -127
- package/src/prices/abstract/ISwapPrice.ts +390 -390
- package/src/prices/providers/BinancePriceProvider.ts +48 -48
- package/src/prices/providers/CoinGeckoPriceProvider.ts +46 -46
- package/src/prices/providers/CoinPaprikaPriceProvider.ts +49 -49
- package/src/prices/providers/CustomPriceProvider.ts +40 -40
- package/src/prices/providers/KrakenPriceProvider.ts +83 -83
- package/src/prices/providers/OKXPriceProvider.ts +59 -59
- package/src/prices/providers/abstract/ExchangePriceProvider.ts +31 -31
- package/src/prices/providers/abstract/HttpPriceProvider.ts +14 -14
- package/src/storage/IUnifiedStorage.ts +136 -136
- package/src/storage/UnifiedSwapStorage.ts +175 -175
- package/src/storage-browser/IndexedDBUnifiedStorage.ts +350 -350
- package/src/storage-browser/LocalStorageManager.ts +106 -106
- package/src/swapper/Swapper.ts +2570 -2570
- package/src/swapper/SwapperFactory.ts +307 -307
- package/src/swapper/SwapperUtils.ts +610 -610
- package/src/swapper/SwapperWithChain.ts +707 -707
- package/src/swapper/SwapperWithSigner.ts +511 -511
- package/src/swaps/IAddressSwap.ts +30 -30
- package/src/swaps/IBTCWalletSwap.ts +92 -92
- package/src/swaps/IClaimableSwap.ts +65 -65
- package/src/swaps/IClaimableSwapWrapper.ts +17 -17
- package/src/swaps/IRefundableSwap.ts +58 -58
- package/src/swaps/ISwap.ts +775 -775
- package/src/swaps/ISwapWithGasDrop.ts +25 -25
- package/src/swaps/ISwapWrapper.ts +564 -564
- package/src/swaps/escrow_swaps/IEscrowSelfInitSwap.ts +217 -217
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +271 -271
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +284 -284
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +172 -172
- package/src/swaps/escrow_swaps/frombtc/IFromBTCSelfInitSwap.ts +300 -300
- package/src/swaps/escrow_swaps/frombtc/IFromBTCWrapper.ts +107 -107
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +1670 -1671
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +603 -603
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +1883 -1883
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +752 -752
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +1753 -1753
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +612 -612
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +1327 -1327
- package/src/swaps/escrow_swaps/tobtc/IToBTCWrapper.ts +138 -138
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNSwap.ts +304 -304
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +787 -787
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCSwap.ts +206 -206
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +403 -403
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +2148 -2148
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +1238 -1238
- package/src/swaps/trusted/ln/LnForGasSwap.ts +753 -753
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +90 -90
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +843 -843
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +133 -133
- package/src/types/AmountData.ts +9 -9
- package/src/types/CustomPriceFunction.ts +11 -11
- package/src/types/PriceInfoType.ts +66 -66
- package/src/types/SwapExecutionAction.ts +323 -323
- package/src/types/SwapExecutionStep.ts +224 -224
- package/src/types/SwapStateInfo.ts +6 -6
- package/src/types/SwapWithSigner.ts +61 -61
- package/src/types/Token.ts +163 -163
- package/src/types/TokenAmount.ts +167 -167
- package/src/types/fees/Fee.ts +56 -56
- package/src/types/fees/FeeBreakdown.ts +11 -11
- package/src/types/fees/PercentagePPM.ts +26 -26
- package/src/types/lnurl/LNURLPay.ts +79 -79
- package/src/types/lnurl/LNURLWithdraw.ts +61 -61
- package/src/types/wallets/LightningInvoiceCreateService.ts +30 -30
- package/src/types/wallets/MinimalBitcoinWalletInterface.ts +21 -21
- package/src/types/wallets/MinimalLightningNetworkWalletInterface.ts +9 -9
- package/src/utils/AutomaticClockDriftCorrection.ts +71 -71
- package/src/utils/BitcoinUtils.ts +164 -164
- package/src/utils/BitcoinWalletUtils.ts +15 -15
- package/src/utils/Logger.ts +14 -14
- package/src/utils/RetryUtils.ts +78 -78
- package/src/utils/SwapUtils.ts +99 -99
- package/src/utils/TimeoutUtils.ts +49 -49
- package/src/utils/TokenUtils.ts +33 -33
- package/src/utils/TypeUtils.ts +8 -8
- package/src/utils/Utils.ts +221 -221
|
@@ -1,843 +1,843 @@
|
|
|
1
|
-
import {SwapType} from "../../../enums/SwapType";
|
|
2
|
-
import {ChainType} from "@atomiqlabs/base";
|
|
3
|
-
import {toBigInt} from "../../../utils/Utils";
|
|
4
|
-
import {parsePsbtTransaction, toOutputScript} from "../../../utils/BitcoinUtils";
|
|
5
|
-
import {isISwapInit, ISwap, ISwapInit} from "../../ISwap";
|
|
6
|
-
import {TrustedAddressStatusResponseCodes} from "../../../intermediaries/apis/IntermediaryAPI";
|
|
7
|
-
import {OnchainForGasSwapTypeDefinition, OnchainForGasWrapper} from "./OnchainForGasWrapper";
|
|
8
|
-
import {Fee} from "../../../types/fees/Fee";
|
|
9
|
-
import {IBitcoinWallet, isIBitcoinWallet} from "../../../bitcoin/wallet/IBitcoinWallet";
|
|
10
|
-
import {IAddressSwap} from "../../IAddressSwap";
|
|
11
|
-
import {IBTCWalletSwap} from "../../IBTCWalletSwap";
|
|
12
|
-
import {Transaction} from "@scure/btc-signer";
|
|
13
|
-
import {SingleAddressBitcoinWallet} from "../../../bitcoin/wallet/SingleAddressBitcoinWallet";
|
|
14
|
-
import {Buffer} from "buffer";
|
|
15
|
-
import {
|
|
16
|
-
MinimalBitcoinWalletInterface,
|
|
17
|
-
MinimalBitcoinWalletInterfaceWithSigner
|
|
18
|
-
} from "../../../types/wallets/MinimalBitcoinWalletInterface";
|
|
19
|
-
import {FeeType} from "../../../enums/FeeType";
|
|
20
|
-
import {ppmToPercentage} from "../../../types/fees/PercentagePPM";
|
|
21
|
-
import {TokenAmount, toTokenAmount} from "../../../types/TokenAmount";
|
|
22
|
-
import {BitcoinTokens, BtcToken, SCToken} from "../../../types/Token";
|
|
23
|
-
import {getLogger, LoggerType} from "../../../utils/Logger";
|
|
24
|
-
import {timeoutPromise} from "../../../utils/TimeoutUtils";
|
|
25
|
-
import {toBitcoinWallet} from "../../../utils/BitcoinWalletUtils";
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* State enum for trusted on-chain gas swaps
|
|
29
|
-
*
|
|
30
|
-
* @category Swaps/Trusted Gas Swaps
|
|
31
|
-
*/
|
|
32
|
-
export enum OnchainForGasSwapState {
|
|
33
|
-
/**
|
|
34
|
-
* The swap quote expired without user sending in the BTC
|
|
35
|
-
*/
|
|
36
|
-
EXPIRED = -3,
|
|
37
|
-
/**
|
|
38
|
-
* The swap has failed after the intermediary already received the BTC on the source chain
|
|
39
|
-
*/
|
|
40
|
-
FAILED = -2,
|
|
41
|
-
/**
|
|
42
|
-
* Swap was refunded and BTC returned to the user's refund address
|
|
43
|
-
*/
|
|
44
|
-
REFUNDED = -1,
|
|
45
|
-
/**
|
|
46
|
-
* Swap was created, send the BTC to the swap address
|
|
47
|
-
*/
|
|
48
|
-
PR_CREATED = 0,
|
|
49
|
-
/**
|
|
50
|
-
* The swap is finished after the intermediary sent funds on the destination chain
|
|
51
|
-
*/
|
|
52
|
-
FINISHED = 1,
|
|
53
|
-
/**
|
|
54
|
-
* Swap is refundable because the intermediary cannot honor the swap request on the destination chain
|
|
55
|
-
*/
|
|
56
|
-
REFUNDABLE = 2
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const OnchainForGasSwapStateDescription: Record<OnchainForGasSwapState, string> = {
|
|
60
|
-
[OnchainForGasSwapState.EXPIRED]:
|
|
61
|
-
"The swap quote expired without user sending in the BTC",
|
|
62
|
-
[OnchainForGasSwapState.FAILED]:
|
|
63
|
-
"The swap has failed after the intermediary already received the BTC on the source chain",
|
|
64
|
-
[OnchainForGasSwapState.REFUNDED]:
|
|
65
|
-
"Swap was refunded and BTC returned to the user's refund address",
|
|
66
|
-
[OnchainForGasSwapState.PR_CREATED]:
|
|
67
|
-
"Swap was created, send the BTC to the swap address",
|
|
68
|
-
[OnchainForGasSwapState.FINISHED]:
|
|
69
|
-
"The swap is finished after the intermediary sent funds on the destination chain",
|
|
70
|
-
[OnchainForGasSwapState.REFUNDABLE]:
|
|
71
|
-
"Swap is refundable because the intermediary cannot honor the swap request on the destination chain",
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export type OnchainForGasSwapInit = ISwapInit & {
|
|
75
|
-
paymentHash: string;
|
|
76
|
-
sequence: bigint;
|
|
77
|
-
address: string;
|
|
78
|
-
inputAmount: bigint;
|
|
79
|
-
outputAmount: bigint;
|
|
80
|
-
recipient: string;
|
|
81
|
-
token: string;
|
|
82
|
-
refundAddress?: string;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
export function isOnchainForGasSwapInit(obj: any): obj is OnchainForGasSwapInit {
|
|
86
|
-
return typeof(obj.paymentHash)==="string" &&
|
|
87
|
-
typeof(obj.sequence)==="bigint" &&
|
|
88
|
-
typeof(obj.address)==="string" &&
|
|
89
|
-
typeof(obj.inputAmount)==="bigint" &&
|
|
90
|
-
typeof(obj.outputAmount)==="bigint" &&
|
|
91
|
-
typeof(obj.recipient)==="string" &&
|
|
92
|
-
typeof(obj.token)==="string" &&
|
|
93
|
-
(obj.refundAddress==null || typeof(obj.refundAddress)==="string") &&
|
|
94
|
-
isISwapInit(obj);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Trusted swap for Bitcoin -> Smart chains, to be used for minor amounts to get gas tokens on the
|
|
99
|
-
* destination chain, which is only needed for Solana, which still uses legacy swaps
|
|
100
|
-
*
|
|
101
|
-
* @category Swaps/Trusted Gas Swaps
|
|
102
|
-
*/
|
|
103
|
-
export class OnchainForGasSwap<T extends ChainType = ChainType> extends ISwap<T, OnchainForGasSwapTypeDefinition<T>, OnchainForGasSwapState> implements IAddressSwap, IBTCWalletSwap {
|
|
104
|
-
protected readonly TYPE: SwapType.TRUSTED_FROM_BTC = SwapType.TRUSTED_FROM_BTC;
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* @internal
|
|
108
|
-
*/
|
|
109
|
-
protected readonly swapStateDescription = OnchainForGasSwapStateDescription;
|
|
110
|
-
/**
|
|
111
|
-
* @internal
|
|
112
|
-
*/
|
|
113
|
-
protected readonly swapStateName = (state: number) => OnchainForGasSwapState[state];
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* @internal
|
|
117
|
-
*/
|
|
118
|
-
protected readonly logger: LoggerType;
|
|
119
|
-
|
|
120
|
-
//State: PR_CREATED
|
|
121
|
-
private readonly paymentHash: string;
|
|
122
|
-
private readonly sequence: bigint;
|
|
123
|
-
private readonly address: string;
|
|
124
|
-
private readonly recipient: string;
|
|
125
|
-
private readonly token: string;
|
|
126
|
-
private inputAmount: bigint;
|
|
127
|
-
private outputAmount: bigint;
|
|
128
|
-
private refundAddress?: string;
|
|
129
|
-
|
|
130
|
-
//State: FINISHED
|
|
131
|
-
/**
|
|
132
|
-
* Destination transaction ID on the smart chain side
|
|
133
|
-
* @private
|
|
134
|
-
*/
|
|
135
|
-
private scTxId?: string;
|
|
136
|
-
/**
|
|
137
|
-
* Source transaction ID on the source (bitcoin) side
|
|
138
|
-
* @private
|
|
139
|
-
*/
|
|
140
|
-
private txId?: string;
|
|
141
|
-
|
|
142
|
-
//State: REFUNDED
|
|
143
|
-
/**
|
|
144
|
-
* Transaction ID on the source (bitcoin) side used for refunding the funds back to the user
|
|
145
|
-
* @private
|
|
146
|
-
*/
|
|
147
|
-
private refundTxId?: string;
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* @internal
|
|
151
|
-
*/
|
|
152
|
-
protected readonly wrapper: OnchainForGasWrapper<T>;
|
|
153
|
-
|
|
154
|
-
constructor(wrapper: OnchainForGasWrapper<T>, init: OnchainForGasSwapInit);
|
|
155
|
-
constructor(wrapper: OnchainForGasWrapper<T>, obj: any);
|
|
156
|
-
constructor(
|
|
157
|
-
wrapper: OnchainForGasWrapper<T>,
|
|
158
|
-
initOrObj: OnchainForGasSwapInit | any
|
|
159
|
-
) {
|
|
160
|
-
if(isOnchainForGasSwapInit(initOrObj) && initOrObj.url!=null) initOrObj.url += "/frombtc_trusted";
|
|
161
|
-
super(wrapper, initOrObj);
|
|
162
|
-
this.wrapper = wrapper;
|
|
163
|
-
if(isOnchainForGasSwapInit(initOrObj)) {
|
|
164
|
-
this.paymentHash = initOrObj.paymentHash;
|
|
165
|
-
this.sequence = initOrObj.sequence;
|
|
166
|
-
this.address = initOrObj.address;
|
|
167
|
-
this.inputAmount = initOrObj.inputAmount;
|
|
168
|
-
this.outputAmount = initOrObj.outputAmount;
|
|
169
|
-
this.recipient = initOrObj.recipient;
|
|
170
|
-
this.token = initOrObj.token;
|
|
171
|
-
this.refundAddress = initOrObj.refundAddress;
|
|
172
|
-
this._state = OnchainForGasSwapState.PR_CREATED;
|
|
173
|
-
} else {
|
|
174
|
-
this.paymentHash = initOrObj.paymentHash;
|
|
175
|
-
this.sequence = toBigInt(initOrObj.sequence);
|
|
176
|
-
this.address = initOrObj.address;
|
|
177
|
-
this.inputAmount = toBigInt(initOrObj.inputAmount);
|
|
178
|
-
this.outputAmount = toBigInt(initOrObj.outputAmount);
|
|
179
|
-
this.recipient = initOrObj.recipient;
|
|
180
|
-
this.token = initOrObj.token;
|
|
181
|
-
this.refundAddress = initOrObj.refundAddress;
|
|
182
|
-
this.scTxId = initOrObj.scTxId;
|
|
183
|
-
this.txId = initOrObj.txId;
|
|
184
|
-
this.refundTxId = initOrObj.refundTxId;
|
|
185
|
-
}
|
|
186
|
-
this.logger = getLogger("OnchainForGas("+this.getId()+"): ");
|
|
187
|
-
this.tryRecomputeSwapPrice();
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* @inheritDoc
|
|
192
|
-
* @internal
|
|
193
|
-
*/
|
|
194
|
-
protected upgradeVersion() {
|
|
195
|
-
if(this.version == null) {
|
|
196
|
-
//Noop
|
|
197
|
-
this.version = 1;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* @inheritDoc
|
|
203
|
-
* @internal
|
|
204
|
-
*/
|
|
205
|
-
protected tryRecomputeSwapPrice() {
|
|
206
|
-
if(this.swapFeeBtc==null && this.swapFee!=null) {
|
|
207
|
-
this.swapFeeBtc = this.swapFee * this.getInput().rawAmount / this.getOutAmountWithoutFee();
|
|
208
|
-
}
|
|
209
|
-
super.tryRecomputeSwapPrice();
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
//////////////////////////////
|
|
214
|
-
//// Getters & utils
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* @inheritDoc
|
|
218
|
-
* @internal
|
|
219
|
-
*/
|
|
220
|
-
_getEscrowHash(): string {
|
|
221
|
-
return this.paymentHash;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* @inheritDoc
|
|
226
|
-
*/
|
|
227
|
-
getOutputAddress(): string | null {
|
|
228
|
-
return this.recipient;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* @inheritDoc
|
|
233
|
-
*/
|
|
234
|
-
getInputAddress(): string | null {
|
|
235
|
-
//TODO: Fuck this, it's not used anyway
|
|
236
|
-
return null;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* @inheritDoc
|
|
241
|
-
*/
|
|
242
|
-
getInputTxId(): string | null {
|
|
243
|
-
return this.txId ?? null;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* @inheritDoc
|
|
248
|
-
*/
|
|
249
|
-
getOutputTxId(): string | null {
|
|
250
|
-
return this.scTxId ?? null;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* @inheritDoc
|
|
255
|
-
*/
|
|
256
|
-
getId(): string {
|
|
257
|
-
return this.paymentHash;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* @inheritDoc
|
|
262
|
-
*/
|
|
263
|
-
getAddress(): string {
|
|
264
|
-
return this.address;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* @inheritDoc
|
|
269
|
-
*/
|
|
270
|
-
getHyperlink(): string {
|
|
271
|
-
return "bitcoin:"+this.address+"?amount="+encodeURIComponent((Number(this.inputAmount)/100000000).toString(10));
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* @inheritDoc
|
|
276
|
-
*/
|
|
277
|
-
requiresAction(): boolean {
|
|
278
|
-
return this._state===OnchainForGasSwapState.REFUNDABLE;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* @inheritDoc
|
|
283
|
-
*/
|
|
284
|
-
isFinished(): boolean {
|
|
285
|
-
return this._state===OnchainForGasSwapState.FINISHED || this._state===OnchainForGasSwapState.FAILED || this._state===OnchainForGasSwapState.EXPIRED || this._state===OnchainForGasSwapState.REFUNDED;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* @inheritDoc
|
|
290
|
-
*/
|
|
291
|
-
isQuoteExpired(): boolean {
|
|
292
|
-
return this._state===OnchainForGasSwapState.EXPIRED;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* @inheritDoc
|
|
297
|
-
*/
|
|
298
|
-
isQuoteSoftExpired(): boolean {
|
|
299
|
-
return this.expiry<Date.now();
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* @inheritDoc
|
|
304
|
-
*/
|
|
305
|
-
isFailed(): boolean {
|
|
306
|
-
return this._state===OnchainForGasSwapState.FAILED;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* @inheritDoc
|
|
311
|
-
*/
|
|
312
|
-
isSuccessful(): boolean {
|
|
313
|
-
return this._state===OnchainForGasSwapState.FINISHED;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* @inheritDoc
|
|
318
|
-
*/
|
|
319
|
-
isInProgress(): boolean {
|
|
320
|
-
return (this._state===OnchainForGasSwapState.PR_CREATED && this.txId!=null) ||
|
|
321
|
-
(this._state===OnchainForGasSwapState.REFUNDABLE && this.refundAddress!=null);
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* @inheritDoc
|
|
326
|
-
* @internal
|
|
327
|
-
*/
|
|
328
|
-
_verifyQuoteDefinitelyExpired(): Promise<boolean> {
|
|
329
|
-
return Promise.resolve(this.expiry<Date.now());
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* @inheritDoc
|
|
334
|
-
* @internal
|
|
335
|
-
*/
|
|
336
|
-
_verifyQuoteValid(): Promise<boolean> {
|
|
337
|
-
return Promise.resolve(this.expiry>Date.now());
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
//////////////////////////////
|
|
342
|
-
//// Amounts & fees
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Returns an output amount in base units without a swap fee included, hence this value
|
|
346
|
-
* is larger than the actual output amount
|
|
347
|
-
*
|
|
348
|
-
* @internal
|
|
349
|
-
*/
|
|
350
|
-
protected getOutAmountWithoutFee(): bigint {
|
|
351
|
-
return this.outputAmount + (this.swapFee ?? 0n);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* @inheritDoc
|
|
356
|
-
*/
|
|
357
|
-
getOutputToken(): SCToken<T["ChainId"]> {
|
|
358
|
-
return this.wrapper._tokens[this.wrapper._chain.getNativeCurrencyAddress()];
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* @inheritDoc
|
|
363
|
-
*/
|
|
364
|
-
getOutput(): TokenAmount<SCToken<T["ChainId"]>, true> {
|
|
365
|
-
return toTokenAmount(
|
|
366
|
-
this.outputAmount, this.wrapper._tokens[this.wrapper._chain.getNativeCurrencyAddress()],
|
|
367
|
-
this.wrapper._prices, this.pricingInfo
|
|
368
|
-
);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* @inheritDoc
|
|
373
|
-
*/
|
|
374
|
-
getInputToken(): BtcToken<false> {
|
|
375
|
-
return BitcoinTokens.BTC;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* @inheritDoc
|
|
380
|
-
*/
|
|
381
|
-
getInput(): TokenAmount<BtcToken<false>, true> {
|
|
382
|
-
return toTokenAmount(this.inputAmount, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo);
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* @inheritDoc
|
|
387
|
-
*/
|
|
388
|
-
getInputWithoutFee(): TokenAmount<BtcToken<false>, true> {
|
|
389
|
-
return toTokenAmount(
|
|
390
|
-
this.inputAmount - (this.swapFeeBtc ?? 0n), BitcoinTokens.BTC,
|
|
391
|
-
this.wrapper._prices, this.pricingInfo
|
|
392
|
-
);
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* Returns the swap fee charged by the intermediary (LP) on this swap
|
|
397
|
-
*
|
|
398
|
-
* @internal
|
|
399
|
-
*/
|
|
400
|
-
protected getSwapFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
401
|
-
if(this.pricingInfo==null) throw new Error("No pricing info known!");
|
|
402
|
-
const feeWithoutBaseFee = this.swapFeeBtc==null ? 0n : this.swapFeeBtc - this.pricingInfo.satsBaseFee;
|
|
403
|
-
const swapFeePPM = feeWithoutBaseFee * 1000000n / this.getInputWithoutFee().rawAmount;
|
|
404
|
-
|
|
405
|
-
const amountInSrcToken = toTokenAmount(
|
|
406
|
-
this.swapFeeBtc ?? 0n, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo
|
|
407
|
-
);
|
|
408
|
-
return {
|
|
409
|
-
amountInSrcToken,
|
|
410
|
-
amountInDstToken: toTokenAmount(
|
|
411
|
-
this.swapFee ?? 0n, this.wrapper._tokens[this.wrapper._chain.getNativeCurrencyAddress()],
|
|
412
|
-
this.wrapper._prices, this.pricingInfo
|
|
413
|
-
),
|
|
414
|
-
currentUsdValue: amountInSrcToken.currentUsdValue,
|
|
415
|
-
usdValue: amountInSrcToken.usdValue,
|
|
416
|
-
pastUsdValue: amountInSrcToken.pastUsdValue,
|
|
417
|
-
composition: {
|
|
418
|
-
base: toTokenAmount(this.pricingInfo.satsBaseFee, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo),
|
|
419
|
-
percentage: ppmToPercentage(swapFeePPM)
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* @inheritDoc
|
|
426
|
-
*/
|
|
427
|
-
getFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
428
|
-
return this.getSwapFee();
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* @inheritDoc
|
|
433
|
-
*/
|
|
434
|
-
getFeeBreakdown(): [{type: FeeType.SWAP, fee: Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>>}] {
|
|
435
|
-
return [{
|
|
436
|
-
type: FeeType.SWAP,
|
|
437
|
-
fee: this.getSwapFee()
|
|
438
|
-
}];
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* @inheritDoc
|
|
443
|
-
*/
|
|
444
|
-
getRequiredConfirmationsCount(): number {
|
|
445
|
-
return 1;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
/**
|
|
449
|
-
* @inheritDoc
|
|
450
|
-
*/
|
|
451
|
-
async getFundedPsbt(
|
|
452
|
-
_bitcoinWallet: IBitcoinWallet | MinimalBitcoinWalletInterface,
|
|
453
|
-
feeRate?: number,
|
|
454
|
-
additionalOutputs?: ({amount: bigint, outputScript: Uint8Array} | {amount: bigint, address: string})[]
|
|
455
|
-
): Promise<{psbt: Transaction, psbtHex: string, psbtBase64: string, signInputs: number[]}> {
|
|
456
|
-
if(this._state!==OnchainForGasSwapState.PR_CREATED)
|
|
457
|
-
throw new Error("Swap already paid for!");
|
|
458
|
-
|
|
459
|
-
let bitcoinWallet: IBitcoinWallet;
|
|
460
|
-
if(isIBitcoinWallet(_bitcoinWallet)) {
|
|
461
|
-
bitcoinWallet = _bitcoinWallet;
|
|
462
|
-
} else {
|
|
463
|
-
bitcoinWallet = new SingleAddressBitcoinWallet(this.wrapper._btcRpc, this.wrapper._options.bitcoinNetwork, _bitcoinWallet);
|
|
464
|
-
}
|
|
465
|
-
//TODO: Maybe re-introduce fee rate check here if passed from the user
|
|
466
|
-
if(feeRate==null) {
|
|
467
|
-
feeRate = await bitcoinWallet.getFeeRate();
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
const basePsbt = new Transaction({
|
|
471
|
-
allowUnknownOutputs: true,
|
|
472
|
-
allowLegacyWitnessUtxo: true
|
|
473
|
-
});
|
|
474
|
-
basePsbt.addOutput({
|
|
475
|
-
amount: this.outputAmount,
|
|
476
|
-
script: toOutputScript(this.wrapper._options.bitcoinNetwork, this.address)
|
|
477
|
-
});
|
|
478
|
-
if(additionalOutputs!=null) additionalOutputs.forEach(output => {
|
|
479
|
-
basePsbt.addOutput({
|
|
480
|
-
amount: output.amount,
|
|
481
|
-
script: (output as {outputScript: Uint8Array}).outputScript ?? toOutputScript(this.wrapper._options.bitcoinNetwork, (output as {address: string}).address)
|
|
482
|
-
});
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
const psbt = await bitcoinWallet.fundPsbt(basePsbt, feeRate);
|
|
486
|
-
//Sign every input
|
|
487
|
-
const signInputs: number[] = [];
|
|
488
|
-
for(let i=0;i<psbt.inputsLength;i++) {
|
|
489
|
-
signInputs.push(i);
|
|
490
|
-
}
|
|
491
|
-
const serializedPsbt = Buffer.from(psbt.toPSBT());
|
|
492
|
-
return {
|
|
493
|
-
psbt,
|
|
494
|
-
psbtHex: serializedPsbt.toString("hex"),
|
|
495
|
-
psbtBase64: serializedPsbt.toString("base64"),
|
|
496
|
-
signInputs
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* @inheritDoc
|
|
502
|
-
*/
|
|
503
|
-
async submitPsbt(_psbt: Transaction | string): Promise<string> {
|
|
504
|
-
const psbt = parsePsbtTransaction(_psbt);
|
|
505
|
-
if(this._state!==OnchainForGasSwapState.PR_CREATED)
|
|
506
|
-
throw new Error("Swap already paid for!");
|
|
507
|
-
|
|
508
|
-
//Ensure not expired
|
|
509
|
-
if(this.expiry<Date.now()) {
|
|
510
|
-
throw new Error("Swap expired!");
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
const output0 = psbt.getOutput(0);
|
|
514
|
-
if(output0.amount!==this.outputAmount)
|
|
515
|
-
throw new Error("PSBT output amount invalid, expected: "+this.outputAmount+" got: "+output0.amount);
|
|
516
|
-
const expectedOutputScript = toOutputScript(this.wrapper._options.bitcoinNetwork, this.address);
|
|
517
|
-
if(output0.script==null || !expectedOutputScript.equals(output0.script))
|
|
518
|
-
throw new Error("PSBT output script invalid!");
|
|
519
|
-
|
|
520
|
-
if(!psbt.isFinal) psbt.finalize();
|
|
521
|
-
|
|
522
|
-
return await this.wrapper._btcRpc.sendRawTransaction(Buffer.from(psbt.toBytes(true, true)).toString("hex"));
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
/**
|
|
526
|
-
* @inheritDoc
|
|
527
|
-
*/
|
|
528
|
-
async estimateBitcoinFee(_bitcoinWallet: IBitcoinWallet | MinimalBitcoinWalletInterface, feeRate?: number): Promise<TokenAmount<BtcToken<false>, true> | null> {
|
|
529
|
-
const bitcoinWallet: IBitcoinWallet = toBitcoinWallet(_bitcoinWallet, this.wrapper._btcRpc, this.wrapper._options.bitcoinNetwork);
|
|
530
|
-
const txFee = await bitcoinWallet.getTransactionFee(this.address, this.inputAmount, feeRate);
|
|
531
|
-
if(txFee==null) return null;
|
|
532
|
-
return toTokenAmount(BigInt(txFee), BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo);
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* @inheritDoc
|
|
537
|
-
*/
|
|
538
|
-
async sendBitcoinTransaction(wallet: IBitcoinWallet | MinimalBitcoinWalletInterfaceWithSigner, feeRate?: number): Promise<string> {
|
|
539
|
-
if(this._state!==OnchainForGasSwapState.PR_CREATED)
|
|
540
|
-
throw new Error("Swap already paid for!");
|
|
541
|
-
|
|
542
|
-
//Ensure not expired
|
|
543
|
-
if(this.expiry<Date.now()) {
|
|
544
|
-
throw new Error("Swap expired!");
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
if(isIBitcoinWallet(wallet)) {
|
|
548
|
-
return await wallet.sendTransaction(this.address, this.inputAmount, feeRate);
|
|
549
|
-
} else {
|
|
550
|
-
const {psbt, psbtHex, psbtBase64, signInputs} = await this.getFundedPsbt(wallet, feeRate);
|
|
551
|
-
const signedPsbt = await wallet.signPsbt({
|
|
552
|
-
psbt, psbtHex, psbtBase64
|
|
553
|
-
}, signInputs);
|
|
554
|
-
return await this.submitPsbt(signedPsbt);
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
/**
|
|
559
|
-
* @inheritDoc
|
|
560
|
-
*
|
|
561
|
-
* @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
|
|
562
|
-
* if not provided an address is returned instead.
|
|
563
|
-
*/
|
|
564
|
-
async getExecutionAction(options?: {
|
|
565
|
-
bitcoinWallet?: MinimalBitcoinWalletInterface
|
|
566
|
-
}): Promise<never> {
|
|
567
|
-
throw new Error("Not supported");
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
/**
|
|
571
|
-
* @inheritDoc
|
|
572
|
-
*/
|
|
573
|
-
async getExecutionSteps(): Promise<never> {
|
|
574
|
-
throw new Error("Not supported");
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
/**
|
|
578
|
-
* @inheritDoc
|
|
579
|
-
*/
|
|
580
|
-
async getExecutionStatus(): Promise<never> {
|
|
581
|
-
throw new Error("Not supported");
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
/**
|
|
585
|
-
* @internal
|
|
586
|
-
* @inheritDoc
|
|
587
|
-
*/
|
|
588
|
-
_submitExecutionTransactions(): Promise<string[]> {
|
|
589
|
-
throw new Error("Not supported");
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
/**
|
|
593
|
-
* @remarks Not supported
|
|
594
|
-
*/
|
|
595
|
-
async execute(): Promise<boolean> {
|
|
596
|
-
throw new Error("Not supported");
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
//////////////////////////////
|
|
600
|
-
//// Payment
|
|
601
|
-
|
|
602
|
-
/**
|
|
603
|
-
* Queries the intermediary (LP) node for the state of the swap
|
|
604
|
-
*
|
|
605
|
-
* @param save Whether the save the result or not
|
|
606
|
-
*
|
|
607
|
-
* @returns Whether the swap was successful as `boolean` or `null` if the swap is still pending
|
|
608
|
-
* @internal
|
|
609
|
-
*/
|
|
610
|
-
protected async checkAddress(save: boolean = true): Promise<boolean | null> {
|
|
611
|
-
if(
|
|
612
|
-
this._state===OnchainForGasSwapState.FAILED ||
|
|
613
|
-
this._state===OnchainForGasSwapState.EXPIRED ||
|
|
614
|
-
this._state===OnchainForGasSwapState.REFUNDED
|
|
615
|
-
) return false;
|
|
616
|
-
if(this._state===OnchainForGasSwapState.FINISHED) return false;
|
|
617
|
-
if(this.url==null) return false;
|
|
618
|
-
|
|
619
|
-
const response = await this.wrapper._lpApi.getTrustedAddressStatus(
|
|
620
|
-
this.url, this.paymentHash, this.sequence, this.wrapper._options.getRequestTimeout
|
|
621
|
-
);
|
|
622
|
-
switch(response.code) {
|
|
623
|
-
case TrustedAddressStatusResponseCodes.AWAIT_PAYMENT:
|
|
624
|
-
if(this.txId!=null) {
|
|
625
|
-
this.txId = undefined;
|
|
626
|
-
if(save) await this._save();
|
|
627
|
-
return true;
|
|
628
|
-
}
|
|
629
|
-
return false;
|
|
630
|
-
case TrustedAddressStatusResponseCodes.AWAIT_CONFIRMATION:
|
|
631
|
-
case TrustedAddressStatusResponseCodes.PENDING:
|
|
632
|
-
case TrustedAddressStatusResponseCodes.TX_SENT:
|
|
633
|
-
const inputAmount = BigInt(response.data.adjustedAmount);
|
|
634
|
-
const outputAmount = BigInt(response.data.adjustedTotal);
|
|
635
|
-
const adjustedFee = response.data.adjustedFee==null ? null : BigInt(response.data.adjustedFee);
|
|
636
|
-
const adjustedFeeSats = response.data.adjustedFeeSats==null ? null : BigInt(response.data.adjustedFeeSats);
|
|
637
|
-
const txId = response.data.txId;
|
|
638
|
-
if(
|
|
639
|
-
this.txId!=txId ||
|
|
640
|
-
this.inputAmount !== inputAmount ||
|
|
641
|
-
this.outputAmount !== outputAmount
|
|
642
|
-
) {
|
|
643
|
-
this.txId = txId;
|
|
644
|
-
this.inputAmount = inputAmount;
|
|
645
|
-
this.outputAmount = outputAmount;
|
|
646
|
-
if(adjustedFee!=null) this.swapFee = adjustedFee;
|
|
647
|
-
if(adjustedFeeSats!=null) this.swapFeeBtc = adjustedFeeSats;
|
|
648
|
-
if(save) await this._save();
|
|
649
|
-
return true;
|
|
650
|
-
}
|
|
651
|
-
return false;
|
|
652
|
-
case TrustedAddressStatusResponseCodes.PAID:
|
|
653
|
-
const txStatus = await this.wrapper._chain.getTxIdStatus(response.data.txId);
|
|
654
|
-
if(txStatus==="success") {
|
|
655
|
-
this._state = OnchainForGasSwapState.FINISHED;
|
|
656
|
-
this.scTxId = response.data.txId;
|
|
657
|
-
if(save) await this._saveAndEmit();
|
|
658
|
-
return true;
|
|
659
|
-
}
|
|
660
|
-
return false;
|
|
661
|
-
case TrustedAddressStatusResponseCodes.EXPIRED:
|
|
662
|
-
this._state = OnchainForGasSwapState.EXPIRED;
|
|
663
|
-
if(save) await this._saveAndEmit();
|
|
664
|
-
return true;
|
|
665
|
-
case TrustedAddressStatusResponseCodes.REFUNDABLE:
|
|
666
|
-
if(this._state===OnchainForGasSwapState.REFUNDABLE) return null;
|
|
667
|
-
this._state = OnchainForGasSwapState.REFUNDABLE;
|
|
668
|
-
if(save) await this._saveAndEmit();
|
|
669
|
-
return true;
|
|
670
|
-
case TrustedAddressStatusResponseCodes.REFUNDED:
|
|
671
|
-
this._state = OnchainForGasSwapState.REFUNDED;
|
|
672
|
-
this.refundTxId = response.data.txId;
|
|
673
|
-
if(save) await this._saveAndEmit();
|
|
674
|
-
return true;
|
|
675
|
-
default:
|
|
676
|
-
this._state = OnchainForGasSwapState.FAILED;
|
|
677
|
-
if(save) await this._saveAndEmit();
|
|
678
|
-
return true;
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
/**
|
|
683
|
-
* Sets the bitcoin address used for possible refunds in case something goes wrong with the swap
|
|
684
|
-
*
|
|
685
|
-
* @param refundAddress Bitcoin address to receive the refund to
|
|
686
|
-
* @internal
|
|
687
|
-
*/
|
|
688
|
-
protected async setRefundAddress(refundAddress: string): Promise<void> {
|
|
689
|
-
if(this.refundAddress!=null) {
|
|
690
|
-
if(this.refundAddress!==refundAddress) throw new Error("Different refund address already set!");
|
|
691
|
-
return;
|
|
692
|
-
}
|
|
693
|
-
if(this.url==null) throw new Error("LP URL not known, cannot set refund address!");
|
|
694
|
-
await this.wrapper._lpApi.setTrustedRefundAddress(
|
|
695
|
-
this.url, this.paymentHash, this.sequence, refundAddress, this.wrapper._options.getRequestTimeout
|
|
696
|
-
);
|
|
697
|
-
this.refundAddress = refundAddress;
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
/**
|
|
701
|
-
* @inheritDoc
|
|
702
|
-
*/
|
|
703
|
-
async waitForBitcoinTransaction(
|
|
704
|
-
updateCallback?: (txId?: string, confirmations?: number, targetConfirmations?: number, txEtaMs?: number) => void,
|
|
705
|
-
checkIntervalSeconds: number = 5,
|
|
706
|
-
abortSignal?: AbortSignal
|
|
707
|
-
): Promise<string> {
|
|
708
|
-
if(this._state!==OnchainForGasSwapState.PR_CREATED) throw new Error("Must be in PR_CREATED state!");
|
|
709
|
-
|
|
710
|
-
if(!this.initiated) {
|
|
711
|
-
this.initiated = true;
|
|
712
|
-
await this._saveAndEmit();
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
while(
|
|
716
|
-
!abortSignal?.aborted &&
|
|
717
|
-
this._state===OnchainForGasSwapState.PR_CREATED
|
|
718
|
-
) {
|
|
719
|
-
await this.checkAddress(true);
|
|
720
|
-
if(this.txId!=null && updateCallback!=null) {
|
|
721
|
-
const res = await this.wrapper._btcRpc.getTransaction(this.txId);
|
|
722
|
-
if(res==null) {
|
|
723
|
-
updateCallback();
|
|
724
|
-
} else if(res.confirmations!=null && res.confirmations>0) {
|
|
725
|
-
updateCallback(res.txid, res.confirmations, 1, 0);
|
|
726
|
-
} else {
|
|
727
|
-
const delay = await this.wrapper._btcRpc.getConfirmationDelay(res, 1);
|
|
728
|
-
updateCallback(res.txid, 0, 1, delay ?? undefined);
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
if(this._state===OnchainForGasSwapState.PR_CREATED)
|
|
732
|
-
await timeoutPromise(checkIntervalSeconds*1000, abortSignal);
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
if(
|
|
736
|
-
(this._state as OnchainForGasSwapState)===OnchainForGasSwapState.REFUNDABLE ||
|
|
737
|
-
(this._state as OnchainForGasSwapState)===OnchainForGasSwapState.REFUNDED
|
|
738
|
-
) return this.txId!;
|
|
739
|
-
if(this.isQuoteExpired()) throw new Error("Swap expired");
|
|
740
|
-
if(this.isFailed()) throw new Error("Swap failed");
|
|
741
|
-
return this.txId!;
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
/**
|
|
745
|
-
* Waits till the LP processes a refund for a failed swap. The swap must be in
|
|
746
|
-
* {@link OnchainForGasSwapState.REFUNDABLE} state
|
|
747
|
-
*
|
|
748
|
-
* @param checkIntervalSeconds How often to check (default 5 seconds)
|
|
749
|
-
* @param abortSignal Abort signal
|
|
750
|
-
*/
|
|
751
|
-
async waitTillRefunded(
|
|
752
|
-
checkIntervalSeconds?: number,
|
|
753
|
-
abortSignal?: AbortSignal
|
|
754
|
-
): Promise<void> {
|
|
755
|
-
checkIntervalSeconds ??= 5;
|
|
756
|
-
if(this._state===OnchainForGasSwapState.REFUNDED) return;
|
|
757
|
-
if(this._state!==OnchainForGasSwapState.REFUNDABLE) throw new Error("Must be in REFUNDABLE state!");
|
|
758
|
-
|
|
759
|
-
while(
|
|
760
|
-
!abortSignal?.aborted &&
|
|
761
|
-
this._state===OnchainForGasSwapState.REFUNDABLE
|
|
762
|
-
) {
|
|
763
|
-
await this.checkAddress(true);
|
|
764
|
-
if(this._state===OnchainForGasSwapState.REFUNDABLE)
|
|
765
|
-
await timeoutPromise(checkIntervalSeconds*1000, abortSignal);
|
|
766
|
-
}
|
|
767
|
-
if(this.isQuoteExpired()) throw new Error("Swap expired");
|
|
768
|
-
if(this.isFailed()) throw new Error("Swap failed");
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
/**
|
|
772
|
-
* Requests a refund after the swap failed, this also waits till the refund is actually sent by the
|
|
773
|
-
* intermediary (LP). The swap must be in {@link OnchainForGasSwapState.REFUNDABLE} state
|
|
774
|
-
*
|
|
775
|
-
* @param refundAddress Bitcoin address to receive the refund to
|
|
776
|
-
* @param abortSignal Abort signal
|
|
777
|
-
*/
|
|
778
|
-
async requestRefund(refundAddress?: string, abortSignal?: AbortSignal): Promise<void> {
|
|
779
|
-
if(refundAddress!=null) await this.setRefundAddress(refundAddress);
|
|
780
|
-
await this.waitTillRefunded(undefined, abortSignal);
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
//////////////////////////////
|
|
785
|
-
//// Storage
|
|
786
|
-
|
|
787
|
-
/**
|
|
788
|
-
* @inheritDoc
|
|
789
|
-
*/
|
|
790
|
-
serialize(): any{
|
|
791
|
-
return {
|
|
792
|
-
...super.serialize(),
|
|
793
|
-
paymentHash: this.paymentHash,
|
|
794
|
-
sequence: this.sequence==null ? null : this.sequence.toString(10),
|
|
795
|
-
address: this.address,
|
|
796
|
-
inputAmount: this.inputAmount==null ? null : this.inputAmount.toString(10),
|
|
797
|
-
outputAmount: this.outputAmount==null ? null : this.outputAmount.toString(10),
|
|
798
|
-
recipient: this.recipient,
|
|
799
|
-
token: this.token,
|
|
800
|
-
refundAddress: this.refundAddress,
|
|
801
|
-
scTxId: this.scTxId,
|
|
802
|
-
txId: this.txId,
|
|
803
|
-
refundTxId: this.refundTxId,
|
|
804
|
-
};
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
/**
|
|
808
|
-
* @inheritDoc
|
|
809
|
-
* @internal
|
|
810
|
-
*/
|
|
811
|
-
_getInitiator(): string {
|
|
812
|
-
return this.recipient;
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
//////////////////////////////
|
|
817
|
-
//// Swap ticks & sync
|
|
818
|
-
|
|
819
|
-
/**
|
|
820
|
-
* @inheritDoc
|
|
821
|
-
* @internal
|
|
822
|
-
*/
|
|
823
|
-
async _sync(save?: boolean): Promise<boolean> {
|
|
824
|
-
if(this._state===OnchainForGasSwapState.PR_CREATED) {
|
|
825
|
-
//Check if it's maybe already paid
|
|
826
|
-
const result = await this.checkAddress(false);
|
|
827
|
-
if(result) {
|
|
828
|
-
if(save) await this._saveAndEmit();
|
|
829
|
-
return true;
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
return false;
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
/**
|
|
836
|
-
* @inheritDoc
|
|
837
|
-
* @internal
|
|
838
|
-
*/
|
|
839
|
-
_tick(save?: boolean): Promise<boolean> {
|
|
840
|
-
return Promise.resolve(false);
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
}
|
|
1
|
+
import {SwapType} from "../../../enums/SwapType";
|
|
2
|
+
import {ChainType} from "@atomiqlabs/base";
|
|
3
|
+
import {toBigInt} from "../../../utils/Utils";
|
|
4
|
+
import {parsePsbtTransaction, toOutputScript} from "../../../utils/BitcoinUtils";
|
|
5
|
+
import {isISwapInit, ISwap, ISwapInit} from "../../ISwap";
|
|
6
|
+
import {TrustedAddressStatusResponseCodes} from "../../../intermediaries/apis/IntermediaryAPI";
|
|
7
|
+
import {OnchainForGasSwapTypeDefinition, OnchainForGasWrapper} from "./OnchainForGasWrapper";
|
|
8
|
+
import {Fee} from "../../../types/fees/Fee";
|
|
9
|
+
import {IBitcoinWallet, isIBitcoinWallet} from "../../../bitcoin/wallet/IBitcoinWallet";
|
|
10
|
+
import {IAddressSwap} from "../../IAddressSwap";
|
|
11
|
+
import {IBTCWalletSwap} from "../../IBTCWalletSwap";
|
|
12
|
+
import {Transaction} from "@scure/btc-signer";
|
|
13
|
+
import {SingleAddressBitcoinWallet} from "../../../bitcoin/wallet/SingleAddressBitcoinWallet";
|
|
14
|
+
import {Buffer} from "buffer";
|
|
15
|
+
import {
|
|
16
|
+
MinimalBitcoinWalletInterface,
|
|
17
|
+
MinimalBitcoinWalletInterfaceWithSigner
|
|
18
|
+
} from "../../../types/wallets/MinimalBitcoinWalletInterface";
|
|
19
|
+
import {FeeType} from "../../../enums/FeeType";
|
|
20
|
+
import {ppmToPercentage} from "../../../types/fees/PercentagePPM";
|
|
21
|
+
import {TokenAmount, toTokenAmount} from "../../../types/TokenAmount";
|
|
22
|
+
import {BitcoinTokens, BtcToken, SCToken} from "../../../types/Token";
|
|
23
|
+
import {getLogger, LoggerType} from "../../../utils/Logger";
|
|
24
|
+
import {timeoutPromise} from "../../../utils/TimeoutUtils";
|
|
25
|
+
import {toBitcoinWallet} from "../../../utils/BitcoinWalletUtils";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* State enum for trusted on-chain gas swaps
|
|
29
|
+
*
|
|
30
|
+
* @category Swaps/Trusted Gas Swaps
|
|
31
|
+
*/
|
|
32
|
+
export enum OnchainForGasSwapState {
|
|
33
|
+
/**
|
|
34
|
+
* The swap quote expired without user sending in the BTC
|
|
35
|
+
*/
|
|
36
|
+
EXPIRED = -3,
|
|
37
|
+
/**
|
|
38
|
+
* The swap has failed after the intermediary already received the BTC on the source chain
|
|
39
|
+
*/
|
|
40
|
+
FAILED = -2,
|
|
41
|
+
/**
|
|
42
|
+
* Swap was refunded and BTC returned to the user's refund address
|
|
43
|
+
*/
|
|
44
|
+
REFUNDED = -1,
|
|
45
|
+
/**
|
|
46
|
+
* Swap was created, send the BTC to the swap address
|
|
47
|
+
*/
|
|
48
|
+
PR_CREATED = 0,
|
|
49
|
+
/**
|
|
50
|
+
* The swap is finished after the intermediary sent funds on the destination chain
|
|
51
|
+
*/
|
|
52
|
+
FINISHED = 1,
|
|
53
|
+
/**
|
|
54
|
+
* Swap is refundable because the intermediary cannot honor the swap request on the destination chain
|
|
55
|
+
*/
|
|
56
|
+
REFUNDABLE = 2
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const OnchainForGasSwapStateDescription: Record<OnchainForGasSwapState, string> = {
|
|
60
|
+
[OnchainForGasSwapState.EXPIRED]:
|
|
61
|
+
"The swap quote expired without user sending in the BTC",
|
|
62
|
+
[OnchainForGasSwapState.FAILED]:
|
|
63
|
+
"The swap has failed after the intermediary already received the BTC on the source chain",
|
|
64
|
+
[OnchainForGasSwapState.REFUNDED]:
|
|
65
|
+
"Swap was refunded and BTC returned to the user's refund address",
|
|
66
|
+
[OnchainForGasSwapState.PR_CREATED]:
|
|
67
|
+
"Swap was created, send the BTC to the swap address",
|
|
68
|
+
[OnchainForGasSwapState.FINISHED]:
|
|
69
|
+
"The swap is finished after the intermediary sent funds on the destination chain",
|
|
70
|
+
[OnchainForGasSwapState.REFUNDABLE]:
|
|
71
|
+
"Swap is refundable because the intermediary cannot honor the swap request on the destination chain",
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export type OnchainForGasSwapInit = ISwapInit & {
|
|
75
|
+
paymentHash: string;
|
|
76
|
+
sequence: bigint;
|
|
77
|
+
address: string;
|
|
78
|
+
inputAmount: bigint;
|
|
79
|
+
outputAmount: bigint;
|
|
80
|
+
recipient: string;
|
|
81
|
+
token: string;
|
|
82
|
+
refundAddress?: string;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export function isOnchainForGasSwapInit(obj: any): obj is OnchainForGasSwapInit {
|
|
86
|
+
return typeof(obj.paymentHash)==="string" &&
|
|
87
|
+
typeof(obj.sequence)==="bigint" &&
|
|
88
|
+
typeof(obj.address)==="string" &&
|
|
89
|
+
typeof(obj.inputAmount)==="bigint" &&
|
|
90
|
+
typeof(obj.outputAmount)==="bigint" &&
|
|
91
|
+
typeof(obj.recipient)==="string" &&
|
|
92
|
+
typeof(obj.token)==="string" &&
|
|
93
|
+
(obj.refundAddress==null || typeof(obj.refundAddress)==="string") &&
|
|
94
|
+
isISwapInit(obj);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Trusted swap for Bitcoin -> Smart chains, to be used for minor amounts to get gas tokens on the
|
|
99
|
+
* destination chain, which is only needed for Solana, which still uses legacy swaps
|
|
100
|
+
*
|
|
101
|
+
* @category Swaps/Trusted Gas Swaps
|
|
102
|
+
*/
|
|
103
|
+
export class OnchainForGasSwap<T extends ChainType = ChainType> extends ISwap<T, OnchainForGasSwapTypeDefinition<T>, OnchainForGasSwapState> implements IAddressSwap, IBTCWalletSwap {
|
|
104
|
+
protected readonly TYPE: SwapType.TRUSTED_FROM_BTC = SwapType.TRUSTED_FROM_BTC;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @internal
|
|
108
|
+
*/
|
|
109
|
+
protected readonly swapStateDescription = OnchainForGasSwapStateDescription;
|
|
110
|
+
/**
|
|
111
|
+
* @internal
|
|
112
|
+
*/
|
|
113
|
+
protected readonly swapStateName = (state: number) => OnchainForGasSwapState[state];
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @internal
|
|
117
|
+
*/
|
|
118
|
+
protected readonly logger: LoggerType;
|
|
119
|
+
|
|
120
|
+
//State: PR_CREATED
|
|
121
|
+
private readonly paymentHash: string;
|
|
122
|
+
private readonly sequence: bigint;
|
|
123
|
+
private readonly address: string;
|
|
124
|
+
private readonly recipient: string;
|
|
125
|
+
private readonly token: string;
|
|
126
|
+
private inputAmount: bigint;
|
|
127
|
+
private outputAmount: bigint;
|
|
128
|
+
private refundAddress?: string;
|
|
129
|
+
|
|
130
|
+
//State: FINISHED
|
|
131
|
+
/**
|
|
132
|
+
* Destination transaction ID on the smart chain side
|
|
133
|
+
* @private
|
|
134
|
+
*/
|
|
135
|
+
private scTxId?: string;
|
|
136
|
+
/**
|
|
137
|
+
* Source transaction ID on the source (bitcoin) side
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
private txId?: string;
|
|
141
|
+
|
|
142
|
+
//State: REFUNDED
|
|
143
|
+
/**
|
|
144
|
+
* Transaction ID on the source (bitcoin) side used for refunding the funds back to the user
|
|
145
|
+
* @private
|
|
146
|
+
*/
|
|
147
|
+
private refundTxId?: string;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @internal
|
|
151
|
+
*/
|
|
152
|
+
protected readonly wrapper: OnchainForGasWrapper<T>;
|
|
153
|
+
|
|
154
|
+
constructor(wrapper: OnchainForGasWrapper<T>, init: OnchainForGasSwapInit);
|
|
155
|
+
constructor(wrapper: OnchainForGasWrapper<T>, obj: any);
|
|
156
|
+
constructor(
|
|
157
|
+
wrapper: OnchainForGasWrapper<T>,
|
|
158
|
+
initOrObj: OnchainForGasSwapInit | any
|
|
159
|
+
) {
|
|
160
|
+
if(isOnchainForGasSwapInit(initOrObj) && initOrObj.url!=null) initOrObj.url += "/frombtc_trusted";
|
|
161
|
+
super(wrapper, initOrObj);
|
|
162
|
+
this.wrapper = wrapper;
|
|
163
|
+
if(isOnchainForGasSwapInit(initOrObj)) {
|
|
164
|
+
this.paymentHash = initOrObj.paymentHash;
|
|
165
|
+
this.sequence = initOrObj.sequence;
|
|
166
|
+
this.address = initOrObj.address;
|
|
167
|
+
this.inputAmount = initOrObj.inputAmount;
|
|
168
|
+
this.outputAmount = initOrObj.outputAmount;
|
|
169
|
+
this.recipient = initOrObj.recipient;
|
|
170
|
+
this.token = initOrObj.token;
|
|
171
|
+
this.refundAddress = initOrObj.refundAddress;
|
|
172
|
+
this._state = OnchainForGasSwapState.PR_CREATED;
|
|
173
|
+
} else {
|
|
174
|
+
this.paymentHash = initOrObj.paymentHash;
|
|
175
|
+
this.sequence = toBigInt(initOrObj.sequence);
|
|
176
|
+
this.address = initOrObj.address;
|
|
177
|
+
this.inputAmount = toBigInt(initOrObj.inputAmount);
|
|
178
|
+
this.outputAmount = toBigInt(initOrObj.outputAmount);
|
|
179
|
+
this.recipient = initOrObj.recipient;
|
|
180
|
+
this.token = initOrObj.token;
|
|
181
|
+
this.refundAddress = initOrObj.refundAddress;
|
|
182
|
+
this.scTxId = initOrObj.scTxId;
|
|
183
|
+
this.txId = initOrObj.txId;
|
|
184
|
+
this.refundTxId = initOrObj.refundTxId;
|
|
185
|
+
}
|
|
186
|
+
this.logger = getLogger("OnchainForGas("+this.getId()+"): ");
|
|
187
|
+
this.tryRecomputeSwapPrice();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @inheritDoc
|
|
192
|
+
* @internal
|
|
193
|
+
*/
|
|
194
|
+
protected upgradeVersion() {
|
|
195
|
+
if(this.version == null) {
|
|
196
|
+
//Noop
|
|
197
|
+
this.version = 1;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @inheritDoc
|
|
203
|
+
* @internal
|
|
204
|
+
*/
|
|
205
|
+
protected tryRecomputeSwapPrice() {
|
|
206
|
+
if(this.swapFeeBtc==null && this.swapFee!=null) {
|
|
207
|
+
this.swapFeeBtc = this.swapFee * this.getInput().rawAmount / this.getOutAmountWithoutFee();
|
|
208
|
+
}
|
|
209
|
+
super.tryRecomputeSwapPrice();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
//////////////////////////////
|
|
214
|
+
//// Getters & utils
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @inheritDoc
|
|
218
|
+
* @internal
|
|
219
|
+
*/
|
|
220
|
+
_getEscrowHash(): string {
|
|
221
|
+
return this.paymentHash;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* @inheritDoc
|
|
226
|
+
*/
|
|
227
|
+
getOutputAddress(): string | null {
|
|
228
|
+
return this.recipient;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @inheritDoc
|
|
233
|
+
*/
|
|
234
|
+
getInputAddress(): string | null {
|
|
235
|
+
//TODO: Fuck this, it's not used anyway
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* @inheritDoc
|
|
241
|
+
*/
|
|
242
|
+
getInputTxId(): string | null {
|
|
243
|
+
return this.txId ?? null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @inheritDoc
|
|
248
|
+
*/
|
|
249
|
+
getOutputTxId(): string | null {
|
|
250
|
+
return this.scTxId ?? null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* @inheritDoc
|
|
255
|
+
*/
|
|
256
|
+
getId(): string {
|
|
257
|
+
return this.paymentHash;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* @inheritDoc
|
|
262
|
+
*/
|
|
263
|
+
getAddress(): string {
|
|
264
|
+
return this.address;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @inheritDoc
|
|
269
|
+
*/
|
|
270
|
+
getHyperlink(): string {
|
|
271
|
+
return "bitcoin:"+this.address+"?amount="+encodeURIComponent((Number(this.inputAmount)/100000000).toString(10));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* @inheritDoc
|
|
276
|
+
*/
|
|
277
|
+
requiresAction(): boolean {
|
|
278
|
+
return this._state===OnchainForGasSwapState.REFUNDABLE;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* @inheritDoc
|
|
283
|
+
*/
|
|
284
|
+
isFinished(): boolean {
|
|
285
|
+
return this._state===OnchainForGasSwapState.FINISHED || this._state===OnchainForGasSwapState.FAILED || this._state===OnchainForGasSwapState.EXPIRED || this._state===OnchainForGasSwapState.REFUNDED;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* @inheritDoc
|
|
290
|
+
*/
|
|
291
|
+
isQuoteExpired(): boolean {
|
|
292
|
+
return this._state===OnchainForGasSwapState.EXPIRED;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @inheritDoc
|
|
297
|
+
*/
|
|
298
|
+
isQuoteSoftExpired(): boolean {
|
|
299
|
+
return this.expiry<Date.now();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* @inheritDoc
|
|
304
|
+
*/
|
|
305
|
+
isFailed(): boolean {
|
|
306
|
+
return this._state===OnchainForGasSwapState.FAILED;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @inheritDoc
|
|
311
|
+
*/
|
|
312
|
+
isSuccessful(): boolean {
|
|
313
|
+
return this._state===OnchainForGasSwapState.FINISHED;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* @inheritDoc
|
|
318
|
+
*/
|
|
319
|
+
isInProgress(): boolean {
|
|
320
|
+
return (this._state===OnchainForGasSwapState.PR_CREATED && this.txId!=null) ||
|
|
321
|
+
(this._state===OnchainForGasSwapState.REFUNDABLE && this.refundAddress!=null);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* @inheritDoc
|
|
326
|
+
* @internal
|
|
327
|
+
*/
|
|
328
|
+
_verifyQuoteDefinitelyExpired(): Promise<boolean> {
|
|
329
|
+
return Promise.resolve(this.expiry<Date.now());
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* @inheritDoc
|
|
334
|
+
* @internal
|
|
335
|
+
*/
|
|
336
|
+
_verifyQuoteValid(): Promise<boolean> {
|
|
337
|
+
return Promise.resolve(this.expiry>Date.now());
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
//////////////////////////////
|
|
342
|
+
//// Amounts & fees
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Returns an output amount in base units without a swap fee included, hence this value
|
|
346
|
+
* is larger than the actual output amount
|
|
347
|
+
*
|
|
348
|
+
* @internal
|
|
349
|
+
*/
|
|
350
|
+
protected getOutAmountWithoutFee(): bigint {
|
|
351
|
+
return this.outputAmount + (this.swapFee ?? 0n);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* @inheritDoc
|
|
356
|
+
*/
|
|
357
|
+
getOutputToken(): SCToken<T["ChainId"]> {
|
|
358
|
+
return this.wrapper._tokens[this.wrapper._chain.getNativeCurrencyAddress()];
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* @inheritDoc
|
|
363
|
+
*/
|
|
364
|
+
getOutput(): TokenAmount<SCToken<T["ChainId"]>, true> {
|
|
365
|
+
return toTokenAmount(
|
|
366
|
+
this.outputAmount, this.wrapper._tokens[this.wrapper._chain.getNativeCurrencyAddress()],
|
|
367
|
+
this.wrapper._prices, this.pricingInfo
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* @inheritDoc
|
|
373
|
+
*/
|
|
374
|
+
getInputToken(): BtcToken<false> {
|
|
375
|
+
return BitcoinTokens.BTC;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* @inheritDoc
|
|
380
|
+
*/
|
|
381
|
+
getInput(): TokenAmount<BtcToken<false>, true> {
|
|
382
|
+
return toTokenAmount(this.inputAmount, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @inheritDoc
|
|
387
|
+
*/
|
|
388
|
+
getInputWithoutFee(): TokenAmount<BtcToken<false>, true> {
|
|
389
|
+
return toTokenAmount(
|
|
390
|
+
this.inputAmount - (this.swapFeeBtc ?? 0n), BitcoinTokens.BTC,
|
|
391
|
+
this.wrapper._prices, this.pricingInfo
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Returns the swap fee charged by the intermediary (LP) on this swap
|
|
397
|
+
*
|
|
398
|
+
* @internal
|
|
399
|
+
*/
|
|
400
|
+
protected getSwapFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
401
|
+
if(this.pricingInfo==null) throw new Error("No pricing info known!");
|
|
402
|
+
const feeWithoutBaseFee = this.swapFeeBtc==null ? 0n : this.swapFeeBtc - this.pricingInfo.satsBaseFee;
|
|
403
|
+
const swapFeePPM = feeWithoutBaseFee * 1000000n / this.getInputWithoutFee().rawAmount;
|
|
404
|
+
|
|
405
|
+
const amountInSrcToken = toTokenAmount(
|
|
406
|
+
this.swapFeeBtc ?? 0n, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo
|
|
407
|
+
);
|
|
408
|
+
return {
|
|
409
|
+
amountInSrcToken,
|
|
410
|
+
amountInDstToken: toTokenAmount(
|
|
411
|
+
this.swapFee ?? 0n, this.wrapper._tokens[this.wrapper._chain.getNativeCurrencyAddress()],
|
|
412
|
+
this.wrapper._prices, this.pricingInfo
|
|
413
|
+
),
|
|
414
|
+
currentUsdValue: amountInSrcToken.currentUsdValue,
|
|
415
|
+
usdValue: amountInSrcToken.usdValue,
|
|
416
|
+
pastUsdValue: amountInSrcToken.pastUsdValue,
|
|
417
|
+
composition: {
|
|
418
|
+
base: toTokenAmount(this.pricingInfo.satsBaseFee, BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo),
|
|
419
|
+
percentage: ppmToPercentage(swapFeePPM)
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* @inheritDoc
|
|
426
|
+
*/
|
|
427
|
+
getFee(): Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>> {
|
|
428
|
+
return this.getSwapFee();
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* @inheritDoc
|
|
433
|
+
*/
|
|
434
|
+
getFeeBreakdown(): [{type: FeeType.SWAP, fee: Fee<T["ChainId"], BtcToken<false>, SCToken<T["ChainId"]>>}] {
|
|
435
|
+
return [{
|
|
436
|
+
type: FeeType.SWAP,
|
|
437
|
+
fee: this.getSwapFee()
|
|
438
|
+
}];
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* @inheritDoc
|
|
443
|
+
*/
|
|
444
|
+
getRequiredConfirmationsCount(): number {
|
|
445
|
+
return 1;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* @inheritDoc
|
|
450
|
+
*/
|
|
451
|
+
async getFundedPsbt(
|
|
452
|
+
_bitcoinWallet: IBitcoinWallet | MinimalBitcoinWalletInterface,
|
|
453
|
+
feeRate?: number,
|
|
454
|
+
additionalOutputs?: ({amount: bigint, outputScript: Uint8Array} | {amount: bigint, address: string})[]
|
|
455
|
+
): Promise<{psbt: Transaction, psbtHex: string, psbtBase64: string, signInputs: number[]}> {
|
|
456
|
+
if(this._state!==OnchainForGasSwapState.PR_CREATED)
|
|
457
|
+
throw new Error("Swap already paid for!");
|
|
458
|
+
|
|
459
|
+
let bitcoinWallet: IBitcoinWallet;
|
|
460
|
+
if(isIBitcoinWallet(_bitcoinWallet)) {
|
|
461
|
+
bitcoinWallet = _bitcoinWallet;
|
|
462
|
+
} else {
|
|
463
|
+
bitcoinWallet = new SingleAddressBitcoinWallet(this.wrapper._btcRpc, this.wrapper._options.bitcoinNetwork, _bitcoinWallet);
|
|
464
|
+
}
|
|
465
|
+
//TODO: Maybe re-introduce fee rate check here if passed from the user
|
|
466
|
+
if(feeRate==null) {
|
|
467
|
+
feeRate = await bitcoinWallet.getFeeRate();
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const basePsbt = new Transaction({
|
|
471
|
+
allowUnknownOutputs: true,
|
|
472
|
+
allowLegacyWitnessUtxo: true
|
|
473
|
+
});
|
|
474
|
+
basePsbt.addOutput({
|
|
475
|
+
amount: this.outputAmount,
|
|
476
|
+
script: toOutputScript(this.wrapper._options.bitcoinNetwork, this.address)
|
|
477
|
+
});
|
|
478
|
+
if(additionalOutputs!=null) additionalOutputs.forEach(output => {
|
|
479
|
+
basePsbt.addOutput({
|
|
480
|
+
amount: output.amount,
|
|
481
|
+
script: (output as {outputScript: Uint8Array}).outputScript ?? toOutputScript(this.wrapper._options.bitcoinNetwork, (output as {address: string}).address)
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
const psbt = await bitcoinWallet.fundPsbt(basePsbt, feeRate);
|
|
486
|
+
//Sign every input
|
|
487
|
+
const signInputs: number[] = [];
|
|
488
|
+
for(let i=0;i<psbt.inputsLength;i++) {
|
|
489
|
+
signInputs.push(i);
|
|
490
|
+
}
|
|
491
|
+
const serializedPsbt = Buffer.from(psbt.toPSBT());
|
|
492
|
+
return {
|
|
493
|
+
psbt,
|
|
494
|
+
psbtHex: serializedPsbt.toString("hex"),
|
|
495
|
+
psbtBase64: serializedPsbt.toString("base64"),
|
|
496
|
+
signInputs
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* @inheritDoc
|
|
502
|
+
*/
|
|
503
|
+
async submitPsbt(_psbt: Transaction | string): Promise<string> {
|
|
504
|
+
const psbt = parsePsbtTransaction(_psbt);
|
|
505
|
+
if(this._state!==OnchainForGasSwapState.PR_CREATED)
|
|
506
|
+
throw new Error("Swap already paid for!");
|
|
507
|
+
|
|
508
|
+
//Ensure not expired
|
|
509
|
+
if(this.expiry<Date.now()) {
|
|
510
|
+
throw new Error("Swap expired!");
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const output0 = psbt.getOutput(0);
|
|
514
|
+
if(output0.amount!==this.outputAmount)
|
|
515
|
+
throw new Error("PSBT output amount invalid, expected: "+this.outputAmount+" got: "+output0.amount);
|
|
516
|
+
const expectedOutputScript = toOutputScript(this.wrapper._options.bitcoinNetwork, this.address);
|
|
517
|
+
if(output0.script==null || !expectedOutputScript.equals(output0.script))
|
|
518
|
+
throw new Error("PSBT output script invalid!");
|
|
519
|
+
|
|
520
|
+
if(!psbt.isFinal) psbt.finalize();
|
|
521
|
+
|
|
522
|
+
return await this.wrapper._btcRpc.sendRawTransaction(Buffer.from(psbt.toBytes(true, true)).toString("hex"));
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* @inheritDoc
|
|
527
|
+
*/
|
|
528
|
+
async estimateBitcoinFee(_bitcoinWallet: IBitcoinWallet | MinimalBitcoinWalletInterface, feeRate?: number): Promise<TokenAmount<BtcToken<false>, true> | null> {
|
|
529
|
+
const bitcoinWallet: IBitcoinWallet = toBitcoinWallet(_bitcoinWallet, this.wrapper._btcRpc, this.wrapper._options.bitcoinNetwork);
|
|
530
|
+
const txFee = await bitcoinWallet.getTransactionFee(this.address, this.inputAmount, feeRate);
|
|
531
|
+
if(txFee==null) return null;
|
|
532
|
+
return toTokenAmount(BigInt(txFee), BitcoinTokens.BTC, this.wrapper._prices, this.pricingInfo);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* @inheritDoc
|
|
537
|
+
*/
|
|
538
|
+
async sendBitcoinTransaction(wallet: IBitcoinWallet | MinimalBitcoinWalletInterfaceWithSigner, feeRate?: number): Promise<string> {
|
|
539
|
+
if(this._state!==OnchainForGasSwapState.PR_CREATED)
|
|
540
|
+
throw new Error("Swap already paid for!");
|
|
541
|
+
|
|
542
|
+
//Ensure not expired
|
|
543
|
+
if(this.expiry<Date.now()) {
|
|
544
|
+
throw new Error("Swap expired!");
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if(isIBitcoinWallet(wallet)) {
|
|
548
|
+
return await wallet.sendTransaction(this.address, this.inputAmount, feeRate);
|
|
549
|
+
} else {
|
|
550
|
+
const {psbt, psbtHex, psbtBase64, signInputs} = await this.getFundedPsbt(wallet, feeRate);
|
|
551
|
+
const signedPsbt = await wallet.signPsbt({
|
|
552
|
+
psbt, psbtHex, psbtBase64
|
|
553
|
+
}, signInputs);
|
|
554
|
+
return await this.submitPsbt(signedPsbt);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* @inheritDoc
|
|
560
|
+
*
|
|
561
|
+
* @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
|
|
562
|
+
* if not provided an address is returned instead.
|
|
563
|
+
*/
|
|
564
|
+
async getExecutionAction(options?: {
|
|
565
|
+
bitcoinWallet?: MinimalBitcoinWalletInterface
|
|
566
|
+
}): Promise<never> {
|
|
567
|
+
throw new Error("Not supported");
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* @inheritDoc
|
|
572
|
+
*/
|
|
573
|
+
async getExecutionSteps(): Promise<never> {
|
|
574
|
+
throw new Error("Not supported");
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* @inheritDoc
|
|
579
|
+
*/
|
|
580
|
+
async getExecutionStatus(): Promise<never> {
|
|
581
|
+
throw new Error("Not supported");
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* @internal
|
|
586
|
+
* @inheritDoc
|
|
587
|
+
*/
|
|
588
|
+
_submitExecutionTransactions(): Promise<string[]> {
|
|
589
|
+
throw new Error("Not supported");
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* @remarks Not supported
|
|
594
|
+
*/
|
|
595
|
+
async execute(): Promise<boolean> {
|
|
596
|
+
throw new Error("Not supported");
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
//////////////////////////////
|
|
600
|
+
//// Payment
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Queries the intermediary (LP) node for the state of the swap
|
|
604
|
+
*
|
|
605
|
+
* @param save Whether the save the result or not
|
|
606
|
+
*
|
|
607
|
+
* @returns Whether the swap was successful as `boolean` or `null` if the swap is still pending
|
|
608
|
+
* @internal
|
|
609
|
+
*/
|
|
610
|
+
protected async checkAddress(save: boolean = true): Promise<boolean | null> {
|
|
611
|
+
if(
|
|
612
|
+
this._state===OnchainForGasSwapState.FAILED ||
|
|
613
|
+
this._state===OnchainForGasSwapState.EXPIRED ||
|
|
614
|
+
this._state===OnchainForGasSwapState.REFUNDED
|
|
615
|
+
) return false;
|
|
616
|
+
if(this._state===OnchainForGasSwapState.FINISHED) return false;
|
|
617
|
+
if(this.url==null) return false;
|
|
618
|
+
|
|
619
|
+
const response = await this.wrapper._lpApi.getTrustedAddressStatus(
|
|
620
|
+
this.url, this.paymentHash, this.sequence, this.wrapper._options.getRequestTimeout
|
|
621
|
+
);
|
|
622
|
+
switch(response.code) {
|
|
623
|
+
case TrustedAddressStatusResponseCodes.AWAIT_PAYMENT:
|
|
624
|
+
if(this.txId!=null) {
|
|
625
|
+
this.txId = undefined;
|
|
626
|
+
if(save) await this._save();
|
|
627
|
+
return true;
|
|
628
|
+
}
|
|
629
|
+
return false;
|
|
630
|
+
case TrustedAddressStatusResponseCodes.AWAIT_CONFIRMATION:
|
|
631
|
+
case TrustedAddressStatusResponseCodes.PENDING:
|
|
632
|
+
case TrustedAddressStatusResponseCodes.TX_SENT:
|
|
633
|
+
const inputAmount = BigInt(response.data.adjustedAmount);
|
|
634
|
+
const outputAmount = BigInt(response.data.adjustedTotal);
|
|
635
|
+
const adjustedFee = response.data.adjustedFee==null ? null : BigInt(response.data.adjustedFee);
|
|
636
|
+
const adjustedFeeSats = response.data.adjustedFeeSats==null ? null : BigInt(response.data.adjustedFeeSats);
|
|
637
|
+
const txId = response.data.txId;
|
|
638
|
+
if(
|
|
639
|
+
this.txId!=txId ||
|
|
640
|
+
this.inputAmount !== inputAmount ||
|
|
641
|
+
this.outputAmount !== outputAmount
|
|
642
|
+
) {
|
|
643
|
+
this.txId = txId;
|
|
644
|
+
this.inputAmount = inputAmount;
|
|
645
|
+
this.outputAmount = outputAmount;
|
|
646
|
+
if(adjustedFee!=null) this.swapFee = adjustedFee;
|
|
647
|
+
if(adjustedFeeSats!=null) this.swapFeeBtc = adjustedFeeSats;
|
|
648
|
+
if(save) await this._save();
|
|
649
|
+
return true;
|
|
650
|
+
}
|
|
651
|
+
return false;
|
|
652
|
+
case TrustedAddressStatusResponseCodes.PAID:
|
|
653
|
+
const txStatus = await this.wrapper._chain.getTxIdStatus(response.data.txId);
|
|
654
|
+
if(txStatus==="success") {
|
|
655
|
+
this._state = OnchainForGasSwapState.FINISHED;
|
|
656
|
+
this.scTxId = response.data.txId;
|
|
657
|
+
if(save) await this._saveAndEmit();
|
|
658
|
+
return true;
|
|
659
|
+
}
|
|
660
|
+
return false;
|
|
661
|
+
case TrustedAddressStatusResponseCodes.EXPIRED:
|
|
662
|
+
this._state = OnchainForGasSwapState.EXPIRED;
|
|
663
|
+
if(save) await this._saveAndEmit();
|
|
664
|
+
return true;
|
|
665
|
+
case TrustedAddressStatusResponseCodes.REFUNDABLE:
|
|
666
|
+
if(this._state===OnchainForGasSwapState.REFUNDABLE) return null;
|
|
667
|
+
this._state = OnchainForGasSwapState.REFUNDABLE;
|
|
668
|
+
if(save) await this._saveAndEmit();
|
|
669
|
+
return true;
|
|
670
|
+
case TrustedAddressStatusResponseCodes.REFUNDED:
|
|
671
|
+
this._state = OnchainForGasSwapState.REFUNDED;
|
|
672
|
+
this.refundTxId = response.data.txId;
|
|
673
|
+
if(save) await this._saveAndEmit();
|
|
674
|
+
return true;
|
|
675
|
+
default:
|
|
676
|
+
this._state = OnchainForGasSwapState.FAILED;
|
|
677
|
+
if(save) await this._saveAndEmit();
|
|
678
|
+
return true;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Sets the bitcoin address used for possible refunds in case something goes wrong with the swap
|
|
684
|
+
*
|
|
685
|
+
* @param refundAddress Bitcoin address to receive the refund to
|
|
686
|
+
* @internal
|
|
687
|
+
*/
|
|
688
|
+
protected async setRefundAddress(refundAddress: string): Promise<void> {
|
|
689
|
+
if(this.refundAddress!=null) {
|
|
690
|
+
if(this.refundAddress!==refundAddress) throw new Error("Different refund address already set!");
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
if(this.url==null) throw new Error("LP URL not known, cannot set refund address!");
|
|
694
|
+
await this.wrapper._lpApi.setTrustedRefundAddress(
|
|
695
|
+
this.url, this.paymentHash, this.sequence, refundAddress, this.wrapper._options.getRequestTimeout
|
|
696
|
+
);
|
|
697
|
+
this.refundAddress = refundAddress;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* @inheritDoc
|
|
702
|
+
*/
|
|
703
|
+
async waitForBitcoinTransaction(
|
|
704
|
+
updateCallback?: (txId?: string, confirmations?: number, targetConfirmations?: number, txEtaMs?: number) => void,
|
|
705
|
+
checkIntervalSeconds: number = 5,
|
|
706
|
+
abortSignal?: AbortSignal
|
|
707
|
+
): Promise<string> {
|
|
708
|
+
if(this._state!==OnchainForGasSwapState.PR_CREATED) throw new Error("Must be in PR_CREATED state!");
|
|
709
|
+
|
|
710
|
+
if(!this.initiated) {
|
|
711
|
+
this.initiated = true;
|
|
712
|
+
await this._saveAndEmit();
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
while(
|
|
716
|
+
!abortSignal?.aborted &&
|
|
717
|
+
this._state===OnchainForGasSwapState.PR_CREATED
|
|
718
|
+
) {
|
|
719
|
+
await this.checkAddress(true);
|
|
720
|
+
if(this.txId!=null && updateCallback!=null) {
|
|
721
|
+
const res = await this.wrapper._btcRpc.getTransaction(this.txId);
|
|
722
|
+
if(res==null) {
|
|
723
|
+
updateCallback();
|
|
724
|
+
} else if(res.confirmations!=null && res.confirmations>0) {
|
|
725
|
+
updateCallback(res.txid, res.confirmations, 1, 0);
|
|
726
|
+
} else {
|
|
727
|
+
const delay = await this.wrapper._btcRpc.getConfirmationDelay(res, 1);
|
|
728
|
+
updateCallback(res.txid, 0, 1, delay ?? undefined);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
if(this._state===OnchainForGasSwapState.PR_CREATED)
|
|
732
|
+
await timeoutPromise(checkIntervalSeconds*1000, abortSignal);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
if(
|
|
736
|
+
(this._state as OnchainForGasSwapState)===OnchainForGasSwapState.REFUNDABLE ||
|
|
737
|
+
(this._state as OnchainForGasSwapState)===OnchainForGasSwapState.REFUNDED
|
|
738
|
+
) return this.txId!;
|
|
739
|
+
if(this.isQuoteExpired()) throw new Error("Swap expired");
|
|
740
|
+
if(this.isFailed()) throw new Error("Swap failed");
|
|
741
|
+
return this.txId!;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Waits till the LP processes a refund for a failed swap. The swap must be in
|
|
746
|
+
* {@link OnchainForGasSwapState.REFUNDABLE} state
|
|
747
|
+
*
|
|
748
|
+
* @param checkIntervalSeconds How often to check (default 5 seconds)
|
|
749
|
+
* @param abortSignal Abort signal
|
|
750
|
+
*/
|
|
751
|
+
async waitTillRefunded(
|
|
752
|
+
checkIntervalSeconds?: number,
|
|
753
|
+
abortSignal?: AbortSignal
|
|
754
|
+
): Promise<void> {
|
|
755
|
+
checkIntervalSeconds ??= 5;
|
|
756
|
+
if(this._state===OnchainForGasSwapState.REFUNDED) return;
|
|
757
|
+
if(this._state!==OnchainForGasSwapState.REFUNDABLE) throw new Error("Must be in REFUNDABLE state!");
|
|
758
|
+
|
|
759
|
+
while(
|
|
760
|
+
!abortSignal?.aborted &&
|
|
761
|
+
this._state===OnchainForGasSwapState.REFUNDABLE
|
|
762
|
+
) {
|
|
763
|
+
await this.checkAddress(true);
|
|
764
|
+
if(this._state===OnchainForGasSwapState.REFUNDABLE)
|
|
765
|
+
await timeoutPromise(checkIntervalSeconds*1000, abortSignal);
|
|
766
|
+
}
|
|
767
|
+
if(this.isQuoteExpired()) throw new Error("Swap expired");
|
|
768
|
+
if(this.isFailed()) throw new Error("Swap failed");
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/**
|
|
772
|
+
* Requests a refund after the swap failed, this also waits till the refund is actually sent by the
|
|
773
|
+
* intermediary (LP). The swap must be in {@link OnchainForGasSwapState.REFUNDABLE} state
|
|
774
|
+
*
|
|
775
|
+
* @param refundAddress Bitcoin address to receive the refund to
|
|
776
|
+
* @param abortSignal Abort signal
|
|
777
|
+
*/
|
|
778
|
+
async requestRefund(refundAddress?: string, abortSignal?: AbortSignal): Promise<void> {
|
|
779
|
+
if(refundAddress!=null) await this.setRefundAddress(refundAddress);
|
|
780
|
+
await this.waitTillRefunded(undefined, abortSignal);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
//////////////////////////////
|
|
785
|
+
//// Storage
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* @inheritDoc
|
|
789
|
+
*/
|
|
790
|
+
serialize(): any{
|
|
791
|
+
return {
|
|
792
|
+
...super.serialize(),
|
|
793
|
+
paymentHash: this.paymentHash,
|
|
794
|
+
sequence: this.sequence==null ? null : this.sequence.toString(10),
|
|
795
|
+
address: this.address,
|
|
796
|
+
inputAmount: this.inputAmount==null ? null : this.inputAmount.toString(10),
|
|
797
|
+
outputAmount: this.outputAmount==null ? null : this.outputAmount.toString(10),
|
|
798
|
+
recipient: this.recipient,
|
|
799
|
+
token: this.token,
|
|
800
|
+
refundAddress: this.refundAddress,
|
|
801
|
+
scTxId: this.scTxId,
|
|
802
|
+
txId: this.txId,
|
|
803
|
+
refundTxId: this.refundTxId,
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* @inheritDoc
|
|
809
|
+
* @internal
|
|
810
|
+
*/
|
|
811
|
+
_getInitiator(): string {
|
|
812
|
+
return this.recipient;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
//////////////////////////////
|
|
817
|
+
//// Swap ticks & sync
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* @inheritDoc
|
|
821
|
+
* @internal
|
|
822
|
+
*/
|
|
823
|
+
async _sync(save?: boolean): Promise<boolean> {
|
|
824
|
+
if(this._state===OnchainForGasSwapState.PR_CREATED) {
|
|
825
|
+
//Check if it's maybe already paid
|
|
826
|
+
const result = await this.checkAddress(false);
|
|
827
|
+
if(result) {
|
|
828
|
+
if(save) await this._saveAndEmit();
|
|
829
|
+
return true;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
return false;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* @inheritDoc
|
|
837
|
+
* @internal
|
|
838
|
+
*/
|
|
839
|
+
_tick(save?: boolean): Promise<boolean> {
|
|
840
|
+
return Promise.resolve(false);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
}
|