@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.
@@ -25,6 +25,7 @@ export declare class Aggregator {
25
25
  * Do swap
26
26
  * @param {QuoteSwapParams & { protocol?: Protocol }} params Swap parameters. If protocol is not set,
27
27
  * estimateSwap will be call and swap will be done in protocol with the greatest expected amount
28
+ * Approve the tx if needed
28
29
  * @returns the swap with the greatest expected amount estimated in the supported protocols
29
30
  */
30
31
  doSwap(params: QuoteSwapParams & {
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { Aggregator } from './aggregator';
2
- export { IProtocol, QuoteSwapParams, QuoteSwap, TxSubmitted, SwapHistory, SwapHistoryParams, SwapResume, Config, } from './types';
2
+ export { IProtocol, QuoteSwapParams, QuoteSwap, TxSubmitted, SwapHistory, SwapHistoryParams, SwapResume, Config, ApproveParams, IsApprovedParams, } from './types';
package/lib/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { CachedValue, isSynthAsset, isTradeAsset, CryptoAmount, baseAmount, eqAsset, assetFromStringEx, assetToString } from '@xchainjs/xchain-util';
1
+ import { CachedValue, isSynthAsset, isTradeAsset, CryptoAmount, baseAmount, eqAsset, assetFromStringEx, assetToString, isTokenAsset } from '@xchainjs/xchain-util';
2
2
  import { AssetCacao, MAYAChain } from '@xchainjs/xchain-mayachain';
3
3
  import { MayachainAMM } from '@xchainjs/xchain-mayachain-amm';
4
4
  import { MayachainQuery } from '@xchainjs/xchain-mayachain-query';
@@ -25423,6 +25423,12 @@ class ChainflipProtocol {
25423
25423
  return this.sdk.getAssets();
25424
25424
  }, 24 * 60 * 60 * 1000);
25425
25425
  }
25426
+ approveRouterToSpend(_params) {
25427
+ throw new Error('Method not implemented.');
25428
+ }
25429
+ shouldBeApproved(_params) {
25430
+ throw new Error('Method not implemented.');
25431
+ }
25426
25432
  /**
25427
25433
  * Check if an asset is supported in the protocol
25428
25434
  * @param {Asset} asset Asset to check if it is supported
@@ -25593,6 +25599,33 @@ class MayachainProtocol {
25593
25599
  this.mayachainQuery = new MayachainQuery();
25594
25600
  this.mayachainAmm = new MayachainAMM(this.mayachainQuery, configuration === null || configuration === void 0 ? void 0 : configuration.wallet);
25595
25601
  this.configuration = configuration;
25602
+ this.wallet = configuration === null || configuration === void 0 ? void 0 : configuration.wallet;
25603
+ }
25604
+ /**
25605
+ * Aprove tx for ERC-20
25606
+ * @param {ApproveParams} approveParams params to approve tx
25607
+ * @returns {TxSubmitted} Transaction hash and URL of the swap
25608
+ */
25609
+ approveRouterToSpend(params) {
25610
+ var _a;
25611
+ return __awaiter(this, void 0, void 0, function* () {
25612
+ const { asset, amount } = params;
25613
+ const txSubmitted = yield this.mayachainAmm.approveRouterToSpend({ asset, amount });
25614
+ yield ((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.awaitTxConfirmed(asset.chain, txSubmitted.hash));
25615
+ return txSubmitted;
25616
+ });
25617
+ }
25618
+ /**
25619
+ * Check if tx is approved for ERC-20
25620
+ * @param {IsApprovedParams} isApprovedParams params to check if tx is approved
25621
+ * @returns {string[]} array of errors
25622
+ */
25623
+ shouldBeApproved(params) {
25624
+ return __awaiter(this, void 0, void 0, function* () {
25625
+ const { asset, amount, address } = params;
25626
+ const errors = yield this.mayachainAmm.isRouterApprovedToSpend({ asset, amount, address });
25627
+ return errors.some((error) => error === 'Maya router has not been approved to spend this amount');
25628
+ });
25596
25629
  }
25597
25630
  /**
25598
25631
  * Check if an asset is supported in the protocol
@@ -25681,6 +25714,33 @@ class ThorchainProtocol {
25681
25714
  this.thorchainQuery = new ThorchainQuery();
25682
25715
  this.thorchainAmm = new ThorchainAMM(this.thorchainQuery, configuration === null || configuration === void 0 ? void 0 : configuration.wallet);
25683
25716
  this.configuration = configuration;
25717
+ this.wallet = configuration === null || configuration === void 0 ? void 0 : configuration.wallet;
25718
+ }
25719
+ /**
25720
+ * Aprove tx for ERC-20 and wait until tx is confirmed
25721
+ * @param {ApproveParams} approveParams params to approve tx
25722
+ * @returns {TxSubmitted} Transaction hash and URL of the swap
25723
+ */
25724
+ approveRouterToSpend(params) {
25725
+ var _a;
25726
+ return __awaiter(this, void 0, void 0, function* () {
25727
+ const { asset, amount } = params;
25728
+ const txSubmitted = yield this.thorchainAmm.approveRouterToSpend({ asset, amount });
25729
+ yield ((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.awaitTxConfirmed(asset.chain, txSubmitted.hash));
25730
+ return txSubmitted;
25731
+ });
25732
+ }
25733
+ /**
25734
+ * Check if tx should be approved for ERC-20
25735
+ * @param {IsApprovedParams} isApprovedParams params to check if tx is approved
25736
+ * @returns {boolean} array of errors
25737
+ */
25738
+ shouldBeApproved(params) {
25739
+ return __awaiter(this, void 0, void 0, function* () {
25740
+ const { asset, amount, address } = params;
25741
+ const errors = yield this.thorchainAmm.isRouterApprovedToSpend({ asset, amount, address });
25742
+ return errors.some((error) => error === 'Thorchain router has not been approved to spend this amount');
25743
+ });
25684
25744
  }
25685
25745
  /**
25686
25746
  * Check if an asset is supported in the protocol
@@ -25843,14 +25903,25 @@ class Aggregator {
25843
25903
  * Do swap
25844
25904
  * @param {QuoteSwapParams & { protocol?: Protocol }} params Swap parameters. If protocol is not set,
25845
25905
  * estimateSwap will be call and swap will be done in protocol with the greatest expected amount
25906
+ * Approve the tx if needed
25846
25907
  * @returns the swap with the greatest expected amount estimated in the supported protocols
25847
25908
  */
25848
25909
  doSwap(params) {
25849
25910
  return __awaiter(this, void 0, void 0, function* () {
25850
25911
  const protocolName = params.protocol ? params.protocol : (yield this.estimateSwap(params)).protocol;
25851
25912
  const protocol = this.protocols.find((protocol) => protocol.name === protocolName);
25852
- if (!protocol)
25913
+ if (!protocol) {
25853
25914
  throw Error(`${protocolName} protocol is not supported`);
25915
+ }
25916
+ if (isTokenAsset(params.fromAsset)) {
25917
+ if (yield protocol.shouldBeApproved({
25918
+ asset: params.fromAsset,
25919
+ amount: params.amount,
25920
+ address: params.fromAddress || '',
25921
+ })) {
25922
+ yield protocol.approveRouterToSpend({ asset: params.fromAsset, amount: params.amount });
25923
+ }
25924
+ }
25854
25925
  return protocol.doSwap(params);
25855
25926
  });
25856
25927
  }