@xchainjs/xchain-thorchain-amm 0.8.21 → 0.8.22

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/lib/index.js CHANGED
@@ -149,9 +149,9 @@ class Wallet {
149
149
  /**
150
150
  * Contructor to create a Wallet
151
151
  *
152
- * @param phrase - mnemonic phrase
153
- * @param thorchainCache - an instance of the ThorchainCache (could be pointing to stagenet,testnet,mainnet)
154
- * @param chainConfigs - Config by chain
152
+ * @param phrase - Mnemonic phrase
153
+ * @param thorchainCache - An instance of the ThorchainCache (could be pointing to stagenet,testnet,mainnet)
154
+ * @param chainConfigs - Configuration settings for different blockchain chains
155
155
  * @returns Wallet
156
156
  */
157
157
  constructor(phrase, thorchainQuery, chainConfigs = {}) {
@@ -177,20 +177,24 @@ class Wallet {
177
177
  };
178
178
  }
179
179
  /**
180
- * Fetch balances for all wallets
181
- *
182
- * @returns AllBalances[]
180
+ * Fetch the balance of all the chains.
181
+ * @returns An array of AllBalances objects containing balance information for all chains
183
182
  */
184
183
  getAllBalances() {
185
184
  return __awaiter(this, void 0, void 0, function* () {
186
185
  const allBalances = [];
186
+ // Loop through each chain's client and fetch the balance
187
187
  for (const [chain, client] of Object.entries(this.clients)) {
188
+ // Get the wallet address for the current chain
188
189
  const address = yield client.getAddressAsync(0);
189
190
  try {
191
+ // Fetch the balance for the wallet address
190
192
  const balances = yield client.getBalance(address);
193
+ // Push the balance information to the allBalances array
191
194
  allBalances.push({ chain, address, balances });
192
195
  }
193
196
  catch (err) {
197
+ // If an error occurs, push an error message to the allBalances array
194
198
  allBalances.push({ chain, address, balances: err.message });
195
199
  }
196
200
  }
@@ -198,15 +202,16 @@ class Wallet {
198
202
  });
199
203
  }
200
204
  /**
201
- * Executes a Swap from THORChainAMM.doSwap()
202
- *
203
- * @param swap object with all the required details for a swap.
204
- * @returns transaction details and explorer url
205
+ * Executes a swap using the provided swap details.
206
+ * @param swap Object containing details required for the swap
207
+ * @returns Object containing transaction details and explorer URL
205
208
  * @see ThorchainAMM.doSwap()
206
209
  */
207
210
  executeSwap(swap) {
208
211
  return __awaiter(this, void 0, void 0, function* () {
212
+ // Validate the swap details
209
213
  yield this.validateSwap(swap);
214
+ // Determine whether the destination asset is THORChain or a synthetic asset
210
215
  if (swap.input.asset.chain === xchainThorchain.THORChain || swap.input.asset.synth) {
211
216
  return yield this.swapRuneTo(swap);
212
217
  }
@@ -215,20 +220,20 @@ class Wallet {
215
220
  }
216
221
  });
217
222
  }
218
- /** Validate swap object
219
- *
220
- * @param swap - swap parameters
223
+ /** Validates the swap object details.
224
+ * @param swap - Object containing swap parameters
225
+ * @returns An array of validation errors
221
226
  */
222
227
  validateSwap(swap) {
223
228
  return __awaiter(this, void 0, void 0, function* () {
224
229
  const errors = [];
225
230
  const isThorchainDestinationAsset = swap.destinationAsset.synth || swap.destinationAsset.chain === xchainThorchain.THORChain;
226
231
  const chain = isThorchainDestinationAsset ? xchainThorchain.THORChain : swap.destinationAsset.chain;
227
- // check address
232
+ // Check if the destination address is valid
228
233
  if (swap.destinationAddress && !this.clients[chain].validateAddress(swap.destinationAddress)) {
229
234
  errors.push(`destinationAddress ${swap.destinationAddress} is not a valid address`);
230
235
  }
231
- // Affiliate address should be THORName or THORAddress
236
+ // Affiliate address should be a valid THOR address or THORName
232
237
  const checkAffiliateAddress = swap.memo.split(':');
233
238
  if (checkAffiliateAddress.length > 4) {
234
239
  const affiliateAddress = checkAffiliateAddress[4];
@@ -239,9 +244,10 @@ class Wallet {
239
244
  errors.push(`affiliateAddress ${affiliateAddress} is not a valid THOR address`);
240
245
  }
241
246
  }
242
- // if input == synth return errors.
247
+ // If input asset is synthetic, return errors
243
248
  if (swap.input.asset.synth)
244
249
  return errors;
250
+ // Check if the input asset is an ERC20 asset and whether the router has been approved to spend this amount
245
251
  if (this.isERC20Asset(swap.input.asset)) {
246
252
  const isApprovedResult = yield this.evmHelpers[swap.input.asset.chain].isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
247
253
  if (!isApprovedResult) {
@@ -251,16 +257,21 @@ class Wallet {
251
257
  return errors;
252
258
  });
253
259
  }
260
+ /**
261
+ * Checks if a given string is a valid THORName.
262
+ * @param name - Name to check
263
+ * @returns Boolean indicating whether the name is a valid THORName
264
+ */
254
265
  isThorname(name) {
255
266
  return __awaiter(this, void 0, void 0, function* () {
256
267
  const thornameDetails = yield this.thorchainQuery.thorchainCache.midgardQuery.midgardCache.midgard.getTHORNameDetails(name); // Update when thorchainCache expose getTHORNameDetails method
257
268
  return thornameDetails !== undefined;
258
269
  });
259
270
  }
260
- /** Function handles all swaps from Rune to asset
271
+ /** Swaps assets from RUNE to another asset.
261
272
  *
262
- * @param swap - swap parameters
263
- * @returns - tx submitted object
273
+ * @param swap - Swap details parameter
274
+ * @returns - Object containing transaction details and explorer URL
264
275
  */
265
276
  swapRuneTo(swap) {
266
277
  return __awaiter(this, void 0, void 0, function* () {
@@ -273,17 +284,19 @@ class Wallet {
273
284
  return { hash, url: this.clients.THOR.getExplorerTxUrl(hash) };
274
285
  });
275
286
  }
276
- /** Function handles all swaps from Non Rune
277
- *
278
- * @param swap - swap object
279
- * @returns - TxSubmitted object
287
+ /** Swaps assets that are not RUNE.
288
+ * @param swap - swap details object
289
+ * @returns - Object containing transaction details and explorer URL
280
290
  */
281
291
  swapNonRune(swap) {
282
292
  return __awaiter(this, void 0, void 0, function* () {
283
293
  const client = this.clients[swap.input.asset.chain];
294
+ // Retrieve inbound details for the asset's chain
284
295
  const inbound = (yield this.thorchainQuery.thorchainCache.getInboundDetails())[swap.input.asset.chain];
296
+ // Check if there is an inbound address for the asset's chain
285
297
  if (!(inbound === null || inbound === void 0 ? void 0 : inbound.address))
286
298
  throw Error(`no asgard address found for ${swap.input.asset.chain}`);
299
+ // Handle swaps for EVM chains
287
300
  if (this.isEVMChain(swap.input.asset)) {
288
301
  const params = {
289
302
  walletIndex: 0,
@@ -296,6 +309,7 @@ class Wallet {
296
309
  return { hash, url: client.getExplorerTxUrl(hash) };
297
310
  }
298
311
  else {
312
+ // Handle swaps for non-EVM chains
299
313
  const params = {
300
314
  walletIndex: 0,
301
315
  asset: swap.input.asset,
@@ -308,10 +322,10 @@ class Wallet {
308
322
  }
309
323
  });
310
324
  }
311
- /** Function handles liquidity Add
325
+ /** Handles adding liquidity to the pool.
312
326
  * BASED OFF https://dev.thorchain.or›g/thorchain-dev/network/memos
313
- * @param params input parameters needed to add liquidity
314
- * @returns transaction details submitted
327
+ * @param params Input parameters for adding liquidity
328
+ * @returns An array of transaction details submitted
315
329
  */
316
330
  addLiquidity(params) {
317
331
  return __awaiter(this, void 0, void 0, function* () {
@@ -324,7 +338,7 @@ class Wallet {
324
338
  // const waitTimeSeconds = params.waitTimeSeconds
325
339
  let constructedMemo = '';
326
340
  const txSubmitted = [];
327
- // symmetrical add
341
+ // Symmetrical add
328
342
  if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.gt(0)) {
329
343
  constructedMemo = `+:${params.assetPool}:${addressRune}`;
330
344
  txSubmitted.push(yield this.addAssetLP(params, constructedMemo, assetClient, inboundAsgard));
@@ -333,23 +347,22 @@ class Wallet {
333
347
  return txSubmitted;
334
348
  }
335
349
  else if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.eq(0)) {
336
- // asymmetrical asset only
350
+ // Asymmetrical asset only
337
351
  constructedMemo = `+:${params.assetPool}`;
338
352
  txSubmitted.push(yield this.addAssetLP(params, constructedMemo, assetClient, inboundAsgard));
339
353
  return txSubmitted;
340
354
  }
341
355
  else {
342
- // asymmetrical rune only
356
+ // Asymmetrical rune only
343
357
  constructedMemo = `+:${params.assetPool}`;
344
358
  txSubmitted.push(yield this.addRuneLP(params, constructedMemo, thorchainClient));
345
359
  return txSubmitted;
346
360
  }
347
361
  });
348
362
  }
349
- /** Function handles liquidity Withdraw
350
- *
351
- * @param params - parameters required for liquidity position
352
- * @returns object with tx response, url and wait time in seconds
363
+ /** Handles withdrawing liquidity from the pool.
364
+ * @param params - Parameters required for liquidity position
365
+ * @returns Object with transaction response, URL, and wait time in seconds
353
366
  */
354
367
  withdrawLiquidity(params) {
355
368
  return __awaiter(this, void 0, void 0, function* () {
@@ -369,13 +382,13 @@ class Wallet {
369
382
  return txSubmitted;
370
383
  }
371
384
  else if (params.assetAddress && !params.runeAddress) {
372
- // asymmetrical asset only
385
+ // Asymmetrical asset only
373
386
  constructedMemo = `-:${params.assetPool}:${basisPoints}`;
374
387
  txSubmitted.push(yield this.withdrawAssetLP(params, constructedMemo, assetClient, inboundAsgard));
375
388
  return txSubmitted;
376
389
  }
377
390
  else {
378
- // asymmetrical rune only
391
+ // Asymmetrical rune only
379
392
  constructedMemo = `-:${params.assetPool}:${basisPoints}`;
380
393
  txSubmitted.push(yield this.withdrawRuneLP(params, constructedMemo, thorchainClient));
381
394
  return txSubmitted;
@@ -383,15 +396,16 @@ class Wallet {
383
396
  });
384
397
  }
385
398
  /**
386
- *
387
- * @param assetAmount - amount to add
388
- * @param memo - memo required
399
+ * Adds funds to a savers account.
400
+ * @param assetAmount - The amount of assets to add.
401
+ * @param memo - The memo required for the transaction.
389
402
  * @param waitTimeSeconds - expected wait for the transaction to be processed
390
- * @returns
403
+ * @returns An object containing transaction details and explorer URL.
391
404
  */
392
405
  addSavers(assetAmount, memo, toAddress) {
393
406
  return __awaiter(this, void 0, void 0, function* () {
394
407
  const assetClient = this.clients[assetAmount.asset.chain];
408
+ // Handle EVM chains
395
409
  if (this.isEVMChain(assetAmount.asset)) {
396
410
  const addParams = {
397
411
  wallIndex: 0,
@@ -423,7 +437,7 @@ class Wallet {
423
437
  return { hash, url: assetClient.getExplorerAddressUrl(assetClient.getAddress()) };
424
438
  }
425
439
  }
426
- else {
440
+ else { // Handle other chains
427
441
  const addParams = {
428
442
  wallIndex: 0,
429
443
  asset: assetAmount.asset,
@@ -443,15 +457,16 @@ class Wallet {
443
457
  });
444
458
  }
445
459
  /**
446
- *
447
- * @param assetAmount - amount to withdraw
448
- * @param memo - memo required
460
+ * Withdraws funds from a savers account.
461
+ * @param assetAmount - The amount of assets to withdraw.
462
+ * @param memo - The memo required for the transaction.
449
463
  * @param waitTimeSeconds - expected wait for the transaction to be processed
450
- * @returns
464
+ * @returns An object containing transaction details and explorer URL.
451
465
  */
452
466
  withdrawSavers(assetAmount, memo, toAddress) {
453
467
  return __awaiter(this, void 0, void 0, function* () {
454
468
  const assetClient = this.clients[assetAmount.asset.chain];
469
+ // Handle EVM chains
455
470
  if (this.isEVMChain(assetAmount.asset)) {
456
471
  const addParams = {
457
472
  wallIndex: 0,
@@ -483,7 +498,7 @@ class Wallet {
483
498
  return { hash, url: yield assetClient.getExplorerAddressUrl(yield assetClient.getAddressAsync()) };
484
499
  }
485
500
  }
486
- else {
501
+ else { // Handle other chains
487
502
  const addParams = {
488
503
  wallIndex: 0,
489
504
  asset: assetAmount.asset,
@@ -502,9 +517,15 @@ class Wallet {
502
517
  }
503
518
  });
504
519
  }
520
+ /**
521
+ * Opens a new loan.
522
+ * @param params - Parameters required to open a loan.
523
+ * @returns An object containing transaction details and explorer URL.
524
+ */
505
525
  loanOpen(params) {
506
526
  return __awaiter(this, void 0, void 0, function* () {
507
527
  const assetClient = this.clients[params.amount.asset.chain];
528
+ // Handle EVM chains
508
529
  if (this.isEVMChain(params.amount.asset)) {
509
530
  const addParams = {
510
531
  wallIndex: 0,
@@ -536,7 +557,7 @@ class Wallet {
536
557
  return { hash, url: assetClient.getExplorerAddressUrl(yield assetClient.getAddressAsync()) };
537
558
  }
538
559
  }
539
- else {
560
+ else { // Handle other chains
540
561
  const addParams = {
541
562
  wallIndex: 0,
542
563
  asset: params.amount.asset,
@@ -555,9 +576,15 @@ class Wallet {
555
576
  }
556
577
  });
557
578
  }
579
+ /**
580
+ * Closes an existing loan.
581
+ * @param params - Parameters required to close a loan.
582
+ * @returns An object containing transaction details and explorer URL.
583
+ */
558
584
  loanClose(params) {
559
585
  return __awaiter(this, void 0, void 0, function* () {
560
586
  const assetClient = this.clients[params.amount.asset.chain];
587
+ // Handle EVM chains
561
588
  if (this.isEVMChain(params.amount.asset)) {
562
589
  const addParams = {
563
590
  wallIndex: 0,
@@ -589,7 +616,7 @@ class Wallet {
589
616
  return { hash, url: assetClient.getExplorerAddressUrl(yield assetClient.getAddressAsync()) };
590
617
  }
591
618
  }
592
- else {
619
+ else { // Handle other chains
593
620
  const addParams = {
594
621
  wallIndex: 0,
595
622
  asset: params.amount.asset,
@@ -608,7 +635,7 @@ class Wallet {
608
635
  }
609
636
  });
610
637
  }
611
- /**
638
+ /**Registers a THORName with default parameters.
612
639
  * Register a THORName with a default expirity of one year. By default chain and chainAddress is getting from wallet instance and is BTC.
613
640
  * By default owner is getting from wallet
614
641
  * @param thorname - Name to register
@@ -622,13 +649,18 @@ class Wallet {
622
649
  */
623
650
  registerThorname(params) {
624
651
  return __awaiter(this, void 0, void 0, function* () {
652
+ // Obtain the client corresponding to the specified chain or default to BTCChain
625
653
  const chainClient = this.clients[params.chain || xchainBitcoin.BTCChain];
626
654
  const thorClient = this.clients.THOR;
655
+ // Check if both chainClient and thorClient exist
627
656
  if (!chainClient || !thorClient) {
628
657
  throw Error('Can not find a wallet client');
629
658
  }
659
+ // Estimate the THORName registration fee
630
660
  const thornameEstimation = yield this.thorchainQuery.estimateThorname(Object.assign(Object.assign({}, params), { chain: params.chain || xchainBitcoin.BTCChain, chainAddress: params.chainAddress || (yield chainClient.getAddressAsync()), owner: params.owner || (yield thorClient.getAddressAsync()) }));
661
+ // Cast thorClient to ThorchainClient
631
662
  const castedThorClient = thorClient;
663
+ // Deposit the registration fee for the THORName
632
664
  const result = yield castedThorClient.deposit({
633
665
  asset: thornameEstimation.value.asset,
634
666
  amount: thornameEstimation.value.baseAmount,
@@ -637,7 +669,7 @@ class Wallet {
637
669
  return result;
638
670
  });
639
671
  }
640
- /**
672
+ /**Updates an existing THORName with default parameters.
641
673
  * Register a THORName with a default expirity of one year. By default chain and chainAddress is getting from wallet instance and is BTC.
642
674
  * By default owner is getting from wallet
643
675
  * @param thorname - Name to register
@@ -650,17 +682,24 @@ class Wallet {
650
682
  */
651
683
  updateThorname(params) {
652
684
  return __awaiter(this, void 0, void 0, function* () {
685
+ // Obtain the client corresponding to the specified chain or default to BTCChain
653
686
  const chainClient = this.clients[params.chain || xchainBitcoin.BTCChain];
654
687
  const thorClient = this.clients.THOR;
688
+ // Check if both chainClient and thorClient exist
655
689
  if (!chainClient || !thorClient) {
656
690
  throw Error('Can not find a wallet client');
657
691
  }
692
+ // Retrieve details of the existing THORName
658
693
  const thornameDetail = yield this.thorchainQuery.getThornameDetails(params.thorname);
694
+ // Verify ownership of the THORName
659
695
  if ((thornameDetail === null || thornameDetail === void 0 ? void 0 : thornameDetail.owner) !== (yield thorClient.getAddressAsync())) {
660
696
  throw Error('You cannot update a domain that is not yours');
661
697
  }
698
+ // Estimate the updated THORName registration fee
662
699
  const thornameEstimation = yield this.thorchainQuery.estimateThorname(Object.assign(Object.assign({}, params), { chain: params.chain || xchainBitcoin.BTCChain, isUpdate: true, preferredAsset: params.preferredAsset || xchainUtil.assetFromString(thornameDetail.preferredAsset), chainAddress: params.chainAddress || (yield chainClient.getAddressAsync()) }));
700
+ // Cast thorClient to ThorchainClient
663
701
  const castedThorClient = thorClient;
702
+ // Deposit the updated registration fee for the THORName
664
703
  const result = yield castedThorClient.deposit({
665
704
  asset: thornameEstimation.value.asset,
666
705
  amount: thornameEstimation.value.baseAmount,
@@ -669,18 +708,19 @@ class Wallet {
669
708
  return result;
670
709
  });
671
710
  }
672
- /** Function handles liquidity add for all non rune assets
673
- *
674
- * @param params - parameters for add liquidity
675
- * @param constructedMemo - memo needed for thorchain
676
- * @param waitTimeSeconds - wait time for the tx to be confirmed
677
- * @param assetClient - passing XchainClient
678
- * @param inboundAsgard - inbound Asgard address for the LP
679
- * @returns - tx object
711
+ /** Handles liquidity addition for non-RUNE assets.
712
+ * @param params - Parameters for add liquidity
713
+ * @param constructedMemo - Memo needed for thorchain
714
+ * @param waitTimeSeconds - Wait time for the tx to be confirmed
715
+ * @param assetClient - XChainClient for the asset.
716
+ * @param inboundAsgard - Inbound Asgard address for the LP.
717
+ * @returns - Transaction object.
680
718
  */
681
719
  addAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
682
720
  return __awaiter(this, void 0, void 0, function* () {
721
+ // Handle EVM chains
683
722
  if (this.isEVMChain(params.asset.asset)) {
723
+ // Prepare parameters for sending a deposit on EVM chains
684
724
  const addParams = {
685
725
  wallIndex: 0,
686
726
  asset: params.asset.asset,
@@ -692,8 +732,10 @@ class Wallet {
692
732
  const hash = yield evmHelper.sendDeposit(addParams);
693
733
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
694
734
  }
695
- else if (this.isUTXOChain(params.asset.asset)) {
735
+ else if (this.isUTXOChain(params.asset.asset)) { // Handle UTXO chains
736
+ // Get fee rates for UTXO chains
696
737
  const feeRates = yield assetClient.getFeeRates(xchainClient.Protocol.THORCHAIN);
738
+ // Prepare parameters for transferring assets on UTXO chains
697
739
  const addParams = {
698
740
  wallIndex: 0,
699
741
  asset: params.asset.asset,
@@ -703,15 +745,18 @@ class Wallet {
703
745
  feeRate: feeRates.fast,
704
746
  };
705
747
  try {
748
+ // Transfer assets with the provided parameters
706
749
  const hash = yield assetClient.transfer(addParams);
707
750
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
708
751
  }
709
752
  catch (err) {
753
+ // If an error occurs during the transfer, handle it and return the error message along with the explorer URL of the asset client's address
710
754
  const hash = JSON.stringify(err);
711
755
  return { hash, url: assetClient.getExplorerAddressUrl(yield assetClient.getAddressAsync()) };
712
756
  }
713
757
  }
714
- else {
758
+ else { // Handle other chains
759
+ // Prepare parameters for transferring assets on other chains
715
760
  const addParams = {
716
761
  wallIndex: 0,
717
762
  asset: params.asset.asset,
@@ -720,28 +765,31 @@ class Wallet {
720
765
  memo: constructedMemo,
721
766
  };
722
767
  try {
768
+ // Transfer assets with the provided parameters
723
769
  const hash = yield assetClient.transfer(addParams);
724
770
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
725
771
  }
726
772
  catch (err) {
773
+ // If an error occurs during the transfer, handle it and return the error message along with the explorer URL of the asset client's address
727
774
  const hash = JSON.stringify(err);
728
775
  return { hash, url: assetClient.getExplorerAddressUrl(yield assetClient.getAddressAsync()) };
729
776
  }
730
777
  }
731
778
  });
732
779
  }
733
- /** Function handles liquidity Withdraw for Non rune assets
734
- *
735
- * @param params - parameters for withdraw liquidity
736
- * @param constructedMemo - memo needed for thorchain execution
737
- * @param assetClient - asset client to call transfer
738
- * @param waitTimeSeconds - return back estimated wait
739
- * @param inboundAsgard - destination address
740
- * @returns - tx object
780
+ /** Handles liquidity withdrawal for non-RUNE assets.
781
+ * @param params - Parameters for withdrawing liquidity.
782
+ * @param constructedMemo - Memo needed for Thorchain execution.
783
+ * @param assetClient - Asset client to call transfer.
784
+ * @param waitTimeSeconds - Return back estimated wait
785
+ * @param inboundAsgard - Destination address.
786
+ * @returns - Transaction object.
741
787
  */
742
788
  withdrawAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
743
789
  return __awaiter(this, void 0, void 0, function* () {
790
+ // Handle EVM chains
744
791
  if (this.isEVMChain(params.assetFee.asset)) {
792
+ // Prepare parameters for sending a deposit on EVM chains
745
793
  const withdrawParams = {
746
794
  wallIndex: 0,
747
795
  asset: params.assetFee.asset,
@@ -754,7 +802,9 @@ class Wallet {
754
802
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
755
803
  }
756
804
  else if (this.isUTXOChain(params.assetFee.asset)) {
805
+ // Handle UTXO chains
757
806
  const feeRates = yield assetClient.getFeeRates(xchainClient.Protocol.THORCHAIN);
807
+ // Prepare parameters for transferring assets on UTXO chains
758
808
  const withdrawParams = {
759
809
  wallIndex: 0,
760
810
  asset: params.assetFee.asset,
@@ -764,15 +814,18 @@ class Wallet {
764
814
  feeRate: feeRates.fast,
765
815
  };
766
816
  try {
817
+ // Transfer assets with the provided parameters
767
818
  const hash = yield assetClient.transfer(withdrawParams);
768
819
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
769
820
  }
770
821
  catch (err) {
822
+ // If an error occurs during the transfer, handle it and return the error message along with the explorer URL of the asset client's address
771
823
  const hash = JSON.stringify(err);
772
824
  return { hash, url: assetClient.getExplorerAddressUrl(yield assetClient.getAddressAsync()) };
773
825
  }
774
826
  }
775
827
  else {
828
+ // Handle other chains
776
829
  const withdrawParams = {
777
830
  wallIndex: 0,
778
831
  asset: params.assetFee.asset,
@@ -781,21 +834,22 @@ class Wallet {
781
834
  memo: constructedMemo,
782
835
  };
783
836
  try {
837
+ // Transfer assets with the provided parameters
784
838
  const hash = yield assetClient.transfer(withdrawParams);
785
839
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
786
840
  }
787
841
  catch (err) {
842
+ // If an error occurs during the transfer, handle it and return the error message along with the explorer URL of the asset client's address
788
843
  const hash = JSON.stringify(err);
789
844
  return { hash, url: assetClient.getExplorerAddressUrl(yield assetClient.getAddressAsync()) };
790
845
  }
791
846
  }
792
847
  });
793
848
  }
794
- /** Function handles liquidity Add for Rune only
795
- *
796
- * @param params - deposit parameters
797
- * @param memo - memo needed to withdraw lp
798
- * @returns - tx object
849
+ /** Handles liquidity addition for Rune only.
850
+ * @param params - Deposit parameters
851
+ * @param memo - Memo needed to withdraw lp
852
+ * @returns - Transaction object.
799
853
  */
800
854
  addRuneLP(params, memo, thorchainClient) {
801
855
  return __awaiter(this, void 0, void 0, function* () {
@@ -809,11 +863,10 @@ class Wallet {
809
863
  return { hash, url: thorchainClient.getExplorerTxUrl(hash) };
810
864
  });
811
865
  }
812
- /** Function handles liquidity Withdraw for Rune only
813
- *
814
- * @param params - withdraw parameters
815
- * @param memo - memo needed to withdraw lp
816
- * @returns - tx object
866
+ /** Handles liquidity withdrawal for Rune only.
867
+ * @param params - Withdraw parameters
868
+ * @param memo - Memo needed to withdraw lp
869
+ * @returns - Transaction object.
817
870
  */
818
871
  withdrawRuneLP(params, memo, thorchainClient) {
819
872
  return __awaiter(this, void 0, void 0, function* () {
@@ -827,19 +880,35 @@ class Wallet {
827
880
  return { hash, url: thorchainClient.getExplorerTxUrl(hash) };
828
881
  });
829
882
  }
883
+ /**
884
+ * Checks if an asset is an ERC20 asset.
885
+ * @param asset - Asset to check.
886
+ * @returns - Boolean indicating whether the asset is an ERC20 asset.
887
+ */
830
888
  isERC20Asset(asset) {
831
889
  const isGasAsset = ['ETH', 'BSC', 'AVAX'].includes(asset.symbol);
832
890
  return this.isEVMChain(asset) && !isGasAsset;
833
891
  }
892
+ /**
893
+ * Checks if an asset belongs to an EVM chain.
894
+ * @param asset - Asset to check.
895
+ * @returns - Boolean indicating whether the asset belongs to an EVM chain.
896
+ */
834
897
  isEVMChain(asset) {
835
898
  const isEvmChain = ['ETH', 'BSC', 'AVAX'].includes(asset.chain);
836
899
  return isEvmChain;
837
900
  }
901
+ /**
902
+ * Checks if an asset belongs to a UTXO chain.
903
+ * @param asset - Asset to check.
904
+ * @returns - Boolean indicating whether the asset belongs to a UTXO chain.
905
+ */
838
906
  isUTXOChain(asset) {
839
907
  return ['BTC', 'BCH', 'DOGE'].includes(asset.chain);
840
908
  }
841
909
  }
842
910
 
911
+ // Create a default ThorchainQuery instance
843
912
  const defaultQuery = new xchainThorchainQuery.ThorchainQuery();
844
913
  /**
845
914
  * THORChain Class for interacting with THORChain.
@@ -848,7 +917,7 @@ const defaultQuery = new xchainThorchainQuery.ThorchainQuery();
848
917
  */
849
918
  class ThorchainAMM {
850
919
  /**
851
- * Contructor to create a ThorchainAMM
920
+ * Contructor to create a ThorchainAMM instance
852
921
  *
853
922
  * @param thorchainQuery - an instance of the ThorchainQuery
854
923
  * @returns ThorchainAMM
@@ -857,16 +926,18 @@ class ThorchainAMM {
857
926
  this.thorchainQuery = thorchainQuery;
858
927
  }
859
928
  /**
860
- * Provides a swap estimate for the given swap detail. Will check the params for errors before trying to get the estimate.
861
- * Uses current pool data, works out inbound and outboud fee, affiliate fees and works out the expected wait time for the swap (in and out)
862
- *
863
- * @param params - amount to swap
929
+ * * Provides an estimate for a swap based on the given swap details.
930
+ * Checks the parameters for errors before attempting to retrieve the estimate.
931
+ * Utilizes current pool data to calculate inbound and outbound fees, affiliate fees,
932
+ * and the expected wait time for the swap (inbound and outbound).
933
+ * @param params Parameters for the swap, including the amount to swap.
864
934
 
865
- * @returns The SwapEstimate
935
+ * @returns The estimated swap details.
866
936
  */
867
937
  estimateSwap({ fromAsset, amount, destinationAsset, destinationAddress, affiliateAddress = '', interfaceID = `555`, affiliateBps = 0, toleranceBps, wallet, walletIndex, }) {
868
938
  return __awaiter(this, void 0, void 0, function* () {
869
939
  let errors = [];
940
+ // If a wallet is provided, validate the swap parameters
870
941
  if (wallet) {
871
942
  const params = {
872
943
  input: amount,
@@ -878,6 +949,7 @@ class ThorchainAMM {
878
949
  };
879
950
  errors = yield wallet.validateSwap(params);
880
951
  }
952
+ // Get the swap estimate from the ThorchainQuery instance
881
953
  const estimate = yield this.thorchainQuery.quoteSwap({
882
954
  fromAsset,
883
955
  amount,
@@ -888,25 +960,27 @@ class ThorchainAMM {
888
960
  affiliateBps,
889
961
  toleranceBps,
890
962
  });
963
+ // Add any validation errors to the estimate
891
964
  estimate.txEstimate.errors.push(...errors);
892
965
  estimate.txEstimate.canSwap = errors.length == 0;
893
966
  return estimate;
894
967
  });
895
968
  }
896
969
  /**
897
- * Conducts a swap with the given inputs. Should be called after estimateSwap() to ensure the swap is valid
970
+ * Conducts a swap with the given inputs. This method should be called after estimateSwap() to ensure the swap is valid.
898
971
  *
899
- * @param wallet - wallet to use
900
- * @param params - swap params
901
- * @returns {SwapSubmitted} - Tx Hash, URL of BlockExplorer and expected wait time.
972
+ * @param wallet - The wallet to use for the swap.
973
+ * @param params - The swap parameters.
974
+ * @returns {SwapSubmitted} - The transaction hash, URL of BlockExplorer, and expected wait time.
902
975
  */
903
976
  doSwap(wallet, params) {
904
977
  return __awaiter(this, void 0, void 0, function* () {
905
- // Thorchain-query call satisfies the data needed for executeSwap to be called.
978
+ // Retrieve swap details from ThorchainQuery to ensure validity
906
979
  const txDetails = yield this.thorchainQuery.quoteSwap(params);
907
980
  if (!txDetails.txEstimate.canSwap) {
908
981
  throw Error(txDetails.txEstimate.errors.join('\n'));
909
982
  }
983
+ // Execute the swap using the provided wallet
910
984
  return yield wallet.executeSwap({
911
985
  input: params.amount,
912
986
  destinationAsset: params.destinationAsset,
@@ -918,19 +992,19 @@ class ThorchainAMM {
918
992
  });
919
993
  }
920
994
  /**
921
- * Wraps estimate from thorchain query
922
- * @param params - estimate add liquidity
923
- * @returns - Estimate add lp object
924
- */
995
+ * Wraps the estimate from ThorchainQuery for adding liquidity.
996
+ * @param params - The parameters for estimating adding liquidity.
997
+ * @returns - The estimated liquidity addition object.
998
+ */
925
999
  estimateAddLiquidity(params) {
926
1000
  return __awaiter(this, void 0, void 0, function* () {
927
1001
  return yield this.thorchainQuery.estimateAddLP(params);
928
1002
  });
929
1003
  }
930
1004
  /**
931
- * Wraps estimate withdraw from thorchain query
932
- * @param params - estimate withdraw liquidity
933
- * @returns - Estimate withdraw lp object
1005
+ * Wraps the estimate from ThorchainQuery for withdrawing liquidity.
1006
+ * @param params - The parameters for estimating withdrawing liquidity.
1007
+ * @returns - The estimated liquidity withdrawal object.
934
1008
  */
935
1009
  estimateWithdrawLiquidity(params) {
936
1010
  return __awaiter(this, void 0, void 0, function* () {
@@ -938,9 +1012,9 @@ class ThorchainAMM {
938
1012
  });
939
1013
  }
940
1014
  /**
941
- *
942
- * @param wallet - wallet class
943
- * @param params - liquidity parameters
1015
+ * If there is no existing liquidity position, it is created automatically.
1016
+ * @param wallet - Wallet class
1017
+ * @param params - Liquidity parameter
944
1018
  * @returns
945
1019
  */
946
1020
  addLiquidityPosition(wallet, params) {
@@ -958,14 +1032,14 @@ class ThorchainAMM {
958
1032
  });
959
1033
  }
960
1034
  /**
961
- *
962
- * @param params - liquidity parameters
963
- * @param wallet - wallet needed to perform tx
964
- * @return
1035
+ * Withdraws liquidity from a position.
1036
+ * @param params - The wallet to perform the transaction.
1037
+ * @param wallet - The parameters for withdrawing liquidity.
1038
+ * @return - The array of transaction submissions.
965
1039
  */
966
1040
  withdrawLiquidityPosition(wallet, params) {
967
1041
  return __awaiter(this, void 0, void 0, function* () {
968
- // Caution Dust Limits: BTC,BCH,LTC chains 10k sats; DOGE 1m Sats; ETH 0 wei; THOR 0 RUNE.
1042
+ // Caution Dust Limits: BTC, BCH, LTC chains: 10k sats; DOGE: 1m Sats; ETH: 0 wei; THOR: 0 RUNE.
969
1043
  const withdrawParams = yield this.thorchainQuery.estimateWithdrawLP(params);
970
1044
  return yield wallet.withdrawLiquidity({
971
1045
  assetFee: withdrawParams.inbound.fees.asset,
@@ -979,9 +1053,9 @@ class ThorchainAMM {
979
1053
  });
980
1054
  }
981
1055
  /**
982
- *
983
- * @param addAssetAmount
984
- * @returns
1056
+ * Estimates adding to a saver.
1057
+ * @param addAssetAmount The amount to add to the saver.
1058
+ * @returns The estimated addition to the saver object.
985
1059
  */
986
1060
  estimateAddSaver(addAssetAmount) {
987
1061
  return __awaiter(this, void 0, void 0, function* () {
@@ -989,9 +1063,9 @@ class ThorchainAMM {
989
1063
  });
990
1064
  }
991
1065
  /**
992
- *
993
- * @param withdrawParams
994
- * @returns
1066
+ * Estimates withdrawing from a saver.
1067
+ * @param withdrawParams The parameters for withdrawing from the saver.
1068
+ * @returns The estimated withdrawal from the saver object.
995
1069
  */
996
1070
  estimateWithdrawSaver(withdrawParams) {
997
1071
  return __awaiter(this, void 0, void 0, function* () {
@@ -999,9 +1073,9 @@ class ThorchainAMM {
999
1073
  });
1000
1074
  }
1001
1075
  /**
1002
- *
1003
- * @param getsaver
1004
- * @returns
1076
+ * Retrieves the position of a saver.
1077
+ * @param getsaver The parameters to retrieve the saver position.
1078
+ * @returns The saver position object.
1005
1079
  */
1006
1080
  getSaverPosition(getsaver) {
1007
1081
  return __awaiter(this, void 0, void 0, function* () {
@@ -1009,10 +1083,10 @@ class ThorchainAMM {
1009
1083
  });
1010
1084
  }
1011
1085
  /**
1012
- *
1086
+ * Adds assets to a saver.
1013
1087
  * @param wallet - wallet needed to execute tx
1014
- * @param addAssetAmount - asset amount being added to savers
1015
- * @returns - submitted tx
1088
+ * @param addAssetAmount - The amount to add to the saver.
1089
+ * @returns - The submitted transaction.
1016
1090
  */
1017
1091
  addSaver(wallet, addAssetAmount) {
1018
1092
  return __awaiter(this, void 0, void 0, function* () {
@@ -1023,10 +1097,10 @@ class ThorchainAMM {
1023
1097
  });
1024
1098
  }
1025
1099
  /**
1026
- *
1027
- * @param wallet - wallet to execute the transaction
1028
- * @param withdrawParams - params needed for withdraw
1029
- * @returns
1100
+ * Withdraws assets from a saver.
1101
+ * @param wallet - The wallet to perform the transaction.
1102
+ * @param withdrawParams - The parameters for withdrawing from the saver.
1103
+ * @returns The submitted transaction.
1030
1104
  */
1031
1105
  withdrawSaver(wallet, withdrawParams) {
1032
1106
  return __awaiter(this, void 0, void 0, function* () {
@@ -1037,9 +1111,9 @@ class ThorchainAMM {
1037
1111
  });
1038
1112
  }
1039
1113
  /**
1040
- *
1041
- * @param loanOpenParams
1042
- * @returns
1114
+ * Retrieves a quote for opening a loan.
1115
+ * @param loanOpenParams The parameters for opening the loan.
1116
+ * @returns The quote for opening the loan.
1043
1117
  */
1044
1118
  getLoanQuoteOpen(loanOpenParams) {
1045
1119
  return __awaiter(this, void 0, void 0, function* () {
@@ -1047,9 +1121,9 @@ class ThorchainAMM {
1047
1121
  });
1048
1122
  }
1049
1123
  /**
1050
- *
1051
- * @param loanCloseParams
1052
- * @returns
1124
+ * Retrieves a quote for closing a loan.
1125
+ * @param loanCloseParams The parameters for closing the loan.
1126
+ * @returns The quote for closing the loan.
1053
1127
  */
1054
1128
  getLoanQuoteClose(loanCloseParams) {
1055
1129
  return __awaiter(this, void 0, void 0, function* () {
@@ -1057,10 +1131,10 @@ class ThorchainAMM {
1057
1131
  });
1058
1132
  }
1059
1133
  /**
1060
- *
1061
- * @param wallet - wallet needed to execute transaction
1062
- * @param loanOpenParams - params needed to open the loan
1063
- * @returns - submitted tx
1134
+ * Opens a loan.
1135
+ * @param wallet - The wallet to perform the transaction.
1136
+ * @param loanOpenParams - The parameters for opening the loan.
1137
+ * @returns - The submitted transaction.
1064
1138
  */
1065
1139
  addLoan(wallet, loanOpenParams) {
1066
1140
  return __awaiter(this, void 0, void 0, function* () {
@@ -1075,16 +1149,19 @@ class ThorchainAMM {
1075
1149
  });
1076
1150
  }
1077
1151
  /**
1078
- *
1079
- * @param wallet - wallet to execute the transaction
1080
- * @param loanCloseParams - params needed for withdrawing the loan
1081
- * @returns
1152
+ * Withdraws assets from a loan.
1153
+ * @param wallet - The wallet to perform the transaction.
1154
+ * @param loanCloseParams - The parameters for withdrawing from the loan.
1155
+ * @returns The submitted transaction.
1082
1156
  */
1083
1157
  withdrawLoan(wallet, loanCloseParams) {
1084
1158
  return __awaiter(this, void 0, void 0, function* () {
1159
+ // Retrieves the quote for closing the loan
1085
1160
  const withdrawLoan = yield this.thorchainQuery.getLoanQuoteClose(loanCloseParams);
1161
+ // Checks if there are any errors in the quote
1086
1162
  if (withdrawLoan.errors.length > 0)
1087
1163
  throw Error(`${withdrawLoan.errors}`);
1164
+ // Executes the loan close transaction
1088
1165
  return yield wallet.loanClose({
1089
1166
  memo: `${withdrawLoan.memo}`,
1090
1167
  amount: loanCloseParams.amount,
@@ -1093,9 +1170,9 @@ class ThorchainAMM {
1093
1170
  });
1094
1171
  }
1095
1172
  /**
1096
- * Get all Thornames and its data associated owned by an address
1097
- * @param address - address
1098
- * @returns thornames data
1173
+ * Retrieves all Thornames and their associated data owned by an address.
1174
+ * @param address - The address to retrieve Thornames for.
1175
+ * @returns The Thornames data.
1099
1176
  */
1100
1177
  getThornamesByAddress(address) {
1101
1178
  return __awaiter(this, void 0, void 0, function* () {