@paraspell/swap 14.5.2 → 14.5.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/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createChainClient as createChainClient$1, getPaymentInfo, registerSwapExtension } from '@paraspell/sdk';
2
- import { UnsupportedOperationError, assertCurrencyCore, dryRun, getDryRunError, DryRunFailedError, isConfig, getNativeAssets, RoutingResolutionError, findAssetInfoById, getOtherAssets, getChainProviders, parseUnits, getNativeAssetSymbol, padValueBy, formatUnits as formatUnits$1, AmountTooLowError, getBalance, localizeLocation, getAssets, Parents, isSymbolMatch, getParaId, UnableToComputeError, findNativeAssetInfoOrThrow, InvalidCurrencyError, findAssetInfoOrThrow, findAssetInfoByLoc, isSymbolSpecifier, findBestMatches, findAssetInfoBySymbol, getSupportedAssetsImpl, isAssetEqual, findAssetInfoOnDestImpl, normalizeExchange, EXCHANGE_CHAINS, deepEqual, reverseTransformLocation, getAssetsImpl, ScenarioNotSupportedError, applyDecimalAbstraction, findAssetInfo, replaceBigInt, Builder, isChainEvm, convertBuilderConfig, handleSwapExecuteTransfer, getXcmFee, getOriginXcmFee as getOriginXcmFee$1, InvalidAddressError, MissingParameterError, BatchMode, createChainClient as createChainClient$2, getExistentialDeposit, buildHopInfo, getRelayChainSymbol, aggregateHopFees, buildDestInfo, buildOriginInfo } from '@paraspell/sdk-core';
2
+ import { UnsupportedOperationError, assertCurrencyCore, dryRun, getDryRunError, DryRunFailedError, isConfig, getNativeAssets, RoutingResolutionError, findAssetInfoById, getOtherAssets, getChainProviders, parseUnits, getNativeAssetSymbol, padValueBy, formatUnits as formatUnits$1, AmountTooLowError, getBalance, localizeLocation, getAssets, isAssetEqual, findNativeAssetInfoOrThrow, isSymbolMatch, getParaId, UnableToComputeError, InvalidCurrencyError, findAssetInfoOrThrow, findAssetInfoByLoc, isSymbolSpecifier, findBestMatches, findAssetInfoBySymbol, getSupportedAssetsImpl, findAssetInfoOnDestImpl, normalizeExchange, EXCHANGE_CHAINS, deepEqual, reverseTransformLocation, getAssetsImpl, ScenarioNotSupportedError, applyDecimalAbstraction, findAssetInfo, replaceBigInt, Builder, isChainEvm, convertBuilderConfig, handleSwapExecuteTransfer, getXcmFee, getOriginXcmFee as getOriginXcmFee$1, InvalidAddressError, MissingParameterError, BatchMode, createChainClient as createChainClient$2, getExistentialDeposit, buildHopInfo, getRelayChainSymbol, aggregateHopFees, buildDestInfo, buildOriginInfo } from '@paraspell/sdk-core';
3
3
  import { toHex } from 'polkadot-api/utils';
4
4
  import 'polkadot-api';
5
5
  import { Wallet } from '@acala-network/sdk';
@@ -12,10 +12,8 @@ import { WsProvider, ApiPromise } from '@polkadot/api';
12
12
  import { lowercaseFirstLetter, snakeToCamel } from '@paraspell/sdk-common';
13
13
  import { Amount, Token, getCurrencyCombinations } from '@crypto-dex-sdk/currency';
14
14
  import { Percent } from '@crypto-dex-sdk/math';
15
- import { PAIR_ADDRESSES, isNativeCurrency, addressToNodeCurrency, SwapRouter } from '@crypto-dex-sdk/parachains-bifrost';
15
+ import { getPairs, uniqePairKey, PAIR_ADDRESSES, isNativeCurrency, addressToNodeCurrency, SwapRouter } from '@crypto-dex-sdk/parachains-bifrost';
16
16
  import { Trade, Pair } from '@crypto-dex-sdk/amm';
17
- import { ParachainId } from '@crypto-dex-sdk/chain';
18
- import { addressToZenlinkAssetId } from '@crypto-dex-sdk/format';
19
17
  import { DEFULT_TOKEN_LIST_MAP, WrappedTokenInfo } from '@crypto-dex-sdk/token-lists';
20
18
  import { createSdkContext } from '@galacticcouncil/sdk-next';
21
19
  import { parseUnits as parseUnits$1, ethers } from 'ethers-v6';
@@ -2110,6 +2108,22 @@ var Hydration = {
2110
2108
  ]
2111
2109
  }
2112
2110
  },
2111
+ {
2112
+ parents: 1,
2113
+ interior: {
2114
+ X2: [
2115
+ {
2116
+ Parachain: 2034
2117
+ },
2118
+ {
2119
+ AccountKey20: {
2120
+ network: null,
2121
+ key: "0xb94025f14fdc01f8e3b0c34311bcbae0febd1a4a"
2122
+ }
2123
+ }
2124
+ ]
2125
+ }
2126
+ },
2113
2127
  {
2114
2128
  parents: 1,
2115
2129
  interior: {
@@ -2126,6 +2140,22 @@ var Hydration = {
2126
2140
  ]
2127
2141
  }
2128
2142
  },
2143
+ {
2144
+ parents: 1,
2145
+ interior: {
2146
+ X2: [
2147
+ {
2148
+ Parachain: 2034
2149
+ },
2150
+ {
2151
+ AccountKey20: {
2152
+ network: null,
2153
+ key: "0x3a31ff0a1e07da4158677ce861980cdf2e316b78"
2154
+ }
2155
+ }
2156
+ ]
2157
+ }
2158
+ },
2129
2159
  {
2130
2160
  parents: 2,
2131
2161
  interior: {
@@ -2691,6 +2721,31 @@ var Hydration = {
2691
2721
  ]
2692
2722
  }
2693
2723
  },
2724
+ {
2725
+ parents: 1,
2726
+ interior: {
2727
+ X4: [
2728
+ {
2729
+ Parachain: 2034
2730
+ },
2731
+ {
2732
+ GeneralKey: {
2733
+ length: 2,
2734
+ data: "0x7768000000000000000000000000000000000000000000000000000000000000"
2735
+ }
2736
+ },
2737
+ {
2738
+ GeneralIndex: 15
2739
+ },
2740
+ {
2741
+ GeneralKey: {
2742
+ length: 32,
2743
+ data: "0xb55c490bafb82aeb4b950fa479341c1b5fbfa814f8253b6acdf8426b7cd9d3c0"
2744
+ }
2745
+ }
2746
+ ]
2747
+ }
2748
+ },
2694
2749
  {
2695
2750
  parents: 2,
2696
2751
  interior: {
@@ -7854,6 +7909,7 @@ var Logger = {
7854
7909
  var ExchangeChain = /*#__PURE__*/function () {
7855
7910
  function ExchangeChain(chain) {
7856
7911
  _classCallCheck(this, ExchangeChain);
7912
+ this.supportsRecipient = false;
7857
7913
  this._chain = chain;
7858
7914
  }
7859
7915
  return _createClass(ExchangeChain, [{
@@ -7862,28 +7918,31 @@ var ExchangeChain = /*#__PURE__*/function () {
7862
7918
  return this._chain;
7863
7919
  }
7864
7920
  }, {
7865
- key: "handleMultiSwap",
7921
+ key: "swapCurrency",
7866
7922
  value: function () {
7867
- var _handleMultiSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
7868
- var singleSwapResult;
7923
+ var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
7924
+ var sender, destination, recipient;
7869
7925
  return _regenerator().w(function (_context) {
7870
7926
  while (1) switch (_context.n) {
7871
7927
  case 0:
7872
- _context.n = 1;
7873
- return this.swapCurrency(options, toDestTransactionFee);
7928
+ sender = options.sender, destination = options.destination;
7929
+ recipient = destination ? sender : options.recipient;
7930
+ if (!(!this.supportsRecipient && recipient !== sender)) {
7931
+ _context.n = 1;
7932
+ break;
7933
+ }
7934
+ throw new UnsupportedOperationError("".concat(this.chain, " cannot deposit swapped tokens to a different recipient."));
7874
7935
  case 1:
7875
- singleSwapResult = _context.v;
7876
- return _context.a(2, {
7877
- txs: [singleSwapResult.tx],
7878
- amountOut: singleSwapResult.amountOut
7879
- });
7936
+ return _context.a(2, this.buildSwap(_objectSpread2(_objectSpread2({}, options), {}, {
7937
+ recipient: recipient
7938
+ }), toDestTransactionFee));
7880
7939
  }
7881
7940
  }, _callee, this);
7882
7941
  }));
7883
- function handleMultiSwap(_x, _x2) {
7884
- return _handleMultiSwap.apply(this, arguments);
7942
+ function swapCurrency(_x, _x2) {
7943
+ return _swapCurrency.apply(this, arguments);
7885
7944
  }
7886
- return handleMultiSwap;
7945
+ return swapCurrency;
7887
7946
  }()
7888
7947
  }, {
7889
7948
  key: "createApiInstance",
@@ -8116,9 +8175,9 @@ var AcalaExchange = /*#__PURE__*/function (_ExchangeChain) {
8116
8175
  }
8117
8176
  _inherits(AcalaExchange, _ExchangeChain);
8118
8177
  return _createClass(AcalaExchange, [{
8119
- key: "swapCurrency",
8178
+ key: "buildSwap",
8120
8179
  value: function () {
8121
- var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
8180
+ var _buildSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
8122
8181
  var api, apiPjs, assetFrom, assetTo, amount, sender, origin, slippagePct, isForFeeEstimation, wallet, fromToken, toToken, acalaDex, dex, swapFee, totalNativeCurrencyFee, balance, pctDestFee, amountWithoutFee, tradeResult, tx, amountOutRes, amountOut, nativeAssetSymbol, amountOutWithFee;
8123
8182
  return _regenerator().w(function (_context) {
8124
8183
  while (1) switch (_context.n) {
@@ -8188,7 +8247,7 @@ var AcalaExchange = /*#__PURE__*/function (_ExchangeChain) {
8188
8247
  _context.n = 7;
8189
8248
  break;
8190
8249
  }
8191
- amountOutWithFee = padValueBy(amountOut - toDestTransactionFee, FEE_BUFFER_PCT);
8250
+ amountOutWithFee = amountOut - padValueBy(toDestTransactionFee, FEE_BUFFER_PCT);
8192
8251
  Logger.log('Amount out with fee:', amountOutWithFee);
8193
8252
  Logger.log('Amount out decimals', toToken.decimals);
8194
8253
  return _context.a(2, {
@@ -8205,10 +8264,10 @@ var AcalaExchange = /*#__PURE__*/function (_ExchangeChain) {
8205
8264
  }
8206
8265
  }, _callee, this);
8207
8266
  }));
8208
- function swapCurrency(_x, _x2) {
8209
- return _swapCurrency.apply(this, arguments);
8267
+ function buildSwap(_x, _x2) {
8268
+ return _buildSwap.apply(this, arguments);
8210
8269
  }
8211
- return swapCurrency;
8270
+ return buildSwap;
8212
8271
  }()
8213
8272
  }, {
8214
8273
  key: "getAmountOut",
@@ -8380,6 +8439,7 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8380
8439
  _classCallCheck(this, AssetHubExchange);
8381
8440
  _this = _callSuper(this, AssetHubExchange, arguments);
8382
8441
  _this.apiType = 'GENERIC';
8442
+ _this.supportsRecipient = true;
8383
8443
  return _this;
8384
8444
  }
8385
8445
  _inherits(AssetHubExchange, _ExchangeChain);
@@ -8430,225 +8490,148 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8430
8490
  return quoteOrThrow;
8431
8491
  }()
8432
8492
  }, {
8433
- key: "swapCurrency",
8493
+ key: "quoteSwapOrThrow",
8434
8494
  value: function () {
8435
- var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options, toDestTxFee) {
8436
- var api, assetFrom, assetTo, amount, sender, slippagePct, origin, localizedFrom, localizedTo, pctDestFee, amountWithoutFee, amountOut, slippageMultiplier, minAmountOut, tx, toDestFeeCurrencyTo, finalAmountOut, _t2;
8495
+ var _quoteSwapOrThrow = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(api, assetFrom, assetTo, nativeAsset, amount) {
8496
+ var from, to, _native, nativeAmountOut, _t2, _t3, _t4, _t5;
8437
8497
  return _regenerator().w(function (_context2) {
8438
8498
  while (1) switch (_context2.n) {
8439
8499
  case 0:
8440
- api = options.api, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, sender = options.sender, slippagePct = options.slippagePct, origin = options.origin;
8441
- localizedFrom = localizeLocation(this.chain, assetFrom.location);
8442
- localizedTo = localizeLocation(this.chain, assetTo.location);
8443
- pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
8444
- amountWithoutFee = padValueBy(amount, pctDestFee);
8445
- _context2.n = 1;
8446
- return this.quoteOrThrow(api, localizedFrom, localizedTo, amountWithoutFee);
8500
+ if (!isAssetEqual(assetFrom, assetTo)) {
8501
+ _context2.n = 1;
8502
+ break;
8503
+ }
8504
+ throw new RoutingResolutionError('Cannot swap asset to itself.');
8447
8505
  case 1:
8448
- amountOut = _context2.v;
8449
- slippageMultiplier = Number(slippagePct);
8450
- minAmountOut = padValueBy(amountOut, -slippageMultiplier);
8451
- tx = api.deserializeExtrinsics({
8452
- module: 'AssetConversion',
8453
- method: 'swap_exact_tokens_for_tokens',
8454
- params: {
8455
- path: [localizedFrom, localizedTo],
8456
- amount_in: amountWithoutFee,
8457
- amount_out_min: minAmountOut,
8458
- send_to: sender,
8459
- keep_alive: !!assetFrom.isNative
8460
- }
8461
- });
8462
- if (!(toDestTxFee === 0n || assetTo.symbol == getNativeAssetSymbol(this.chain))) {
8463
- _context2.n = 2;
8506
+ from = localizeLocation(this.chain, assetFrom.location);
8507
+ to = localizeLocation(this.chain, assetTo.location);
8508
+ if (!(isAssetEqual(assetFrom, nativeAsset) || isAssetEqual(assetTo, nativeAsset))) {
8509
+ _context2.n = 3;
8464
8510
  break;
8465
8511
  }
8466
- _t2 = toDestTxFee;
8467
- _context2.n = 4;
8468
- break;
8512
+ _t2 = [from, to];
8513
+ _context2.n = 2;
8514
+ return this.quoteOrThrow(api, from, to, amount);
8469
8515
  case 2:
8470
- _context2.n = 3;
8471
- return this.quoteOrThrow(api, {
8472
- parents: Parents.ONE,
8473
- interior: {
8474
- Here: null
8475
- }
8476
- }, localizedTo, toDestTxFee, true);
8516
+ _t3 = _context2.v;
8517
+ return _context2.a(2, {
8518
+ path: _t2,
8519
+ amountOut: _t3
8520
+ });
8477
8521
  case 3:
8478
- _t2 = _context2.v;
8522
+ _native = localizeLocation(this.chain, nativeAsset.location);
8523
+ _context2.n = 4;
8524
+ return this.quoteOrThrow(api, from, _native, amount);
8479
8525
  case 4:
8480
- toDestFeeCurrencyTo = _t2;
8481
- finalAmountOut = amountOut - padValueBy(toDestFeeCurrencyTo, FEE_BUFFER_PCT);
8482
- if (!(finalAmountOut <= 0n)) {
8526
+ nativeAmountOut = _context2.v;
8527
+ if (!(nativeAmountOut <= 0n)) {
8483
8528
  _context2.n = 5;
8484
8529
  break;
8485
8530
  }
8486
- throw new AmountTooLowError();
8531
+ throw new AmountTooLowError("First hop (".concat(assetFrom.symbol, " -> ").concat(nativeAsset.symbol, ") resulted in zero or negative output."));
8487
8532
  case 5:
8533
+ _t4 = [from, _native, to];
8534
+ _context2.n = 6;
8535
+ return this.quoteOrThrow(api, _native, to, nativeAmountOut);
8536
+ case 6:
8537
+ _t5 = _context2.v;
8488
8538
  return _context2.a(2, {
8489
- tx: tx,
8490
- amountOut: finalAmountOut
8539
+ path: _t4,
8540
+ amountOut: _t5
8491
8541
  });
8492
8542
  }
8493
8543
  }, _callee2, this);
8494
8544
  }));
8495
- function swapCurrency(_x5, _x6) {
8496
- return _swapCurrency.apply(this, arguments);
8545
+ function quoteSwapOrThrow(_x5, _x6, _x7, _x8, _x9) {
8546
+ return _quoteSwapOrThrow.apply(this, arguments);
8497
8547
  }
8498
- return swapCurrency;
8548
+ return quoteSwapOrThrow;
8499
8549
  }()
8500
8550
  }, {
8501
- key: "handleMultiSwap",
8551
+ key: "buildSwap",
8502
8552
  value: function () {
8503
- var _handleMultiSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(options, toDestTransactionFee) {
8504
- var assetFrom, assetTo, nativeAsset, isAssetFromNative, isAssetToNative, singleSwapResult, optionsHop1, resultHop1, hop1Received, assumedInputForHop2, optionsHop2, resultHop2;
8553
+ var _buildSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(options, toDestTxFee) {
8554
+ var api, assetFrom, assetTo, amount, recipient, slippagePct, origin, nativeAsset, pctDestFee, amountWithoutFee, _yield$this$quoteSwap, path, amountOut, slippageMultiplier, minAmountOut, tx, toDestFeeCurrencyTo, finalAmountOut, _t6;
8505
8555
  return _regenerator().w(function (_context3) {
8506
8556
  while (1) switch (_context3.n) {
8507
8557
  case 0:
8508
- assetFrom = options.assetFrom, assetTo = options.assetTo;
8509
- nativeAsset = getExchangeAsset(this.chain, {
8510
- symbol: getNativeAssetSymbol(this.chain)
8511
- });
8512
- if (nativeAsset) {
8513
- _context3.n = 1;
8514
- break;
8515
- }
8516
- throw new RoutingResolutionError('Native asset not found for this exchange chain.');
8558
+ api = options.api, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, recipient = options.recipient, slippagePct = options.slippagePct, origin = options.origin;
8559
+ nativeAsset = findNativeAssetInfoOrThrow(this.chain);
8560
+ pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
8561
+ amountWithoutFee = padValueBy(amount, pctDestFee);
8562
+ _context3.n = 1;
8563
+ return this.quoteSwapOrThrow(api, assetFrom, assetTo, nativeAsset, amountWithoutFee);
8517
8564
  case 1:
8518
- isAssetFromNative = assetFrom.symbol === nativeAsset.symbol;
8519
- isAssetToNative = assetTo.symbol === nativeAsset.symbol;
8520
- if (!(isAssetFromNative && isAssetToNative)) {
8565
+ _yield$this$quoteSwap = _context3.v;
8566
+ path = _yield$this$quoteSwap.path;
8567
+ amountOut = _yield$this$quoteSwap.amountOut;
8568
+ slippageMultiplier = Number(slippagePct);
8569
+ minAmountOut = padValueBy(amountOut, -slippageMultiplier);
8570
+ tx = api.deserializeExtrinsics({
8571
+ module: 'AssetConversion',
8572
+ method: 'swap_exact_tokens_for_tokens',
8573
+ params: {
8574
+ path: path,
8575
+ amount_in: amountWithoutFee,
8576
+ amount_out_min: minAmountOut,
8577
+ send_to: recipient,
8578
+ keep_alive: !!assetFrom.isNative
8579
+ }
8580
+ });
8581
+ if (!(toDestTxFee === 0n || isAssetEqual(assetTo, nativeAsset))) {
8521
8582
  _context3.n = 2;
8522
8583
  break;
8523
8584
  }
8524
- throw new RoutingResolutionError('Cannot swap native asset to itself.');
8585
+ _t6 = toDestTxFee;
8586
+ _context3.n = 4;
8587
+ break;
8525
8588
  case 2:
8526
- if (!(isAssetFromNative || isAssetToNative)) {
8527
- _context3.n = 4;
8528
- break;
8529
- }
8530
8589
  _context3.n = 3;
8531
- return this.swapCurrency(options, toDestTransactionFee);
8590
+ return this.quoteOrThrow(api, localizeLocation(this.chain, nativeAsset.location), localizeLocation(this.chain, assetTo.location), toDestTxFee, true);
8532
8591
  case 3:
8533
- singleSwapResult = _context3.v;
8534
- return _context3.a(2, {
8535
- txs: [singleSwapResult.tx],
8536
- amountOut: singleSwapResult.amountOut
8537
- });
8592
+ _t6 = _context3.v;
8538
8593
  case 4:
8539
- // Multi-hop: AssetA -> Native -> AssetB
8540
- optionsHop1 = _objectSpread2(_objectSpread2({}, options), {}, {
8541
- assetTo: nativeAsset
8542
- });
8543
- _context3.n = 5;
8544
- return this.swapCurrency(optionsHop1, 0n);
8545
- case 5:
8546
- resultHop1 = _context3.v;
8547
- if (!(resultHop1.amountOut <= 0n)) {
8548
- _context3.n = 6;
8549
- break;
8550
- }
8551
- throw new AmountTooLowError("First hop (".concat(assetFrom.symbol, " -> ").concat(nativeAsset.symbol, ") resulted in zero or negative output."));
8552
- case 6:
8553
- hop1Received = resultHop1.amountOut;
8554
- assumedInputForHop2 = padValueBy(hop1Received, -2);
8555
- optionsHop2 = {
8556
- apiType: 'GENERIC',
8557
- api: options.api,
8558
- slippagePct: options.slippagePct,
8559
- sender: options.sender,
8560
- origin: undefined,
8561
- assetFrom: nativeAsset,
8562
- assetTo: assetTo,
8563
- amount: assumedInputForHop2,
8564
- feeCalcAddress: options.feeCalcAddress
8565
- };
8566
- _context3.n = 7;
8567
- return this.swapCurrency(optionsHop2, toDestTransactionFee);
8568
- case 7:
8569
- resultHop2 = _context3.v;
8570
- if (!(resultHop2.amountOut <= 0n)) {
8571
- _context3.n = 8;
8594
+ toDestFeeCurrencyTo = _t6;
8595
+ finalAmountOut = amountOut - padValueBy(toDestFeeCurrencyTo, FEE_BUFFER_PCT);
8596
+ if (!(finalAmountOut <= 0n)) {
8597
+ _context3.n = 5;
8572
8598
  break;
8573
8599
  }
8574
- throw new AmountTooLowError("Second hop (".concat(nativeAsset.symbol, " -> ").concat(assetTo.symbol, ") resulted in zero or negative output."));
8575
- case 8:
8600
+ throw new AmountTooLowError();
8601
+ case 5:
8576
8602
  return _context3.a(2, {
8577
- txs: [resultHop1.tx, resultHop2.tx],
8578
- amountOut: resultHop2.amountOut
8603
+ tx: tx,
8604
+ amountOut: finalAmountOut
8579
8605
  });
8580
- case 9:
8581
- return _context3.a(2);
8582
8606
  }
8583
8607
  }, _callee3, this);
8584
8608
  }));
8585
- function handleMultiSwap(_x7, _x8) {
8586
- return _handleMultiSwap.apply(this, arguments);
8609
+ function buildSwap(_x0, _x1) {
8610
+ return _buildSwap.apply(this, arguments);
8587
8611
  }
8588
- return handleMultiSwap;
8612
+ return buildSwap;
8589
8613
  }()
8590
8614
  }, {
8591
8615
  key: "getAmountOut",
8592
8616
  value: function () {
8593
8617
  var _getAmountOut = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(options) {
8594
- var api, assetFrom, assetTo, amount, origin, nativeAsset, isAssetFromNative, isAssetToNative, pctDestFee, amountWithoutFee, hop1AmountOut, assumedInputForHop2, finalAmountOut;
8618
+ var api, assetFrom, assetTo, amount, origin, _options$slippagePct, slippagePct, pctDestFee, amountWithoutFee, _yield$this$quoteSwap2, amountOut;
8595
8619
  return _regenerator().w(function (_context4) {
8596
8620
  while (1) switch (_context4.n) {
8597
8621
  case 0:
8598
- api = options.api, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, origin = options.origin;
8599
- nativeAsset = getExchangeAsset(this.chain, {
8600
- symbol: getNativeAssetSymbol(this.chain)
8601
- });
8602
- if (nativeAsset) {
8603
- _context4.n = 1;
8604
- break;
8605
- }
8606
- throw new RoutingResolutionError('Native asset not found for this exchange chain.');
8607
- case 1:
8608
- isAssetFromNative = assetFrom.symbol === nativeAsset.symbol;
8609
- isAssetToNative = assetTo.symbol === nativeAsset.symbol;
8610
- if (!(isAssetFromNative && isAssetToNative)) {
8611
- _context4.n = 2;
8612
- break;
8613
- }
8614
- throw new RoutingResolutionError('Cannot swap native asset to itself.');
8615
- case 2:
8622
+ api = options.api, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, origin = options.origin, _options$slippagePct = options.slippagePct, slippagePct = _options$slippagePct === void 0 ? '0' : _options$slippagePct;
8616
8623
  pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
8617
8624
  amountWithoutFee = padValueBy(amount, pctDestFee);
8618
- if (!(isAssetFromNative || isAssetToNative)) {
8619
- _context4.n = 3;
8620
- break;
8621
- }
8622
- return _context4.a(2, this.quoteOrThrow(api, localizeLocation(this.chain, assetFrom.location), localizeLocation(this.chain, assetTo.location), amountWithoutFee));
8623
- case 3:
8624
- _context4.n = 4;
8625
- return this.quoteOrThrow(api, localizeLocation(this.chain, assetFrom.location), localizeLocation(this.chain, nativeAsset.location), amountWithoutFee);
8626
- case 4:
8627
- hop1AmountOut = _context4.v;
8628
- if (!(hop1AmountOut <= 0n)) {
8629
- _context4.n = 5;
8630
- break;
8631
- }
8632
- throw new AmountTooLowError("First hop (".concat(assetFrom.symbol, " -> ").concat(nativeAsset.symbol, ") resulted in zero or negative output."));
8633
- case 5:
8634
- assumedInputForHop2 = padValueBy(hop1AmountOut, -2);
8635
- _context4.n = 6;
8636
- return this.quoteOrThrow(api, localizeLocation(this.chain, nativeAsset.location), localizeLocation(this.chain, assetTo.location), assumedInputForHop2);
8637
- case 6:
8638
- finalAmountOut = _context4.v;
8639
- if (!(finalAmountOut <= 0n)) {
8640
- _context4.n = 7;
8641
- break;
8642
- }
8643
- throw new AmountTooLowError("Second hop (".concat(nativeAsset.symbol, " -> ").concat(assetTo.symbol, ") resulted in zero or negative output."));
8644
- case 7:
8645
- return _context4.a(2, finalAmountOut);
8646
- case 8:
8647
- return _context4.a(2);
8625
+ _context4.n = 1;
8626
+ return this.quoteSwapOrThrow(api, assetFrom, assetTo, findNativeAssetInfoOrThrow(this.chain), amountWithoutFee);
8627
+ case 1:
8628
+ _yield$this$quoteSwap2 = _context4.v;
8629
+ amountOut = _yield$this$quoteSwap2.amountOut;
8630
+ return _context4.a(2, padValueBy(amountOut, -Number(slippagePct)));
8648
8631
  }
8649
8632
  }, _callee4, this);
8650
8633
  }));
8651
- function getAmountOut(_x9) {
8634
+ function getAmountOut(_x10) {
8652
8635
  return _getAmountOut.apply(this, arguments);
8653
8636
  }
8654
8637
  return getAmountOut;
@@ -8664,7 +8647,7 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8664
8647
  }
8665
8648
  }, _callee5, this);
8666
8649
  }));
8667
- function getDexConfig(_x0) {
8650
+ function getDexConfig(_x11) {
8668
8651
  return _getDexConfig2.apply(this, arguments);
8669
8652
  }
8670
8653
  return getDexConfig;
@@ -8672,328 +8655,241 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8672
8655
  }]);
8673
8656
  }(ExchangeChain);
8674
8657
 
8675
- var fetchCallMulti = /*#__PURE__*/function () {
8676
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
8677
- var api, calls, _ref$options, options, included, filtered, values, transformedValues, _t;
8678
- return _regenerator().w(function (_context) {
8679
- while (1) switch (_context.p = _context.n) {
8680
- case 0:
8681
- api = _ref.api, calls = _ref.calls, _ref$options = _ref.options, options = _ref$options === void 0 ? {} : _ref$options;
8682
- if (!(!calls || calls.length === 0 || !api.isConnected)) {
8683
- _context.n = 1;
8684
- break;
8685
- }
8686
- return _context.a(2, options.defaultValue);
8687
- case 1:
8688
- included = calls.map(function (c) {
8689
- return !!c && (!Array.isArray(c) || !!c[0]);
8690
- });
8691
- filtered = calls.filter(function (_, index) {
8692
- return included[index];
8693
- });
8694
- if (!(filtered.length > 0)) {
8695
- _context.n = 5;
8696
- break;
8697
- }
8698
- _context.p = 2;
8699
- _context.n = 3;
8700
- return api.queryMulti(filtered);
8701
- case 3:
8702
- values = _context.v;
8703
- transformedValues = options.transform ? options.transform(values, api) : values;
8704
- return _context.a(2, transformedValues);
8705
- case 4:
8706
- _context.p = 4;
8707
- _t = _context.v;
8708
- // eslint-disable-next-line no-console
8709
- console.error(_t);
8710
- return _context.a(2, options.defaultValue);
8711
- case 5:
8712
- return _context.a(2, options.defaultValue);
8713
- case 6:
8714
- return _context.a(2);
8715
- }
8716
- }, _callee, null, [[2, 4]]);
8658
+ var TOKEN_LIST_NAMES = {
8659
+ BifrostPolkadot: 'bifrost-polkadot',
8660
+ BifrostKusama: 'bifrost-kusama'
8661
+ };
8662
+ var getTokenMap = function getTokenMap(chain, chainId) {
8663
+ var listName = TOKEN_LIST_NAMES[chain];
8664
+ if (!listName) throw new RoutingResolutionError("Bifrost token list not found for ".concat(chain));
8665
+ var list = DEFULT_TOKEN_LIST_MAP[listName];
8666
+ return Object.fromEntries(list.tokens.map(function (info) {
8667
+ return new WrappedTokenInfo(info, list);
8668
+ }).filter(function (token) {
8669
+ return token.chainId === chainId;
8670
+ }).map(function (token) {
8671
+ return [token.address, token];
8717
8672
  }));
8718
- return function fetchCallMulti(_x) {
8719
- return _ref2.apply(this, arguments);
8720
- };
8721
- }();
8722
-
8723
- var getPairs = function getPairs(chainId, currencies) {
8724
- return currencies.filter(function (currencies) {
8725
- var _currencies = _slicedToArray(currencies, 2),
8726
- currencyA = _currencies[0],
8727
- currencyB = _currencies[1];
8728
- return Boolean(chainId && (chainId === ParachainId.BIFROST_KUSAMA || chainId === ParachainId.BIFROST_POLKADOT) && currencyA && currencyB && currencyA.chainId === currencyB.chainId && chainId === currencyA.chainId && !currencyA.wrapped.equals(currencyB.wrapped));
8729
- }).reduce(function (acc, _ref) {
8730
- var _ref2 = _slicedToArray(_ref, 2),
8731
- currencyA = _ref2[0],
8732
- currencyB = _ref2[1];
8733
- var _ref3 = currencyA.wrapped.sortsBefore(currencyB.wrapped) ? [currencyA.wrapped, currencyB.wrapped] : [currencyB.wrapped, currencyA.wrapped],
8734
- _ref4 = _slicedToArray(_ref3, 2),
8735
- token0 = _ref4[0],
8736
- token1 = _ref4[1];
8737
- acc[0].push(token0);
8738
- acc[1].push(token1);
8739
- acc[2].push([addressToZenlinkAssetId(token0.address), addressToZenlinkAssetId(token1.address)]);
8740
- return acc;
8741
- }, [[], [], []]);
8742
8673
  };
8743
- var uniqePairKey = function uniqePairKey(tokenA, tokenB) {
8744
- return "".concat(tokenA.address, "-").concat(tokenB.address);
8674
+ var findToken = function findToken(tokenMap, symbol) {
8675
+ return Object.values(tokenMap).find(function (token) {
8676
+ return isSymbolMatch(token.symbol, symbol);
8677
+ });
8745
8678
  };
8746
- var PairState;
8747
- (function (PairState) {
8748
- PairState[PairState["LOADING"] = 0] = "LOADING";
8749
- PairState[PairState["NOT_EXISTS"] = 1] = "NOT_EXISTS";
8750
- PairState[PairState["EXISTS"] = 2] = "EXISTS";
8751
- PairState[PairState["INVALID"] = 3] = "INVALID";
8752
- })(PairState || (PairState = {}));
8753
- var fetchPairs = /*#__PURE__*/function () {
8754
- var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(api, chainId, currencies) {
8755
- var _getPairs, _getPairs2, tokensA, tokensB, _tokensA$reduce, _tokensA$reduce2, validTokensA, validTokensB, reservesCalls, reserves;
8679
+ var balanceQuery = function balanceQuery(api, account, token) {
8680
+ return isNativeCurrency(token) ? [api.query.system.account, account] : [api.query.tokens.accounts, [account, addressToNodeCurrency(token.address)]];
8681
+ };
8682
+ var freeBalance = function freeBalance(reserve) {
8683
+ return ('data' in reserve ? reserve.data.free : reserve.free).toString();
8684
+ };
8685
+ var getFilteredPairs = /*#__PURE__*/function () {
8686
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(api, chainId, currencies) {
8687
+ var _getPairs, _getPairs2, tokensA, tokensB, pools, poolList, reserves;
8756
8688
  return _regenerator().w(function (_context) {
8757
8689
  while (1) switch (_context.n) {
8758
8690
  case 0:
8759
8691
  _getPairs = getPairs(chainId, currencies), _getPairs2 = _slicedToArray(_getPairs, 2), tokensA = _getPairs2[0], tokensB = _getPairs2[1];
8760
- _tokensA$reduce = tokensA.reduce(function (acc, tokenA, i) {
8761
- var _PAIR_ADDRESSES$pairK;
8692
+ pools = new Map();
8693
+ tokensA.forEach(function (tokenA, i) {
8762
8694
  var tokenB = tokensB[i];
8763
- var pairKey = uniqePairKey(tokenA, tokenB);
8764
- var pairAccount = (_PAIR_ADDRESSES$pairK = PAIR_ADDRESSES[pairKey]) === null || _PAIR_ADDRESSES$pairK === void 0 ? void 0 : _PAIR_ADDRESSES$pairK.account;
8765
- if (pairAccount && api) {
8766
- acc[0].push(tokenA);
8767
- acc[1].push(tokenB);
8768
- if (isNativeCurrency(tokenA)) acc[2].push([api.query.system.account, pairAccount]);else acc[2].push([api.query.tokens.accounts, [pairAccount, addressToNodeCurrency(tokenA.address)]]);
8769
- if (isNativeCurrency(tokenB)) acc[2].push([api.query.system.account, pairAccount]);else acc[2].push([api.query.tokens.accounts, [pairAccount, addressToNodeCurrency(tokenB.address)]]);
8770
- }
8771
- return acc;
8772
- }, [[], [], []]), _tokensA$reduce2 = _slicedToArray(_tokensA$reduce, 3), validTokensA = _tokensA$reduce2[0], validTokensB = _tokensA$reduce2[1], reservesCalls = _tokensA$reduce2[2];
8773
- _context.n = 1;
8774
- return fetchCallMulti({
8775
- api: api,
8776
- calls: reservesCalls,
8777
- options: {
8778
- defaultValue: []
8779
- }
8695
+ var key = uniqePairKey(tokenA, tokenB);
8696
+ var pool = PAIR_ADDRESSES[key];
8697
+ if (pool) pools.set(key, _objectSpread2({
8698
+ tokenA: tokenA,
8699
+ tokenB: tokenB
8700
+ }, pool));
8780
8701
  });
8781
- case 1:
8782
- reserves = _context.v;
8783
- if (!(reservesCalls.length === 0)) {
8784
- _context.n = 2;
8785
- break;
8786
- }
8787
- return _context.a(2, [[PairState.NOT_EXISTS, null]]);
8788
- case 2:
8789
- if (!(!reserves.length || reserves.length !== validTokensA.length * 2)) {
8790
- _context.n = 3;
8702
+ if (!(pools.size === 0)) {
8703
+ _context.n = 1;
8791
8704
  break;
8792
8705
  }
8793
- return _context.a(2, validTokensA.map(function () {
8794
- return [PairState.LOADING, null];
8706
+ return _context.a(2, []);
8707
+ case 1:
8708
+ poolList = Array.from(pools.values());
8709
+ _context.n = 2;
8710
+ return api.queryMulti(poolList.flatMap(function (_ref2) {
8711
+ var tokenA = _ref2.tokenA,
8712
+ tokenB = _ref2.tokenB,
8713
+ account = _ref2.account;
8714
+ return [balanceQuery(api, account, tokenA), balanceQuery(api, account, tokenB)];
8795
8715
  }));
8796
- case 3:
8797
- return _context.a(2, validTokensA.map(function (tokenA, i) {
8798
- var _PAIR_ADDRESSES$pairK2;
8799
- var tokenB = validTokensB[i];
8800
- if (!tokenA || !tokenB || tokenA.equals(tokenB)) return [PairState.INVALID, null];
8801
- var pairKey = uniqePairKey(tokenA, tokenB);
8802
- var reserve0 = reserves[i * 2];
8803
- var reserve1 = reserves[i * 2 + 1];
8804
- var pairAddress = (_PAIR_ADDRESSES$pairK2 = PAIR_ADDRESSES[pairKey]) === null || _PAIR_ADDRESSES$pairK2 === void 0 ? void 0 : _PAIR_ADDRESSES$pairK2.address;
8805
- if (!reserve0 || !reserve1 || reserve0.isEmpty || reserve1.isEmpty || !pairAddress) return [PairState.NOT_EXISTS, null];
8806
- return [PairState.EXISTS, new Pair(Amount.fromRawAmount(tokenA, (reserve0.data || reserve0).free.toString()), Amount.fromRawAmount(tokenB, (reserve1.data || reserve1).free.toString()), pairAddress)];
8716
+ case 2:
8717
+ reserves = _context.v;
8718
+ return _context.a(2, poolList.flatMap(function (_ref3, i) {
8719
+ var tokenA = _ref3.tokenA,
8720
+ tokenB = _ref3.tokenB,
8721
+ address = _ref3.address;
8722
+ var reserveA = reserves[i * 2];
8723
+ var reserveB = reserves[i * 2 + 1];
8724
+ if (reserveA.isEmpty || reserveB.isEmpty) return [];
8725
+ return [new Pair(Amount.fromRawAmount(tokenA, freeBalance(reserveA)), Amount.fromRawAmount(tokenB, freeBalance(reserveB)), address)];
8807
8726
  }));
8808
8727
  }
8809
8728
  }, _callee);
8810
8729
  }));
8811
- return function fetchPairs(_x, _x2, _x3) {
8812
- return _ref5.apply(this, arguments);
8730
+ return function getFilteredPairs(_x, _x2, _x3) {
8731
+ return _ref.apply(this, arguments);
8813
8732
  };
8814
8733
  }();
8815
- var tokensToChainTokenMap = function tokensToChainTokenMap(tokens) {
8816
- var _ref6 = [tokens, tokens.tokens],
8817
- list = _ref6[0],
8818
- infos = _ref6[1];
8819
- var map = infos.reduce(function (map, info) {
8820
- var _map$token$chainId;
8821
- var token = new WrappedTokenInfo(info, list);
8822
- if (((_map$token$chainId = map[token.chainId]) === null || _map$token$chainId === void 0 ? void 0 : _map$token$chainId[token.address]) !== undefined) {
8823
- // eslint-disable-next-line no-console
8824
- console.warn("Duplicate token skipped: ".concat(token.address));
8825
- return map;
8826
- }
8827
- if (!map[token.chainId]) map[token.chainId] = {};
8828
- map[token.chainId][token.address] = {
8829
- token: token,
8830
- list: list
8831
- };
8832
- return map;
8833
- }, {});
8834
- return map;
8835
- };
8836
- var getTokenMap = function getTokenMap(chain, chainId) {
8837
- var _tokenMap$chainId;
8838
- var map = DEFULT_TOKEN_LIST_MAP[chain === 'BifrostPolkadot' ? 'bifrost-polkadot' : 'bifrost-kusama'];
8839
- var tokenMap = tokensToChainTokenMap(map);
8840
- return Object.keys((_tokenMap$chainId = tokenMap[chainId]) !== null && _tokenMap$chainId !== void 0 ? _tokenMap$chainId : {}).reduce(function (newMap, address) {
8841
- newMap[address] = tokenMap[chainId][address].token;
8842
- return newMap;
8843
- }, {});
8844
- };
8845
- var getFilteredPairs = /*#__PURE__*/function () {
8846
- var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(api, chainId, currencies) {
8847
- var pairs;
8848
- return _regenerator().w(function (_context2) {
8849
- while (1) switch (_context2.n) {
8850
- case 0:
8851
- _context2.n = 1;
8852
- return fetchPairs(api, chainId, currencies);
8853
- case 1:
8854
- pairs = _context2.v;
8855
- return _context2.a(2, Object.values(pairs.filter(function (result) {
8856
- return Boolean(result[0] === PairState.EXISTS && result[1]);
8857
- }).map(function (_ref8) {
8858
- var _ref9 = _slicedToArray(_ref8, 2),
8859
- pair = _ref9[1];
8860
- return pair;
8861
- })));
8862
- }
8863
- }, _callee2);
8734
+ var selectPairs = function selectPairs(pairs, chainId, currencies) {
8735
+ var _getPairs3 = getPairs(chainId, currencies),
8736
+ _getPairs4 = _slicedToArray(_getPairs3, 2),
8737
+ tokensA = _getPairs4[0],
8738
+ tokensB = _getPairs4[1];
8739
+ var keys = new Set(tokensA.map(function (tokenA, i) {
8740
+ return uniqePairKey(tokenA, tokensB[i]);
8864
8741
  }));
8865
- return function getFilteredPairs(_x4, _x5, _x6) {
8866
- return _ref7.apply(this, arguments);
8867
- };
8868
- }();
8869
- var getBestTrade = function getBestTrade(chainId, pairs, amountIn, currencyOut) {
8870
- var trades = Trade.bestTradeExactIn(chainId, pairs, [], amountIn, currencyOut);
8871
- if (trades.length < 1) {
8872
- throw new RoutingResolutionError('Trade not found');
8873
- }
8874
- return trades[0];
8742
+ return pairs.filter(function (pair) {
8743
+ return keys.has(uniqePairKey(pair.token0, pair.token1));
8744
+ });
8875
8745
  };
8876
- var findToken = function findToken(tokenMap, symbol) {
8877
- var _Object$values$find;
8878
- return (_Object$values$find = Object.values(tokenMap).find(function (item) {
8879
- return item.wrapped.symbol != null && isSymbolMatch(item.wrapped.symbol, symbol);
8880
- })) === null || _Object$values$find === void 0 ? void 0 : _Object$values$find.wrapped;
8746
+ var getBestTrade = function getBestTrade(chainId, pairs, amountIn, currencyOut) {
8747
+ var _ref4 = pairs.length ? Trade.bestTradeExactIn(chainId, pairs, [], amountIn, currencyOut) : [],
8748
+ _ref5 = _slicedToArray(_ref4, 1),
8749
+ bestTrade = _ref5[0];
8750
+ if (!bestTrade) throw new RoutingResolutionError('Trade not found');
8751
+ return bestTrade;
8881
8752
  };
8882
8753
 
8754
+ var pairKey = function pairKey(symbolA, symbolB) {
8755
+ return [symbolA, symbolB].map(function (s) {
8756
+ return s.toLowerCase();
8757
+ }).sort().join('-');
8758
+ };
8883
8759
  var getDexConfig = /*#__PURE__*/function () {
8884
8760
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(api, chain) {
8885
- var chainId, tokenMap, sdkAssets, assetsMap, assets, pairEntries, tokensList, pairs, seen, findTokenByDesc, syntheticPairs, i, j, a, b, dedup, wrapA, wrapB, tokA, tokB, combos, filtered, minUnit;
8761
+ var chainId, tokenMap, tokens, sdkAssets, assetsMap, assets, findTokenByAssetId, pairs, seen, pairEntries, candidates, i, j, a, b, tokenInfoA, tokenInfoB, tokenA, tokenB, pools, syntheticPairs, _i, _candidates, _candidates$_i, _a, _b, _tokenA, _tokenB, combos, key;
8886
8762
  return _regenerator().w(function (_context) {
8887
- while (1) switch (_context.p = _context.n) {
8763
+ while (1) switch (_context.n) {
8888
8764
  case 0:
8889
8765
  chainId = getParaId(chain);
8890
8766
  tokenMap = getTokenMap(chain, chainId);
8767
+ tokens = Object.values(tokenMap);
8891
8768
  sdkAssets = getAssets(chain);
8892
8769
  assetsMap = new Map();
8893
- Object.values(tokenMap).forEach(function (wrapped) {
8894
- var symbol = wrapped.wrapped.symbol;
8895
- if (!symbol) return;
8770
+ tokens.forEach(function (_ref2) {
8771
+ var symbol = _ref2.symbol;
8896
8772
  var sdkAsset = sdkAssets.find(function (a) {
8897
8773
  return isSymbolMatch(a.symbol, symbol);
8898
8774
  });
8899
- if (!sdkAsset) return;
8900
- assetsMap.set(symbol, sdkAsset);
8775
+ if (sdkAsset) assetsMap.set(symbol, sdkAsset);
8901
8776
  });
8902
8777
  assets = Array.from(assetsMap.values());
8778
+ findTokenByAssetId = function findTokenByAssetId(_ref3) {
8779
+ var chainId = _ref3.chainId,
8780
+ assetType = _ref3.assetType,
8781
+ assetIndex = _ref3.assetIndex;
8782
+ return tokens.find(function (_ref4) {
8783
+ var tokenInfo = _ref4.tokenInfo;
8784
+ return tokenInfo.parachainId === chainId && tokenInfo.assetType === assetType && tokenInfo.assetIndex === assetIndex;
8785
+ });
8786
+ };
8787
+ pairs = [];
8788
+ seen = new Set();
8903
8789
  _context.n = 1;
8904
8790
  return api.query.zenlinkProtocol.pairStatuses.entries();
8905
8791
  case 1:
8906
8792
  pairEntries = _context.v;
8907
- tokensList = Object.values(tokenMap);
8908
- pairs = [];
8909
- seen = new Set();
8910
- findTokenByDesc = function findTokenByDesc(d) {
8911
- return tokensList.find(function (tok) {
8912
- var info = tok.wrapped.tokenInfo;
8913
- return info.parachainId === d.chainId && info.assetType === d.assetType && info.assetIndex === d.assetIndex;
8914
- });
8915
- };
8916
- pairEntries.forEach(function (_ref2) {
8917
- var _ref3 = _slicedToArray(_ref2, 1),
8918
- _ref3$0$args = _slicedToArray(_ref3[0].args, 1),
8919
- keys = _ref3$0$args[0];
8793
+ pairEntries.forEach(function (_ref5) {
8794
+ var _ref6 = _slicedToArray(_ref5, 1),
8795
+ _ref6$0$args = _slicedToArray(_ref6[0].args, 1),
8796
+ keys = _ref6$0$args[0];
8920
8797
  var _keys$toJSON = keys.toJSON(),
8921
8798
  _keys$toJSON2 = _slicedToArray(_keys$toJSON, 2),
8922
- assetAData = _keys$toJSON2[0],
8923
- assetBData = _keys$toJSON2[1];
8924
- var tokA = findTokenByDesc(assetAData);
8925
- var tokB = findTokenByDesc(assetBData);
8926
- if (!tokA || !tokB) return;
8927
- var symA = tokA.wrapped.symbol;
8928
- var symB = tokB.wrapped.symbol;
8929
- var raA = assetsMap.get(symA);
8930
- var raB = assetsMap.get(symB);
8931
- if (!raA || !raB) return;
8932
- var dedup = [symA, symB].map(function (s) {
8933
- return s.toLowerCase();
8934
- }).sort().join('-');
8935
- if (seen.has(dedup)) return;
8936
- seen.add(dedup);
8937
- pairs.push([raA.location, raB.location]);
8799
+ assetIdA = _keys$toJSON2[0],
8800
+ assetIdB = _keys$toJSON2[1];
8801
+ var tokenA = findTokenByAssetId(assetIdA);
8802
+ var tokenB = findTokenByAssetId(assetIdB);
8803
+ if (!tokenA || !tokenB) return;
8804
+ var assetA = assetsMap.get(tokenA.symbol);
8805
+ var assetB = assetsMap.get(tokenB.symbol);
8806
+ if (!assetA || !assetB) return;
8807
+ var key = pairKey(tokenA.symbol, tokenB.symbol);
8808
+ if (seen.has(key)) return;
8809
+ seen.add(key);
8810
+ pairs.push([assetA.location, assetB.location]);
8938
8811
  });
8939
- syntheticPairs = [];
8812
+ candidates = [];
8940
8813
  i = 0;
8941
8814
  case 2:
8942
8815
  if (!(i < assets.length)) {
8943
- _context.n = 11;
8816
+ _context.n = 8;
8944
8817
  break;
8945
8818
  }
8946
8819
  j = i + 1;
8947
8820
  case 3:
8948
8821
  if (!(j < assets.length)) {
8949
- _context.n = 10;
8822
+ _context.n = 7;
8950
8823
  break;
8951
8824
  }
8952
8825
  a = assets[i];
8953
8826
  b = assets[j];
8954
- dedup = [a.symbol, b.symbol].map(function (s) {
8955
- return s.toLowerCase();
8956
- }).sort().join('-');
8957
- if (!seen.has(dedup)) {
8827
+ if (!seen.has(pairKey(a.symbol, b.symbol))) {
8958
8828
  _context.n = 4;
8959
8829
  break;
8960
8830
  }
8961
- return _context.a(3, 9);
8831
+ return _context.a(3, 6);
8962
8832
  case 4:
8963
- wrapA = findToken(tokenMap, a.symbol);
8964
- wrapB = findToken(tokenMap, b.symbol);
8965
- if (!(!wrapA || !wrapB)) {
8833
+ tokenInfoA = findToken(tokenMap, a.symbol);
8834
+ tokenInfoB = findToken(tokenMap, b.symbol);
8835
+ if (!(!tokenInfoA || !tokenInfoB)) {
8966
8836
  _context.n = 5;
8967
8837
  break;
8968
8838
  }
8969
- return _context.a(3, 9);
8839
+ return _context.a(3, 6);
8970
8840
  case 5:
8971
- tokA = new Token(wrapA.wrapped);
8972
- tokB = new Token(wrapB.wrapped);
8973
- _context.p = 6;
8974
- combos = getCurrencyCombinations(chainId, tokA, tokB);
8975
- _context.n = 7;
8976
- return getFilteredPairs(api, chainId, combos);
8977
- case 7:
8978
- filtered = _context.v;
8979
- minUnit = Amount.fromRawAmount(tokA, '1');
8980
- getBestTrade(chainId, filtered, minUnit, tokB);
8981
- syntheticPairs.push([a.location, b.location]);
8982
- seen.add(dedup);
8983
- _context.n = 9;
8984
- break;
8985
- case 8:
8986
- _context.p = 8;
8987
- _context.v;
8988
- case 9:
8841
+ tokenA = new Token(tokenInfoA);
8842
+ tokenB = new Token(tokenInfoB);
8843
+ candidates.push({
8844
+ a: a,
8845
+ b: b,
8846
+ tokenA: tokenA,
8847
+ tokenB: tokenB,
8848
+ combos: getCurrencyCombinations(chainId, tokenA, tokenB)
8849
+ });
8850
+ case 6:
8989
8851
  j++;
8990
8852
  _context.n = 3;
8991
8853
  break;
8992
- case 10:
8854
+ case 7:
8993
8855
  i++;
8994
8856
  _context.n = 2;
8995
8857
  break;
8858
+ case 8:
8859
+ _context.n = 9;
8860
+ return getFilteredPairs(api, chainId, candidates.flatMap(function (_ref7) {
8861
+ var combos = _ref7.combos;
8862
+ return combos;
8863
+ }));
8864
+ case 9:
8865
+ pools = _context.v;
8866
+ syntheticPairs = [];
8867
+ _i = 0, _candidates = candidates;
8868
+ case 10:
8869
+ if (!(_i < _candidates.length)) {
8870
+ _context.n = 13;
8871
+ break;
8872
+ }
8873
+ _candidates$_i = _candidates[_i], _a = _candidates$_i.a, _b = _candidates$_i.b, _tokenA = _candidates$_i.tokenA, _tokenB = _candidates$_i.tokenB, combos = _candidates$_i.combos;
8874
+ key = pairKey(_a.symbol, _b.symbol);
8875
+ if (!seen.has(key)) {
8876
+ _context.n = 11;
8877
+ break;
8878
+ }
8879
+ return _context.a(3, 12);
8996
8880
  case 11:
8881
+ try {
8882
+ getBestTrade(chainId, selectPairs(pools, chainId, combos), Amount.fromRawAmount(_tokenA, '1'), _tokenB);
8883
+ syntheticPairs.push([_a.location, _b.location]);
8884
+ seen.add(key);
8885
+ } catch (_unused) {
8886
+ /* no route ⇒ skip */
8887
+ }
8888
+ case 12:
8889
+ _i++;
8890
+ _context.n = 10;
8891
+ break;
8892
+ case 13:
8997
8893
  return _context.a(2, {
8998
8894
  isOmni: false,
8999
8895
  assets: assets.map(function (a) {
@@ -9002,7 +8898,7 @@ var getDexConfig = /*#__PURE__*/function () {
9002
8898
  pairs: [].concat(pairs, syntheticPairs)
9003
8899
  });
9004
8900
  }
9005
- }, _callee, null, [[6, 8]]);
8901
+ }, _callee);
9006
8902
  }));
9007
8903
  return function getDexConfig(_x, _x2) {
9008
8904
  return _ref.apply(this, arguments);
@@ -9015,91 +8911,72 @@ var BifrostExchange = /*#__PURE__*/function (_ExchangeChain) {
9015
8911
  _classCallCheck(this, BifrostExchange);
9016
8912
  _this = _callSuper(this, BifrostExchange, arguments);
9017
8913
  _this.apiType = 'PJS';
8914
+ _this.supportsRecipient = true;
9018
8915
  return _this;
9019
8916
  }
9020
8917
  _inherits(BifrostExchange, _ExchangeChain);
9021
8918
  return _createClass(BifrostExchange, [{
9022
- key: "swapCurrency",
8919
+ key: "buildSwap",
9023
8920
  value: function () {
9024
- var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTxFee) {
9025
- var apiPjs, assetFrom, assetTo, amount, sender, slippagePct, origin, chainId, tokenMap, tokenWrappedFrom, tokenWrappedTo, tokenFrom, tokenTo, currencyCombinations, pairs, pctDestFee, amountWithoutFee, amountInFinal, trade, allowedSlippage, blockNumber, deadline, _SwapRouter$swapCallP, extrinsic, amountOut, nativeSymbol, amountOutWithFee;
8921
+ var _buildSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTxFee) {
8922
+ var apiPjs, assetFrom, assetTo, amount, recipient, slippagePct, origin, amountWithoutFee, _yield$this$findBestT, trade, tokenTo, amountOut, allowedSlippage, blockNumber, deadline, _SwapRouter$swapCallP, extrinsic, nativeSymbol, amountOutWithFee;
9026
8923
  return _regenerator().w(function (_context) {
9027
8924
  while (1) switch (_context.n) {
9028
8925
  case 0:
9029
- apiPjs = options.apiPjs, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, sender = options.sender, slippagePct = options.slippagePct, origin = options.origin;
9030
- chainId = getParaId(this.chain);
9031
- tokenMap = getTokenMap(this.chain, chainId);
9032
- tokenWrappedFrom = findToken(tokenMap, assetFrom.symbol);
9033
- if (!(tokenWrappedFrom === undefined)) {
9034
- _context.n = 1;
9035
- break;
9036
- }
9037
- throw new RoutingResolutionError('Currency from not found');
9038
- case 1:
9039
- tokenWrappedTo = findToken(tokenMap, assetTo.symbol);
9040
- if (!(tokenWrappedTo === undefined)) {
9041
- _context.n = 2;
9042
- break;
9043
- }
9044
- throw new RoutingResolutionError('Currency to not found');
9045
- case 2:
9046
- tokenFrom = new Token(tokenWrappedFrom.wrapped);
9047
- tokenTo = new Token(tokenWrappedTo.wrapped);
9048
- currencyCombinations = getCurrencyCombinations(chainId, tokenFrom, tokenTo);
9049
- _context.n = 3;
9050
- return getFilteredPairs(apiPjs, chainId, currencyCombinations);
9051
- case 3:
9052
- pairs = _context.v;
8926
+ apiPjs = options.apiPjs, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, recipient = options.recipient, slippagePct = options.slippagePct, origin = options.origin;
9053
8927
  Logger.log('To dest tx fee in native currency:', toDestTxFee);
9054
8928
  Logger.log('Original amount', amount);
9055
- pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
9056
- amountWithoutFee = padValueBy(amount, pctDestFee);
8929
+ amountWithoutFee = padValueBy(amount, origin ? DEST_FEE_BUFFER_PCT : 0);
9057
8930
  Logger.log('Amount modified', amountWithoutFee);
9058
8931
  if (!(amountWithoutFee <= 0n)) {
9059
- _context.n = 4;
8932
+ _context.n = 1;
9060
8933
  break;
9061
8934
  }
9062
8935
  throw new AmountTooLowError();
9063
- case 4:
9064
- amountInFinal = Amount.fromRawAmount(tokenFrom, amountWithoutFee.toString());
9065
- trade = getBestTrade(chainId, pairs, amountInFinal, tokenTo);
8936
+ case 1:
8937
+ _context.n = 2;
8938
+ return this.findBestTrade(apiPjs, assetFrom.symbol, assetTo.symbol, amountWithoutFee);
8939
+ case 2:
8940
+ _yield$this$findBestT = _context.v;
8941
+ trade = _yield$this$findBestT.trade;
8942
+ tokenTo = _yield$this$findBestT.tokenTo;
8943
+ amountOut = _yield$this$findBestT.amountOut;
9066
8944
  allowedSlippage = new Percent(Number(slippagePct) * 100, 10000);
9067
- _context.n = 5;
8945
+ _context.n = 3;
9068
8946
  return apiPjs.derive.chain.bestNumber();
9069
- case 5:
8947
+ case 3:
9070
8948
  blockNumber = _context.v;
9071
8949
  deadline = blockNumber.toNumber() + 20;
9072
8950
  _SwapRouter$swapCallP = SwapRouter.swapCallParameters(trade, {
9073
8951
  api: apiPjs,
9074
8952
  allowedSlippage: allowedSlippage,
9075
- recipient: sender,
8953
+ recipient: recipient,
9076
8954
  deadline: deadline
9077
8955
  }), extrinsic = _SwapRouter$swapCallP.extrinsic;
9078
8956
  if (!(extrinsic === null)) {
9079
- _context.n = 6;
8957
+ _context.n = 4;
9080
8958
  break;
9081
8959
  }
9082
8960
  throw new RoutingResolutionError('Extrinsic is null');
9083
- case 6:
9084
- amountOut = parseUnits(trade.outputAmount.toFixed(), tokenTo.decimals);
8961
+ case 4:
9085
8962
  nativeSymbol = getNativeAssetSymbol(this.chain);
9086
8963
  if (!(tokenTo.symbol === nativeSymbol)) {
9087
- _context.n = 8;
8964
+ _context.n = 6;
9088
8965
  break;
9089
8966
  }
9090
8967
  amountOutWithFee = amountOut - padValueBy(toDestTxFee, FEE_BUFFER_PCT);
9091
8968
  if (!(amountOutWithFee <= 0n)) {
9092
- _context.n = 7;
8969
+ _context.n = 5;
9093
8970
  break;
9094
8971
  }
9095
8972
  throw new AmountTooLowError();
9096
- case 7:
8973
+ case 5:
9097
8974
  Logger.log('Amount out with fee:', amountOutWithFee);
9098
8975
  return _context.a(2, {
9099
8976
  tx: extrinsic[0],
9100
8977
  amountOut: amountOutWithFee
9101
8978
  });
9102
- case 8:
8979
+ case 6:
9103
8980
  Logger.log('Calculated amount out:', amountOut);
9104
8981
  return _context.a(2, {
9105
8982
  tx: extrinsic[0],
@@ -9108,48 +8985,27 @@ var BifrostExchange = /*#__PURE__*/function (_ExchangeChain) {
9108
8985
  }
9109
8986
  }, _callee, this);
9110
8987
  }));
9111
- function swapCurrency(_x, _x2) {
9112
- return _swapCurrency.apply(this, arguments);
8988
+ function buildSwap(_x, _x2) {
8989
+ return _buildSwap.apply(this, arguments);
9113
8990
  }
9114
- return swapCurrency;
8991
+ return buildSwap;
9115
8992
  }()
9116
8993
  }, {
9117
8994
  key: "getAmountOut",
9118
8995
  value: function () {
9119
8996
  var _getAmountOut = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
9120
- var apiPjs, assetFrom, assetTo, amount, origin, chainId, tokenMap, tokenWrappedFrom, tokenWrappedTo, tokenFrom, tokenTo, currencyCombinations, pairs, pctDestFee, amountWithoutFee, amountIn, trade;
8997
+ var apiPjs, assetFrom, assetTo, amount, origin, amountWithoutFee, _yield$this$findBestT2, amountOut;
9121
8998
  return _regenerator().w(function (_context2) {
9122
8999
  while (1) switch (_context2.n) {
9123
9000
  case 0:
9124
9001
  apiPjs = options.apiPjs, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, origin = options.origin;
9125
- chainId = getParaId(this.chain);
9126
- tokenMap = getTokenMap(this.chain, chainId);
9127
- tokenWrappedFrom = findToken(tokenMap, assetFrom.symbol);
9128
- if (!(tokenWrappedFrom === undefined)) {
9129
- _context2.n = 1;
9130
- break;
9131
- }
9132
- throw new RoutingResolutionError('Currency from not found');
9002
+ amountWithoutFee = padValueBy(amount, origin ? DEST_FEE_BUFFER_PCT : 0);
9003
+ _context2.n = 1;
9004
+ return this.findBestTrade(apiPjs, assetFrom.symbol, assetTo.symbol, amountWithoutFee);
9133
9005
  case 1:
9134
- tokenWrappedTo = findToken(tokenMap, assetTo.symbol);
9135
- if (!(tokenWrappedTo === undefined)) {
9136
- _context2.n = 2;
9137
- break;
9138
- }
9139
- throw new RoutingResolutionError('Currency to not found');
9140
- case 2:
9141
- tokenFrom = new Token(tokenWrappedFrom.wrapped);
9142
- tokenTo = new Token(tokenWrappedTo.wrapped);
9143
- currencyCombinations = getCurrencyCombinations(chainId, tokenFrom, tokenTo);
9144
- _context2.n = 3;
9145
- return getFilteredPairs(apiPjs, chainId, currencyCombinations);
9146
- case 3:
9147
- pairs = _context2.v;
9148
- pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
9149
- amountWithoutFee = padValueBy(amount, pctDestFee);
9150
- amountIn = Amount.fromRawAmount(tokenFrom, amountWithoutFee.toString());
9151
- trade = getBestTrade(chainId, pairs, amountIn, tokenTo);
9152
- return _context2.a(2, parseUnits(trade.outputAmount.toFixed(), tokenTo.decimals));
9006
+ _yield$this$findBestT2 = _context2.v;
9007
+ amountOut = _yield$this$findBestT2.amountOut;
9008
+ return _context2.a(2, amountOut);
9153
9009
  }
9154
9010
  }, _callee2, this);
9155
9011
  }));
@@ -9174,6 +9030,50 @@ var BifrostExchange = /*#__PURE__*/function (_ExchangeChain) {
9174
9030
  }
9175
9031
  return getDexConfig$1;
9176
9032
  }()
9033
+ }, {
9034
+ key: "findBestTrade",
9035
+ value: function () {
9036
+ var _findBestTrade = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(api, symbolFrom, symbolTo, amountIn) {
9037
+ var chainId, tokenMap, tokenInfoFrom, tokenInfoTo, tokenFrom, tokenTo, pairs, trade;
9038
+ return _regenerator().w(function (_context4) {
9039
+ while (1) switch (_context4.n) {
9040
+ case 0:
9041
+ chainId = getParaId(this.chain);
9042
+ tokenMap = getTokenMap(this.chain, chainId);
9043
+ tokenInfoFrom = findToken(tokenMap, symbolFrom);
9044
+ if (tokenInfoFrom) {
9045
+ _context4.n = 1;
9046
+ break;
9047
+ }
9048
+ throw new RoutingResolutionError('Currency from not found');
9049
+ case 1:
9050
+ tokenInfoTo = findToken(tokenMap, symbolTo);
9051
+ if (tokenInfoTo) {
9052
+ _context4.n = 2;
9053
+ break;
9054
+ }
9055
+ throw new RoutingResolutionError('Currency to not found');
9056
+ case 2:
9057
+ tokenFrom = new Token(tokenInfoFrom);
9058
+ tokenTo = new Token(tokenInfoTo);
9059
+ _context4.n = 3;
9060
+ return getFilteredPairs(api, chainId, getCurrencyCombinations(chainId, tokenFrom, tokenTo));
9061
+ case 3:
9062
+ pairs = _context4.v;
9063
+ trade = getBestTrade(chainId, pairs, Amount.fromRawAmount(tokenFrom, amountIn.toString()), tokenTo);
9064
+ return _context4.a(2, {
9065
+ trade: trade,
9066
+ tokenTo: tokenTo,
9067
+ amountOut: parseUnits(trade.outputAmount.toFixed(), tokenTo.decimals)
9068
+ });
9069
+ }
9070
+ }, _callee4, this);
9071
+ }));
9072
+ function findBestTrade(_x5, _x6, _x7, _x8) {
9073
+ return _findBestTrade.apply(this, arguments);
9074
+ }
9075
+ return findBestTrade;
9076
+ }()
9177
9077
  }]);
9178
9078
  }(ExchangeChain);
9179
9079
 
@@ -9267,9 +9167,9 @@ var HydrationExchange = /*#__PURE__*/function (_ExchangeChain) {
9267
9167
  }
9268
9168
  _inherits(HydrationExchange, _ExchangeChain);
9269
9169
  return _createClass(HydrationExchange, [{
9270
- key: "swapCurrency",
9170
+ key: "buildSwap",
9271
9171
  value: function () {
9272
- var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
9172
+ var _buildSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
9273
9173
  var apiPapi, origin, assetFrom, assetTo, sender, slippagePct, amount, sdk, tradeRouter, assetClient, txBuilderFactory, currencyFromInfo, currencyToInfo, tradeFee, amountWithoutFee, amountNormalized, trade, substrateTx, tx, amountOut, nativeCurrencyInfo, _findNativeAssetInfoO, nativeCurrencyDecimals, priceInfo, currencyToPrice, feeInCurrencyTo, amountOutModified;
9274
9174
  return _regenerator().w(function (_context) {
9275
9175
  while (1) switch (_context.p = _context.n) {
@@ -9379,10 +9279,10 @@ var HydrationExchange = /*#__PURE__*/function (_ExchangeChain) {
9379
9279
  }
9380
9280
  }, _callee, this, [[2,, 16, 17]]);
9381
9281
  }));
9382
- function swapCurrency(_x, _x2) {
9383
- return _swapCurrency.apply(this, arguments);
9282
+ function buildSwap(_x, _x2) {
9283
+ return _buildSwap.apply(this, arguments);
9384
9284
  }
9385
- return swapCurrency;
9285
+ return buildSwap;
9386
9286
  }()
9387
9287
  }, {
9388
9288
  key: "getAmountOut",
@@ -9887,7 +9787,7 @@ var selectBestExchangeCommon = /*#__PURE__*/function () {
9887
9787
  return _context.a(2, 0);
9888
9788
  case 5:
9889
9789
  config = api.config;
9890
- parsedAmount = applyDecimalAbstraction(options.amount, assetFromExchange === null || assetFromExchange === void 0 ? void 0 : assetFromExchange.decimals, !(isConfig(config) && config.abstractDecimals === false)).toString();
9790
+ parsedAmount = applyDecimalAbstraction(options.amount, assetFromExchange.decimals, !(isConfig(config) && config.abstractDecimals === false)).toString();
9891
9791
  _context.p = 6;
9892
9792
  _context.n = 7;
9893
9793
  return computeAmountOut(dex, assetFromExchange, assetTo, options, parsedAmount);
@@ -10035,7 +9935,7 @@ var selectBestExchange = /*#__PURE__*/function () {
10035
9935
  exchangeConfig = convertBuilderConfig(api.config);
10036
9936
  return _context2.a(2, selectBestExchangeCommon(options, originApi, /*#__PURE__*/function () {
10037
9937
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(dex, assetFromExchange, assetTo, options, parsedAmount) {
10038
- var exchangeInfo, modifiedOptions, toDestTxFee, _yield$dex$handleMult, amountOut, _t, _t2, _t3, _t4, _t5, _t6, _t7, _t8, _t9;
9938
+ var exchangeInfo, modifiedOptions, toDestTxFee, _yield$dex$swapCurren, amountOut, _t, _t2, _t3, _t4, _t5, _t6, _t7, _t8, _t9;
10039
9939
  return _regenerator().w(function (_context) {
10040
9940
  while (1) switch (_context.n) {
10041
9941
  case 0:
@@ -10063,6 +9963,9 @@ var selectBestExchange = /*#__PURE__*/function () {
10063
9963
  modifiedOptions = _objectSpread2(_objectSpread2({}, options), {}, {
10064
9964
  amount: BigInt(parsedAmount),
10065
9965
  exchange: exchangeInfo,
9966
+ // Recipient does not affect the quote. Use the sender so swapCurrency does not reject
9967
+ // exchanges without supportsRecipient, which may still swap via execute
9968
+ recipient: options.sender,
10066
9969
  feeCalcAddress: determineFeeCalcAddress(options.sender, options.recipient)
10067
9970
  });
10068
9971
  _context.n = 3;
@@ -10070,15 +9973,15 @@ var selectBestExchange = /*#__PURE__*/function () {
10070
9973
  case 3:
10071
9974
  toDestTxFee = _context.v;
10072
9975
  _context.n = 4;
10073
- return dex.handleMultiSwap(_objectSpread2(_objectSpread2(_objectSpread2({}, modifiedOptions), pickExchangeApiVariant(modifiedOptions.exchange)), {}, {
9976
+ return dex.swapCurrency(_objectSpread2(_objectSpread2(_objectSpread2({}, modifiedOptions), pickExchangeApiVariant(modifiedOptions.exchange)), {}, {
10074
9977
  api: modifiedOptions.exchange.api,
10075
9978
  assetFrom: modifiedOptions.exchange.assetFrom,
10076
9979
  assetTo: modifiedOptions.exchange.assetTo,
10077
9980
  isForFeeEstimation: isForFeeEstimation
10078
9981
  }), toDestTxFee);
10079
9982
  case 4:
10080
- _yield$dex$handleMult = _context.v;
10081
- amountOut = _yield$dex$handleMult.amountOut;
9983
+ _yield$dex$swapCurren = _context.v;
9984
+ amountOut = _yield$dex$swapCurren.amountOut;
10082
9985
  return _context.a(2, amountOut);
10083
9986
  }
10084
9987
  }, _callee);
@@ -10188,7 +10091,8 @@ var prepareTransformedOptions = /*#__PURE__*/function () {
10188
10091
  _t0,
10189
10092
  _t1,
10190
10093
  _t10,
10191
- _t11;
10094
+ _t11,
10095
+ _t12;
10192
10096
  return _regenerator().w(function (_context) {
10193
10097
  while (1) switch (_context.n) {
10194
10098
  case 0:
@@ -10241,7 +10145,7 @@ var prepareTransformedOptions = /*#__PURE__*/function () {
10241
10145
  _t4 = _objectSpread2({}, options);
10242
10146
  _t5 = {};
10243
10147
  _t6 = applyDecimalAbstraction(amount, (_assetFromOrigin$deci = assetFromOrigin === null || assetFromOrigin === void 0 ? void 0 : assetFromOrigin.decimals) !== null && _assetFromOrigin$deci !== void 0 ? _assetFromOrigin$deci : assetFromExchange.decimals, (exchangeConfig === null || exchangeConfig === void 0 ? void 0 : exchangeConfig.abstractDecimals) !== false);
10244
- _t7 = originSpecified && assetFromOrigin && originApi ? {
10148
+ _t7 = originSpecified && assetFromOrigin ? {
10245
10149
  api: originApi.api,
10246
10150
  chain: from,
10247
10151
  assetFrom: assetFromOrigin,
@@ -10264,13 +10168,15 @@ var prepareTransformedOptions = /*#__PURE__*/function () {
10264
10168
  chain: to,
10265
10169
  address: recipient
10266
10170
  } : undefined;
10267
- _t11 = determineFeeCalcAddress(sender, recipient);
10171
+ _t11 = recipient !== null && recipient !== void 0 ? recipient : sender;
10172
+ _t12 = determineFeeCalcAddress(sender, recipient);
10268
10173
  transformed = _t3(_t4, _t5, {
10269
10174
  amount: _t6,
10270
10175
  origin: _t7,
10271
10176
  exchange: _t1,
10272
10177
  destination: _t10,
10273
- feeCalcAddress: _t11
10178
+ recipient: _t11,
10179
+ feeCalcAddress: _t12
10274
10180
  });
10275
10181
  return _context.a(2, {
10276
10182
  dex: dex,
@@ -10344,10 +10250,11 @@ var buildSwapExecuteTx = /*#__PURE__*/function () {
10344
10250
  sender: evmSenderAddress !== null && evmSenderAddress !== void 0 ? evmSenderAddress : sender,
10345
10251
  recipient: recipient !== null && recipient !== void 0 ? recipient : sender,
10346
10252
  calculateMinAmountOut: function calculateMinAmountOut(amountIn, assetTo) {
10253
+ var slippagePct = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : minAmountOutSlippage;
10347
10254
  return callDexAmountOut(dex, options, {
10348
10255
  amount: amountIn,
10349
10256
  assetTo: assetTo,
10350
- slippagePct: minAmountOutSlippage
10257
+ slippagePct: slippagePct
10351
10258
  });
10352
10259
  }
10353
10260
  });
@@ -10566,9 +10473,12 @@ var createSwapTx = /*#__PURE__*/function () {
10566
10473
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(exchange, options) {
10567
10474
  var isForFeeEstimation,
10568
10475
  toDestTxFee,
10569
- swapResult,
10570
- txs,
10571
- _args2 = arguments;
10476
+ _yield$exchange$swapC,
10477
+ tx,
10478
+ amountOut,
10479
+ _args2 = arguments,
10480
+ _t,
10481
+ _t2;
10572
10482
  return _regenerator().w(function (_context2) {
10573
10483
  while (1) switch (_context2.n) {
10574
10484
  case 0:
@@ -10578,23 +10488,24 @@ var createSwapTx = /*#__PURE__*/function () {
10578
10488
  case 1:
10579
10489
  toDestTxFee = _context2.v;
10580
10490
  _context2.n = 2;
10581
- return exchange.handleMultiSwap(_objectSpread2(_objectSpread2(_objectSpread2({}, options), pickExchangeApiVariant(options.exchange)), {}, {
10491
+ return exchange.swapCurrency(_objectSpread2(_objectSpread2(_objectSpread2({}, options), pickExchangeApiVariant(options.exchange)), {}, {
10582
10492
  api: options.exchange.api,
10583
10493
  assetFrom: options.exchange.assetFrom,
10584
10494
  assetTo: options.exchange.assetTo,
10585
10495
  isForFeeEstimation: isForFeeEstimation
10586
10496
  }), toDestTxFee);
10587
10497
  case 2:
10588
- swapResult = _context2.v;
10498
+ _yield$exchange$swapC = _context2.v;
10499
+ tx = _yield$exchange$swapC.tx;
10500
+ amountOut = _yield$exchange$swapC.amountOut;
10589
10501
  _context2.n = 3;
10590
- return Promise.all(swapResult.txs.map(function (tx) {
10591
- return convertTxToTarget(tx, options.exchange.api);
10592
- }));
10502
+ return convertTxToTarget(tx, options.exchange.api);
10593
10503
  case 3:
10594
- txs = _context2.v;
10504
+ _t = _context2.v;
10505
+ _t2 = amountOut;
10595
10506
  return _context2.a(2, {
10596
- txs: txs,
10597
- amountOut: swapResult.amountOut
10507
+ tx: _t,
10508
+ amountOut: _t2
10598
10509
  });
10599
10510
  }
10600
10511
  }, _callee2);
@@ -10606,7 +10517,7 @@ var createSwapTx = /*#__PURE__*/function () {
10606
10517
 
10607
10518
  var prepareExtrinsics = /*#__PURE__*/function () {
10608
10519
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(dex, options) {
10609
- var api, origin, exchange, destination, sender, _yield$buildSwapExecu, tx, _amountOut, toExchangeTx, _yield$createSwapTx, swapTxs, amountOut, toDestTx, _t, _t2, _t3;
10520
+ var api, origin, exchange, destination, sender, _yield$buildSwapExecu, tx, _amountOut, toExchangeTx, _yield$createSwapTx, swapTx, amountOut, toDestTx, _t, _t2, _t3;
10610
10521
  return _regenerator().w(function (_context) {
10611
10522
  while (1) switch (_context.p = _context.n) {
10612
10523
  case 0:
@@ -10623,7 +10534,7 @@ var prepareExtrinsics = /*#__PURE__*/function () {
10623
10534
  tx = _yield$buildSwapExecu.tx;
10624
10535
  _amountOut = _yield$buildSwapExecu.amountOut;
10625
10536
  return _context.a(2, {
10626
- swapTxs: [tx],
10537
+ swapTx: tx,
10627
10538
  isExecute: true,
10628
10539
  amountOut: _amountOut
10629
10540
  });
@@ -10658,7 +10569,7 @@ var prepareExtrinsics = /*#__PURE__*/function () {
10658
10569
  return createSwapTx(dex, options);
10659
10570
  case 8:
10660
10571
  _yield$createSwapTx = _context.v;
10661
- swapTxs = _yield$createSwapTx.txs;
10572
+ swapTx = _yield$createSwapTx.tx;
10662
10573
  amountOut = _yield$createSwapTx.amountOut;
10663
10574
  if (!(destination && destination.chain !== exchange.chain)) {
10664
10575
  _context.n = 10;
@@ -10682,7 +10593,7 @@ var prepareExtrinsics = /*#__PURE__*/function () {
10682
10593
  toDestTx = _t3;
10683
10594
  return _context.a(2, {
10684
10595
  toExchangeTx: toExchangeTx,
10685
- swapTxs: swapTxs,
10596
+ swapTx: swapTx,
10686
10597
  toDestTx: toDestTx,
10687
10598
  amountOut: BigInt(amountOut)
10688
10599
  });
@@ -10696,7 +10607,7 @@ var prepareExtrinsics = /*#__PURE__*/function () {
10696
10607
 
10697
10608
  var buildTransactions = /*#__PURE__*/function () {
10698
10609
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(dex, options) {
10699
- var origin, exchange, destination, _yield$prepareExtrins, toExchangeTx, swapTxs, toDestTx, amountOut, isExecute, transactions, batchedTx, _origin$api, _origin$chain, batchedSwapTx;
10610
+ var origin, exchange, destination, _yield$prepareExtrins, toExchangeTx, swapTx, toDestTx, amountOut, isExecute, transactions, batchedTx, _origin$api, _origin$chain;
10700
10611
  return _regenerator().w(function (_context) {
10701
10612
  while (1) switch (_context.n) {
10702
10613
  case 0:
@@ -10706,7 +10617,7 @@ var buildTransactions = /*#__PURE__*/function () {
10706
10617
  case 1:
10707
10618
  _yield$prepareExtrins = _context.v;
10708
10619
  toExchangeTx = _yield$prepareExtrins.toExchangeTx;
10709
- swapTxs = _yield$prepareExtrins.swapTxs;
10620
+ swapTx = _yield$prepareExtrins.swapTx;
10710
10621
  toDestTx = _yield$prepareExtrins.toDestTx;
10711
10622
  amountOut = _yield$prepareExtrins.amountOut;
10712
10623
  isExecute = _yield$prepareExtrins.isExecute;
@@ -10721,7 +10632,7 @@ var buildTransactions = /*#__PURE__*/function () {
10721
10632
  });
10722
10633
  }
10723
10634
  if (toDestTx) {
10724
- batchedTx = exchange.api.callBatchMethod([].concat(_toConsumableArray(swapTxs), [toDestTx]), BatchMode.BATCH_ALL);
10635
+ batchedTx = exchange.api.callBatchMethod([swapTx, toDestTx], BatchMode.BATCH_ALL);
10725
10636
  transactions.push({
10726
10637
  api: exchange.api.api,
10727
10638
  chain: dex.chain,
@@ -10731,24 +10642,13 @@ var buildTransactions = /*#__PURE__*/function () {
10731
10642
  amountOut: amountOut
10732
10643
  });
10733
10644
  } else {
10734
- if (swapTxs.length === 1) {
10735
- transactions.push({
10736
- api: isExecute ? (_origin$api = origin === null || origin === void 0 ? void 0 : origin.api) !== null && _origin$api !== void 0 ? _origin$api : exchange.api.api : exchange.api.api,
10737
- chain: isExecute ? (_origin$chain = origin === null || origin === void 0 ? void 0 : origin.chain) !== null && _origin$chain !== void 0 ? _origin$chain : dex.chain : dex.chain,
10738
- tx: swapTxs[0],
10739
- amountOut: amountOut,
10740
- type: 'SWAP'
10741
- });
10742
- } else {
10743
- batchedSwapTx = exchange.api.callBatchMethod(swapTxs, BatchMode.BATCH_ALL);
10744
- transactions.push({
10745
- api: exchange.api.api,
10746
- chain: dex.chain,
10747
- tx: batchedSwapTx,
10748
- amountOut: amountOut,
10749
- type: 'SWAP'
10750
- });
10751
- }
10645
+ transactions.push({
10646
+ api: isExecute ? (_origin$api = origin === null || origin === void 0 ? void 0 : origin.api) !== null && _origin$api !== void 0 ? _origin$api : exchange.api.api : exchange.api.api,
10647
+ chain: isExecute ? (_origin$chain = origin === null || origin === void 0 ? void 0 : origin.chain) !== null && _origin$chain !== void 0 ? _origin$chain : dex.chain : dex.chain,
10648
+ tx: swapTx,
10649
+ amountOut: amountOut,
10650
+ type: 'SWAP'
10651
+ });
10752
10652
  }
10753
10653
  return _context.a(2, transactions);
10754
10654
  }
@@ -10862,7 +10762,7 @@ var getOriginXcmFee = /*#__PURE__*/function () {
10862
10762
  var getSwapFee = /*#__PURE__*/function () {
10863
10763
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(exchange, options, disableFallback) {
10864
10764
  var _result$fee;
10865
- var sender, _options$exchange, assetFrom, api, amount, txs, amountOut, isForFeeEstimation, swapResult, buildTx, result, finalFee, _t;
10765
+ var sender, _options$exchange, assetFrom, api, amount, amountOut, isForFeeEstimation, _yield$createSwapTx, buildTx, result, _t;
10866
10766
  return _regenerator().w(function (_context2) {
10867
10767
  while (1) switch (_context2.p = _context2.n) {
10868
10768
  case 0:
@@ -10872,9 +10772,8 @@ var getSwapFee = /*#__PURE__*/function () {
10872
10772
  _context2.n = 2;
10873
10773
  return createSwapTx(exchange, options, isForFeeEstimation);
10874
10774
  case 2:
10875
- swapResult = _context2.v;
10876
- txs = swapResult.txs;
10877
- amountOut = swapResult.amountOut;
10775
+ _yield$createSwapTx = _context2.v;
10776
+ amountOut = _yield$createSwapTx.amountOut;
10878
10777
  _context2.n = 5;
10879
10778
  break;
10880
10779
  case 3:
@@ -10886,12 +10785,11 @@ var getSwapFee = /*#__PURE__*/function () {
10886
10785
  }
10887
10786
  throw _t;
10888
10787
  case 4:
10889
- txs = [null];
10890
10788
  amountOut = 0n;
10891
10789
  case 5:
10892
10790
  buildTx = /*#__PURE__*/function () {
10893
10791
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(overrideAmount) {
10894
- var txOptions, _yield$createSwapTx, txs;
10792
+ var txOptions, _yield$createSwapTx2, tx;
10895
10793
  return _regenerator().w(function (_context) {
10896
10794
  while (1) switch (_context.n) {
10897
10795
  case 0:
@@ -10901,9 +10799,9 @@ var getSwapFee = /*#__PURE__*/function () {
10901
10799
  _context.n = 1;
10902
10800
  return createSwapTx(exchange, txOptions, isForFeeEstimation);
10903
10801
  case 1:
10904
- _yield$createSwapTx = _context.v;
10905
- txs = _yield$createSwapTx.txs;
10906
- return _context.a(2, txs[0]);
10802
+ _yield$createSwapTx2 = _context.v;
10803
+ tx = _yield$createSwapTx2.tx;
10804
+ return _context.a(2, tx);
10907
10805
  }
10908
10806
  }, _callee);
10909
10807
  }));
@@ -10926,10 +10824,9 @@ var getSwapFee = /*#__PURE__*/function () {
10926
10824
  });
10927
10825
  case 6:
10928
10826
  result = _context2.v;
10929
- finalFee = ((_result$fee = result.fee) !== null && _result$fee !== void 0 ? _result$fee : 0n) * BigInt(txs.length);
10930
10827
  return _context2.a(2, {
10931
10828
  result: _objectSpread2(_objectSpread2({}, result), {}, {
10932
- fee: finalFee
10829
+ fee: (_result$fee = result.fee) !== null && _result$fee !== void 0 ? _result$fee : 0n
10933
10830
  }),
10934
10831
  amountOut: amountOut
10935
10832
  });
@@ -11032,7 +10929,7 @@ var getRouterFees = /*#__PURE__*/function () {
11032
10929
  mergedHops = [].concat(_toConsumableArray((_sendingChain$hops = sendingChain === null || sendingChain === void 0 ? void 0 : sendingChain.hops) !== null && _sendingChain$hops !== void 0 ? _sendingChain$hops : []), _toConsumableArray(sendingChain && receivingChain ? [{
11033
10930
  chain: exchange.chain,
11034
10931
  result: _objectSpread2(_objectSpread2({}, swapChain), {}, {
11035
- fee: swapChain.fee + ((_receivingChain$origi = receivingChain === null || receivingChain === void 0 ? void 0 : receivingChain.origin.fee) !== null && _receivingChain$origi !== void 0 ? _receivingChain$origi : 0n),
10932
+ fee: swapChain.fee + ((_receivingChain$origi = receivingChain.origin.fee) !== null && _receivingChain$origi !== void 0 ? _receivingChain$origi : 0n),
11036
10933
  isExchange: true
11037
10934
  })
11038
10935
  }] : []), _toConsumableArray((_receivingChain$hops = receivingChain === null || receivingChain === void 0 ? void 0 : receivingChain.hops) !== null && _receivingChain$hops !== void 0 ? _receivingChain$hops : []));
@@ -11220,7 +11117,6 @@ var getBestAmountOut = /*#__PURE__*/function () {
11220
11117
 
11221
11118
  var computeExchangeMinAmount = /*#__PURE__*/function () {
11222
11119
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(dex, options) {
11223
- var _result$fee;
11224
11120
  var exchange, existentialDeposit, nativeSymbol, isNativeAsset, _yield$getSwapFee, result, swapFee;
11225
11121
  return _regenerator().w(function (_context) {
11226
11122
  while (1) switch (_context.n) {
@@ -11242,7 +11138,7 @@ var computeExchangeMinAmount = /*#__PURE__*/function () {
11242
11138
  case 2:
11243
11139
  _yield$getSwapFee = _context.v;
11244
11140
  result = _yield$getSwapFee.result;
11245
- swapFee = (_result$fee = result.fee) !== null && _result$fee !== void 0 ? _result$fee : 0n;
11141
+ swapFee = result.fee;
11246
11142
  return _context.a(2, existentialDeposit + swapFee + 1n);
11247
11143
  }
11248
11144
  }, _callee);
@@ -11290,7 +11186,7 @@ var getMinTransferableAmount = /*#__PURE__*/function () {
11290
11186
 
11291
11187
  var computeLocalTransferableAmount = /*#__PURE__*/function () {
11292
11188
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(dex, options) {
11293
- var exchange, sender, currency, balance, existentialDeposit, swapFee, nativeSymbol, _result$fee, _yield$getSwapFee, result, transferable;
11189
+ var exchange, sender, currency, balance, existentialDeposit, swapFee, nativeSymbol, _yield$getSwapFee, result, transferable;
11294
11190
  return _regenerator().w(function (_context) {
11295
11191
  while (1) switch (_context.n) {
11296
11192
  case 0:
@@ -11319,7 +11215,7 @@ var computeLocalTransferableAmount = /*#__PURE__*/function () {
11319
11215
  case 2:
11320
11216
  _yield$getSwapFee = _context.v;
11321
11217
  result = _yield$getSwapFee.result;
11322
- swapFee = (_result$fee = result.fee) !== null && _result$fee !== void 0 ? _result$fee : 0n;
11218
+ swapFee = result.fee;
11323
11219
  case 3:
11324
11220
  transferable = balance - existentialDeposit - swapFee;
11325
11221
  return _context.a(2, transferable > 0n ? transferable : 0n);
@@ -11707,7 +11603,7 @@ var buildExchangeDestInfo = function buildExchangeDestInfo(exchange, recipient,
11707
11603
  var getSwapInfo = /*#__PURE__*/function () {
11708
11604
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(initialOptions) {
11709
11605
  var _origin$chain, _destination$chain, _sendingInfo$hops, _receivingInfo$hops, _sendingInfo$origin, _receivingInfo$destin;
11710
- var _yield$prepareTransfo, options, dex, api, origin, exchange, currencyTo, destination, amount, sender, evmSenderAddress, senderAddress, sendingInfo, _yield$getSwapFee, swapChain, amountOut, receivingInfo, originChain, destChain, exchangeHopEntry, hops, finalOrigin, finalDestination, _t, _t2, _t3, _t4, _t5;
11606
+ var _yield$prepareTransfo, options, dex, api, origin, exchange, currencyTo, destination, amount, sender, evmSenderAddress, recipient, senderAddress, sendingInfo, _yield$getSwapFee, swapChain, amountOut, receivingInfo, originChain, destChain, exchangeHopEntry, hops, finalOrigin, finalDestination, _t, _t2, _t3, _t4, _t5;
11711
11607
  return _regenerator().w(function (_context2) {
11712
11608
  while (1) switch (_context2.p = _context2.n) {
11713
11609
  case 0:
@@ -11737,7 +11633,7 @@ var getSwapInfo = /*#__PURE__*/function () {
11737
11633
  throw _t;
11738
11634
  case 5:
11739
11635
  validateRoutedFeeAsset(options);
11740
- api = options.api, origin = options.origin, exchange = options.exchange, currencyTo = options.currencyTo, destination = options.destination, amount = options.amount, sender = options.sender, evmSenderAddress = options.evmSenderAddress;
11636
+ api = options.api, origin = options.origin, exchange = options.exchange, currencyTo = options.currencyTo, destination = options.destination, amount = options.amount, sender = options.sender, evmSenderAddress = options.evmSenderAddress, recipient = options.recipient;
11741
11637
  assertCurrencyCore(currencyTo);
11742
11638
  senderAddress = evmSenderAddress !== null && evmSenderAddress !== void 0 ? evmSenderAddress : sender;
11743
11639
  if (!(origin && origin.chain !== exchange.chain)) {
@@ -11824,7 +11720,7 @@ var getSwapInfo = /*#__PURE__*/function () {
11824
11720
  break;
11825
11721
  case 16:
11826
11722
  _context2.n = 17;
11827
- return buildExchangeDestInfo(exchange, sender, amountOut, currencyTo);
11723
+ return buildExchangeDestInfo(exchange, recipient, amountOut, currencyTo);
11828
11724
  case 17:
11829
11725
  _t5 = _context2.v;
11830
11726
  case 18: