@atomiqlabs/sdk 8.7.7 → 8.9.0
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/api/index.d.ts +1 -0
- package/api/index.js +3 -0
- package/dist/ApiList.d.ts +37 -0
- package/dist/ApiList.js +30 -0
- package/dist/api/ApiEndpoints.d.ts +393 -0
- package/dist/api/ApiEndpoints.js +2 -0
- package/dist/api/ApiParser.d.ts +10 -0
- package/dist/api/ApiParser.js +134 -0
- package/dist/api/ApiTypes.d.ts +157 -0
- package/dist/api/ApiTypes.js +75 -0
- package/dist/api/SerializedAction.d.ts +40 -0
- package/dist/api/SerializedAction.js +59 -0
- package/dist/api/SwapperApi.d.ts +50 -0
- package/dist/api/SwapperApi.js +431 -0
- package/dist/api/index.d.ts +5 -0
- package/dist/api/index.js +24 -0
- package/dist/bitcoin/coinselect2/accumulative.d.ts +1 -0
- package/dist/bitcoin/coinselect2/accumulative.js +1 -1
- package/dist/bitcoin/coinselect2/blackjack.d.ts +1 -0
- package/dist/bitcoin/coinselect2/blackjack.js +1 -1
- package/dist/bitcoin/coinselect2/index.d.ts +3 -2
- package/dist/bitcoin/coinselect2/index.js +2 -2
- package/dist/bitcoin/coinselect2/utils.d.ts +7 -2
- package/dist/bitcoin/coinselect2/utils.js +45 -10
- package/dist/bitcoin/wallet/BitcoinWallet.d.ts +8 -25
- package/dist/bitcoin/wallet/BitcoinWallet.js +31 -18
- package/dist/bitcoin/wallet/IBitcoinWallet.d.ts +40 -2
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.d.ts +7 -2
- package/dist/bitcoin/wallet/SingleAddressBitcoinWallet.js +10 -4
- package/dist/events/UnifiedSwapEventListener.d.ts +4 -3
- package/dist/events/UnifiedSwapEventListener.js +8 -2
- package/dist/http/HttpUtils.d.ts +4 -2
- package/dist/http/HttpUtils.js +10 -4
- package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +2 -1
- package/dist/http/paramcoders/client/StreamingFetchPromise.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/intermediaries/IntermediaryDiscovery.d.ts +7 -2
- package/dist/intermediaries/IntermediaryDiscovery.js +4 -4
- package/dist/intermediaries/apis/IntermediaryAPI.d.ts +182 -15
- package/dist/intermediaries/apis/IntermediaryAPI.js +192 -31
- package/dist/intermediaries/auth/SignedKeyBasedAuth.d.ts +14 -0
- package/dist/intermediaries/auth/SignedKeyBasedAuth.js +68 -0
- package/dist/storage/IUnifiedStorage.d.ts +45 -3
- package/dist/storage/UnifiedSwapStorage.d.ts +8 -2
- package/dist/storage/UnifiedSwapStorage.js +46 -8
- package/dist/swapper/Swapper.d.ts +77 -4
- package/dist/swapper/Swapper.js +117 -25
- package/dist/swapper/SwapperUtils.d.ts +18 -2
- package/dist/swapper/SwapperUtils.js +39 -1
- package/dist/swaps/ISwap.d.ts +70 -9
- package/dist/swaps/ISwap.js +28 -6
- package/dist/swaps/ISwapWrapper.d.ts +11 -1
- package/dist/swaps/ISwapWrapper.js +23 -3
- package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +1 -1
- package/dist/swaps/escrow_swaps/IEscrowSwap.js +4 -2
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +2 -1
- package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +2 -2
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +3 -2
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +47 -31
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +201 -67
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +6 -6
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +82 -15
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +304 -98
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +6 -6
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +75 -42
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +424 -87
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +7 -7
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +54 -11
- package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +214 -41
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +2 -1
- package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +7 -8
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +3 -1
- package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +5 -5
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +85 -22
- package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +299 -56
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +41 -7
- package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +183 -58
- package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +53 -12
- package/dist/swaps/trusted/ln/LnForGasSwap.js +163 -49
- package/dist/swaps/trusted/ln/LnForGasWrapper.js +1 -2
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +14 -13
- package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +30 -47
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +3 -1
- package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +4 -4
- package/dist/types/SwapExecutionAction.d.ts +141 -34
- package/dist/types/SwapExecutionAction.js +104 -0
- package/dist/types/SwapExecutionStep.d.ts +144 -0
- package/dist/types/SwapExecutionStep.js +87 -0
- package/dist/types/TokenAmount.d.ts +6 -0
- package/dist/types/TokenAmount.js +26 -1
- package/dist/utils/BitcoinUtils.d.ts +4 -0
- package/dist/utils/BitcoinUtils.js +73 -1
- package/dist/utils/BitcoinWalletUtils.d.ts +2 -2
- package/dist/utils/Utils.d.ts +3 -1
- package/dist/utils/Utils.js +7 -1
- package/package.json +7 -4
- package/src/api/ApiEndpoints.ts +427 -0
- package/src/api/ApiParser.ts +138 -0
- package/src/api/ApiTypes.ts +229 -0
- package/src/api/SerializedAction.ts +97 -0
- package/src/api/SwapperApi.ts +545 -0
- package/src/api/index.ts +5 -0
- package/src/bitcoin/coinselect2/accumulative.ts +2 -1
- package/src/bitcoin/coinselect2/blackjack.ts +2 -1
- package/src/bitcoin/coinselect2/index.ts +5 -4
- package/src/bitcoin/coinselect2/utils.ts +55 -14
- package/src/bitcoin/wallet/BitcoinWallet.ts +69 -57
- package/src/bitcoin/wallet/IBitcoinWallet.ts +44 -3
- package/src/bitcoin/wallet/SingleAddressBitcoinWallet.ts +12 -4
- package/src/events/UnifiedSwapEventListener.ts +11 -3
- package/src/http/HttpUtils.ts +10 -4
- package/src/http/paramcoders/client/StreamingFetchPromise.ts +4 -2
- package/src/index.ts +1 -0
- package/src/intermediaries/IntermediaryDiscovery.ts +9 -2
- package/src/intermediaries/apis/IntermediaryAPI.ts +335 -35
- package/src/intermediaries/auth/SignedKeyBasedAuth.ts +69 -0
- package/src/storage/IUnifiedStorage.ts +45 -4
- package/src/storage/UnifiedSwapStorage.ts +42 -8
- package/src/swapper/Swapper.ts +165 -24
- package/src/swapper/SwapperUtils.ts +42 -2
- package/src/swaps/ISwap.ts +88 -16
- package/src/swaps/ISwapWrapper.ts +28 -3
- package/src/swaps/escrow_swaps/IEscrowSwap.ts +5 -3
- package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +3 -1
- package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +4 -1
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +264 -67
- package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +6 -4
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +390 -89
- package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +6 -4
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +548 -94
- package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +7 -5
- package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +276 -45
- package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +7 -6
- package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +5 -3
- package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +413 -64
- package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +239 -61
- package/src/swaps/trusted/ln/LnForGasSwap.ts +211 -47
- package/src/swaps/trusted/ln/LnForGasWrapper.ts +1 -2
- package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +32 -51
- package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +5 -3
- package/src/types/SwapExecutionAction.ts +266 -43
- package/src/types/SwapExecutionStep.ts +224 -0
- package/src/types/TokenAmount.ts +36 -2
- package/src/utils/BitcoinUtils.ts +73 -0
- package/src/utils/BitcoinWalletUtils.ts +2 -2
- package/src/utils/Utils.ts +10 -1
- package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +0 -258
|
@@ -16,11 +16,12 @@ export type QueryParams = {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* Base type for stored objects, every storage object MUST have an `id` field
|
|
19
|
+
* Base type for stored objects, every storage object MUST have an `id` field. The object might also specify a `_meta`
|
|
20
|
+
* field which gets carried to the delete/save operations (and can be used to implement optimistic concurrency)
|
|
20
21
|
*
|
|
21
22
|
* @category Storage
|
|
22
23
|
*/
|
|
23
|
-
export type UnifiedStoredObject = {id: string} & any;
|
|
24
|
+
export type UnifiedStoredObject = {id: string, _meta?: any} & any;
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* Defines simple indexes (for queries that use a single key)
|
|
@@ -64,32 +65,72 @@ export interface IUnifiedStorage<I extends UnifiedStorageIndexes, C extends Unif
|
|
|
64
65
|
* - [[condition1, condition2]] - returns all rows where condition1 AND condition2 is met
|
|
65
66
|
* - [[condition1], [condition2]] - returns all rows where condition1 OR condition2 is met
|
|
66
67
|
* - [[condition1, condition2], [condition3]] - returns all rows where (condition1 AND condition2) OR condition3 is met
|
|
68
|
+
*
|
|
69
|
+
* You can also add an optional `_meta` field in the returned unified storage object which gets attached to that
|
|
70
|
+
* returned object and will be present for subsequent saves and removal of this object, if you specify the `_meta`
|
|
71
|
+
* field here, you need to explicitly handle it in the all the saving and remove functions and not simply serialize
|
|
72
|
+
* it into the storage
|
|
73
|
+
*
|
|
67
74
|
* @param params
|
|
68
75
|
*/
|
|
69
76
|
query(params: Array<Array<QueryParams>>): Promise<Array<UnifiedStoredObject>>;
|
|
70
77
|
|
|
71
78
|
/**
|
|
72
79
|
* Saves an object to storage, updating indexes as needed
|
|
80
|
+
*
|
|
81
|
+
* If the object contains a `_meta` field, this will be also present in the to-be-saved value, to mutate the `_meta`
|
|
82
|
+
* field of the object that is saved, you can mutate the `_meta` field directly on the passed value, which then
|
|
83
|
+
* gets reflected automatically in the existing object. The mutated `_meta` field is copied even if the function
|
|
84
|
+
* throws, hence the implementations must be careful with setting the `_meta` field on the still in-flight requests
|
|
85
|
+
* that might fail.
|
|
86
|
+
*
|
|
73
87
|
* @param value Object to save (must have an id property)
|
|
74
88
|
*/
|
|
75
89
|
save(value: UnifiedStoredObject): Promise<void>;
|
|
76
90
|
|
|
77
91
|
/**
|
|
78
92
|
* Saves multiple objects to storage in a batch operation
|
|
93
|
+
*
|
|
94
|
+
* If the objects contain a `_meta` field, this will be also present in the to-be-saved values, to mutate the `_meta`
|
|
95
|
+
* field of the objects that are saved, you can mutate the `_meta` field directly on the passed values, which then
|
|
96
|
+
* gets reflected automatically in the existing objects. The mutated `_meta` field is copied even if the function
|
|
97
|
+
* throws, hence the implementations must be careful with setting the `_meta` field on the still in-flight requests
|
|
98
|
+
* that might fail.
|
|
99
|
+
*
|
|
79
100
|
* @param value Array of objects to save
|
|
101
|
+
* @param lenient In lenient mode the persistency layer doesn't throw on individual swap failures due to
|
|
102
|
+
* optimistic concurrency, or other (implementation specific), this flag is to be used when the saving of the swap
|
|
103
|
+
* isn't mission-critical for executing next steps (e.g. in tick or sync loops)
|
|
80
104
|
*/
|
|
81
|
-
saveAll(value: UnifiedStoredObject[]): Promise<void>;
|
|
105
|
+
saveAll(value: UnifiedStoredObject[], lenient?: boolean): Promise<void>;
|
|
82
106
|
|
|
83
107
|
/**
|
|
84
108
|
* Removes an object from storage
|
|
109
|
+
*
|
|
110
|
+
* If the object contains a `_meta` field, this will be also present in the to-be-removed value, to mutate the `_meta`
|
|
111
|
+
* field of the object that is saved, you can mutate the `_meta` field directly on the passed value, which then
|
|
112
|
+
* gets reflected automatically in the existing object. The mutated `_meta` field is copied even if the function
|
|
113
|
+
* throws, hence the implementations must be careful with setting the `_meta` field on the still in-flight requests
|
|
114
|
+
* that might fail.
|
|
115
|
+
*
|
|
85
116
|
* @param value Object to remove (must have an id property)
|
|
86
117
|
*/
|
|
87
118
|
remove(value: UnifiedStoredObject): Promise<void>;
|
|
88
119
|
|
|
89
120
|
/**
|
|
90
121
|
* Removes multiple objects from storage in a batch operation
|
|
122
|
+
*
|
|
123
|
+
* If the objects contain a `_meta` field, this will be also present in the to-be-removed values, to mutate the `_meta`
|
|
124
|
+
* field of the objects that are saved, you can mutate the `_meta` field directly on the passed values, which then
|
|
125
|
+
* gets reflected automatically in the existing objects. The mutated `_meta` field is copied even if the function
|
|
126
|
+
* throws, hence the implementations must be careful with setting the `_meta` field on the still in-flight requests
|
|
127
|
+
* that might fail.
|
|
128
|
+
*
|
|
91
129
|
* @param value Array of objects to remove
|
|
130
|
+
* @param lenient In lenient mode the persistency layer doesn't throw on individual swap failures due to
|
|
131
|
+
* optimistic concurrency, or other (implementation specific), this flag is to be used when the saving of the swap
|
|
132
|
+
* isn't mission-critical for executing next steps (e.g. in tick or sync loops)
|
|
92
133
|
*/
|
|
93
|
-
removeAll(value: UnifiedStoredObject[]): Promise<void>;
|
|
134
|
+
removeAll(value: UnifiedStoredObject[], lenient?: boolean): Promise<void>;
|
|
94
135
|
|
|
95
136
|
}
|
|
@@ -104,18 +104,35 @@ export class UnifiedSwapStorage<T extends ChainType> {
|
|
|
104
104
|
*/
|
|
105
105
|
async save<S extends ISwap<T>>(value: S): Promise<void> {
|
|
106
106
|
if(!this.noWeakRefMap) this.weakRefCache.set(value.getId(), new WeakRef<ISwap<T>>(value));
|
|
107
|
-
|
|
107
|
+
const serialized = value.serialize();
|
|
108
|
+
try {
|
|
109
|
+
await this.storage.save(serialized);
|
|
110
|
+
} finally {
|
|
111
|
+
value._meta = serialized._meta;
|
|
112
|
+
}
|
|
108
113
|
value._persisted = true;
|
|
109
114
|
}
|
|
110
115
|
|
|
111
116
|
/**
|
|
112
117
|
* Saves multiple swaps to storage in a batch operation
|
|
113
118
|
* @param values Array of swaps to save
|
|
119
|
+
* @param lenient In lenient mode the underlying persistent layer doesn't throw on individual swap failures due to
|
|
120
|
+
* optimistic concurrency, or other (implementation specific), this flag is to be used when the saving of the swap
|
|
121
|
+
* isn't mission-critical for executing next steps (e.g. in tick or sync loops)
|
|
114
122
|
*/
|
|
115
|
-
async saveAll<S extends ISwap<T>>(values: S[]): Promise<void> {
|
|
123
|
+
async saveAll<S extends ISwap<T>>(values: S[], lenient?: boolean): Promise<void> {
|
|
116
124
|
if(!this.noWeakRefMap) values.forEach(value => this.weakRefCache.set(value.getId(), new WeakRef<ISwap<T>>(value)));
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
const serialized = values.map(obj => obj.serialize());
|
|
126
|
+
try {
|
|
127
|
+
await this.storage.saveAll(serialized, lenient);
|
|
128
|
+
} finally {
|
|
129
|
+
values.forEach((value, index) => {
|
|
130
|
+
value._meta = serialized[index]._meta;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
values.forEach((value) => {
|
|
134
|
+
value._persisted = true;
|
|
135
|
+
});
|
|
119
136
|
}
|
|
120
137
|
|
|
121
138
|
/**
|
|
@@ -124,18 +141,35 @@ export class UnifiedSwapStorage<T extends ChainType> {
|
|
|
124
141
|
*/
|
|
125
142
|
async remove<S extends ISwap<T>>(value: S): Promise<void> {
|
|
126
143
|
if(!this.noWeakRefMap) this.weakRefCache.delete(value.getId());
|
|
127
|
-
|
|
144
|
+
const serialized = value.serialize();
|
|
145
|
+
try {
|
|
146
|
+
await this.storage.remove(serialized);
|
|
147
|
+
} finally {
|
|
148
|
+
value._meta = serialized._meta;
|
|
149
|
+
}
|
|
128
150
|
value._persisted = false;
|
|
129
151
|
}
|
|
130
152
|
|
|
131
153
|
/**
|
|
132
154
|
* Removes multiple swaps from storage in a batch operation
|
|
133
155
|
* @param values Array of swaps to remove
|
|
156
|
+
* @param lenient In lenient mode the underlying persistent layer doesn't throw on individual swap failures due to
|
|
157
|
+
* optimistic concurrency, or other (implementation specific), this flag is to be used when the saving of the swap
|
|
158
|
+
* isn't mission-critical for executing next steps (e.g. in tick or sync loops)
|
|
134
159
|
*/
|
|
135
|
-
async removeAll<S extends ISwap<T>>(values: S[]): Promise<void> {
|
|
160
|
+
async removeAll<S extends ISwap<T>>(values: S[], lenient?: boolean): Promise<void> {
|
|
136
161
|
if(!this.noWeakRefMap) values.forEach(value => this.weakRefCache.delete(value.getId()));
|
|
137
|
-
|
|
138
|
-
|
|
162
|
+
const serialized = values.map(obj => obj.serialize());
|
|
163
|
+
try {
|
|
164
|
+
await this.storage.removeAll(serialized, lenient);
|
|
165
|
+
} finally {
|
|
166
|
+
values.forEach((value, index) => {
|
|
167
|
+
value._meta = serialized[index]._meta;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
values.forEach((value) => {
|
|
171
|
+
value._persisted = false;
|
|
172
|
+
});
|
|
139
173
|
}
|
|
140
174
|
|
|
141
175
|
}
|
package/src/swapper/Swapper.ts
CHANGED
|
@@ -65,6 +65,11 @@ import {NotNever} from "../utils/TypeUtils";
|
|
|
65
65
|
import {IEscrowSwap} from "../swaps/escrow_swaps/IEscrowSwap";
|
|
66
66
|
import {LightningInvoiceCreateService, isLightningInvoiceCreateService} from "../types/wallets/LightningInvoiceCreateService";
|
|
67
67
|
import {SwapSide} from "../enums/SwapSide";
|
|
68
|
+
import {IntermediaryAPI} from "../intermediaries/apis/IntermediaryAPI";
|
|
69
|
+
import {BitcoinWalletUtxo, BitcoinWalletUtxoBase, IBitcoinWallet} from "../bitcoin/wallet/IBitcoinWallet";
|
|
70
|
+
import {MinimalBitcoinWalletInterface} from "../types/wallets/MinimalBitcoinWalletInterface";
|
|
71
|
+
import {toBitcoinWallet} from "../utils/BitcoinWalletUtils";
|
|
72
|
+
import {getSignedKeyBasedAuthHandler} from "../intermediaries/auth/SignedKeyBasedAuth";
|
|
68
73
|
|
|
69
74
|
/**
|
|
70
75
|
* Configuration options for the Swapper
|
|
@@ -126,8 +131,9 @@ export type SwapperOptions = {
|
|
|
126
131
|
noTimers?: boolean,
|
|
127
132
|
/**
|
|
128
133
|
* By setting this flag, the swapper doesn't subscribe to on-chain events. To make sure the swap states are
|
|
129
|
-
* properly updated you should call the {@link Swapper._syncSwaps} function periodically
|
|
130
|
-
*
|
|
134
|
+
* properly updated you should either call the {@link Swapper._syncSwaps} function periodically, or use the
|
|
135
|
+
* {@link Swapper._pollChainEvents} function to manually poll for on-chain events. This flag should be set
|
|
136
|
+
* when you run an environment that doesn't support long-running timers and websocket connections - e.g.
|
|
131
137
|
* serverless environments like Azure Function Apps or AWS Lambda
|
|
132
138
|
*/
|
|
133
139
|
noEvents?: boolean,
|
|
@@ -157,7 +163,7 @@ export type SwapperOptions = {
|
|
|
157
163
|
* want to only create a swap, and then later on retrieve it with the `swapper.getSwapById()` function.
|
|
158
164
|
*
|
|
159
165
|
* Setting this to `false` means the SDK only saves and persists swaps that are considered initiated, i.e. when
|
|
160
|
-
* `commit()`, `execute()` or `waitTillPayment` is called (or their respective txs... prefixed variations). This
|
|
166
|
+
* `commit()`, `execute()` or `waitTillPayment()` is called (or their respective txs... prefixed variations). This
|
|
161
167
|
* might save calls to the persistent storage for swaps that are never initiated. This is useful in e.g.
|
|
162
168
|
* frontend implementations where the frontend holds the swap object reference until it is initiated anyway, not
|
|
163
169
|
* necessitating the saving of the swap data to the persistent storage until it is actually initiated.
|
|
@@ -167,7 +173,14 @@ export type SwapperOptions = {
|
|
|
167
173
|
* Automatically checks system time on initialize, if the system time drifts too far from the actual time
|
|
168
174
|
* (as checked from multiple server sources) it adjusts the `Date.now()` function to return proper actual time.
|
|
169
175
|
*/
|
|
170
|
-
automaticClockDriftCorrection?: boolean
|
|
176
|
+
automaticClockDriftCorrection?: boolean,
|
|
177
|
+
/**
|
|
178
|
+
* Used in centralized API deployments to allow higher rate limits from LPs
|
|
179
|
+
*/
|
|
180
|
+
signedKeyBasedAuth?: {
|
|
181
|
+
certificate: string,
|
|
182
|
+
privateKey: string
|
|
183
|
+
}
|
|
171
184
|
};
|
|
172
185
|
|
|
173
186
|
/**
|
|
@@ -304,6 +317,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
304
317
|
* Pricing API used by the SDK
|
|
305
318
|
*/
|
|
306
319
|
readonly prices: ISwapPrice<T>;
|
|
320
|
+
/**
|
|
321
|
+
* API for contacting LPs
|
|
322
|
+
*/
|
|
323
|
+
readonly lpApi: IntermediaryAPI;
|
|
307
324
|
/**
|
|
308
325
|
* Intermediary discovery instance
|
|
309
326
|
*/
|
|
@@ -359,6 +376,13 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
359
376
|
this._tokens[chainId][tokenData.address] = this._tokensByTicker[chainId][tokenData.ticker] = tokenData;
|
|
360
377
|
}
|
|
361
378
|
|
|
379
|
+
const lpApi = new IntermediaryAPI(
|
|
380
|
+
this.options.signedKeyBasedAuth!=null
|
|
381
|
+
? getSignedKeyBasedAuthHandler(this.options.signedKeyBasedAuth.certificate, this.options.signedKeyBasedAuth.privateKey)
|
|
382
|
+
: undefined
|
|
383
|
+
);
|
|
384
|
+
this.lpApi = lpApi;
|
|
385
|
+
|
|
362
386
|
this.swapStateListener = (swap: ISwap) => {
|
|
363
387
|
this.emit("swapState", swap);
|
|
364
388
|
};
|
|
@@ -410,6 +434,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
410
434
|
pricing,
|
|
411
435
|
this._tokens[chainId],
|
|
412
436
|
versions,
|
|
437
|
+
lpApi,
|
|
413
438
|
{
|
|
414
439
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
415
440
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
@@ -425,6 +450,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
425
450
|
this._tokens[chainId],
|
|
426
451
|
versions,
|
|
427
452
|
this._bitcoinRpc,
|
|
453
|
+
lpApi,
|
|
428
454
|
{
|
|
429
455
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
430
456
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
@@ -441,6 +467,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
441
467
|
this._tokens[chainId],
|
|
442
468
|
versions,
|
|
443
469
|
lightningApi,
|
|
470
|
+
lpApi,
|
|
444
471
|
{
|
|
445
472
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
446
473
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
@@ -458,6 +485,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
458
485
|
versions,
|
|
459
486
|
versionedContracts,
|
|
460
487
|
this._bitcoinRpc,
|
|
488
|
+
lpApi,
|
|
461
489
|
{
|
|
462
490
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
463
491
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
@@ -472,6 +500,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
472
500
|
chainInterface,
|
|
473
501
|
pricing,
|
|
474
502
|
this._tokens[chainId],
|
|
503
|
+
lpApi,
|
|
475
504
|
{
|
|
476
505
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
477
506
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
@@ -486,6 +515,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
486
515
|
pricing,
|
|
487
516
|
this._tokens[chainId],
|
|
488
517
|
bitcoinRpc,
|
|
518
|
+
lpApi,
|
|
489
519
|
{
|
|
490
520
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
491
521
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
@@ -506,6 +536,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
506
536
|
versions,
|
|
507
537
|
versionedContracts,
|
|
508
538
|
bitcoinRpc,
|
|
539
|
+
lpApi,
|
|
509
540
|
{
|
|
510
541
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
511
542
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
@@ -527,6 +558,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
527
558
|
versions,
|
|
528
559
|
lightningApi,
|
|
529
560
|
this.messenger,
|
|
561
|
+
lpApi,
|
|
530
562
|
{
|
|
531
563
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
532
564
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
@@ -563,9 +595,9 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
563
595
|
|
|
564
596
|
const contracts = objectMap(chainsData, (data) => data.versions ?? {[data.defaultVersion ?? "v1"]: {swapContract: data.swapContract, spvVaultContract: data.spvVaultContract}});
|
|
565
597
|
if(options.intermediaryUrl!=null) {
|
|
566
|
-
this.intermediaryDiscovery = new IntermediaryDiscovery(contracts, options.registryUrl, Array.isArray(options.intermediaryUrl) ? options.intermediaryUrl : [options.intermediaryUrl], options.getRequestTimeout);
|
|
598
|
+
this.intermediaryDiscovery = new IntermediaryDiscovery(contracts, lpApi, options.registryUrl, Array.isArray(options.intermediaryUrl) ? options.intermediaryUrl : [options.intermediaryUrl], options.getRequestTimeout);
|
|
567
599
|
} else {
|
|
568
|
-
this.intermediaryDiscovery = new IntermediaryDiscovery(contracts, options.registryUrl, undefined, options.getRequestTimeout);
|
|
600
|
+
this.intermediaryDiscovery = new IntermediaryDiscovery(contracts, lpApi, options.registryUrl, undefined, options.getRequestTimeout);
|
|
569
601
|
}
|
|
570
602
|
|
|
571
603
|
this.intermediaryDiscovery.on("removed", (intermediaries: Intermediary[]) => {
|
|
@@ -578,7 +610,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
578
610
|
}
|
|
579
611
|
|
|
580
612
|
private async _init(): Promise<void> {
|
|
581
|
-
this.logger.debug("init(): Initializing swapper
|
|
613
|
+
this.logger.debug("init(): Initializing swapper");
|
|
582
614
|
|
|
583
615
|
const abortController = new AbortController();
|
|
584
616
|
|
|
@@ -664,7 +696,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
664
696
|
)
|
|
665
697
|
}
|
|
666
698
|
|
|
667
|
-
|
|
699
|
+
await unifiedChainEvents.start(this.options.noEvents);
|
|
668
700
|
this.logger.debug("init(): Intialized events: "+chainIdentifier);
|
|
669
701
|
|
|
670
702
|
for(let key in wrappers) {
|
|
@@ -706,6 +738,13 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
706
738
|
}
|
|
707
739
|
}
|
|
708
740
|
|
|
741
|
+
/**
|
|
742
|
+
* Whether the SDK is initialized (after {@link init} is called)
|
|
743
|
+
*/
|
|
744
|
+
isInitialized(): boolean {
|
|
745
|
+
return this.initialized;
|
|
746
|
+
}
|
|
747
|
+
|
|
709
748
|
/**
|
|
710
749
|
* Stops listening for onchain events and closes this Swapper instance
|
|
711
750
|
*/
|
|
@@ -745,7 +784,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
745
784
|
quote: Promise<S>,
|
|
746
785
|
intermediary: Intermediary
|
|
747
786
|
}[]>,
|
|
748
|
-
amountData:
|
|
787
|
+
amountData: { amount?: bigint, token: string, exactIn: boolean },
|
|
749
788
|
swapType: SwapType,
|
|
750
789
|
maxWaitTimeMS: number = 2000
|
|
751
790
|
): Promise<S> {
|
|
@@ -755,7 +794,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
755
794
|
|
|
756
795
|
const inBtc: boolean = swapType===SwapType.TO_BTCLN || swapType===SwapType.TO_BTC ? !amountData.exactIn : amountData.exactIn;
|
|
757
796
|
|
|
758
|
-
if(!inBtc) {
|
|
797
|
+
if(!inBtc || amountData.amount==null) {
|
|
759
798
|
//Get candidates not based on the amount
|
|
760
799
|
candidates = this.intermediaryDiscovery.getSwapCandidates(chainIdentifier, swapType, amountData.token);
|
|
761
800
|
} else {
|
|
@@ -769,7 +808,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
769
808
|
await this.intermediaryDiscovery.reloadIntermediaries();
|
|
770
809
|
swapLimitsChanged = true;
|
|
771
810
|
|
|
772
|
-
if(!inBtc) {
|
|
811
|
+
if(!inBtc || amountData.amount==null) {
|
|
773
812
|
//Get candidates not based on the amount
|
|
774
813
|
candidates = this.intermediaryDiscovery.getSwapCandidates(chainIdentifier, swapType, amountData.token);
|
|
775
814
|
} else {
|
|
@@ -859,8 +898,10 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
859
898
|
}
|
|
860
899
|
if(min!=null && max!=null) {
|
|
861
900
|
let msg = "Swap amount too high or too low! Try swapping a different amount.";
|
|
862
|
-
if(
|
|
863
|
-
|
|
901
|
+
if(amountData.amount!=null) {
|
|
902
|
+
if(min > amountData.amount) msg = "Swap amount too low! Try swapping a higher amount.";
|
|
903
|
+
if(max < amountData.amount) msg = "Swap amount too high! Try swapping a lower amount.";
|
|
904
|
+
}
|
|
864
905
|
reject(new OutOfBoundsError(msg, 400, min, max));
|
|
865
906
|
return;
|
|
866
907
|
}
|
|
@@ -1102,7 +1143,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1102
1143
|
chainIdentifier: ChainIdentifier,
|
|
1103
1144
|
recipient: string,
|
|
1104
1145
|
tokenAddress: string,
|
|
1105
|
-
amount: bigint,
|
|
1146
|
+
amount: bigint | null,
|
|
1106
1147
|
exactOut: boolean = false,
|
|
1107
1148
|
additionalParams: Record<string, any> | undefined = this.options.defaultAdditionalParameters,
|
|
1108
1149
|
options?: SpvFromBTCOptions
|
|
@@ -1112,7 +1153,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1112
1153
|
if(!this._chains[chainIdentifier].chainInterface.isValidAddress(recipient, true)) throw new Error("Invalid "+chainIdentifier+" address");
|
|
1113
1154
|
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
1114
1155
|
const amountData = {
|
|
1115
|
-
amount,
|
|
1156
|
+
amount: amount ?? undefined,
|
|
1116
1157
|
token: tokenAddress,
|
|
1117
1158
|
exactIn: !exactOut
|
|
1118
1159
|
};
|
|
@@ -1516,6 +1557,9 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1516
1557
|
dst: string | LNURLPay | LightningInvoiceCreateService,
|
|
1517
1558
|
options?: FromBTCLNOptions | SpvFromBTCOptions | FromBTCOptions | ToBTCOptions | (ToBTCLNOptions & {comment?: string}) | FromBTCLNAutoOptions
|
|
1518
1559
|
): Promise<ISwap<T[C]>> {
|
|
1560
|
+
if(typeof(src)==="string") src = this.Utils.stripAddress(src);
|
|
1561
|
+
if(typeof(dst)==="string") dst = this.Utils.stripAddress(dst);
|
|
1562
|
+
|
|
1519
1563
|
const srcToken = typeof(_srcToken)==="string" ? this.getToken(_srcToken) as Token<C> : _srcToken;
|
|
1520
1564
|
const dstToken = typeof(_dstToken)==="string" ? this.getToken(_dstToken) as Token<C> : _dstToken;
|
|
1521
1565
|
const amount = _amount==null ? null : (typeof(_amount)==="bigint" ? _amount : fromDecimal(_amount, exactIn ? srcToken.decimals : dstToken.decimals));
|
|
@@ -1576,6 +1620,73 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1576
1620
|
throw new Error("Unsupported swap type");
|
|
1577
1621
|
}
|
|
1578
1622
|
|
|
1623
|
+
/**
|
|
1624
|
+
* A helper function to sweep all the funds from a given wallet in a single swap, after getting the quote you can
|
|
1625
|
+
* execute the swap by passing the returned `feeRate` and `utxos` to the {@link SpvFromBTCSwap.execute},
|
|
1626
|
+
* {@link SpvFromBTCSwap.getFundedPsbt} or {@link SpvFromBTCSwap.sendBitcoinTransaction} functions along
|
|
1627
|
+
* with `spendFully=true`.
|
|
1628
|
+
*
|
|
1629
|
+
* @example
|
|
1630
|
+
* Create the swap first using this function
|
|
1631
|
+
* ```ts
|
|
1632
|
+
* const {swap, utxos, btcFeeRate} = await swapper.sweepBitcoinWallet(wallet, Tokens.CITREA.CBTC, dstAddress);
|
|
1633
|
+
* ```
|
|
1634
|
+
* Then execute it using one of these execution paths - ensure that you supply the returned `utxos`, `btcFeeRate`
|
|
1635
|
+
* params and also set `spendFully` to `true`!
|
|
1636
|
+
*
|
|
1637
|
+
* a) Execute and pass the returned utxos and btcFeeRate:
|
|
1638
|
+
* ```ts
|
|
1639
|
+
* await swap.execute(wallet, undefined, {feeRate: btcFeeRate, utxos: utxos, spendFully: true});
|
|
1640
|
+
* ```
|
|
1641
|
+
*
|
|
1642
|
+
* b) Get funded PSBT to sign externally:
|
|
1643
|
+
* ```ts
|
|
1644
|
+
* const {psbt, psbtHex, psbtBase64, signInputs} = await swap.getFundedPsbt(wallet, btcFeeRate, undefined, utxos, true);
|
|
1645
|
+
* // Sign the psbt at the specified signInputs indices
|
|
1646
|
+
* const signedPsbt = ...;
|
|
1647
|
+
* // Then submit back to the SDK
|
|
1648
|
+
* await swap.submitPsbt(signedPsbt);
|
|
1649
|
+
* ```
|
|
1650
|
+
*
|
|
1651
|
+
* c) Only sign and send the signed PSBT with the provided wallet:
|
|
1652
|
+
* ```ts
|
|
1653
|
+
* await swap.sendBitcoinTransaction(wallet, btcFeeRate, utxos, true);
|
|
1654
|
+
* ```
|
|
1655
|
+
*/
|
|
1656
|
+
async sweepBitcoinWallet<C extends ChainIds<T>>(
|
|
1657
|
+
srcWallet: IBitcoinWallet | MinimalBitcoinWalletInterface,
|
|
1658
|
+
_dstToken: SCToken<C> | string,
|
|
1659
|
+
dstAddress: string,
|
|
1660
|
+
options?: SpvFromBTCOptions
|
|
1661
|
+
): Promise<{
|
|
1662
|
+
swap: SpvFromBTCSwap<T[C]>,
|
|
1663
|
+
utxos: BitcoinWalletUtxo[],
|
|
1664
|
+
btcFeeRate: number
|
|
1665
|
+
}> {
|
|
1666
|
+
const dstToken = typeof(_dstToken)==="string" ? this.getToken(_dstToken) as Token<C> : _dstToken;
|
|
1667
|
+
if(!isSCToken<C>(dstToken)) throw new Error("Destination token must be a smart chain token!");
|
|
1668
|
+
|
|
1669
|
+
const wallet = toBitcoinWallet(srcWallet, this._bitcoinRpc, this.bitcoinNetwork);
|
|
1670
|
+
if(wallet.getUtxoPool==null) throw new Error("Wallet needs to support the `getUtxoPool()` function!");
|
|
1671
|
+
|
|
1672
|
+
const walletUtxosPromise = wallet.getUtxoPool();
|
|
1673
|
+
const bitcoinFeeRatePromise = options?.bitcoinFeeRate ?? wallet.getFeeRate();
|
|
1674
|
+
|
|
1675
|
+
const swap = await this.createFromBTCSwapNew(
|
|
1676
|
+
dstToken.chainId as C, dstAddress, dstToken.address, null, false, undefined, {
|
|
1677
|
+
...options,
|
|
1678
|
+
sourceWalletUtxos: walletUtxosPromise,
|
|
1679
|
+
bitcoinFeeRate: bitcoinFeeRatePromise
|
|
1680
|
+
}
|
|
1681
|
+
);
|
|
1682
|
+
|
|
1683
|
+
return {
|
|
1684
|
+
swap,
|
|
1685
|
+
utxos: await walletUtxosPromise,
|
|
1686
|
+
btcFeeRate: Math.max(swap.minimumBtcFeeRate, await bitcoinFeeRatePromise)
|
|
1687
|
+
};
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1579
1690
|
/**
|
|
1580
1691
|
* Returns all swaps
|
|
1581
1692
|
*/
|
|
@@ -1601,14 +1712,14 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1601
1712
|
}
|
|
1602
1713
|
|
|
1603
1714
|
/**
|
|
1604
|
-
* Returns all swaps
|
|
1715
|
+
* Returns all swaps which are pending (i.e. not in their final state yet)
|
|
1605
1716
|
*/
|
|
1606
|
-
|
|
1717
|
+
getPendingSwaps(): Promise<ISwap[]>;
|
|
1607
1718
|
/**
|
|
1608
|
-
* Returns swaps
|
|
1719
|
+
* Returns swaps which are pending (i.e. not in their final state yet) for the specific chain, and optionally also for a specific signer's address
|
|
1609
1720
|
*/
|
|
1610
|
-
|
|
1611
|
-
async
|
|
1721
|
+
getPendingSwaps<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<ISwap<T[C]>[]>;
|
|
1722
|
+
async getPendingSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<ISwap[]> {
|
|
1612
1723
|
if(chainId==null) {
|
|
1613
1724
|
const res: ISwap[][] = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
1614
1725
|
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
@@ -1622,7 +1733,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1622
1733
|
}
|
|
1623
1734
|
return unifiedSwapStorage.query(queryParams, reviver);
|
|
1624
1735
|
}));
|
|
1625
|
-
return res.flat()
|
|
1736
|
+
return res.flat();
|
|
1626
1737
|
} else {
|
|
1627
1738
|
const {unifiedSwapStorage, reviver, wrappers} = this._chains[chainId];
|
|
1628
1739
|
const queryParams: Array<QueryParams[]> = [];
|
|
@@ -1633,7 +1744,23 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1633
1744
|
swapTypeQueryParams.push({key: "state", value: wrapper._pendingSwapStates});
|
|
1634
1745
|
queryParams.push(swapTypeQueryParams);
|
|
1635
1746
|
}
|
|
1636
|
-
return
|
|
1747
|
+
return await unifiedSwapStorage.query(queryParams, reviver);
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
/**
|
|
1752
|
+
* Returns all swaps where an action is required (either claim or refund)
|
|
1753
|
+
*/
|
|
1754
|
+
getActionableSwaps(): Promise<ISwap[]>;
|
|
1755
|
+
/**
|
|
1756
|
+
* Returns swaps where an action is required (either claim or refund) for the specific chain, and optionally also for a specific signer's address
|
|
1757
|
+
*/
|
|
1758
|
+
getActionableSwaps<C extends ChainIds<T>>(chainId: C, signer?: string): Promise<ISwap<T[C]>[]>;
|
|
1759
|
+
async getActionableSwaps<C extends ChainIds<T>>(chainId?: C, signer?: string): Promise<ISwap[]> {
|
|
1760
|
+
if(chainId==null) {
|
|
1761
|
+
return (await this.getPendingSwaps()).filter(swap => swap.requiresAction());
|
|
1762
|
+
} else {
|
|
1763
|
+
return (await this.getPendingSwaps(chainId, signer)).filter(swap => swap.requiresAction());
|
|
1637
1764
|
}
|
|
1638
1765
|
}
|
|
1639
1766
|
|
|
@@ -1831,8 +1958,8 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1831
1958
|
}
|
|
1832
1959
|
|
|
1833
1960
|
this.logger.debug("_syncSwaps(): Done syncing "+swaps.length+" swaps, saving "+changedSwaps.length+" changed swaps, removing "+removeSwaps.length+" swaps!");
|
|
1834
|
-
await unifiedSwapStorage.saveAll(changedSwaps);
|
|
1835
|
-
await unifiedSwapStorage.removeAll(removeSwaps);
|
|
1961
|
+
await unifiedSwapStorage.saveAll(changedSwaps, true);
|
|
1962
|
+
await unifiedSwapStorage.removeAll(removeSwaps, true);
|
|
1836
1963
|
|
|
1837
1964
|
changedSwaps.forEach(swap => swap._emitEvent());
|
|
1838
1965
|
removeSwaps.forEach(swap => swap._emitEvent());
|
|
@@ -1889,6 +2016,20 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
|
|
|
1889
2016
|
}
|
|
1890
2017
|
}
|
|
1891
2018
|
|
|
2019
|
+
/**
|
|
2020
|
+
* When the swapper is initiated with the `noEvents` config this function allows you to manually poll for on-chain
|
|
2021
|
+
* events. It returns an events cursor which you should save and pass to the next call to the `poll()` function.
|
|
2022
|
+
*
|
|
2023
|
+
* @param chainId Chain for which to poll the chain events listener for
|
|
2024
|
+
* @param lastEventCursorState Event cursor state returned from the last call to the `poll()` function
|
|
2025
|
+
*/
|
|
2026
|
+
async _pollChainEvents<C extends ChainIds<T>>(chainId: C, lastEventCursorState?: any): Promise<any> {
|
|
2027
|
+
const chain = this._chains[chainId];
|
|
2028
|
+
if(chain==null) throw new Error(`Invalid chain id ${chainId}!`);
|
|
2029
|
+
|
|
2030
|
+
return chain.unifiedChainEvents.poll(lastEventCursorState);
|
|
2031
|
+
}
|
|
2032
|
+
|
|
1892
2033
|
/**
|
|
1893
2034
|
* Recovers swaps from on-chain historical data.
|
|
1894
2035
|
*
|
|
@@ -337,6 +337,22 @@ export class SwapperUtils<T extends MultiChain> {
|
|
|
337
337
|
return this.parseSmartchainAddress(addressString);
|
|
338
338
|
}
|
|
339
339
|
|
|
340
|
+
/**
|
|
341
|
+
* Strips the URL encoding around `bitcoin:` and `lightning:` addresses, leaving just the raw address
|
|
342
|
+
*
|
|
343
|
+
* @param addressString Address to strip
|
|
344
|
+
*
|
|
345
|
+
* @returns Raw clean address
|
|
346
|
+
*/
|
|
347
|
+
stripAddress(addressString: string): string {
|
|
348
|
+
if(addressString.startsWith("lightning:") || addressString.startsWith("bitcoin:")) {
|
|
349
|
+
addressString = addressString.substring(addressString.indexOf(":")+1);
|
|
350
|
+
const delimeterIndex = addressString.indexOf("?");
|
|
351
|
+
if(delimeterIndex!==-1) addressString = addressString.substring(0, delimeterIndex);
|
|
352
|
+
}
|
|
353
|
+
return addressString;
|
|
354
|
+
}
|
|
355
|
+
|
|
340
356
|
/**
|
|
341
357
|
* Returns a random PSBT that can be used for fee estimation for SPV vault (UTXO-controlled vault) based swaps
|
|
342
358
|
* {@link SwapType.SPV_VAULT_FROM_BTC}, the last output (the LP output) is omitted to allow for coinselection
|
|
@@ -477,11 +493,18 @@ export class SwapperUtils<T extends MultiChain> {
|
|
|
477
493
|
}
|
|
478
494
|
|
|
479
495
|
/**
|
|
480
|
-
* Returns a random address for a given smart chain
|
|
496
|
+
* Returns a random address for a given smart chain or bitcoin
|
|
481
497
|
*
|
|
482
498
|
* @param chainIdentifier
|
|
483
499
|
*/
|
|
484
|
-
randomAddress<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier): string {
|
|
500
|
+
randomAddress<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier | "BITCOIN"): string {
|
|
501
|
+
if(chainIdentifier==="BITCOIN") {
|
|
502
|
+
// Return random p2wkh address
|
|
503
|
+
return Address(this.bitcoinNetwork).encode({
|
|
504
|
+
type: "wpkh",
|
|
505
|
+
hash: randomBytes(20)
|
|
506
|
+
});
|
|
507
|
+
}
|
|
485
508
|
if(this.root._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
486
509
|
return this.root._chains[chainIdentifier].chainInterface.randomAddress();
|
|
487
510
|
}
|
|
@@ -524,6 +547,23 @@ export class SwapperUtils<T extends MultiChain> {
|
|
|
524
547
|
return this.root._chains[chainIdentifier].chainInterface.sendSignedAndConfirm(txs, true, abortSignal, false, onBeforePublish);
|
|
525
548
|
}
|
|
526
549
|
|
|
550
|
+
/**
|
|
551
|
+
* Prepares a set of unsigned transactions for signing, by adding required nonces or recent blockhashes, might
|
|
552
|
+
* also add hints of account deployment on e.g. Starknet
|
|
553
|
+
*
|
|
554
|
+
* @param chainIdentifier A chain for which to prepare the txs
|
|
555
|
+
* @param txs Transactions to prepare
|
|
556
|
+
*/
|
|
557
|
+
prepareUnsignedTransactions<ChainIdentifier extends ChainIds<T>>(
|
|
558
|
+
chainIdentifier: ChainIdentifier,
|
|
559
|
+
txs: T[ChainIdentifier]["TX"][]
|
|
560
|
+
): Promise<T[ChainIdentifier]["TX"][]> {
|
|
561
|
+
if(this.root._chains[chainIdentifier]==null) throw new Error("Invalid chain identifier! Unknown chain: "+chainIdentifier);
|
|
562
|
+
const chainInterface = this.root._chains[chainIdentifier].chainInterface;
|
|
563
|
+
if(chainInterface.prepareTxs==null) throw new Error("Chain doesn't support tx preparation, chainId: "+chainIdentifier);
|
|
564
|
+
return chainInterface.prepareTxs(txs);
|
|
565
|
+
}
|
|
566
|
+
|
|
527
567
|
/**
|
|
528
568
|
* Serializes an unsigned smart chain transaction
|
|
529
569
|
*
|