@atomiqlabs/sdk 8.5.0 → 8.6.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.
@@ -123,9 +123,15 @@ export type SwapperOptions = {
123
123
  */
124
124
  dontFetchLPs?: boolean;
125
125
  /**
126
- * By setting this flag the SDK persists all created swaps. By default, the SDK only saves and persists swaps that
127
- * are considered initiated, i.e. when `commit()`, `execute()` or `waitTillPayment` is called (or their respective
128
- * txs... prefixed variations).
126
+ * Defaults to `true`, this means every swap regardless of it being initiated (i.e. when `commit()`, `execute()` or
127
+ * `waitTillPayment` is called) is saved to the persistent storage. This is a reasonable default for when you
128
+ * want to only create a swap, and then later on retrieve it with the `swapper.getSwapById()` function.
129
+ *
130
+ * Setting this to `false` means the SDK only saves and persists swaps that are considered initiated, i.e. when
131
+ * `commit()`, `execute()` or `waitTillPayment` is called (or their respective txs... prefixed variations). This
132
+ * might save calls to the persistent storage for swaps that are never initiated. This is useful in e.g.
133
+ * frontend implementations where the frontend holds the swap object reference until it is initiated anyway, not
134
+ * necessitating the saving of the swap data to the persistent storage until it is actually initiated.
129
135
  */
130
136
  saveUninitializedSwaps?: boolean;
131
137
  /**
@@ -63,6 +63,7 @@ class Swapper extends events_1.EventEmitter {
63
63
  this.SwapTypeInfo = SwapUtils_1.SwapProtocolInfo;
64
64
  const storagePrefix = options?.storagePrefix ?? "atomiq-";
65
65
  options ??= {};
66
+ options.saveUninitializedSwaps ??= true;
66
67
  options.bitcoinNetwork = options.bitcoinNetwork == null ? base_1.BitcoinNetwork.TESTNET : options.bitcoinNetwork;
67
68
  const swapStorage = options.swapStorage ??= (name) => new IndexedDBUnifiedStorage_1.IndexedDBUnifiedStorage(name);
68
69
  this.options = options;
@@ -109,8 +109,8 @@ class ISwap {
109
109
  // swap exists, it will just never resolve!
110
110
  return new Promise((resolve, reject) => {
111
111
  let listener;
112
- listener = (swap) => {
113
- if (type === "eq" ? swap._state === targetState : type === "gte" ? swap._state >= targetState : swap._state != targetState) {
112
+ listener = () => {
113
+ if (type === "eq" ? this._state === targetState : type === "gte" ? this._state >= targetState : this._state != targetState) {
114
114
  resolve();
115
115
  this.events.removeListener("swapState", listener);
116
116
  }
@@ -102,9 +102,10 @@ class FromBTCLNAutoWrapper extends IFromBTCLNWrapper_1.IFromBTCLNWrapper {
102
102
  swap._commitTxId = event.meta?.txId;
103
103
  swap._commitedAt ??= Date.now();
104
104
  swap._state = FromBTCLNAutoSwap_1.FromBTCLNAutoSwapState.CLAIM_COMMITED;
105
- swap._broadcastSecret().catch(e => {
106
- this.logger.error("processEventInitialize(" + swap.getId() + "): Error when broadcasting swap secret: ", e);
107
- });
105
+ if (swap.hasSecretPreimage())
106
+ swap._broadcastSecret().catch(e => {
107
+ this.logger.error("processEventInitialize(" + swap.getId() + "): Error when broadcasting swap secret: ", e);
108
+ });
108
109
  return true;
109
110
  }
110
111
  return false;
@@ -119,6 +119,7 @@ export declare function isSpvFromBTCSwapInit(obj: any): obj is SpvFromBTCSwapIni
119
119
  * @category Swaps/Bitcoin → Smart chain
120
120
  */
121
121
  export declare class SpvFromBTCSwap<T extends ChainType> extends ISwap<T, SpvFromBTCTypeDefinition<T>> implements IBTCWalletSwap, ISwapWithGasDrop<T>, IClaimableSwap<T, SpvFromBTCTypeDefinition<T>, SpvFromBTCSwapState> {
122
+ protected readonly currentVersion: number;
122
123
  readonly TYPE: SwapType.SPV_VAULT_FROM_BTC;
123
124
  /**
124
125
  * @internal
@@ -169,6 +170,7 @@ export declare class SpvFromBTCSwap<T extends ChainType> extends ISwap<T, SpvFro
169
170
  private readonly frontingFeeShare;
170
171
  private readonly executionFeeShare;
171
172
  private readonly gasPricingInfo?;
173
+ private posted?;
172
174
  /**
173
175
  * @internal
174
176
  */
@@ -143,6 +143,7 @@ class SpvFromBTCSwap extends ISwap_1.ISwap {
143
143
  if (isSpvFromBTCSwapInit(initOrObject) && initOrObject.url != null)
144
144
  initOrObject.url += "/frombtc_spv";
145
145
  super(wrapper, initOrObject);
146
+ this.currentVersion = 2;
146
147
  this.TYPE = SwapType_1.SwapType.SPV_VAULT_FROM_BTC;
147
148
  /**
148
149
  * @internal
@@ -213,6 +214,7 @@ class SpvFromBTCSwap extends ISwap_1.ISwap {
213
214
  this._frontTxId = initOrObject.frontTxId;
214
215
  this.gasPricingInfo = (0, PriceInfoType_1.deserializePriceInfoType)(initOrObject.gasPricingInfo);
215
216
  this.btcTxConfirmedAt = initOrObject.btcTxConfirmedAt;
217
+ this.posted = initOrObject.posted;
216
218
  if (initOrObject.data != null)
217
219
  this._data = new this.wrapper._spvWithdrawalDataDeserializer(initOrObject.data);
218
220
  }
@@ -223,7 +225,12 @@ class SpvFromBTCSwap extends ISwap_1.ISwap {
223
225
  * @inheritDoc
224
226
  * @internal
225
227
  */
226
- upgradeVersion() { }
228
+ upgradeVersion() {
229
+ if (this.version === 1) {
230
+ this.posted = this.initiated;
231
+ this.version = 2;
232
+ }
233
+ }
227
234
  /**
228
235
  * @inheritDoc
229
236
  * @internal
@@ -723,6 +730,7 @@ class SpvFromBTCSwap extends ISwap_1.ISwap {
723
730
  }
724
731
  this._data = data;
725
732
  this.initiated = true;
733
+ this.posted = true;
726
734
  await this._saveAndEmit(SpvFromBTCSwapState.SIGNED);
727
735
  try {
728
736
  await IntermediaryAPI_1.IntermediaryAPI.initSpvFromBTC(this.chainIdentifier, this.url, {
@@ -895,7 +903,7 @@ class SpvFromBTCSwap extends ISwap_1.ISwap {
895
903
  async waitForBitcoinTransaction(updateCallback, checkIntervalSeconds, abortSignal) {
896
904
  if (this._state !== SpvFromBTCSwapState.POSTED &&
897
905
  this._state !== SpvFromBTCSwapState.BROADCASTED &&
898
- !(this._state === SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && this.initiated))
906
+ !(this._state === SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && this.posted))
899
907
  throw new Error("Must be in POSTED or BROADCASTED state!");
900
908
  if (this._data == null)
901
909
  throw new Error("Expected swap to have withdrawal data filled!");
@@ -1201,6 +1209,7 @@ class SpvFromBTCSwap extends ISwap_1.ISwap {
1201
1209
  executionFeeShare: this.executionFeeShare.toString(10),
1202
1210
  genesisSmartChainBlockHeight: this._genesisSmartChainBlockHeight,
1203
1211
  gasPricingInfo: (0, PriceInfoType_1.serializePriceInfoType)(this.gasPricingInfo),
1212
+ posted: this.posted,
1204
1213
  senderAddress: this._senderAddress,
1205
1214
  claimTxId: this._claimTxId,
1206
1215
  frontTxId: this._frontTxId,
@@ -1362,7 +1371,7 @@ class SpvFromBTCSwap extends ISwap_1.ISwap {
1362
1371
  return true;
1363
1372
  }
1364
1373
  }
1365
- if (this._state === SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && !this.initiated) {
1374
+ if (this._state === SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && !this.posted) {
1366
1375
  if (this.expiry < Date.now()) {
1367
1376
  this._state = SpvFromBTCSwapState.QUOTE_EXPIRED;
1368
1377
  if (save)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/sdk",
3
- "version": "8.5.0",
3
+ "version": "8.6.0",
4
4
  "description": "atomiq labs SDK for cross-chain swaps between smart chains and bitcoin",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -152,9 +152,15 @@ export type SwapperOptions = {
152
152
  */
153
153
  dontFetchLPs?: boolean,
154
154
  /**
155
- * By setting this flag the SDK persists all created swaps. By default, the SDK only saves and persists swaps that
156
- * are considered initiated, i.e. when `commit()`, `execute()` or `waitTillPayment` is called (or their respective
157
- * txs... prefixed variations).
155
+ * Defaults to `true`, this means every swap regardless of it being initiated (i.e. when `commit()`, `execute()` or
156
+ * `waitTillPayment` is called) is saved to the persistent storage. This is a reasonable default for when you
157
+ * want to only create a swap, and then later on retrieve it with the `swapper.getSwapById()` function.
158
+ *
159
+ * 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
161
+ * might save calls to the persistent storage for swaps that are never initiated. This is useful in e.g.
162
+ * frontend implementations where the frontend holds the swap object reference until it is initiated anyway, not
163
+ * necessitating the saving of the swap data to the persistent storage until it is actually initiated.
158
164
  */
159
165
  saveUninitializedSwaps?: boolean,
160
166
  /**
@@ -318,6 +324,7 @@ export class Swapper<T extends MultiChain> extends EventEmitter<{
318
324
  const storagePrefix = options?.storagePrefix ?? "atomiq-";
319
325
 
320
326
  options ??= {};
327
+ options.saveUninitializedSwaps ??= true;
321
328
  options.bitcoinNetwork = options.bitcoinNetwork==null ? BitcoinNetwork.TESTNET : options.bitcoinNetwork;
322
329
  const swapStorage = options.swapStorage ??= (name: string) => new IndexedDBUnifiedStorage(name);
323
330
 
@@ -229,9 +229,9 @@ export abstract class ISwap<
229
229
  //TODO: This doesn't hold strong reference to the swap, hence if no other strong reference to the
230
230
  // swap exists, it will just never resolve!
231
231
  return new Promise((resolve, reject) => {
232
- let listener: (swap: D["Swap"]) => void;
233
- listener = (swap) => {
234
- if(type==="eq" ? swap._state===targetState : type==="gte" ? swap._state>=targetState : swap._state!=targetState) {
232
+ let listener: () => void;
233
+ listener = () => {
234
+ if(type==="eq" ? this._state===targetState : type==="gte" ? this._state>=targetState : this._state!=targetState) {
235
235
  resolve();
236
236
  this.events.removeListener("swapState", listener);
237
237
  }
@@ -213,7 +213,7 @@ export class FromBTCLNAutoWrapper<
213
213
  swap._commitTxId = event.meta?.txId;
214
214
  swap._commitedAt ??= Date.now();
215
215
  swap._state = FromBTCLNAutoSwapState.CLAIM_COMMITED;
216
- swap._broadcastSecret().catch(e => {
216
+ if(swap.hasSecretPreimage()) swap._broadcastSecret().catch(e => {
217
217
  this.logger.error("processEventInitialize("+swap.getId()+"): Error when broadcasting swap secret: ", e);
218
218
  });
219
219
  return true;
@@ -193,6 +193,8 @@ export class SpvFromBTCSwap<T extends ChainType>
193
193
  extends ISwap<T, SpvFromBTCTypeDefinition<T>>
194
194
  implements IBTCWalletSwap, ISwapWithGasDrop<T>, IClaimableSwap<T, SpvFromBTCTypeDefinition<T>, SpvFromBTCSwapState> {
195
195
 
196
+ protected readonly currentVersion: number = 2;
197
+
196
198
  readonly TYPE: SwapType.SPV_VAULT_FROM_BTC = SwapType.SPV_VAULT_FROM_BTC;
197
199
 
198
200
  /**
@@ -240,6 +242,8 @@ export class SpvFromBTCSwap<T extends ChainType>
240
242
 
241
243
  private readonly gasPricingInfo?: PriceInfoType;
242
244
 
245
+ private posted?: boolean;
246
+
243
247
  /**
244
248
  * @internal
245
249
  */
@@ -337,6 +341,7 @@ export class SpvFromBTCSwap<T extends ChainType>
337
341
  this._frontTxId = initOrObject.frontTxId;
338
342
  this.gasPricingInfo = deserializePriceInfoType(initOrObject.gasPricingInfo);
339
343
  this.btcTxConfirmedAt = initOrObject.btcTxConfirmedAt;
344
+ this.posted = initOrObject.posted;
340
345
  if(initOrObject.data!=null) this._data = new this.wrapper._spvWithdrawalDataDeserializer(initOrObject.data);
341
346
  }
342
347
  this.tryCalculateSwapFee();
@@ -347,7 +352,12 @@ export class SpvFromBTCSwap<T extends ChainType>
347
352
  * @inheritDoc
348
353
  * @internal
349
354
  */
350
- protected upgradeVersion() { /*NOOP*/ }
355
+ protected upgradeVersion() {
356
+ if(this.version===1) {
357
+ this.posted = this.initiated;
358
+ this.version = 2;
359
+ }
360
+ }
351
361
 
352
362
  /**
353
363
  * @inheritDoc
@@ -973,6 +983,7 @@ export class SpvFromBTCSwap<T extends ChainType>
973
983
 
974
984
  this._data = data;
975
985
  this.initiated = true;
986
+ this.posted = true;
976
987
  await this._saveAndEmit(SpvFromBTCSwapState.SIGNED);
977
988
 
978
989
  try {
@@ -1186,7 +1197,7 @@ export class SpvFromBTCSwap<T extends ChainType>
1186
1197
  if(
1187
1198
  this._state!==SpvFromBTCSwapState.POSTED &&
1188
1199
  this._state!==SpvFromBTCSwapState.BROADCASTED &&
1189
- !(this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && this.initiated)
1200
+ !(this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && this.posted)
1190
1201
  ) throw new Error("Must be in POSTED or BROADCASTED state!");
1191
1202
  if(this._data==null) throw new Error("Expected swap to have withdrawal data filled!");
1192
1203
 
@@ -1529,6 +1540,7 @@ export class SpvFromBTCSwap<T extends ChainType>
1529
1540
  executionFeeShare: this.executionFeeShare.toString(10),
1530
1541
  genesisSmartChainBlockHeight: this._genesisSmartChainBlockHeight,
1531
1542
  gasPricingInfo: serializePriceInfoType(this.gasPricingInfo),
1543
+ posted: this.posted,
1532
1544
 
1533
1545
  senderAddress: this._senderAddress,
1534
1546
  claimTxId: this._claimTxId,
@@ -1704,7 +1716,7 @@ export class SpvFromBTCSwap<T extends ChainType>
1704
1716
  }
1705
1717
  }
1706
1718
 
1707
- if(this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && !this.initiated) {
1719
+ if(this._state===SpvFromBTCSwapState.QUOTE_SOFT_EXPIRED && !this.posted) {
1708
1720
  if(this.expiry<Date.now()) {
1709
1721
  this._state = SpvFromBTCSwapState.QUOTE_EXPIRED;
1710
1722
  if(save) await this._saveAndEmit();