@atomiqlabs/sdk 8.3.6 → 8.4.4
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 +3 -2
- package/dist/bitcoin/wallet/BitcoinWallet.js +15 -1
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +25 -3
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +51 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +2 -0
- package/dist/intermediaries/apis/IntermediaryAPI.js +2 -0
- package/dist/swapper/Swapper.d.ts +2 -1
- package/dist/swapper/Swapper.js +4 -3
- package/dist/swaps/ISwap.d.ts +34 -0
- package/dist/swaps/ISwap.js +10 -0
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +12 -0
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +18 -0
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +54 -4
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +72 -8
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +4 -1
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +23 -4
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +89 -3
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +118 -5
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +2 -0
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +30 -4
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +48 -3
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +69 -4
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +40 -2
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +72 -2
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +51 -1
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +71 -3
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +28 -2
- package/dist/swaps/trusted/ln/LnForGasSwap.js +39 -1
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +28 -3
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +44 -1
- package/dist/types/SwapStateInfo.d.ts +5 -0
- package/dist/types/SwapStateInfo.js +2 -0
- package/dist/types/lnurl/LNURLPay.d.ts +0 -1
- package/dist/types/lnurl/LNURLPay.js +0 -1
- package/dist/types/lnurl/LNURLWithdraw.d.ts +0 -1
- package/dist/types/lnurl/LNURLWithdraw.js +0 -1
- package/package.json +3 -1
- package/src/bitcoin/wallet/BitcoinWallet.ts +21 -4
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +60 -5
- package/src/index.ts +1 -0
- package/src/intermediaries/apis/IntermediaryAPI.ts +5 -1
- package/src/swapper/Swapper.ts +5 -2
- package/src/swaps/ISwap.ts +45 -0
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +20 -0
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +86 -11
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +28 -3
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +137 -9
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +35 -4
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +83 -6
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +77 -4
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +90 -4
- package/src/swaps/trusted/ln/LnForGasSwap.ts +51 -3
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +58 -3
- package/src/types/SwapStateInfo.ts +6 -0
- package/src/types/lnurl/LNURLPay.ts +0 -1
- package/src/types/lnurl/LNURLWithdraw.ts +0 -1
|
@@ -23,7 +23,7 @@ import {ppmToPercentage} from "../../../types/fees/PercentagePPM";
|
|
|
23
23
|
import {TokenAmount, toTokenAmount} from "../../../types/TokenAmount";
|
|
24
24
|
import {BtcToken, SCToken} from "../../../types/Token";
|
|
25
25
|
import {timeoutPromise} from "../../../utils/TimeoutUtils";
|
|
26
|
-
import {SwapExecutionActionCommit} from "../../../types/SwapExecutionAction";
|
|
26
|
+
import {SwapExecutionAction, SwapExecutionActionCommit} from "../../../types/SwapExecutionAction";
|
|
27
27
|
|
|
28
28
|
export type IToBTCSwapInit<T extends SwapData> = IEscrowSelfInitSwapInit<T> & {
|
|
29
29
|
signatureData?: SignatureData,
|
|
@@ -92,6 +92,17 @@ export enum ToBTCSwapState {
|
|
|
92
92
|
REFUNDABLE = 4
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
const ToBTCSwapStateDescription = {
|
|
96
|
+
[ToBTCSwapState.REFUNDED]: "Intermediary (LP) was unable to process the swap and the funds were refunded on the source chain",
|
|
97
|
+
[ToBTCSwapState.QUOTE_EXPIRED]: "Swap has expired for good and there is no way how it can be executed anymore",
|
|
98
|
+
[ToBTCSwapState.QUOTE_SOFT_EXPIRED]: "A swap is expired, though there is still a chance that it will be processed",
|
|
99
|
+
[ToBTCSwapState.CREATED]: "Swap was created, initiate it by creating the swap escrow on the source chain",
|
|
100
|
+
[ToBTCSwapState.COMMITED]: "Swap escrow was initiated (committed) on the source chain, the intermediary (LP) will now process the swap.",
|
|
101
|
+
[ToBTCSwapState.SOFT_CLAIMED]: "The intermediary (LP) has processed the transaction and sent out the funds on the destination chain, but hasn't yet settled the escrow on the source chain.",
|
|
102
|
+
[ToBTCSwapState.CLAIMED]: "Swap was successfully settled by the intermediary (LP) on the source chain",
|
|
103
|
+
[ToBTCSwapState.REFUNDABLE]: "Intermediary (LP) was unable to process the swap and the swap escrow on the source chain is refundable."
|
|
104
|
+
};
|
|
105
|
+
|
|
95
106
|
/**
|
|
96
107
|
* Base class for escrow-based Smart chain -> Bitcoin (on-chain & lightning) swaps
|
|
97
108
|
*
|
|
@@ -101,6 +112,15 @@ export abstract class IToBTCSwap<
|
|
|
101
112
|
T extends ChainType = ChainType,
|
|
102
113
|
D extends IToBTCDefinition<T, IToBTCWrapper<T, D>, IToBTCSwap<T, D>> = IToBTCDefinition<T, IToBTCWrapper<T, any>, IToBTCSwap<T, any>>,
|
|
103
114
|
> extends IEscrowSelfInitSwap<T, D, ToBTCSwapState> implements IRefundableSwap<T, D, ToBTCSwapState> {
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
protected readonly swapStateDescription = ToBTCSwapStateDescription;
|
|
120
|
+
/**
|
|
121
|
+
* @internal
|
|
122
|
+
*/
|
|
123
|
+
protected readonly swapStateName = (state: number) => ToBTCSwapState[state];
|
|
104
124
|
/**
|
|
105
125
|
* @internal
|
|
106
126
|
*/
|
|
@@ -280,6 +300,21 @@ export abstract class IToBTCSwap<
|
|
|
280
300
|
return this._state===ToBTCSwapState.REFUNDED;
|
|
281
301
|
}
|
|
282
302
|
|
|
303
|
+
/**
|
|
304
|
+
* @inheritDoc
|
|
305
|
+
*/
|
|
306
|
+
isInProgress(): boolean {
|
|
307
|
+
return this._state===ToBTCSwapState.COMMITED || this._state===ToBTCSwapState.SOFT_CLAIMED;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Returns the time (in UNIX milliseconds) at which the swap expires and the user is able to unilaterally
|
|
312
|
+
* refund it with the {@link refund} or {@link txsRefund} function.
|
|
313
|
+
*/
|
|
314
|
+
getExpiry(): number {
|
|
315
|
+
return Number(this._data.getExpiry())*1000;
|
|
316
|
+
}
|
|
317
|
+
|
|
283
318
|
/**
|
|
284
319
|
* @inheritDoc
|
|
285
320
|
* @internal
|
|
@@ -501,6 +536,7 @@ export abstract class IToBTCSwap<
|
|
|
501
536
|
|
|
502
537
|
/**
|
|
503
538
|
* @inheritDoc
|
|
539
|
+
*
|
|
504
540
|
* @param options.skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
505
541
|
* (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
|
|
506
542
|
*/
|
|
@@ -521,6 +557,32 @@ export abstract class IToBTCSwap<
|
|
|
521
557
|
];
|
|
522
558
|
}
|
|
523
559
|
|
|
560
|
+
/**
|
|
561
|
+
* @inheritDoc
|
|
562
|
+
*
|
|
563
|
+
* @param options.skipChecks Skip checks like making sure init signature is still valid and swap wasn't commited yet
|
|
564
|
+
* (this is handled on swap creation, if you commit right after quoting, you can use `skipChecks=true`)
|
|
565
|
+
* @param options.refundSmartChainSigner Optional smart chain signer to use when creating refunds transactions
|
|
566
|
+
*/
|
|
567
|
+
async getCurrentActions(options?: {
|
|
568
|
+
skipChecks?: boolean,
|
|
569
|
+
refundSmartChainSigner?: string | T["Signer"] | T["NativeSigner"]
|
|
570
|
+
}): Promise<SwapExecutionAction<T>[]> {
|
|
571
|
+
if(this._state===ToBTCSwapState.CREATED) {
|
|
572
|
+
try {
|
|
573
|
+
return await this.txsExecute(options);
|
|
574
|
+
} catch (e) {}
|
|
575
|
+
}
|
|
576
|
+
if(this.isRefundable()) {
|
|
577
|
+
return [{
|
|
578
|
+
name: "Refund" as const,
|
|
579
|
+
description: "Refund the swap after it failed to execute",
|
|
580
|
+
chain: this.chainIdentifier,
|
|
581
|
+
txs: await this.txsRefund(options?.refundSmartChainSigner)
|
|
582
|
+
}];
|
|
583
|
+
}
|
|
584
|
+
return [];
|
|
585
|
+
}
|
|
524
586
|
|
|
525
587
|
//////////////////////////////
|
|
526
588
|
//// Commit
|
|
@@ -777,10 +839,21 @@ export abstract class IToBTCSwap<
|
|
|
777
839
|
* @throws {SignatureVerificationError} If intermediary returned invalid cooperative refund signature
|
|
778
840
|
* @throws {Error} When state is not refundable
|
|
779
841
|
*/
|
|
780
|
-
async txsRefund(
|
|
842
|
+
async txsRefund(_signer?: string | T["Signer"] | T["NativeSigner"]): Promise<T["TX"][]> {
|
|
781
843
|
if(!this.isRefundable()) throw new Error("Must be in REFUNDABLE state or expired!");
|
|
782
844
|
|
|
783
|
-
signer
|
|
845
|
+
let signer: string;
|
|
846
|
+
if(_signer!=null) {
|
|
847
|
+
if (typeof (_signer) === "string") {
|
|
848
|
+
signer = _signer;
|
|
849
|
+
} else if (isAbstractSigner(_signer)) {
|
|
850
|
+
signer = _signer.getAddress();
|
|
851
|
+
} else {
|
|
852
|
+
signer = (await this.wrapper._chain.wrapSigner(_signer)).getAddress();
|
|
853
|
+
}
|
|
854
|
+
} else {
|
|
855
|
+
signer = this._getInitiator();
|
|
856
|
+
}
|
|
784
857
|
|
|
785
858
|
if(await this.wrapper._contract.isExpired(this._getInitiator(), this._data)) {
|
|
786
859
|
return await this.wrapper._contract.txsRefund(signer, this._data, true, true);
|
|
@@ -1020,4 +1093,4 @@ export abstract class IToBTCSwap<
|
|
|
1020
1093
|
}
|
|
1021
1094
|
return false;
|
|
1022
1095
|
}
|
|
1023
|
-
}
|
|
1096
|
+
}
|
|
@@ -38,7 +38,7 @@ import {
|
|
|
38
38
|
serializePriceInfoType
|
|
39
39
|
} from "../../types/PriceInfoType";
|
|
40
40
|
import {toBitcoinWallet} from "../../utils/BitcoinWalletUtils";
|
|
41
|
-
import {SwapExecutionActionBitcoin} from "../../types/SwapExecutionAction";
|
|
41
|
+
import {SwapExecutionAction, SwapExecutionActionBitcoin} from "../../types/SwapExecutionAction";
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* State enum for SPV vault (UTXO-controlled vault) based swaps
|
|
@@ -109,6 +109,21 @@ export enum SpvFromBTCSwapState {
|
|
|
109
109
|
CLAIMED = 6
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
const SpvFromBTCSwapStateDescription = {
|
|
113
|
+
[SpvFromBTCSwapState.CLOSED]: "Catastrophic failure has occurred when processing the swap on the smart chain side, this implies a bug in the smart contract code or the user and intermediary deliberately creating a bitcoin transaction with invalid format unparsable by the smart contract.",
|
|
114
|
+
[SpvFromBTCSwapState.FAILED]: "Some of the bitcoin swap transaction inputs were double-spent, this means the swap has failed and no BTC was sent",
|
|
115
|
+
[SpvFromBTCSwapState.DECLINED]: "The intermediary (LP) declined to co-sign the submitted PSBT, hence the swap failed",
|
|
116
|
+
[SpvFromBTCSwapState.QUOTE_EXPIRED]: "Swap has expired for good and there is no way how it can be executed anymore",
|
|
117
|
+
[SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED]: "A swap is almost expired, and it should be presented to the user as expired, though there is still a chance that it will be processed",
|
|
118
|
+
[SpvFromBTCSwapState.CREATED]: "Swap was created, get the bitcoin swap PSBT that should be signed by the user's wallet and then submit it back to the SDK.",
|
|
119
|
+
[SpvFromBTCSwapState.SIGNED]: "Swap bitcoin PSBT was submitted by the client to the SDK",
|
|
120
|
+
[SpvFromBTCSwapState.POSTED]: "Swap bitcoin PSBT sent to the intermediary (LP), waiting for the intermediary co-sign it and broadcast.",
|
|
121
|
+
[SpvFromBTCSwapState.BROADCASTED]: "Intermediary (LP) has co-signed and broadcasted the bitcoin transaction.",
|
|
122
|
+
[SpvFromBTCSwapState.FRONTED]: "Settlement on the destination smart chain was fronted and funds were already received by the user, even before the final settlement.",
|
|
123
|
+
[SpvFromBTCSwapState.BTC_TX_CONFIRMED]: "Bitcoin transaction confirmed with necessary amount of confirmations, wait for automatic settlement by the watchtower or settle manually.",
|
|
124
|
+
[SpvFromBTCSwapState.CLAIMED]: "Swap settled on the smart chain and funds received"
|
|
125
|
+
}
|
|
126
|
+
|
|
112
127
|
export type SpvFromBTCSwapInit = ISwapInit & {
|
|
113
128
|
quoteId: string;
|
|
114
129
|
recipient: string;
|
|
@@ -179,6 +194,15 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
179
194
|
implements IBTCWalletSwap, ISwapWithGasDrop<T>, IClaimableSwap<T, SpvFromBTCTypeDefinition<T>, SpvFromBTCSwapState> {
|
|
180
195
|
|
|
181
196
|
readonly TYPE: SwapType.SPV_VAULT_FROM_BTC = SwapType.SPV_VAULT_FROM_BTC;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* @internal
|
|
200
|
+
*/
|
|
201
|
+
protected readonly swapStateDescription = SpvFromBTCSwapStateDescription;
|
|
202
|
+
/**
|
|
203
|
+
* @internal
|
|
204
|
+
*/
|
|
205
|
+
protected readonly swapStateName = (state: number) => SpvFromBTCSwapState[state];
|
|
182
206
|
/**
|
|
183
207
|
* @inheritDoc
|
|
184
208
|
* @internal
|
|
@@ -242,6 +266,12 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
242
266
|
*/
|
|
243
267
|
readonly minimumBtcFeeRate: number;
|
|
244
268
|
|
|
269
|
+
/**
|
|
270
|
+
* Time at which the SDK realized the bitcoin transaction was confirmed
|
|
271
|
+
* @private
|
|
272
|
+
*/
|
|
273
|
+
private btcTxConfirmedAt?: number;
|
|
274
|
+
|
|
245
275
|
constructor(wrapper: SpvFromBTCWrapper<T>, init: SpvFromBTCSwapInit);
|
|
246
276
|
constructor(wrapper: SpvFromBTCWrapper<T>, obj: any);
|
|
247
277
|
constructor(wrapper: SpvFromBTCWrapper<T>, initOrObject: SpvFromBTCSwapInit | any) {
|
|
@@ -306,6 +336,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
306
336
|
this._claimTxId = initOrObject.claimTxId;
|
|
307
337
|
this._frontTxId = initOrObject.frontTxId;
|
|
308
338
|
this.gasPricingInfo = deserializePriceInfoType(initOrObject.gasPricingInfo);
|
|
339
|
+
this.btcTxConfirmedAt = initOrObject.btcTxConfirmedAt;
|
|
309
340
|
if(initOrObject.data!=null) this._data = new this.wrapper._spvWithdrawalDataDeserializer(initOrObject.data);
|
|
310
341
|
}
|
|
311
342
|
this.tryCalculateSwapFee();
|
|
@@ -475,6 +506,15 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
475
506
|
return this._state===SpvFromBTCSwapState.FAILED || this._state===SpvFromBTCSwapState.DECLINED || this._state===SpvFromBTCSwapState.CLOSED;
|
|
476
507
|
}
|
|
477
508
|
|
|
509
|
+
/**
|
|
510
|
+
* @inheritDoc
|
|
511
|
+
*/
|
|
512
|
+
isInProgress(): boolean {
|
|
513
|
+
return this._state===SpvFromBTCSwapState.POSTED ||
|
|
514
|
+
this._state===SpvFromBTCSwapState.BROADCASTED ||
|
|
515
|
+
this._state===SpvFromBTCSwapState.BTC_TX_CONFIRMED;
|
|
516
|
+
}
|
|
517
|
+
|
|
478
518
|
/**
|
|
479
519
|
* @inheritDoc
|
|
480
520
|
*/
|
|
@@ -1033,12 +1073,14 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1033
1073
|
/**
|
|
1034
1074
|
* @inheritDoc
|
|
1035
1075
|
*
|
|
1076
|
+
* @param options.bitcoinFeeRate Optional fee rate to use for the created Bitcoin transaction
|
|
1036
1077
|
* @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
|
|
1037
1078
|
* if not provided a raw PSBT is returned instead which necessitates the implementor to manually add
|
|
1038
1079
|
* inputs to the bitcoin transaction and **set the nSequence field of the 2nd input** (input 1 -
|
|
1039
1080
|
* indexing from 0) to the value returned in `in1sequence`
|
|
1040
1081
|
*/
|
|
1041
1082
|
async txsExecute(options?: {
|
|
1083
|
+
bitcoinFeeRate?: number,
|
|
1042
1084
|
bitcoinWallet?: MinimalBitcoinWalletInterface,
|
|
1043
1085
|
}): Promise<[
|
|
1044
1086
|
SwapExecutionActionBitcoin<"RAW_PSBT" | "FUNDED_PSBT">
|
|
@@ -1053,7 +1095,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1053
1095
|
txs: [
|
|
1054
1096
|
options?.bitcoinWallet==null
|
|
1055
1097
|
? {...await this.getPsbt(), type: "RAW_PSBT"}
|
|
1056
|
-
: {...await this.getFundedPsbt(options.bitcoinWallet), type: "FUNDED_PSBT"}
|
|
1098
|
+
: {...await this.getFundedPsbt(options.bitcoinWallet, options?.bitcoinFeeRate), type: "FUNDED_PSBT"}
|
|
1057
1099
|
]
|
|
1058
1100
|
}
|
|
1059
1101
|
];
|
|
@@ -1063,6 +1105,47 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1063
1105
|
}
|
|
1064
1106
|
|
|
1065
1107
|
|
|
1108
|
+
/**
|
|
1109
|
+
* @inheritDoc
|
|
1110
|
+
*
|
|
1111
|
+
* @param options.bitcoinFeeRate Optional fee rate to use for the created Bitcoin transaction
|
|
1112
|
+
* @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
|
|
1113
|
+
* if not provided a raw PSBT is returned instead which necessitates the implementor to manually add
|
|
1114
|
+
* inputs to the bitcoin transaction and **set the nSequence field of the 2nd input** (input 1 -
|
|
1115
|
+
* indexing from 0) to the value returned in `in1sequence`
|
|
1116
|
+
* @param options.manualSettlementSmartChainSigner Optional smart chain signer to create a manual claim (settlement) transaction
|
|
1117
|
+
* @param options.maxWaitTillAutomaticSettlementSeconds Maximum time to wait for an automatic settlement after
|
|
1118
|
+
* the bitcoin transaction is confirmed (defaults to 60 seconds)
|
|
1119
|
+
*/
|
|
1120
|
+
async getCurrentActions(options?: {
|
|
1121
|
+
bitcoinFeeRate?: number,
|
|
1122
|
+
bitcoinWallet?: MinimalBitcoinWalletInterface,
|
|
1123
|
+
manualSettlementSmartChainSigner?: string | T["Signer"] | T["NativeSigner"],
|
|
1124
|
+
maxWaitTillAutomaticSettlementSeconds?: number
|
|
1125
|
+
}): Promise<SwapExecutionAction<T>[]> {
|
|
1126
|
+
if(this._state===SpvFromBTCSwapState.CREATED) {
|
|
1127
|
+
try {
|
|
1128
|
+
return await this.txsExecute(options);
|
|
1129
|
+
} catch (e) {}
|
|
1130
|
+
}
|
|
1131
|
+
if(this._state===SpvFromBTCSwapState.BTC_TX_CONFIRMED) {
|
|
1132
|
+
if(
|
|
1133
|
+
this.btcTxConfirmedAt==null ||
|
|
1134
|
+
options?.maxWaitTillAutomaticSettlementSeconds===0 ||
|
|
1135
|
+
(Date.now() - this.btcTxConfirmedAt) > (options?.maxWaitTillAutomaticSettlementSeconds ?? 60)*1000
|
|
1136
|
+
) {
|
|
1137
|
+
return [{
|
|
1138
|
+
name: "Claim" as const,
|
|
1139
|
+
description: "Manually settle (claim) the swap on the destination smart chain",
|
|
1140
|
+
chain: this.chainIdentifier,
|
|
1141
|
+
txs: await this.txsClaim(options?.manualSettlementSmartChainSigner)
|
|
1142
|
+
}];
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
return [];
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
|
|
1066
1149
|
//////////////////////////////
|
|
1067
1150
|
//// Bitcoin tx listener
|
|
1068
1151
|
|
|
@@ -1139,6 +1222,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1139
1222
|
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.FRONTED &&
|
|
1140
1223
|
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.CLAIMED
|
|
1141
1224
|
) {
|
|
1225
|
+
this.btcTxConfirmedAt ??= Date.now();
|
|
1142
1226
|
this._state = SpvFromBTCSwapState.BTC_TX_CONFIRMED;
|
|
1143
1227
|
save = true;
|
|
1144
1228
|
}
|
|
@@ -1378,7 +1462,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1378
1462
|
(this._state as SpvFromBTCSwapState)!==SpvFromBTCSwapState.CLAIMED
|
|
1379
1463
|
) {
|
|
1380
1464
|
this._claimTxId = res.txId;
|
|
1381
|
-
await this._saveAndEmit(SpvFromBTCSwapState.
|
|
1465
|
+
await this._saveAndEmit(SpvFromBTCSwapState.CLAIMED);
|
|
1382
1466
|
}
|
|
1383
1467
|
}
|
|
1384
1468
|
if(res.type===SpvWithdrawalStateType.CLOSED) {
|
|
@@ -1449,7 +1533,8 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1449
1533
|
senderAddress: this._senderAddress,
|
|
1450
1534
|
claimTxId: this._claimTxId,
|
|
1451
1535
|
frontTxId: this._frontTxId,
|
|
1452
|
-
data: this._data?.serialize()
|
|
1536
|
+
data: this._data?.serialize(),
|
|
1537
|
+
btcTxConfirmedAt: this.btcTxConfirmedAt
|
|
1453
1538
|
};
|
|
1454
1539
|
}
|
|
1455
1540
|
|
|
@@ -1514,6 +1599,7 @@ export class SpvFromBTCSwap<T extends ChainType>
|
|
|
1514
1599
|
this._state!==SpvFromBTCSwapState.FRONTED &&
|
|
1515
1600
|
this._state!==SpvFromBTCSwapState.CLAIMED
|
|
1516
1601
|
) {
|
|
1602
|
+
this.btcTxConfirmedAt ??= Date.now();
|
|
1517
1603
|
this._state = SpvFromBTCSwapState.BTC_TX_CONFIRMED;
|
|
1518
1604
|
needsSave = true;
|
|
1519
1605
|
}
|
|
@@ -13,7 +13,7 @@ import {TokenAmount, toTokenAmount} from "../../../types/TokenAmount";
|
|
|
13
13
|
import {BitcoinTokens, BtcToken, SCToken} from "../../../types/Token";
|
|
14
14
|
import {getLogger, LoggerType} from "../../../utils/Logger";
|
|
15
15
|
import {timeoutPromise} from "../../../utils/TimeoutUtils";
|
|
16
|
-
import {SwapExecutionActionLightning} from "../../../types/SwapExecutionAction";
|
|
16
|
+
import {SwapExecutionAction, SwapExecutionActionLightning} from "../../../types/SwapExecutionAction";
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* State enum for trusted Lightning gas swaps
|
|
@@ -30,7 +30,7 @@ export enum LnForGasSwapState {
|
|
|
30
30
|
*/
|
|
31
31
|
FAILED = -1,
|
|
32
32
|
/**
|
|
33
|
-
* Swap was created
|
|
33
|
+
* Swap was created, pay the provided lightning network invoice
|
|
34
34
|
*/
|
|
35
35
|
PR_CREATED = 0,
|
|
36
36
|
/**
|
|
@@ -43,6 +43,19 @@ export enum LnForGasSwapState {
|
|
|
43
43
|
FINISHED = 2
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
const LnForGasSwapStateDescription = {
|
|
47
|
+
[LnForGasSwapState.EXPIRED]:
|
|
48
|
+
"The swap quote expired without user sending in the lightning network payment",
|
|
49
|
+
[LnForGasSwapState.FAILED]:
|
|
50
|
+
"The swap has failed after the intermediary already received a lightning network payment on the source",
|
|
51
|
+
[LnForGasSwapState.PR_CREATED]:
|
|
52
|
+
"Swap was created, pay the provided lightning network invoice",
|
|
53
|
+
[LnForGasSwapState.PR_PAID]:
|
|
54
|
+
"User paid the lightning network invoice on the source",
|
|
55
|
+
[LnForGasSwapState.FINISHED]:
|
|
56
|
+
"The swap is finished after the intermediary sent funds on the destination chain"
|
|
57
|
+
}
|
|
58
|
+
|
|
46
59
|
export type LnForGasSwapInit = ISwapInit & {
|
|
47
60
|
pr: string;
|
|
48
61
|
outputAmount: bigint;
|
|
@@ -66,6 +79,16 @@ export function isLnForGasSwapInit(obj: any): obj is LnForGasSwapInit {
|
|
|
66
79
|
*/
|
|
67
80
|
export class LnForGasSwap<T extends ChainType = ChainType> extends ISwap<T, LnForGasSwapTypeDefinition<T>, LnForGasSwapState> implements IAddressSwap {
|
|
68
81
|
protected readonly TYPE: SwapType.TRUSTED_FROM_BTCLN = SwapType.TRUSTED_FROM_BTCLN;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
protected readonly swapStateDescription = LnForGasSwapStateDescription;
|
|
87
|
+
/**
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
protected readonly swapStateName = (state: number) => LnForGasSwapState[state];
|
|
91
|
+
|
|
69
92
|
/**
|
|
70
93
|
* @internal
|
|
71
94
|
*/
|
|
@@ -249,6 +272,13 @@ export class LnForGasSwap<T extends ChainType = ChainType> extends ISwap<T, LnFo
|
|
|
249
272
|
return this._state===LnForGasSwapState.FINISHED;
|
|
250
273
|
}
|
|
251
274
|
|
|
275
|
+
/**
|
|
276
|
+
* @inheritDoc
|
|
277
|
+
*/
|
|
278
|
+
isInProgress(): boolean {
|
|
279
|
+
return (this._state===LnForGasSwapState.PR_CREATED && this.initiated) || this._state===LnForGasSwapState.PR_PAID;
|
|
280
|
+
}
|
|
281
|
+
|
|
252
282
|
/**
|
|
253
283
|
* @inheritDoc
|
|
254
284
|
* @internal
|
|
@@ -397,6 +427,24 @@ export class LnForGasSwap<T extends ChainType = ChainType> extends ISwap<T, LnFo
|
|
|
397
427
|
throw new Error("Invalid swap state to obtain execution txns, required PR_CREATED");
|
|
398
428
|
}
|
|
399
429
|
|
|
430
|
+
/**
|
|
431
|
+
* @remark Not supported
|
|
432
|
+
*/
|
|
433
|
+
async execute(): Promise<boolean> {
|
|
434
|
+
throw new Error("Not supported");
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* @inheritDoc
|
|
439
|
+
*/
|
|
440
|
+
async getCurrentActions(): Promise<SwapExecutionAction<T>[]> {
|
|
441
|
+
try {
|
|
442
|
+
return await this.txsExecute();
|
|
443
|
+
} catch (e) {
|
|
444
|
+
return [];
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
400
448
|
/**
|
|
401
449
|
* Queries the intermediary (LP) node for the state of the swap
|
|
402
450
|
*
|
|
@@ -538,4 +586,4 @@ export class LnForGasSwap<T extends ChainType = ChainType> extends ISwap<T, LnFo
|
|
|
538
586
|
return Promise.resolve(false);
|
|
539
587
|
}
|
|
540
588
|
|
|
541
|
-
}
|
|
589
|
+
}
|
|
@@ -23,7 +23,7 @@ import {BitcoinTokens, BtcToken, SCToken} from "../../../types/Token";
|
|
|
23
23
|
import {getLogger, LoggerType} from "../../../utils/Logger";
|
|
24
24
|
import {timeoutPromise} from "../../../utils/TimeoutUtils";
|
|
25
25
|
import {toBitcoinWallet} from "../../../utils/BitcoinWalletUtils";
|
|
26
|
-
import {SwapExecutionActionBitcoin} from "../../../types/SwapExecutionAction";
|
|
26
|
+
import {SwapExecutionAction, SwapExecutionActionBitcoin} from "../../../types/SwapExecutionAction";
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* State enum for trusted on-chain gas swaps
|
|
@@ -44,7 +44,7 @@ export enum OnchainForGasSwapState {
|
|
|
44
44
|
*/
|
|
45
45
|
REFUNDED = -1,
|
|
46
46
|
/**
|
|
47
|
-
* Swap was created
|
|
47
|
+
* Swap was created, send the BTC to the swap address
|
|
48
48
|
*/
|
|
49
49
|
PR_CREATED = 0,
|
|
50
50
|
/**
|
|
@@ -57,6 +57,21 @@ export enum OnchainForGasSwapState {
|
|
|
57
57
|
REFUNDABLE = 2
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
const OnchainForGasSwapStateDescription: Record<OnchainForGasSwapState, string> = {
|
|
61
|
+
[OnchainForGasSwapState.EXPIRED]:
|
|
62
|
+
"The swap quote expired without user sending in the BTC",
|
|
63
|
+
[OnchainForGasSwapState.FAILED]:
|
|
64
|
+
"The swap has failed after the intermediary already received the BTC on the source chain",
|
|
65
|
+
[OnchainForGasSwapState.REFUNDED]:
|
|
66
|
+
"Swap was refunded and BTC returned to the user's refund address",
|
|
67
|
+
[OnchainForGasSwapState.PR_CREATED]:
|
|
68
|
+
"Swap was created, send the BTC to the swap address",
|
|
69
|
+
[OnchainForGasSwapState.FINISHED]:
|
|
70
|
+
"The swap is finished after the intermediary sent funds on the destination chain",
|
|
71
|
+
[OnchainForGasSwapState.REFUNDABLE]:
|
|
72
|
+
"Swap is refundable because the intermediary cannot honor the swap request on the destination chain",
|
|
73
|
+
};
|
|
74
|
+
|
|
60
75
|
export type OnchainForGasSwapInit = ISwapInit & {
|
|
61
76
|
paymentHash: string;
|
|
62
77
|
sequence: bigint;
|
|
@@ -86,8 +101,18 @@ export function isOnchainForGasSwapInit(obj: any): obj is OnchainForGasSwapInit
|
|
|
86
101
|
*
|
|
87
102
|
* @category Swaps/Trusted Gas Swaps
|
|
88
103
|
*/
|
|
89
|
-
export class OnchainForGasSwap<T extends ChainType = ChainType> extends ISwap<T, OnchainForGasSwapTypeDefinition<T
|
|
104
|
+
export class OnchainForGasSwap<T extends ChainType = ChainType> extends ISwap<T, OnchainForGasSwapTypeDefinition<T>, OnchainForGasSwapState> implements IAddressSwap, IBTCWalletSwap {
|
|
90
105
|
protected readonly TYPE: SwapType.TRUSTED_FROM_BTC = SwapType.TRUSTED_FROM_BTC;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @internal
|
|
109
|
+
*/
|
|
110
|
+
protected readonly swapStateDescription = OnchainForGasSwapStateDescription;
|
|
111
|
+
/**
|
|
112
|
+
* @internal
|
|
113
|
+
*/
|
|
114
|
+
protected readonly swapStateName = (state: number) => OnchainForGasSwapState[state];
|
|
115
|
+
|
|
91
116
|
/**
|
|
92
117
|
* @internal
|
|
93
118
|
*/
|
|
@@ -289,6 +314,14 @@ export class OnchainForGasSwap<T extends ChainType = ChainType> extends ISwap<T,
|
|
|
289
314
|
return this._state===OnchainForGasSwapState.FINISHED;
|
|
290
315
|
}
|
|
291
316
|
|
|
317
|
+
/**
|
|
318
|
+
* @inheritDoc
|
|
319
|
+
*/
|
|
320
|
+
isInProgress(): boolean {
|
|
321
|
+
return (this._state===OnchainForGasSwapState.PR_CREATED && this.txId!=null) ||
|
|
322
|
+
(this._state===OnchainForGasSwapState.REFUNDABLE && this.refundAddress!=null);
|
|
323
|
+
}
|
|
324
|
+
|
|
292
325
|
/**
|
|
293
326
|
* @inheritDoc
|
|
294
327
|
* @internal
|
|
@@ -559,6 +592,28 @@ export class OnchainForGasSwap<T extends ChainType = ChainType> extends ISwap<T,
|
|
|
559
592
|
throw new Error("Invalid swap state to obtain execution txns, required PR_CREATED or CLAIM_COMMITED");
|
|
560
593
|
}
|
|
561
594
|
|
|
595
|
+
/**
|
|
596
|
+
* @remark Not supported
|
|
597
|
+
*/
|
|
598
|
+
async execute(): Promise<boolean> {
|
|
599
|
+
throw new Error("Not supported");
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* @inheritDoc
|
|
604
|
+
*
|
|
605
|
+
* @param options.bitcoinWallet Optional bitcoin wallet address specification to return a funded PSBT,
|
|
606
|
+
* if not provided an address is returned instead.
|
|
607
|
+
*/
|
|
608
|
+
async getCurrentActions(options?: {
|
|
609
|
+
bitcoinWallet?: MinimalBitcoinWalletInterface
|
|
610
|
+
}): Promise<SwapExecutionAction<T>[]> {
|
|
611
|
+
try {
|
|
612
|
+
return await this.txsExecute(options);
|
|
613
|
+
} catch (e) {
|
|
614
|
+
return [];
|
|
615
|
+
}
|
|
616
|
+
}
|
|
562
617
|
|
|
563
618
|
//////////////////////////////
|
|
564
619
|
//// Payment
|