@xchainjs/xchain-aggregator 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -25427,6 +25427,12 @@ class ChainflipProtocol {
25427
25427
  return this.sdk.getAssets();
25428
25428
  }, 24 * 60 * 60 * 1000);
25429
25429
  }
25430
+ approveRouterToSpend(_params) {
25431
+ throw new Error('Method not implemented.');
25432
+ }
25433
+ shouldBeApproved(_params) {
25434
+ throw new Error('Method not implemented.');
25435
+ }
25430
25436
  /**
25431
25437
  * Check if an asset is supported in the protocol
25432
25438
  * @param {Asset} asset Asset to check if it is supported
@@ -25597,6 +25603,33 @@ class MayachainProtocol {
25597
25603
  this.mayachainQuery = new xchainMayachainQuery.MayachainQuery();
25598
25604
  this.mayachainAmm = new xchainMayachainAmm.MayachainAMM(this.mayachainQuery, configuration === null || configuration === void 0 ? void 0 : configuration.wallet);
25599
25605
  this.configuration = configuration;
25606
+ this.wallet = configuration === null || configuration === void 0 ? void 0 : configuration.wallet;
25607
+ }
25608
+ /**
25609
+ * Aprove tx for ERC-20
25610
+ * @param {ApproveParams} approveParams params to approve tx
25611
+ * @returns {TxSubmitted} Transaction hash and URL of the swap
25612
+ */
25613
+ approveRouterToSpend(params) {
25614
+ var _a;
25615
+ return __awaiter(this, void 0, void 0, function* () {
25616
+ const { asset, amount } = params;
25617
+ const txSubmitted = yield this.mayachainAmm.approveRouterToSpend({ asset, amount });
25618
+ yield ((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.awaitTxConfirmed(asset.chain, txSubmitted.hash));
25619
+ return txSubmitted;
25620
+ });
25621
+ }
25622
+ /**
25623
+ * Check if tx is approved for ERC-20
25624
+ * @param {IsApprovedParams} isApprovedParams params to check if tx is approved
25625
+ * @returns {string[]} array of errors
25626
+ */
25627
+ shouldBeApproved(params) {
25628
+ return __awaiter(this, void 0, void 0, function* () {
25629
+ const { asset, amount, address } = params;
25630
+ const errors = yield this.mayachainAmm.isRouterApprovedToSpend({ asset, amount, address });
25631
+ return errors.some((error) => error === 'Maya router has not been approved to spend this amount');
25632
+ });
25600
25633
  }
25601
25634
  /**
25602
25635
  * Check if an asset is supported in the protocol
@@ -25685,6 +25718,33 @@ class ThorchainProtocol {
25685
25718
  this.thorchainQuery = new xchainThorchainQuery.ThorchainQuery();
25686
25719
  this.thorchainAmm = new xchainThorchainAmm.ThorchainAMM(this.thorchainQuery, configuration === null || configuration === void 0 ? void 0 : configuration.wallet);
25687
25720
  this.configuration = configuration;
25721
+ this.wallet = configuration === null || configuration === void 0 ? void 0 : configuration.wallet;
25722
+ }
25723
+ /**
25724
+ * Aprove tx for ERC-20 and wait until tx is confirmed
25725
+ * @param {ApproveParams} approveParams params to approve tx
25726
+ * @returns {TxSubmitted} Transaction hash and URL of the swap
25727
+ */
25728
+ approveRouterToSpend(params) {
25729
+ var _a;
25730
+ return __awaiter(this, void 0, void 0, function* () {
25731
+ const { asset, amount } = params;
25732
+ const txSubmitted = yield this.thorchainAmm.approveRouterToSpend({ asset, amount });
25733
+ yield ((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.awaitTxConfirmed(asset.chain, txSubmitted.hash));
25734
+ return txSubmitted;
25735
+ });
25736
+ }
25737
+ /**
25738
+ * Check if tx should be approved for ERC-20
25739
+ * @param {IsApprovedParams} isApprovedParams params to check if tx is approved
25740
+ * @returns {boolean} array of errors
25741
+ */
25742
+ shouldBeApproved(params) {
25743
+ return __awaiter(this, void 0, void 0, function* () {
25744
+ const { asset, amount, address } = params;
25745
+ const errors = yield this.thorchainAmm.isRouterApprovedToSpend({ asset, amount, address });
25746
+ return errors.some((error) => error === 'Thorchain router has not been approved to spend this amount');
25747
+ });
25688
25748
  }
25689
25749
  /**
25690
25750
  * Check if an asset is supported in the protocol
@@ -25847,14 +25907,25 @@ class Aggregator {
25847
25907
  * Do swap
25848
25908
  * @param {QuoteSwapParams & { protocol?: Protocol }} params Swap parameters. If protocol is not set,
25849
25909
  * estimateSwap will be call and swap will be done in protocol with the greatest expected amount
25910
+ * Approve the tx if needed
25850
25911
  * @returns the swap with the greatest expected amount estimated in the supported protocols
25851
25912
  */
25852
25913
  doSwap(params) {
25853
25914
  return __awaiter(this, void 0, void 0, function* () {
25854
25915
  const protocolName = params.protocol ? params.protocol : (yield this.estimateSwap(params)).protocol;
25855
25916
  const protocol = this.protocols.find((protocol) => protocol.name === protocolName);
25856
- if (!protocol)
25917
+ if (!protocol) {
25857
25918
  throw Error(`${protocolName} protocol is not supported`);
25919
+ }
25920
+ if (xchainUtil.isTokenAsset(params.fromAsset)) {
25921
+ if (yield protocol.shouldBeApproved({
25922
+ asset: params.fromAsset,
25923
+ amount: params.amount,
25924
+ address: params.fromAddress || '',
25925
+ })) {
25926
+ yield protocol.approveRouterToSpend({ asset: params.fromAsset, amount: params.amount });
25927
+ }
25928
+ }
25858
25929
  return protocol.doSwap(params);
25859
25930
  });
25860
25931
  }