@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
package/dist/swapper/Swapper.js
CHANGED
|
@@ -35,6 +35,9 @@ const LNURLPay_1 = require("../types/lnurl/LNURLPay");
|
|
|
35
35
|
const RetryUtils_1 = require("../utils/RetryUtils");
|
|
36
36
|
const IEscrowSwap_1 = require("../swaps/escrow_swaps/IEscrowSwap");
|
|
37
37
|
const LightningInvoiceCreateService_1 = require("../types/wallets/LightningInvoiceCreateService");
|
|
38
|
+
const IntermediaryAPI_1 = require("../intermediaries/apis/IntermediaryAPI");
|
|
39
|
+
const BitcoinWalletUtils_1 = require("../utils/BitcoinWalletUtils");
|
|
40
|
+
const SignedKeyBasedAuth_1 = require("../intermediaries/auth/SignedKeyBasedAuth");
|
|
38
41
|
/**
|
|
39
42
|
* Core orchestrator for all atomiq swap operations
|
|
40
43
|
*
|
|
@@ -87,6 +90,10 @@ class Swapper extends events_1.EventEmitter {
|
|
|
87
90
|
this._tokensByTicker[chainId] ??= {};
|
|
88
91
|
this._tokens[chainId][tokenData.address] = this._tokensByTicker[chainId][tokenData.ticker] = tokenData;
|
|
89
92
|
}
|
|
93
|
+
const lpApi = new IntermediaryAPI_1.IntermediaryAPI(this.options.signedKeyBasedAuth != null
|
|
94
|
+
? (0, SignedKeyBasedAuth_1.getSignedKeyBasedAuthHandler)(this.options.signedKeyBasedAuth.certificate, this.options.signedKeyBasedAuth.privateKey)
|
|
95
|
+
: undefined);
|
|
96
|
+
this.lpApi = lpApi;
|
|
90
97
|
this.swapStateListener = (swap) => {
|
|
91
98
|
this.emit("swapState", swap);
|
|
92
99
|
};
|
|
@@ -117,35 +124,35 @@ class Swapper extends events_1.EventEmitter {
|
|
|
117
124
|
const unifiedSwapStorage = new UnifiedSwapStorage_1.UnifiedSwapStorage(storageHandler, this.options.noSwapCache);
|
|
118
125
|
const unifiedChainEvents = new UnifiedSwapEventListener_1.UnifiedSwapEventListener(unifiedSwapStorage, chainEvents);
|
|
119
126
|
const wrappers = {};
|
|
120
|
-
wrappers[SwapType_1.SwapType.TO_BTCLN] = new ToBTCLNWrapper_1.ToBTCLNWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, {
|
|
127
|
+
wrappers[SwapType_1.SwapType.TO_BTCLN] = new ToBTCLNWrapper_1.ToBTCLNWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, lpApi, {
|
|
121
128
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
122
129
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
123
130
|
saveUninitializedSwaps: this.options.saveUninitializedSwaps,
|
|
124
131
|
});
|
|
125
|
-
wrappers[SwapType_1.SwapType.TO_BTC] = new ToBTCWrapper_1.ToBTCWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, this._bitcoinRpc, {
|
|
132
|
+
wrappers[SwapType_1.SwapType.TO_BTC] = new ToBTCWrapper_1.ToBTCWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, this._bitcoinRpc, lpApi, {
|
|
126
133
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
127
134
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
128
135
|
saveUninitializedSwaps: this.options.saveUninitializedSwaps,
|
|
129
136
|
bitcoinNetwork: this._btcNetwork
|
|
130
137
|
});
|
|
131
|
-
wrappers[SwapType_1.SwapType.FROM_BTCLN] = new FromBTCLNWrapper_1.FromBTCLNWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, lightningApi, {
|
|
138
|
+
wrappers[SwapType_1.SwapType.FROM_BTCLN] = new FromBTCLNWrapper_1.FromBTCLNWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, lightningApi, lpApi, {
|
|
132
139
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
133
140
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
134
141
|
saveUninitializedSwaps: this.options.saveUninitializedSwaps,
|
|
135
142
|
unsafeSkipLnNodeCheck: this.bitcoinNetwork === base_1.BitcoinNetwork.TESTNET4 || this.bitcoinNetwork === base_1.BitcoinNetwork.REGTEST
|
|
136
143
|
});
|
|
137
|
-
wrappers[SwapType_1.SwapType.FROM_BTC] = new FromBTCWrapper_1.FromBTCWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, versionedContracts, this._bitcoinRpc, {
|
|
144
|
+
wrappers[SwapType_1.SwapType.FROM_BTC] = new FromBTCWrapper_1.FromBTCWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, versionedContracts, this._bitcoinRpc, lpApi, {
|
|
138
145
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
139
146
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
140
147
|
saveUninitializedSwaps: this.options.saveUninitializedSwaps,
|
|
141
148
|
bitcoinNetwork: this._btcNetwork
|
|
142
149
|
});
|
|
143
|
-
wrappers[SwapType_1.SwapType.TRUSTED_FROM_BTCLN] = new LnForGasWrapper_1.LnForGasWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], {
|
|
150
|
+
wrappers[SwapType_1.SwapType.TRUSTED_FROM_BTCLN] = new LnForGasWrapper_1.LnForGasWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], lpApi, {
|
|
144
151
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
145
152
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
146
153
|
saveUninitializedSwaps: this.options.saveUninitializedSwaps,
|
|
147
154
|
});
|
|
148
|
-
wrappers[SwapType_1.SwapType.TRUSTED_FROM_BTC] = new OnchainForGasWrapper_1.OnchainForGasWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], bitcoinRpc, {
|
|
155
|
+
wrappers[SwapType_1.SwapType.TRUSTED_FROM_BTC] = new OnchainForGasWrapper_1.OnchainForGasWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], bitcoinRpc, lpApi, {
|
|
149
156
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
150
157
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
151
158
|
saveUninitializedSwaps: this.options.saveUninitializedSwaps,
|
|
@@ -153,7 +160,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
153
160
|
});
|
|
154
161
|
// This is gated on the default version of the contracts
|
|
155
162
|
if (spvVaultContract != null) {
|
|
156
|
-
wrappers[SwapType_1.SwapType.SPV_VAULT_FROM_BTC] = new SpvFromBTCWrapper_1.SpvFromBTCWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, versionedContracts, bitcoinRpc, {
|
|
163
|
+
wrappers[SwapType_1.SwapType.SPV_VAULT_FROM_BTC] = new SpvFromBTCWrapper_1.SpvFromBTCWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, versionedContracts, bitcoinRpc, lpApi, {
|
|
157
164
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
158
165
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
159
166
|
saveUninitializedSwaps: this.options.saveUninitializedSwaps,
|
|
@@ -162,7 +169,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
162
169
|
}
|
|
163
170
|
// This is gated on the default version of the contracts
|
|
164
171
|
if (swapContract.supportsInitWithoutClaimer) {
|
|
165
|
-
wrappers[SwapType_1.SwapType.FROM_BTCLN_AUTO] = new FromBTCLNAutoWrapper_1.FromBTCLNAutoWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, lightningApi, this.messenger, {
|
|
172
|
+
wrappers[SwapType_1.SwapType.FROM_BTCLN_AUTO] = new FromBTCLNAutoWrapper_1.FromBTCLNAutoWrapper(key, unifiedSwapStorage, unifiedChainEvents, chainInterface, pricing, this._tokens[chainId], versions, lightningApi, this.messenger, lpApi, {
|
|
166
173
|
getRequestTimeout: this.options.getRequestTimeout,
|
|
167
174
|
postRequestTimeout: this.options.postRequestTimeout,
|
|
168
175
|
saveUninitializedSwaps: this.options.saveUninitializedSwaps,
|
|
@@ -189,10 +196,10 @@ class Swapper extends events_1.EventEmitter {
|
|
|
189
196
|
});
|
|
190
197
|
const contracts = (0, Utils_1.objectMap)(chainsData, (data) => data.versions ?? { [data.defaultVersion ?? "v1"]: { swapContract: data.swapContract, spvVaultContract: data.spvVaultContract } });
|
|
191
198
|
if (options.intermediaryUrl != null) {
|
|
192
|
-
this.intermediaryDiscovery = new IntermediaryDiscovery_1.IntermediaryDiscovery(contracts, options.registryUrl, Array.isArray(options.intermediaryUrl) ? options.intermediaryUrl : [options.intermediaryUrl], options.getRequestTimeout);
|
|
199
|
+
this.intermediaryDiscovery = new IntermediaryDiscovery_1.IntermediaryDiscovery(contracts, lpApi, options.registryUrl, Array.isArray(options.intermediaryUrl) ? options.intermediaryUrl : [options.intermediaryUrl], options.getRequestTimeout);
|
|
193
200
|
}
|
|
194
201
|
else {
|
|
195
|
-
this.intermediaryDiscovery = new IntermediaryDiscovery_1.IntermediaryDiscovery(contracts, options.registryUrl, undefined, options.getRequestTimeout);
|
|
202
|
+
this.intermediaryDiscovery = new IntermediaryDiscovery_1.IntermediaryDiscovery(contracts, lpApi, options.registryUrl, undefined, options.getRequestTimeout);
|
|
196
203
|
}
|
|
197
204
|
this.intermediaryDiscovery.on("removed", (intermediaries) => {
|
|
198
205
|
this.emit("lpsRemoved", intermediaries);
|
|
@@ -202,7 +209,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
202
209
|
});
|
|
203
210
|
}
|
|
204
211
|
async _init() {
|
|
205
|
-
this.logger.debug("init(): Initializing swapper
|
|
212
|
+
this.logger.debug("init(): Initializing swapper");
|
|
206
213
|
const abortController = new AbortController();
|
|
207
214
|
const promises = [];
|
|
208
215
|
let automaticClockDriftCorrectionPromise = undefined;
|
|
@@ -270,8 +277,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
270
277
|
return swap;
|
|
271
278
|
});
|
|
272
279
|
}
|
|
273
|
-
|
|
274
|
-
await unifiedChainEvents.start();
|
|
280
|
+
await unifiedChainEvents.start(this.options.noEvents);
|
|
275
281
|
this.logger.debug("init(): Intialized events: " + chainIdentifier);
|
|
276
282
|
for (let key in wrappers) {
|
|
277
283
|
// this.logger.debug("init(): Initializing "+SwapType[key]+": "+chainIdentifier);
|
|
@@ -306,6 +312,12 @@ class Swapper extends events_1.EventEmitter {
|
|
|
306
312
|
throw e;
|
|
307
313
|
}
|
|
308
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Whether the SDK is initialized (after {@link init} is called)
|
|
317
|
+
*/
|
|
318
|
+
isInitialized() {
|
|
319
|
+
return this.initialized;
|
|
320
|
+
}
|
|
309
321
|
/**
|
|
310
322
|
* Stops listening for onchain events and closes this Swapper instance
|
|
311
323
|
*/
|
|
@@ -343,7 +355,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
343
355
|
throw new Error("Invalid chain identifier! Unknown chain: " + chainIdentifier);
|
|
344
356
|
let candidates;
|
|
345
357
|
const inBtc = swapType === SwapType_1.SwapType.TO_BTCLN || swapType === SwapType_1.SwapType.TO_BTC ? !amountData.exactIn : amountData.exactIn;
|
|
346
|
-
if (!inBtc) {
|
|
358
|
+
if (!inBtc || amountData.amount == null) {
|
|
347
359
|
//Get candidates not based on the amount
|
|
348
360
|
candidates = this.intermediaryDiscovery.getSwapCandidates(chainIdentifier, swapType, amountData.token);
|
|
349
361
|
}
|
|
@@ -355,7 +367,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
355
367
|
this.logger.warn("createSwap(): No valid intermediary found to execute the swap with, reloading intermediary database...");
|
|
356
368
|
await this.intermediaryDiscovery.reloadIntermediaries();
|
|
357
369
|
swapLimitsChanged = true;
|
|
358
|
-
if (!inBtc) {
|
|
370
|
+
if (!inBtc || amountData.amount == null) {
|
|
359
371
|
//Get candidates not based on the amount
|
|
360
372
|
candidates = this.intermediaryDiscovery.getSwapCandidates(chainIdentifier, swapType, amountData.token);
|
|
361
373
|
}
|
|
@@ -441,10 +453,12 @@ class Swapper extends events_1.EventEmitter {
|
|
|
441
453
|
}
|
|
442
454
|
if (min != null && max != null) {
|
|
443
455
|
let msg = "Swap amount too high or too low! Try swapping a different amount.";
|
|
444
|
-
if (
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
456
|
+
if (amountData.amount != null) {
|
|
457
|
+
if (min > amountData.amount)
|
|
458
|
+
msg = "Swap amount too low! Try swapping a higher amount.";
|
|
459
|
+
if (max < amountData.amount)
|
|
460
|
+
msg = "Swap amount too high! Try swapping a lower amount.";
|
|
461
|
+
}
|
|
448
462
|
reject(new RequestError_1.OutOfBoundsError(msg, 400, min, max));
|
|
449
463
|
return;
|
|
450
464
|
}
|
|
@@ -612,7 +626,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
612
626
|
throw new Error("Invalid " + chainIdentifier + " address");
|
|
613
627
|
recipient = this._chains[chainIdentifier].chainInterface.normalizeAddress(recipient);
|
|
614
628
|
const amountData = {
|
|
615
|
-
amount,
|
|
629
|
+
amount: amount ?? undefined,
|
|
616
630
|
token: tokenAddress,
|
|
617
631
|
exactIn: !exactOut
|
|
618
632
|
};
|
|
@@ -831,6 +845,10 @@ class Swapper extends events_1.EventEmitter {
|
|
|
831
845
|
* @param options Options for the swap
|
|
832
846
|
*/
|
|
833
847
|
swap(_srcToken, _dstToken, _amount, exactIn, src, dst, options) {
|
|
848
|
+
if (typeof (src) === "string")
|
|
849
|
+
src = this.Utils.stripAddress(src);
|
|
850
|
+
if (typeof (dst) === "string")
|
|
851
|
+
dst = this.Utils.stripAddress(dst);
|
|
834
852
|
const srcToken = typeof (_srcToken) === "string" ? this.getToken(_srcToken) : _srcToken;
|
|
835
853
|
const dstToken = typeof (_dstToken) === "string" ? this.getToken(_dstToken) : _dstToken;
|
|
836
854
|
const amount = _amount == null ? null : (typeof (_amount) === "bigint" ? _amount : (0, Utils_1.fromDecimal)(_amount, exactIn ? srcToken.decimals : dstToken.decimals));
|
|
@@ -907,6 +925,59 @@ class Swapper extends events_1.EventEmitter {
|
|
|
907
925
|
}
|
|
908
926
|
throw new Error("Unsupported swap type");
|
|
909
927
|
}
|
|
928
|
+
/**
|
|
929
|
+
* A helper function to sweep all the funds from a given wallet in a single swap, after getting the quote you can
|
|
930
|
+
* execute the swap by passing the returned `feeRate` and `utxos` to the {@link SpvFromBTCSwap.execute},
|
|
931
|
+
* {@link SpvFromBTCSwap.getFundedPsbt} or {@link SpvFromBTCSwap.sendBitcoinTransaction} functions along
|
|
932
|
+
* with `spendFully=true`.
|
|
933
|
+
*
|
|
934
|
+
* @example
|
|
935
|
+
* Create the swap first using this function
|
|
936
|
+
* ```ts
|
|
937
|
+
* const {swap, utxos, btcFeeRate} = await swapper.sweepBitcoinWallet(wallet, Tokens.CITREA.CBTC, dstAddress);
|
|
938
|
+
* ```
|
|
939
|
+
* Then execute it using one of these execution paths - ensure that you supply the returned `utxos`, `btcFeeRate`
|
|
940
|
+
* params and also set `spendFully` to `true`!
|
|
941
|
+
*
|
|
942
|
+
* a) Execute and pass the returned utxos and btcFeeRate:
|
|
943
|
+
* ```ts
|
|
944
|
+
* await swap.execute(wallet, undefined, {feeRate: btcFeeRate, utxos: utxos, spendFully: true});
|
|
945
|
+
* ```
|
|
946
|
+
*
|
|
947
|
+
* b) Get funded PSBT to sign externally:
|
|
948
|
+
* ```ts
|
|
949
|
+
* const {psbt, psbtHex, psbtBase64, signInputs} = await swap.getFundedPsbt(wallet, btcFeeRate, undefined, utxos, true);
|
|
950
|
+
* // Sign the psbt at the specified signInputs indices
|
|
951
|
+
* const signedPsbt = ...;
|
|
952
|
+
* // Then submit back to the SDK
|
|
953
|
+
* await swap.submitPsbt(signedPsbt);
|
|
954
|
+
* ```
|
|
955
|
+
*
|
|
956
|
+
* c) Only sign and send the signed PSBT with the provided wallet:
|
|
957
|
+
* ```ts
|
|
958
|
+
* await swap.sendBitcoinTransaction(wallet, btcFeeRate, utxos, true);
|
|
959
|
+
* ```
|
|
960
|
+
*/
|
|
961
|
+
async sweepBitcoinWallet(srcWallet, _dstToken, dstAddress, options) {
|
|
962
|
+
const dstToken = typeof (_dstToken) === "string" ? this.getToken(_dstToken) : _dstToken;
|
|
963
|
+
if (!(0, Token_1.isSCToken)(dstToken))
|
|
964
|
+
throw new Error("Destination token must be a smart chain token!");
|
|
965
|
+
const wallet = (0, BitcoinWalletUtils_1.toBitcoinWallet)(srcWallet, this._bitcoinRpc, this.bitcoinNetwork);
|
|
966
|
+
if (wallet.getUtxoPool == null)
|
|
967
|
+
throw new Error("Wallet needs to support the `getUtxoPool()` function!");
|
|
968
|
+
const walletUtxosPromise = wallet.getUtxoPool();
|
|
969
|
+
const bitcoinFeeRatePromise = options?.bitcoinFeeRate ?? wallet.getFeeRate();
|
|
970
|
+
const swap = await this.createFromBTCSwapNew(dstToken.chainId, dstAddress, dstToken.address, null, false, undefined, {
|
|
971
|
+
...options,
|
|
972
|
+
sourceWalletUtxos: walletUtxosPromise,
|
|
973
|
+
bitcoinFeeRate: bitcoinFeeRatePromise
|
|
974
|
+
});
|
|
975
|
+
return {
|
|
976
|
+
swap,
|
|
977
|
+
utxos: await walletUtxosPromise,
|
|
978
|
+
btcFeeRate: Math.max(swap.minimumBtcFeeRate, await bitcoinFeeRatePromise)
|
|
979
|
+
};
|
|
980
|
+
}
|
|
910
981
|
async getAllSwaps(chainId, signer) {
|
|
911
982
|
const queryParams = [];
|
|
912
983
|
if (signer != null)
|
|
@@ -923,7 +994,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
923
994
|
return await unifiedSwapStorage.query([queryParams], reviver);
|
|
924
995
|
}
|
|
925
996
|
}
|
|
926
|
-
async
|
|
997
|
+
async getPendingSwaps(chainId, signer) {
|
|
927
998
|
if (chainId == null) {
|
|
928
999
|
const res = await Promise.all(Object.keys(this._chains).map((chainId) => {
|
|
929
1000
|
const { unifiedSwapStorage, reviver, wrappers } = this._chains[chainId];
|
|
@@ -938,7 +1009,7 @@ class Swapper extends events_1.EventEmitter {
|
|
|
938
1009
|
}
|
|
939
1010
|
return unifiedSwapStorage.query(queryParams, reviver);
|
|
940
1011
|
}));
|
|
941
|
-
return res.flat()
|
|
1012
|
+
return res.flat();
|
|
942
1013
|
}
|
|
943
1014
|
else {
|
|
944
1015
|
const { unifiedSwapStorage, reviver, wrappers } = this._chains[chainId];
|
|
@@ -951,7 +1022,15 @@ class Swapper extends events_1.EventEmitter {
|
|
|
951
1022
|
swapTypeQueryParams.push({ key: "state", value: wrapper._pendingSwapStates });
|
|
952
1023
|
queryParams.push(swapTypeQueryParams);
|
|
953
1024
|
}
|
|
954
|
-
return
|
|
1025
|
+
return await unifiedSwapStorage.query(queryParams, reviver);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
async getActionableSwaps(chainId, signer) {
|
|
1029
|
+
if (chainId == null) {
|
|
1030
|
+
return (await this.getPendingSwaps()).filter(swap => swap.requiresAction());
|
|
1031
|
+
}
|
|
1032
|
+
else {
|
|
1033
|
+
return (await this.getPendingSwaps(chainId, signer)).filter(swap => swap.requiresAction());
|
|
955
1034
|
}
|
|
956
1035
|
}
|
|
957
1036
|
async getRefundableSwaps(chainId, signer) {
|
|
@@ -1133,8 +1212,8 @@ class Swapper extends events_1.EventEmitter {
|
|
|
1133
1212
|
removeSwaps.push(...result.removeSwaps);
|
|
1134
1213
|
}
|
|
1135
1214
|
this.logger.debug("_syncSwaps(): Done syncing " + swaps.length + " swaps, saving " + changedSwaps.length + " changed swaps, removing " + removeSwaps.length + " swaps!");
|
|
1136
|
-
await unifiedSwapStorage.saveAll(changedSwaps);
|
|
1137
|
-
await unifiedSwapStorage.removeAll(removeSwaps);
|
|
1215
|
+
await unifiedSwapStorage.saveAll(changedSwaps, true);
|
|
1216
|
+
await unifiedSwapStorage.removeAll(removeSwaps, true);
|
|
1138
1217
|
changedSwaps.forEach(swap => swap._emitEvent());
|
|
1139
1218
|
removeSwaps.forEach(swap => swap._emitEvent());
|
|
1140
1219
|
}
|
|
@@ -1188,6 +1267,19 @@ class Swapper extends events_1.EventEmitter {
|
|
|
1188
1267
|
await this.syncSwapsForChain(chainId, signer);
|
|
1189
1268
|
}
|
|
1190
1269
|
}
|
|
1270
|
+
/**
|
|
1271
|
+
* When the swapper is initiated with the `noEvents` config this function allows you to manually poll for on-chain
|
|
1272
|
+
* events. It returns an events cursor which you should save and pass to the next call to the `poll()` function.
|
|
1273
|
+
*
|
|
1274
|
+
* @param chainId Chain for which to poll the chain events listener for
|
|
1275
|
+
* @param lastEventCursorState Event cursor state returned from the last call to the `poll()` function
|
|
1276
|
+
*/
|
|
1277
|
+
async _pollChainEvents(chainId, lastEventCursorState) {
|
|
1278
|
+
const chain = this._chains[chainId];
|
|
1279
|
+
if (chain == null)
|
|
1280
|
+
throw new Error(`Invalid chain id ${chainId}!`);
|
|
1281
|
+
return chain.unifiedChainEvents.poll(lastEventCursorState);
|
|
1282
|
+
}
|
|
1191
1283
|
/**
|
|
1192
1284
|
* Recovers swaps from on-chain historical data.
|
|
1193
1285
|
*
|
|
@@ -100,6 +100,14 @@ export declare class SwapperUtils<T extends MultiChain> {
|
|
|
100
100
|
max?: TokenAmount;
|
|
101
101
|
amount?: TokenAmount;
|
|
102
102
|
} | null;
|
|
103
|
+
/**
|
|
104
|
+
* Strips the URL encoding around `bitcoin:` and `lightning:` addresses, leaving just the raw address
|
|
105
|
+
*
|
|
106
|
+
* @param addressString Address to strip
|
|
107
|
+
*
|
|
108
|
+
* @returns Raw clean address
|
|
109
|
+
*/
|
|
110
|
+
stripAddress(addressString: string): string;
|
|
103
111
|
/**
|
|
104
112
|
* Returns a random PSBT that can be used for fee estimation for SPV vault (UTXO-controlled vault) based swaps
|
|
105
113
|
* {@link SwapType.SPV_VAULT_FROM_BTC}, the last output (the LP output) is omitted to allow for coinselection
|
|
@@ -151,11 +159,11 @@ export declare class SwapperUtils<T extends MultiChain> {
|
|
|
151
159
|
*/
|
|
152
160
|
randomSigner<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier): T[ChainIdentifier]["Signer"];
|
|
153
161
|
/**
|
|
154
|
-
* Returns a random address for a given smart chain
|
|
162
|
+
* Returns a random address for a given smart chain or bitcoin
|
|
155
163
|
*
|
|
156
164
|
* @param chainIdentifier
|
|
157
165
|
*/
|
|
158
|
-
randomAddress<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier): string;
|
|
166
|
+
randomAddress<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier | "BITCOIN"): string;
|
|
159
167
|
/**
|
|
160
168
|
* Signs and broadcasts the supplied smart chain transaction
|
|
161
169
|
*
|
|
@@ -175,6 +183,14 @@ export declare class SwapperUtils<T extends MultiChain> {
|
|
|
175
183
|
* @param onBeforePublish Callback invoked before a transaction is sent (invoked for every transaction to be sent)
|
|
176
184
|
*/
|
|
177
185
|
sendSignedAndConfirm<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier, txs: T[ChainIdentifier]["SignedTXType"][], abortSignal?: AbortSignal, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]>;
|
|
186
|
+
/**
|
|
187
|
+
* Prepares a set of unsigned transactions for signing, by adding required nonces or recent blockhashes, might
|
|
188
|
+
* also add hints of account deployment on e.g. Starknet
|
|
189
|
+
*
|
|
190
|
+
* @param chainIdentifier A chain for which to prepare the txs
|
|
191
|
+
* @param txs Transactions to prepare
|
|
192
|
+
*/
|
|
193
|
+
prepareUnsignedTransactions<ChainIdentifier extends ChainIds<T>>(chainIdentifier: ChainIdentifier, txs: T[ChainIdentifier]["TX"][]): Promise<T[ChainIdentifier]["TX"][]>;
|
|
178
194
|
/**
|
|
179
195
|
* Serializes an unsigned smart chain transaction
|
|
180
196
|
*
|
|
@@ -284,6 +284,22 @@ class SwapperUtils {
|
|
|
284
284
|
return resultLightningInvoice;
|
|
285
285
|
return this.parseSmartchainAddress(addressString);
|
|
286
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Strips the URL encoding around `bitcoin:` and `lightning:` addresses, leaving just the raw address
|
|
289
|
+
*
|
|
290
|
+
* @param addressString Address to strip
|
|
291
|
+
*
|
|
292
|
+
* @returns Raw clean address
|
|
293
|
+
*/
|
|
294
|
+
stripAddress(addressString) {
|
|
295
|
+
if (addressString.startsWith("lightning:") || addressString.startsWith("bitcoin:")) {
|
|
296
|
+
addressString = addressString.substring(addressString.indexOf(":") + 1);
|
|
297
|
+
const delimeterIndex = addressString.indexOf("?");
|
|
298
|
+
if (delimeterIndex !== -1)
|
|
299
|
+
addressString = addressString.substring(0, delimeterIndex);
|
|
300
|
+
}
|
|
301
|
+
return addressString;
|
|
302
|
+
}
|
|
287
303
|
/**
|
|
288
304
|
* Returns a random PSBT that can be used for fee estimation for SPV vault (UTXO-controlled vault) based swaps
|
|
289
305
|
* {@link SwapType.SPV_VAULT_FROM_BTC}, the last output (the LP output) is omitted to allow for coinselection
|
|
@@ -397,11 +413,18 @@ class SwapperUtils {
|
|
|
397
413
|
return this.root._chains[chainIdentifier].chainInterface.randomSigner();
|
|
398
414
|
}
|
|
399
415
|
/**
|
|
400
|
-
* Returns a random address for a given smart chain
|
|
416
|
+
* Returns a random address for a given smart chain or bitcoin
|
|
401
417
|
*
|
|
402
418
|
* @param chainIdentifier
|
|
403
419
|
*/
|
|
404
420
|
randomAddress(chainIdentifier) {
|
|
421
|
+
if (chainIdentifier === "BITCOIN") {
|
|
422
|
+
// Return random p2wkh address
|
|
423
|
+
return (0, btc_signer_1.Address)(this.bitcoinNetwork).encode({
|
|
424
|
+
type: "wpkh",
|
|
425
|
+
hash: (0, Utils_1.randomBytes)(20)
|
|
426
|
+
});
|
|
427
|
+
}
|
|
405
428
|
if (this.root._chains[chainIdentifier] == null)
|
|
406
429
|
throw new Error("Invalid chain identifier! Unknown chain: " + chainIdentifier);
|
|
407
430
|
return this.root._chains[chainIdentifier].chainInterface.randomAddress();
|
|
@@ -433,6 +456,21 @@ class SwapperUtils {
|
|
|
433
456
|
throw new Error("Invalid chain identifier! Unknown chain: " + chainIdentifier);
|
|
434
457
|
return this.root._chains[chainIdentifier].chainInterface.sendSignedAndConfirm(txs, true, abortSignal, false, onBeforePublish);
|
|
435
458
|
}
|
|
459
|
+
/**
|
|
460
|
+
* Prepares a set of unsigned transactions for signing, by adding required nonces or recent blockhashes, might
|
|
461
|
+
* also add hints of account deployment on e.g. Starknet
|
|
462
|
+
*
|
|
463
|
+
* @param chainIdentifier A chain for which to prepare the txs
|
|
464
|
+
* @param txs Transactions to prepare
|
|
465
|
+
*/
|
|
466
|
+
prepareUnsignedTransactions(chainIdentifier, txs) {
|
|
467
|
+
if (this.root._chains[chainIdentifier] == null)
|
|
468
|
+
throw new Error("Invalid chain identifier! Unknown chain: " + chainIdentifier);
|
|
469
|
+
const chainInterface = this.root._chains[chainIdentifier].chainInterface;
|
|
470
|
+
if (chainInterface.prepareTxs == null)
|
|
471
|
+
throw new Error("Chain doesn't support tx preparation, chainId: " + chainIdentifier);
|
|
472
|
+
return chainInterface.prepareTxs(txs);
|
|
473
|
+
}
|
|
436
474
|
/**
|
|
437
475
|
* Serializes an unsigned smart chain transaction
|
|
438
476
|
*
|
package/dist/swaps/ISwap.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { SwapExecutionAction } from "../types/SwapExecutionAction";
|
|
|
13
13
|
import { LoggerType } from "../utils/Logger";
|
|
14
14
|
import { PriceInfoType } from "../types/PriceInfoType";
|
|
15
15
|
import { SwapStateInfo } from "../types/SwapStateInfo";
|
|
16
|
+
import { SwapExecutionStep } from "../types/SwapExecutionStep";
|
|
16
17
|
/**
|
|
17
18
|
* Initialization data for creating a swap
|
|
18
19
|
*
|
|
@@ -124,6 +125,12 @@ export declare abstract class ISwap<T extends ChainType = ChainType, D extends S
|
|
|
124
125
|
* @internal
|
|
125
126
|
*/
|
|
126
127
|
_contractVersion?: string;
|
|
128
|
+
/**
|
|
129
|
+
* Storage specific metadata that can be used for e.g. optimistic concurrency
|
|
130
|
+
*
|
|
131
|
+
* @internal
|
|
132
|
+
*/
|
|
133
|
+
_meta?: any;
|
|
127
134
|
/**
|
|
128
135
|
* Event emitter emitting `"swapState"` event when swap's state changes
|
|
129
136
|
*/
|
|
@@ -163,12 +170,6 @@ export declare abstract class ISwap<T extends ChainType = ChainType, D extends S
|
|
|
163
170
|
* @internal
|
|
164
171
|
*/
|
|
165
172
|
protected waitTillState(targetState: S, type?: "eq" | "gte" | "neq", abortSignal?: AbortSignal): Promise<void>;
|
|
166
|
-
/**
|
|
167
|
-
* Returns a list of steps or transactions required to finish and settle the swap
|
|
168
|
-
*
|
|
169
|
-
* @param options Additional options for executing the swap
|
|
170
|
-
*/
|
|
171
|
-
abstract txsExecute(options?: any): Promise<SwapExecutionAction<T>[]>;
|
|
172
173
|
/**
|
|
173
174
|
* Executes the swap with the provided wallet, the exact arguments for this functions differ for various swap
|
|
174
175
|
* types. Check the `execute()` function signature in the respective swap class to see the required arguments.
|
|
@@ -185,6 +186,12 @@ export declare abstract class ISwap<T extends ChainType = ChainType, D extends S
|
|
|
185
186
|
* @internal
|
|
186
187
|
*/
|
|
187
188
|
protected tryRecomputeSwapPrice(): void;
|
|
189
|
+
/**
|
|
190
|
+
* Returns the specific state along with the human-readable description of that state
|
|
191
|
+
*
|
|
192
|
+
* @internal
|
|
193
|
+
*/
|
|
194
|
+
protected _getStateInfo(state: S): SwapStateInfo<S>;
|
|
188
195
|
/**
|
|
189
196
|
* Re-fetches & revalidates the price data based on the current market prices
|
|
190
197
|
*/
|
|
@@ -209,6 +216,13 @@ export declare abstract class ISwap<T extends ChainType = ChainType, D extends S
|
|
|
209
216
|
* @internal
|
|
210
217
|
*/
|
|
211
218
|
protected checkSigner(signer: T["Signer"] | string): void;
|
|
219
|
+
/**
|
|
220
|
+
* Await and prepares a list of passed transactions
|
|
221
|
+
*
|
|
222
|
+
* @param txsPromise
|
|
223
|
+
* @internal
|
|
224
|
+
*/
|
|
225
|
+
protected prepareTransactions(txsPromise: Promise<T["TX"][]>): Promise<T["TX"][]>;
|
|
212
226
|
/**
|
|
213
227
|
* Returns an escrow hash of the swap
|
|
214
228
|
*
|
|
@@ -312,10 +326,57 @@ export declare abstract class ISwap<T extends ChainType = ChainType, D extends S
|
|
|
312
326
|
*/
|
|
313
327
|
getStateInfo(): SwapStateInfo<S>;
|
|
314
328
|
/**
|
|
315
|
-
* Returns a state-dependent
|
|
316
|
-
*
|
|
329
|
+
* Returns a current state-dependent action for the user to execute, or `undefined` if there is no more action
|
|
330
|
+
* required for this swap - this means that the swap is probably finished (either expired, failed or settled).
|
|
331
|
+
*
|
|
332
|
+
* @param options Optional options argument for the additional action context (i.e. passing bitcoin wallet info to
|
|
333
|
+
* get funded PSBTs or passing the externally-generated swap secret), see the actual type in the respective swap
|
|
334
|
+
* classes
|
|
335
|
+
*/
|
|
336
|
+
abstract getExecutionAction(options?: any): Promise<SwapExecutionAction | undefined>;
|
|
337
|
+
/**
|
|
338
|
+
* Returns a list of execution steps the user has to go through for a given swap, to see the possible execution
|
|
339
|
+
* steps check out {@link SwapExecutionStep}.
|
|
340
|
+
*
|
|
341
|
+
* @param options Optional options argument for the additional steps context (i.e. automatic settlement timeout),
|
|
342
|
+
* see the actual type in the respective swap classes
|
|
343
|
+
*/
|
|
344
|
+
abstract getExecutionSteps(options?: any): Promise<SwapExecutionStep[]>;
|
|
345
|
+
/**
|
|
346
|
+
* Returns the current action and the full execution steps for a given swap. Prefer this to calling
|
|
347
|
+
* {@link getExecutionSteps} and {@link getExecutionAction} separately - if called sequentially they might
|
|
348
|
+
* return the respective steps/actions in different states if you hit the state transition boundary.
|
|
349
|
+
*
|
|
350
|
+
* @param options Optional options argument for the additional execution status context, see the actual type in
|
|
351
|
+
* the respective swap classes
|
|
352
|
+
*/
|
|
353
|
+
abstract getExecutionStatus(options?: {
|
|
354
|
+
skipBuildingAction?: boolean;
|
|
355
|
+
} & any): Promise<{
|
|
356
|
+
steps: SwapExecutionStep[];
|
|
357
|
+
currentAction: SwapExecutionAction | undefined;
|
|
358
|
+
stateInfo: SwapStateInfo<S>;
|
|
359
|
+
}>;
|
|
360
|
+
/**
|
|
361
|
+
* Submits signed transactions obtained from the execution action back to the swap.
|
|
362
|
+
*
|
|
363
|
+
* @remarks This endpoint will also wait till the submitted transactions are confirmed (on a smart-chain side)
|
|
364
|
+
* and till the swap state change is observed from the authoritative chain/intermediary state.
|
|
365
|
+
*
|
|
366
|
+
* If invalid transactions are submitted, i.e. sending a simple noop or transfer transaction instead of the
|
|
367
|
+
* expected tx, this call may wait indefinitely unless aborted via the AbortSignal.
|
|
368
|
+
*
|
|
369
|
+
* @param txs Signed transactions
|
|
370
|
+
* @param abortSignal Abort signal
|
|
371
|
+
* @param requiredStates Optional list of states that the swap has to be in for the transactions to be
|
|
372
|
+
* submitted, else throws
|
|
373
|
+
* @param idempotent Whether the tx submission should be handled idempotently, meaning if any of the supplied
|
|
374
|
+
* transactions are already processed as e.g. init, claim, refund or execution transactions the function just
|
|
375
|
+
* returns these transaction IDs without actually submitting them
|
|
376
|
+
*
|
|
377
|
+
* @internal
|
|
317
378
|
*/
|
|
318
|
-
abstract
|
|
379
|
+
abstract _submitExecutionTransactions(txs: (T["SignedTXType"] | string | any)[], abortSignal?: AbortSignal, requiredStates?: S[], idempotent?: boolean): Promise<string[]>;
|
|
319
380
|
/**
|
|
320
381
|
* Returns output amount of the swap, user receives this much
|
|
321
382
|
*/
|
package/dist/swaps/ISwap.js
CHANGED
|
@@ -99,6 +99,7 @@ class ISwap {
|
|
|
99
99
|
this.createdAt = swapInitOrObj.createdAt ?? swapInitOrObj.expiry;
|
|
100
100
|
this._randomNonce = swapInitOrObj.randomNonce;
|
|
101
101
|
this._contractVersion = swapInitOrObj.contractVersion;
|
|
102
|
+
this._meta = swapInitOrObj._meta;
|
|
102
103
|
}
|
|
103
104
|
if (this.version !== this.currentVersion) {
|
|
104
105
|
this.upgradeVersion();
|
|
@@ -159,6 +160,18 @@ class ISwap {
|
|
|
159
160
|
}
|
|
160
161
|
}
|
|
161
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* Returns the specific state along with the human-readable description of that state
|
|
165
|
+
*
|
|
166
|
+
* @internal
|
|
167
|
+
*/
|
|
168
|
+
_getStateInfo(state) {
|
|
169
|
+
return {
|
|
170
|
+
state: state,
|
|
171
|
+
name: this.swapStateName(state),
|
|
172
|
+
description: this.swapStateDescription[state]
|
|
173
|
+
};
|
|
174
|
+
}
|
|
162
175
|
/**
|
|
163
176
|
* Re-fetches & revalidates the price data based on the current market prices
|
|
164
177
|
*/
|
|
@@ -225,6 +238,18 @@ class ISwap {
|
|
|
225
238
|
if ((typeof (signer) === "string" ? signer : signer.getAddress()) !== this._getInitiator())
|
|
226
239
|
throw new Error("Invalid signer provided!");
|
|
227
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Await and prepares a list of passed transactions
|
|
243
|
+
*
|
|
244
|
+
* @param txsPromise
|
|
245
|
+
* @internal
|
|
246
|
+
*/
|
|
247
|
+
async prepareTransactions(txsPromise) {
|
|
248
|
+
const txs = await txsPromise;
|
|
249
|
+
if (this.wrapper._chain.prepareTxs == null)
|
|
250
|
+
return txs;
|
|
251
|
+
return await this.wrapper._chain.prepareTxs(txs);
|
|
252
|
+
}
|
|
228
253
|
/**
|
|
229
254
|
* Sets this swap as initiated
|
|
230
255
|
* @internal
|
|
@@ -269,11 +294,7 @@ class ISwap {
|
|
|
269
294
|
* Returns the current state of the swap along with the human-readable description of the state
|
|
270
295
|
*/
|
|
271
296
|
getStateInfo() {
|
|
272
|
-
return
|
|
273
|
-
state: this._state,
|
|
274
|
-
name: this.swapStateName(this._state),
|
|
275
|
-
description: this.swapStateDescription[this._state]
|
|
276
|
-
};
|
|
297
|
+
return this._getStateInfo(this._state);
|
|
277
298
|
}
|
|
278
299
|
//////////////////////////////
|
|
279
300
|
//// Storage
|
|
@@ -331,7 +352,8 @@ class ISwap {
|
|
|
331
352
|
exactIn: this.exactIn,
|
|
332
353
|
createdAt: this.createdAt,
|
|
333
354
|
randomNonce: this._randomNonce,
|
|
334
|
-
contractVersion: this._contractVersion
|
|
355
|
+
contractVersion: this._contractVersion,
|
|
356
|
+
_meta: this._meta
|
|
335
357
|
};
|
|
336
358
|
}
|
|
337
359
|
//////////////////////////////
|
|
@@ -9,6 +9,7 @@ import { SwapType } from "../enums/SwapType";
|
|
|
9
9
|
import { UnifiedSwapStorage } from "../storage/UnifiedSwapStorage";
|
|
10
10
|
import { SCToken } from "../types/Token";
|
|
11
11
|
import { PriceInfoType } from "../types/PriceInfoType";
|
|
12
|
+
import { IntermediaryAPI } from "../intermediaries/apis/IntermediaryAPI";
|
|
12
13
|
export declare const DEFAULT_MAX_PARALLEL_SWAP_TICKS = 50;
|
|
13
14
|
export declare const DEFAULT_MAX_PARALLEL_SWAP_SYNCS = 50;
|
|
14
15
|
/**
|
|
@@ -132,6 +133,11 @@ export declare abstract class ISwapWrapper<T extends ChainType, D extends SwapTy
|
|
|
132
133
|
readonly _tokens: {
|
|
133
134
|
[tokenAddress: string]: SCToken<T["ChainId"]>;
|
|
134
135
|
};
|
|
136
|
+
/**
|
|
137
|
+
* LP API Used to communicate with the LPs
|
|
138
|
+
* @internal
|
|
139
|
+
*/
|
|
140
|
+
readonly _lpApi: IntermediaryAPI;
|
|
135
141
|
/**
|
|
136
142
|
* Chain identifier string of this wrapper
|
|
137
143
|
*/
|
|
@@ -142,7 +148,7 @@ export declare abstract class ISwapWrapper<T extends ChainType, D extends SwapTy
|
|
|
142
148
|
readonly events: EventEmitter<{
|
|
143
149
|
swapState: [D["Swap"]];
|
|
144
150
|
}>;
|
|
145
|
-
constructor(chainIdentifier: T["ChainId"], unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], prices: ISwapPrice, tokens: WrapperCtorTokens, options: O, events?: EventEmitter<{
|
|
151
|
+
constructor(chainIdentifier: T["ChainId"], unifiedStorage: UnifiedSwapStorage<T>, unifiedChainEvents: UnifiedSwapEventListener<T>, chain: T["ChainInterface"], prices: ISwapPrice, tokens: WrapperCtorTokens, lpApi: IntermediaryAPI, options: O, events?: EventEmitter<{
|
|
146
152
|
swapState: [ISwap];
|
|
147
153
|
}>);
|
|
148
154
|
/**
|
|
@@ -282,4 +288,8 @@ export declare abstract class ISwapWrapper<T extends ChainType, D extends SwapTy
|
|
|
282
288
|
* @internal
|
|
283
289
|
*/
|
|
284
290
|
_getPendingSwap(id: string): D["Swap"] | null;
|
|
291
|
+
/**
|
|
292
|
+
* @internal
|
|
293
|
+
*/
|
|
294
|
+
_getSignerAddress(signer?: string | T["Signer"] | T["NativeSigner"]): Promise<string | undefined>;
|
|
285
295
|
}
|