@paraspell/swap 14.5.3 → 14.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -24,6 +24,8 @@ type TSwapOptionsCommon<TApi, TRes, TSigner, TCustomChain extends string = never
24
24
  amount: bigint;
25
25
  slippagePct: string;
26
26
  sender: string;
27
+ recipient: string;
28
+ destination?: TDestinationInfo;
27
29
  feeCalcAddress: string;
28
30
  origin?: TOriginInfo<TApi>;
29
31
  isForFeeEstimation?: boolean;
@@ -151,6 +153,7 @@ type TAdditionalTransferOptions<TApi, TRes, TSigner, TCustomChain extends string
151
153
  origin?: TOriginInfo<TApi>;
152
154
  exchange: TExchangeInfo<TApi, TRes, TSigner, TCustomChain>;
153
155
  destination?: TDestinationInfo;
156
+ recipient: string;
154
157
  feeCalcAddress: string;
155
158
  };
156
159
  type TPairs = TLocation[][];
package/dist/index.mjs CHANGED
@@ -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';
@@ -364,7 +362,7 @@ function _toPrimitive(t, r) {
364
362
  if ("object" != typeof i) return i;
365
363
  throw new TypeError("@@toPrimitive must return a primitive value.");
366
364
  }
367
- return (String )(t);
365
+ return ("string" === r ? String : Number)(t);
368
366
  }
369
367
  function _toPropertyKey(t) {
370
368
  var i = _toPrimitive(t, "string");
@@ -2110,22 +2108,6 @@ var Hydration = {
2110
2108
  ]
2111
2109
  }
2112
2110
  },
2113
- {
2114
- parents: 1,
2115
- interior: {
2116
- X2: [
2117
- {
2118
- Parachain: 2034
2119
- },
2120
- {
2121
- AccountKey20: {
2122
- network: null,
2123
- key: "0xb94025f14fdc01f8e3b0c34311bcbae0febd1a4a"
2124
- }
2125
- }
2126
- ]
2127
- }
2128
- },
2129
2111
  {
2130
2112
  parents: 1,
2131
2113
  interior: {
@@ -2152,7 +2134,7 @@ var Hydration = {
2152
2134
  {
2153
2135
  AccountKey20: {
2154
2136
  network: null,
2155
- key: "0x3a31ff0a1e07da4158677ce861980cdf2e316b78"
2137
+ key: "0x2405024a3d2ecc0a97818490616bd8763652e1e5"
2156
2138
  }
2157
2139
  }
2158
2140
  ]
@@ -2518,6 +2500,22 @@ var Hydration = {
2518
2500
  ]
2519
2501
  }
2520
2502
  },
2503
+ {
2504
+ parents: 1,
2505
+ interior: {
2506
+ X2: [
2507
+ {
2508
+ Parachain: 2034
2509
+ },
2510
+ {
2511
+ AccountKey20: {
2512
+ network: null,
2513
+ key: "0xc92051fa011a1ce83f50a9b1ea8ebbddfa8ca272"
2514
+ }
2515
+ }
2516
+ ]
2517
+ }
2518
+ },
2521
2519
  {
2522
2520
  parents: 1,
2523
2521
  interior: {
@@ -2543,6 +2541,22 @@ var Hydration = {
2543
2541
  ]
2544
2542
  }
2545
2543
  },
2544
+ {
2545
+ parents: 1,
2546
+ interior: {
2547
+ X2: [
2548
+ {
2549
+ Parachain: 2034
2550
+ },
2551
+ {
2552
+ AccountKey20: {
2553
+ network: null,
2554
+ key: "0x8894940baa4c7c26d8d84247d93c6d2e40a5af4f"
2555
+ }
2556
+ }
2557
+ ]
2558
+ }
2559
+ },
2546
2560
  {
2547
2561
  parents: 1,
2548
2562
  interior: {
@@ -7911,6 +7925,7 @@ var Logger = {
7911
7925
  var ExchangeChain = /*#__PURE__*/function () {
7912
7926
  function ExchangeChain(chain) {
7913
7927
  _classCallCheck(this, ExchangeChain);
7928
+ this.supportsRecipient = false;
7914
7929
  this._chain = chain;
7915
7930
  }
7916
7931
  return _createClass(ExchangeChain, [{
@@ -7919,33 +7934,60 @@ var ExchangeChain = /*#__PURE__*/function () {
7919
7934
  return this._chain;
7920
7935
  }
7921
7936
  }, {
7922
- key: "createApiInstance",
7937
+ key: "swapCurrency",
7923
7938
  value: function () {
7924
- var _createApiInstance = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(builderOptions) {
7939
+ var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
7940
+ var sender, destination, recipient;
7925
7941
  return _regenerator().w(function (_context) {
7926
7942
  while (1) switch (_context.n) {
7927
7943
  case 0:
7928
- return _context.a(2, createChainClient(this.chain, builderOptions));
7944
+ sender = options.sender, destination = options.destination;
7945
+ recipient = destination ? sender : options.recipient;
7946
+ if (!(!this.supportsRecipient && recipient !== sender)) {
7947
+ _context.n = 1;
7948
+ break;
7949
+ }
7950
+ throw new UnsupportedOperationError("".concat(this.chain, " cannot deposit swapped tokens to a different recipient."));
7951
+ case 1:
7952
+ return _context.a(2, this.buildSwap(_objectSpread2(_objectSpread2({}, options), {}, {
7953
+ recipient: recipient
7954
+ }), toDestTransactionFee));
7929
7955
  }
7930
7956
  }, _callee, this);
7931
7957
  }));
7932
- function createApiInstance(_x) {
7933
- return _createApiInstance.apply(this, arguments);
7958
+ function swapCurrency(_x, _x2) {
7959
+ return _swapCurrency.apply(this, arguments);
7934
7960
  }
7935
- return createApiInstance;
7961
+ return swapCurrency;
7936
7962
  }()
7937
7963
  }, {
7938
- key: "createApiInstancePapi",
7964
+ key: "createApiInstance",
7939
7965
  value: function () {
7940
- var _createApiInstancePapi = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(builderOptions) {
7966
+ var _createApiInstance = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(builderOptions) {
7941
7967
  return _regenerator().w(function (_context2) {
7942
7968
  while (1) switch (_context2.n) {
7943
7969
  case 0:
7944
- return _context2.a(2, createChainClient$1(this.chain, builderOptions));
7970
+ return _context2.a(2, createChainClient(this.chain, builderOptions));
7945
7971
  }
7946
7972
  }, _callee2, this);
7947
7973
  }));
7948
- function createApiInstancePapi(_x2) {
7974
+ function createApiInstance(_x3) {
7975
+ return _createApiInstance.apply(this, arguments);
7976
+ }
7977
+ return createApiInstance;
7978
+ }()
7979
+ }, {
7980
+ key: "createApiInstancePapi",
7981
+ value: function () {
7982
+ var _createApiInstancePapi = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(builderOptions) {
7983
+ return _regenerator().w(function (_context3) {
7984
+ while (1) switch (_context3.n) {
7985
+ case 0:
7986
+ return _context3.a(2, createChainClient$1(this.chain, builderOptions));
7987
+ }
7988
+ }, _callee3, this);
7989
+ }));
7990
+ function createApiInstancePapi(_x4) {
7949
7991
  return _createApiInstancePapi.apply(this, arguments);
7950
7992
  }
7951
7993
  return createApiInstancePapi;
@@ -8149,9 +8191,9 @@ var AcalaExchange = /*#__PURE__*/function (_ExchangeChain) {
8149
8191
  }
8150
8192
  _inherits(AcalaExchange, _ExchangeChain);
8151
8193
  return _createClass(AcalaExchange, [{
8152
- key: "swapCurrency",
8194
+ key: "buildSwap",
8153
8195
  value: function () {
8154
- var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
8196
+ var _buildSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
8155
8197
  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;
8156
8198
  return _regenerator().w(function (_context) {
8157
8199
  while (1) switch (_context.n) {
@@ -8238,10 +8280,10 @@ var AcalaExchange = /*#__PURE__*/function (_ExchangeChain) {
8238
8280
  }
8239
8281
  }, _callee, this);
8240
8282
  }));
8241
- function swapCurrency(_x, _x2) {
8242
- return _swapCurrency.apply(this, arguments);
8283
+ function buildSwap(_x, _x2) {
8284
+ return _buildSwap.apply(this, arguments);
8243
8285
  }
8244
- return swapCurrency;
8286
+ return buildSwap;
8245
8287
  }()
8246
8288
  }, {
8247
8289
  key: "getAmountOut",
@@ -8413,6 +8455,7 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8413
8455
  _classCallCheck(this, AssetHubExchange);
8414
8456
  _this = _callSuper(this, AssetHubExchange, arguments);
8415
8457
  _this.apiType = 'GENERIC';
8458
+ _this.supportsRecipient = true;
8416
8459
  return _this;
8417
8460
  }
8418
8461
  _inherits(AssetHubExchange, _ExchangeChain);
@@ -8521,14 +8564,14 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8521
8564
  return quoteSwapOrThrow;
8522
8565
  }()
8523
8566
  }, {
8524
- key: "swapCurrency",
8567
+ key: "buildSwap",
8525
8568
  value: function () {
8526
- var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(options, toDestTxFee) {
8527
- var api, assetFrom, assetTo, amount, sender, slippagePct, origin, nativeAsset, pctDestFee, amountWithoutFee, _yield$this$quoteSwap, path, amountOut, slippageMultiplier, minAmountOut, tx, toDestFeeCurrencyTo, finalAmountOut, _t6;
8569
+ var _buildSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(options, toDestTxFee) {
8570
+ var api, assetFrom, assetTo, amount, recipient, slippagePct, origin, nativeAsset, pctDestFee, amountWithoutFee, _yield$this$quoteSwap, path, amountOut, slippageMultiplier, minAmountOut, tx, toDestFeeCurrencyTo, finalAmountOut, _t6;
8528
8571
  return _regenerator().w(function (_context3) {
8529
8572
  while (1) switch (_context3.n) {
8530
8573
  case 0:
8531
- api = options.api, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, sender = options.sender, slippagePct = options.slippagePct, origin = options.origin;
8574
+ api = options.api, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, recipient = options.recipient, slippagePct = options.slippagePct, origin = options.origin;
8532
8575
  nativeAsset = findNativeAssetInfoOrThrow(this.chain);
8533
8576
  pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
8534
8577
  amountWithoutFee = padValueBy(amount, pctDestFee);
@@ -8547,7 +8590,7 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8547
8590
  path: path,
8548
8591
  amount_in: amountWithoutFee,
8549
8592
  amount_out_min: minAmountOut,
8550
- send_to: sender,
8593
+ send_to: recipient,
8551
8594
  keep_alive: !!assetFrom.isNative
8552
8595
  }
8553
8596
  });
@@ -8579,20 +8622,20 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8579
8622
  }
8580
8623
  }, _callee3, this);
8581
8624
  }));
8582
- function swapCurrency(_x0, _x1) {
8583
- return _swapCurrency.apply(this, arguments);
8625
+ function buildSwap(_x0, _x1) {
8626
+ return _buildSwap.apply(this, arguments);
8584
8627
  }
8585
- return swapCurrency;
8628
+ return buildSwap;
8586
8629
  }()
8587
8630
  }, {
8588
8631
  key: "getAmountOut",
8589
8632
  value: function () {
8590
8633
  var _getAmountOut = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(options) {
8591
- var api, assetFrom, assetTo, amount, origin, pctDestFee, amountWithoutFee, _yield$this$quoteSwap2, amountOut;
8634
+ var api, assetFrom, assetTo, amount, origin, _options$slippagePct, slippagePct, pctDestFee, amountWithoutFee, _yield$this$quoteSwap2, amountOut;
8592
8635
  return _regenerator().w(function (_context4) {
8593
8636
  while (1) switch (_context4.n) {
8594
8637
  case 0:
8595
- api = options.api, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, origin = options.origin;
8638
+ 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;
8596
8639
  pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
8597
8640
  amountWithoutFee = padValueBy(amount, pctDestFee);
8598
8641
  _context4.n = 1;
@@ -8600,7 +8643,7 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8600
8643
  case 1:
8601
8644
  _yield$this$quoteSwap2 = _context4.v;
8602
8645
  amountOut = _yield$this$quoteSwap2.amountOut;
8603
- return _context4.a(2, amountOut);
8646
+ return _context4.a(2, padValueBy(amountOut, -Number(slippagePct)));
8604
8647
  }
8605
8648
  }, _callee4, this);
8606
8649
  }));
@@ -8628,328 +8671,241 @@ var AssetHubExchange = /*#__PURE__*/function (_ExchangeChain) {
8628
8671
  }]);
8629
8672
  }(ExchangeChain);
8630
8673
 
8631
- var fetchCallMulti = /*#__PURE__*/function () {
8632
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
8633
- var api, calls, _ref$options, options, included, filtered, values, transformedValues, _t;
8634
- return _regenerator().w(function (_context) {
8635
- while (1) switch (_context.p = _context.n) {
8636
- case 0:
8637
- api = _ref.api, calls = _ref.calls, _ref$options = _ref.options, options = _ref$options === void 0 ? {} : _ref$options;
8638
- if (!(!calls || calls.length === 0 || !api.isConnected)) {
8639
- _context.n = 1;
8640
- break;
8641
- }
8642
- return _context.a(2, options.defaultValue);
8643
- case 1:
8644
- included = calls.map(function (c) {
8645
- return !!c && (!Array.isArray(c) || !!c[0]);
8646
- });
8647
- filtered = calls.filter(function (_, index) {
8648
- return included[index];
8649
- });
8650
- if (!(filtered.length > 0)) {
8651
- _context.n = 5;
8652
- break;
8653
- }
8654
- _context.p = 2;
8655
- _context.n = 3;
8656
- return api.queryMulti(filtered);
8657
- case 3:
8658
- values = _context.v;
8659
- transformedValues = options.transform ? options.transform(values, api) : values;
8660
- return _context.a(2, transformedValues);
8661
- case 4:
8662
- _context.p = 4;
8663
- _t = _context.v;
8664
- // eslint-disable-next-line no-console
8665
- console.error(_t);
8666
- return _context.a(2, options.defaultValue);
8667
- case 5:
8668
- return _context.a(2, options.defaultValue);
8669
- case 6:
8670
- return _context.a(2);
8671
- }
8672
- }, _callee, null, [[2, 4]]);
8674
+ var TOKEN_LIST_NAMES = {
8675
+ BifrostPolkadot: 'bifrost-polkadot',
8676
+ BifrostKusama: 'bifrost-kusama'
8677
+ };
8678
+ var getTokenMap = function getTokenMap(chain, chainId) {
8679
+ var listName = TOKEN_LIST_NAMES[chain];
8680
+ if (!listName) throw new RoutingResolutionError("Bifrost token list not found for ".concat(chain));
8681
+ var list = DEFULT_TOKEN_LIST_MAP[listName];
8682
+ return Object.fromEntries(list.tokens.map(function (info) {
8683
+ return new WrappedTokenInfo(info, list);
8684
+ }).filter(function (token) {
8685
+ return token.chainId === chainId;
8686
+ }).map(function (token) {
8687
+ return [token.address, token];
8673
8688
  }));
8674
- return function fetchCallMulti(_x) {
8675
- return _ref2.apply(this, arguments);
8676
- };
8677
- }();
8678
-
8679
- var getPairs = function getPairs(chainId, currencies) {
8680
- return currencies.filter(function (currencies) {
8681
- var _currencies = _slicedToArray(currencies, 2),
8682
- currencyA = _currencies[0],
8683
- currencyB = _currencies[1];
8684
- 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));
8685
- }).reduce(function (acc, _ref) {
8686
- var _ref2 = _slicedToArray(_ref, 2),
8687
- currencyA = _ref2[0],
8688
- currencyB = _ref2[1];
8689
- var _ref3 = currencyA.wrapped.sortsBefore(currencyB.wrapped) ? [currencyA.wrapped, currencyB.wrapped] : [currencyB.wrapped, currencyA.wrapped],
8690
- _ref4 = _slicedToArray(_ref3, 2),
8691
- token0 = _ref4[0],
8692
- token1 = _ref4[1];
8693
- acc[0].push(token0);
8694
- acc[1].push(token1);
8695
- acc[2].push([addressToZenlinkAssetId(token0.address), addressToZenlinkAssetId(token1.address)]);
8696
- return acc;
8697
- }, [[], [], []]);
8698
8689
  };
8699
- var uniqePairKey = function uniqePairKey(tokenA, tokenB) {
8700
- return "".concat(tokenA.address, "-").concat(tokenB.address);
8690
+ var findToken = function findToken(tokenMap, symbol) {
8691
+ return Object.values(tokenMap).find(function (token) {
8692
+ return isSymbolMatch(token.symbol, symbol);
8693
+ });
8701
8694
  };
8702
- var PairState;
8703
- (function (PairState) {
8704
- PairState[PairState["LOADING"] = 0] = "LOADING";
8705
- PairState[PairState["NOT_EXISTS"] = 1] = "NOT_EXISTS";
8706
- PairState[PairState["EXISTS"] = 2] = "EXISTS";
8707
- PairState[PairState["INVALID"] = 3] = "INVALID";
8708
- })(PairState || (PairState = {}));
8709
- var fetchPairs = /*#__PURE__*/function () {
8710
- var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(api, chainId, currencies) {
8711
- var _getPairs, _getPairs2, tokensA, tokensB, _tokensA$reduce, _tokensA$reduce2, validTokensA, validTokensB, reservesCalls, reserves;
8695
+ var balanceQuery = function balanceQuery(api, account, token) {
8696
+ return isNativeCurrency(token) ? [api.query.system.account, account] : [api.query.tokens.accounts, [account, addressToNodeCurrency(token.address)]];
8697
+ };
8698
+ var freeBalance = function freeBalance(reserve) {
8699
+ return ('data' in reserve ? reserve.data.free : reserve.free).toString();
8700
+ };
8701
+ var getFilteredPairs = /*#__PURE__*/function () {
8702
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(api, chainId, currencies) {
8703
+ var _getPairs, _getPairs2, tokensA, tokensB, pools, poolList, reserves;
8712
8704
  return _regenerator().w(function (_context) {
8713
8705
  while (1) switch (_context.n) {
8714
8706
  case 0:
8715
8707
  _getPairs = getPairs(chainId, currencies), _getPairs2 = _slicedToArray(_getPairs, 2), tokensA = _getPairs2[0], tokensB = _getPairs2[1];
8716
- _tokensA$reduce = tokensA.reduce(function (acc, tokenA, i) {
8717
- var _PAIR_ADDRESSES$pairK;
8708
+ pools = new Map();
8709
+ tokensA.forEach(function (tokenA, i) {
8718
8710
  var tokenB = tokensB[i];
8719
- var pairKey = uniqePairKey(tokenA, tokenB);
8720
- var pairAccount = (_PAIR_ADDRESSES$pairK = PAIR_ADDRESSES[pairKey]) === null || _PAIR_ADDRESSES$pairK === void 0 ? void 0 : _PAIR_ADDRESSES$pairK.account;
8721
- if (pairAccount && api) {
8722
- acc[0].push(tokenA);
8723
- acc[1].push(tokenB);
8724
- if (isNativeCurrency(tokenA)) acc[2].push([api.query.system.account, pairAccount]);else acc[2].push([api.query.tokens.accounts, [pairAccount, addressToNodeCurrency(tokenA.address)]]);
8725
- if (isNativeCurrency(tokenB)) acc[2].push([api.query.system.account, pairAccount]);else acc[2].push([api.query.tokens.accounts, [pairAccount, addressToNodeCurrency(tokenB.address)]]);
8726
- }
8727
- return acc;
8728
- }, [[], [], []]), _tokensA$reduce2 = _slicedToArray(_tokensA$reduce, 3), validTokensA = _tokensA$reduce2[0], validTokensB = _tokensA$reduce2[1], reservesCalls = _tokensA$reduce2[2];
8729
- _context.n = 1;
8730
- return fetchCallMulti({
8731
- api: api,
8732
- calls: reservesCalls,
8733
- options: {
8734
- defaultValue: []
8735
- }
8711
+ var key = uniqePairKey(tokenA, tokenB);
8712
+ var pool = PAIR_ADDRESSES[key];
8713
+ if (pool) pools.set(key, _objectSpread2({
8714
+ tokenA: tokenA,
8715
+ tokenB: tokenB
8716
+ }, pool));
8736
8717
  });
8737
- case 1:
8738
- reserves = _context.v;
8739
- if (!(reservesCalls.length === 0)) {
8740
- _context.n = 2;
8741
- break;
8742
- }
8743
- return _context.a(2, [[PairState.NOT_EXISTS, null]]);
8744
- case 2:
8745
- if (!(!reserves.length || reserves.length !== validTokensA.length * 2)) {
8746
- _context.n = 3;
8718
+ if (!(pools.size === 0)) {
8719
+ _context.n = 1;
8747
8720
  break;
8748
8721
  }
8749
- return _context.a(2, validTokensA.map(function () {
8750
- return [PairState.LOADING, null];
8722
+ return _context.a(2, []);
8723
+ case 1:
8724
+ poolList = Array.from(pools.values());
8725
+ _context.n = 2;
8726
+ return api.queryMulti(poolList.flatMap(function (_ref2) {
8727
+ var tokenA = _ref2.tokenA,
8728
+ tokenB = _ref2.tokenB,
8729
+ account = _ref2.account;
8730
+ return [balanceQuery(api, account, tokenA), balanceQuery(api, account, tokenB)];
8751
8731
  }));
8752
- case 3:
8753
- return _context.a(2, validTokensA.map(function (tokenA, i) {
8754
- var _PAIR_ADDRESSES$pairK2;
8755
- var tokenB = validTokensB[i];
8756
- if (!tokenA || !tokenB || tokenA.equals(tokenB)) return [PairState.INVALID, null];
8757
- var pairKey = uniqePairKey(tokenA, tokenB);
8758
- var reserve0 = reserves[i * 2];
8759
- var reserve1 = reserves[i * 2 + 1];
8760
- var pairAddress = (_PAIR_ADDRESSES$pairK2 = PAIR_ADDRESSES[pairKey]) === null || _PAIR_ADDRESSES$pairK2 === void 0 ? void 0 : _PAIR_ADDRESSES$pairK2.address;
8761
- if (!reserve0 || !reserve1 || reserve0.isEmpty || reserve1.isEmpty || !pairAddress) return [PairState.NOT_EXISTS, null];
8762
- return [PairState.EXISTS, new Pair(Amount.fromRawAmount(tokenA, (reserve0.data || reserve0).free.toString()), Amount.fromRawAmount(tokenB, (reserve1.data || reserve1).free.toString()), pairAddress)];
8732
+ case 2:
8733
+ reserves = _context.v;
8734
+ return _context.a(2, poolList.flatMap(function (_ref3, i) {
8735
+ var tokenA = _ref3.tokenA,
8736
+ tokenB = _ref3.tokenB,
8737
+ address = _ref3.address;
8738
+ var reserveA = reserves[i * 2];
8739
+ var reserveB = reserves[i * 2 + 1];
8740
+ if (reserveA.isEmpty || reserveB.isEmpty) return [];
8741
+ return [new Pair(Amount.fromRawAmount(tokenA, freeBalance(reserveA)), Amount.fromRawAmount(tokenB, freeBalance(reserveB)), address)];
8763
8742
  }));
8764
8743
  }
8765
8744
  }, _callee);
8766
8745
  }));
8767
- return function fetchPairs(_x, _x2, _x3) {
8768
- return _ref5.apply(this, arguments);
8746
+ return function getFilteredPairs(_x, _x2, _x3) {
8747
+ return _ref.apply(this, arguments);
8769
8748
  };
8770
8749
  }();
8771
- var tokensToChainTokenMap = function tokensToChainTokenMap(tokens) {
8772
- var _ref6 = [tokens, tokens.tokens],
8773
- list = _ref6[0],
8774
- infos = _ref6[1];
8775
- var map = infos.reduce(function (map, info) {
8776
- var _map$token$chainId;
8777
- var token = new WrappedTokenInfo(info, list);
8778
- if (((_map$token$chainId = map[token.chainId]) === null || _map$token$chainId === void 0 ? void 0 : _map$token$chainId[token.address]) !== undefined) {
8779
- // eslint-disable-next-line no-console
8780
- console.warn("Duplicate token skipped: ".concat(token.address));
8781
- return map;
8782
- }
8783
- if (!map[token.chainId]) map[token.chainId] = {};
8784
- map[token.chainId][token.address] = {
8785
- token: token,
8786
- list: list
8787
- };
8788
- return map;
8789
- }, {});
8790
- return map;
8791
- };
8792
- var getTokenMap = function getTokenMap(chain, chainId) {
8793
- var _tokenMap$chainId;
8794
- var map = DEFULT_TOKEN_LIST_MAP[chain === 'BifrostPolkadot' ? 'bifrost-polkadot' : 'bifrost-kusama'];
8795
- var tokenMap = tokensToChainTokenMap(map);
8796
- return Object.keys((_tokenMap$chainId = tokenMap[chainId]) !== null && _tokenMap$chainId !== void 0 ? _tokenMap$chainId : {}).reduce(function (newMap, address) {
8797
- newMap[address] = tokenMap[chainId][address].token;
8798
- return newMap;
8799
- }, {});
8800
- };
8801
- var getFilteredPairs = /*#__PURE__*/function () {
8802
- var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(api, chainId, currencies) {
8803
- var pairs;
8804
- return _regenerator().w(function (_context2) {
8805
- while (1) switch (_context2.n) {
8806
- case 0:
8807
- _context2.n = 1;
8808
- return fetchPairs(api, chainId, currencies);
8809
- case 1:
8810
- pairs = _context2.v;
8811
- return _context2.a(2, Object.values(pairs.filter(function (result) {
8812
- return Boolean(result[0] === PairState.EXISTS && result[1]);
8813
- }).map(function (_ref8) {
8814
- var _ref9 = _slicedToArray(_ref8, 2),
8815
- pair = _ref9[1];
8816
- return pair;
8817
- })));
8818
- }
8819
- }, _callee2);
8750
+ var selectPairs = function selectPairs(pairs, chainId, currencies) {
8751
+ var _getPairs3 = getPairs(chainId, currencies),
8752
+ _getPairs4 = _slicedToArray(_getPairs3, 2),
8753
+ tokensA = _getPairs4[0],
8754
+ tokensB = _getPairs4[1];
8755
+ var keys = new Set(tokensA.map(function (tokenA, i) {
8756
+ return uniqePairKey(tokenA, tokensB[i]);
8820
8757
  }));
8821
- return function getFilteredPairs(_x4, _x5, _x6) {
8822
- return _ref7.apply(this, arguments);
8823
- };
8824
- }();
8825
- var getBestTrade = function getBestTrade(chainId, pairs, amountIn, currencyOut) {
8826
- var trades = Trade.bestTradeExactIn(chainId, pairs, [], amountIn, currencyOut);
8827
- if (trades.length < 1) {
8828
- throw new RoutingResolutionError('Trade not found');
8829
- }
8830
- return trades[0];
8758
+ return pairs.filter(function (pair) {
8759
+ return keys.has(uniqePairKey(pair.token0, pair.token1));
8760
+ });
8831
8761
  };
8832
- var findToken = function findToken(tokenMap, symbol) {
8833
- var _Object$values$find;
8834
- return (_Object$values$find = Object.values(tokenMap).find(function (item) {
8835
- return item.wrapped.symbol != null && isSymbolMatch(item.wrapped.symbol, symbol);
8836
- })) === null || _Object$values$find === void 0 ? void 0 : _Object$values$find.wrapped;
8762
+ var getBestTrade = function getBestTrade(chainId, pairs, amountIn, currencyOut) {
8763
+ var _ref4 = pairs.length ? Trade.bestTradeExactIn(chainId, pairs, [], amountIn, currencyOut) : [],
8764
+ _ref5 = _slicedToArray(_ref4, 1),
8765
+ bestTrade = _ref5[0];
8766
+ if (!bestTrade) throw new RoutingResolutionError('Trade not found');
8767
+ return bestTrade;
8837
8768
  };
8838
8769
 
8770
+ var pairKey = function pairKey(symbolA, symbolB) {
8771
+ return [symbolA, symbolB].map(function (s) {
8772
+ return s.toLowerCase();
8773
+ }).sort().join('-');
8774
+ };
8839
8775
  var getDexConfig = /*#__PURE__*/function () {
8840
8776
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(api, chain) {
8841
- var chainId, tokenMap, sdkAssets, assetsMap, assets, pairEntries, tokensList, pairs, seen, findTokenByDesc, syntheticPairs, i, j, a, b, dedup, wrapA, wrapB, tokA, tokB, combos, filtered, minUnit;
8777
+ 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;
8842
8778
  return _regenerator().w(function (_context) {
8843
- while (1) switch (_context.p = _context.n) {
8779
+ while (1) switch (_context.n) {
8844
8780
  case 0:
8845
8781
  chainId = getParaId(chain);
8846
8782
  tokenMap = getTokenMap(chain, chainId);
8783
+ tokens = Object.values(tokenMap);
8847
8784
  sdkAssets = getAssets(chain);
8848
8785
  assetsMap = new Map();
8849
- Object.values(tokenMap).forEach(function (wrapped) {
8850
- var symbol = wrapped.wrapped.symbol;
8851
- if (!symbol) return;
8786
+ tokens.forEach(function (_ref2) {
8787
+ var symbol = _ref2.symbol;
8852
8788
  var sdkAsset = sdkAssets.find(function (a) {
8853
8789
  return isSymbolMatch(a.symbol, symbol);
8854
8790
  });
8855
- if (!sdkAsset) return;
8856
- assetsMap.set(symbol, sdkAsset);
8791
+ if (sdkAsset) assetsMap.set(symbol, sdkAsset);
8857
8792
  });
8858
8793
  assets = Array.from(assetsMap.values());
8794
+ findTokenByAssetId = function findTokenByAssetId(_ref3) {
8795
+ var chainId = _ref3.chainId,
8796
+ assetType = _ref3.assetType,
8797
+ assetIndex = _ref3.assetIndex;
8798
+ return tokens.find(function (_ref4) {
8799
+ var tokenInfo = _ref4.tokenInfo;
8800
+ return tokenInfo.parachainId === chainId && tokenInfo.assetType === assetType && tokenInfo.assetIndex === assetIndex;
8801
+ });
8802
+ };
8803
+ pairs = [];
8804
+ seen = new Set();
8859
8805
  _context.n = 1;
8860
8806
  return api.query.zenlinkProtocol.pairStatuses.entries();
8861
8807
  case 1:
8862
8808
  pairEntries = _context.v;
8863
- tokensList = Object.values(tokenMap);
8864
- pairs = [];
8865
- seen = new Set();
8866
- findTokenByDesc = function findTokenByDesc(d) {
8867
- return tokensList.find(function (tok) {
8868
- var info = tok.wrapped.tokenInfo;
8869
- return info.parachainId === d.chainId && info.assetType === d.assetType && info.assetIndex === d.assetIndex;
8870
- });
8871
- };
8872
- pairEntries.forEach(function (_ref2) {
8873
- var _ref3 = _slicedToArray(_ref2, 1),
8874
- _ref3$0$args = _slicedToArray(_ref3[0].args, 1),
8875
- keys = _ref3$0$args[0];
8809
+ pairEntries.forEach(function (_ref5) {
8810
+ var _ref6 = _slicedToArray(_ref5, 1),
8811
+ _ref6$0$args = _slicedToArray(_ref6[0].args, 1),
8812
+ keys = _ref6$0$args[0];
8876
8813
  var _keys$toJSON = keys.toJSON(),
8877
8814
  _keys$toJSON2 = _slicedToArray(_keys$toJSON, 2),
8878
- assetAData = _keys$toJSON2[0],
8879
- assetBData = _keys$toJSON2[1];
8880
- var tokA = findTokenByDesc(assetAData);
8881
- var tokB = findTokenByDesc(assetBData);
8882
- if (!tokA || !tokB) return;
8883
- var symA = tokA.wrapped.symbol;
8884
- var symB = tokB.wrapped.symbol;
8885
- var raA = assetsMap.get(symA);
8886
- var raB = assetsMap.get(symB);
8887
- if (!raA || !raB) return;
8888
- var dedup = [symA, symB].map(function (s) {
8889
- return s.toLowerCase();
8890
- }).sort().join('-');
8891
- if (seen.has(dedup)) return;
8892
- seen.add(dedup);
8893
- pairs.push([raA.location, raB.location]);
8815
+ assetIdA = _keys$toJSON2[0],
8816
+ assetIdB = _keys$toJSON2[1];
8817
+ var tokenA = findTokenByAssetId(assetIdA);
8818
+ var tokenB = findTokenByAssetId(assetIdB);
8819
+ if (!tokenA || !tokenB) return;
8820
+ var assetA = assetsMap.get(tokenA.symbol);
8821
+ var assetB = assetsMap.get(tokenB.symbol);
8822
+ if (!assetA || !assetB) return;
8823
+ var key = pairKey(tokenA.symbol, tokenB.symbol);
8824
+ if (seen.has(key)) return;
8825
+ seen.add(key);
8826
+ pairs.push([assetA.location, assetB.location]);
8894
8827
  });
8895
- syntheticPairs = [];
8828
+ candidates = [];
8896
8829
  i = 0;
8897
8830
  case 2:
8898
8831
  if (!(i < assets.length)) {
8899
- _context.n = 11;
8832
+ _context.n = 8;
8900
8833
  break;
8901
8834
  }
8902
8835
  j = i + 1;
8903
8836
  case 3:
8904
8837
  if (!(j < assets.length)) {
8905
- _context.n = 10;
8838
+ _context.n = 7;
8906
8839
  break;
8907
8840
  }
8908
8841
  a = assets[i];
8909
8842
  b = assets[j];
8910
- dedup = [a.symbol, b.symbol].map(function (s) {
8911
- return s.toLowerCase();
8912
- }).sort().join('-');
8913
- if (!seen.has(dedup)) {
8843
+ if (!seen.has(pairKey(a.symbol, b.symbol))) {
8914
8844
  _context.n = 4;
8915
8845
  break;
8916
8846
  }
8917
- return _context.a(3, 9);
8847
+ return _context.a(3, 6);
8918
8848
  case 4:
8919
- wrapA = findToken(tokenMap, a.symbol);
8920
- wrapB = findToken(tokenMap, b.symbol);
8921
- if (!(!wrapA || !wrapB)) {
8849
+ tokenInfoA = findToken(tokenMap, a.symbol);
8850
+ tokenInfoB = findToken(tokenMap, b.symbol);
8851
+ if (!(!tokenInfoA || !tokenInfoB)) {
8922
8852
  _context.n = 5;
8923
8853
  break;
8924
8854
  }
8925
- return _context.a(3, 9);
8855
+ return _context.a(3, 6);
8926
8856
  case 5:
8927
- tokA = new Token(wrapA.wrapped);
8928
- tokB = new Token(wrapB.wrapped);
8929
- _context.p = 6;
8930
- combos = getCurrencyCombinations(chainId, tokA, tokB);
8931
- _context.n = 7;
8932
- return getFilteredPairs(api, chainId, combos);
8933
- case 7:
8934
- filtered = _context.v;
8935
- minUnit = Amount.fromRawAmount(tokA, '1');
8936
- getBestTrade(chainId, filtered, minUnit, tokB);
8937
- syntheticPairs.push([a.location, b.location]);
8938
- seen.add(dedup);
8939
- _context.n = 9;
8940
- break;
8941
- case 8:
8942
- _context.p = 8;
8943
- _context.v;
8944
- case 9:
8857
+ tokenA = new Token(tokenInfoA);
8858
+ tokenB = new Token(tokenInfoB);
8859
+ candidates.push({
8860
+ a: a,
8861
+ b: b,
8862
+ tokenA: tokenA,
8863
+ tokenB: tokenB,
8864
+ combos: getCurrencyCombinations(chainId, tokenA, tokenB)
8865
+ });
8866
+ case 6:
8945
8867
  j++;
8946
8868
  _context.n = 3;
8947
8869
  break;
8948
- case 10:
8870
+ case 7:
8949
8871
  i++;
8950
8872
  _context.n = 2;
8951
8873
  break;
8874
+ case 8:
8875
+ _context.n = 9;
8876
+ return getFilteredPairs(api, chainId, candidates.flatMap(function (_ref7) {
8877
+ var combos = _ref7.combos;
8878
+ return combos;
8879
+ }));
8880
+ case 9:
8881
+ pools = _context.v;
8882
+ syntheticPairs = [];
8883
+ _i = 0, _candidates = candidates;
8884
+ case 10:
8885
+ if (!(_i < _candidates.length)) {
8886
+ _context.n = 13;
8887
+ break;
8888
+ }
8889
+ _candidates$_i = _candidates[_i], _a = _candidates$_i.a, _b = _candidates$_i.b, _tokenA = _candidates$_i.tokenA, _tokenB = _candidates$_i.tokenB, combos = _candidates$_i.combos;
8890
+ key = pairKey(_a.symbol, _b.symbol);
8891
+ if (!seen.has(key)) {
8892
+ _context.n = 11;
8893
+ break;
8894
+ }
8895
+ return _context.a(3, 12);
8952
8896
  case 11:
8897
+ try {
8898
+ getBestTrade(chainId, selectPairs(pools, chainId, combos), Amount.fromRawAmount(_tokenA, '1'), _tokenB);
8899
+ syntheticPairs.push([_a.location, _b.location]);
8900
+ seen.add(key);
8901
+ } catch (_unused) {
8902
+ /* no route ⇒ skip */
8903
+ }
8904
+ case 12:
8905
+ _i++;
8906
+ _context.n = 10;
8907
+ break;
8908
+ case 13:
8953
8909
  return _context.a(2, {
8954
8910
  isOmni: false,
8955
8911
  assets: assets.map(function (a) {
@@ -8958,7 +8914,7 @@ var getDexConfig = /*#__PURE__*/function () {
8958
8914
  pairs: [].concat(pairs, syntheticPairs)
8959
8915
  });
8960
8916
  }
8961
- }, _callee, null, [[6, 8]]);
8917
+ }, _callee);
8962
8918
  }));
8963
8919
  return function getDexConfig(_x, _x2) {
8964
8920
  return _ref.apply(this, arguments);
@@ -8971,91 +8927,72 @@ var BifrostExchange = /*#__PURE__*/function (_ExchangeChain) {
8971
8927
  _classCallCheck(this, BifrostExchange);
8972
8928
  _this = _callSuper(this, BifrostExchange, arguments);
8973
8929
  _this.apiType = 'PJS';
8930
+ _this.supportsRecipient = true;
8974
8931
  return _this;
8975
8932
  }
8976
8933
  _inherits(BifrostExchange, _ExchangeChain);
8977
8934
  return _createClass(BifrostExchange, [{
8978
- key: "swapCurrency",
8935
+ key: "buildSwap",
8979
8936
  value: function () {
8980
- var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTxFee) {
8981
- 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;
8937
+ var _buildSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTxFee) {
8938
+ var apiPjs, assetFrom, assetTo, amount, recipient, slippagePct, origin, amountWithoutFee, _yield$this$findBestT, trade, tokenTo, amountOut, allowedSlippage, blockNumber, deadline, _SwapRouter$swapCallP, extrinsic, nativeSymbol, amountOutWithFee;
8982
8939
  return _regenerator().w(function (_context) {
8983
8940
  while (1) switch (_context.n) {
8984
8941
  case 0:
8985
- apiPjs = options.apiPjs, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, sender = options.sender, slippagePct = options.slippagePct, origin = options.origin;
8986
- chainId = getParaId(this.chain);
8987
- tokenMap = getTokenMap(this.chain, chainId);
8988
- tokenWrappedFrom = findToken(tokenMap, assetFrom.symbol);
8989
- if (!(tokenWrappedFrom === undefined)) {
8990
- _context.n = 1;
8991
- break;
8992
- }
8993
- throw new RoutingResolutionError('Currency from not found');
8994
- case 1:
8995
- tokenWrappedTo = findToken(tokenMap, assetTo.symbol);
8996
- if (!(tokenWrappedTo === undefined)) {
8997
- _context.n = 2;
8998
- break;
8999
- }
9000
- throw new RoutingResolutionError('Currency to not found');
9001
- case 2:
9002
- tokenFrom = new Token(tokenWrappedFrom.wrapped);
9003
- tokenTo = new Token(tokenWrappedTo.wrapped);
9004
- currencyCombinations = getCurrencyCombinations(chainId, tokenFrom, tokenTo);
9005
- _context.n = 3;
9006
- return getFilteredPairs(apiPjs, chainId, currencyCombinations);
9007
- case 3:
9008
- pairs = _context.v;
8942
+ apiPjs = options.apiPjs, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, recipient = options.recipient, slippagePct = options.slippagePct, origin = options.origin;
9009
8943
  Logger.log('To dest tx fee in native currency:', toDestTxFee);
9010
8944
  Logger.log('Original amount', amount);
9011
- pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
9012
- amountWithoutFee = padValueBy(amount, pctDestFee);
8945
+ amountWithoutFee = padValueBy(amount, origin ? DEST_FEE_BUFFER_PCT : 0);
9013
8946
  Logger.log('Amount modified', amountWithoutFee);
9014
8947
  if (!(amountWithoutFee <= 0n)) {
9015
- _context.n = 4;
8948
+ _context.n = 1;
9016
8949
  break;
9017
8950
  }
9018
8951
  throw new AmountTooLowError();
9019
- case 4:
9020
- amountInFinal = Amount.fromRawAmount(tokenFrom, amountWithoutFee.toString());
9021
- trade = getBestTrade(chainId, pairs, amountInFinal, tokenTo);
8952
+ case 1:
8953
+ _context.n = 2;
8954
+ return this.findBestTrade(apiPjs, assetFrom.symbol, assetTo.symbol, amountWithoutFee);
8955
+ case 2:
8956
+ _yield$this$findBestT = _context.v;
8957
+ trade = _yield$this$findBestT.trade;
8958
+ tokenTo = _yield$this$findBestT.tokenTo;
8959
+ amountOut = _yield$this$findBestT.amountOut;
9022
8960
  allowedSlippage = new Percent(Number(slippagePct) * 100, 10000);
9023
- _context.n = 5;
8961
+ _context.n = 3;
9024
8962
  return apiPjs.derive.chain.bestNumber();
9025
- case 5:
8963
+ case 3:
9026
8964
  blockNumber = _context.v;
9027
8965
  deadline = blockNumber.toNumber() + 20;
9028
8966
  _SwapRouter$swapCallP = SwapRouter.swapCallParameters(trade, {
9029
8967
  api: apiPjs,
9030
8968
  allowedSlippage: allowedSlippage,
9031
- recipient: sender,
8969
+ recipient: recipient,
9032
8970
  deadline: deadline
9033
8971
  }), extrinsic = _SwapRouter$swapCallP.extrinsic;
9034
8972
  if (!(extrinsic === null)) {
9035
- _context.n = 6;
8973
+ _context.n = 4;
9036
8974
  break;
9037
8975
  }
9038
8976
  throw new RoutingResolutionError('Extrinsic is null');
9039
- case 6:
9040
- amountOut = parseUnits(trade.outputAmount.toFixed(), tokenTo.decimals);
8977
+ case 4:
9041
8978
  nativeSymbol = getNativeAssetSymbol(this.chain);
9042
8979
  if (!(tokenTo.symbol === nativeSymbol)) {
9043
- _context.n = 8;
8980
+ _context.n = 6;
9044
8981
  break;
9045
8982
  }
9046
8983
  amountOutWithFee = amountOut - padValueBy(toDestTxFee, FEE_BUFFER_PCT);
9047
8984
  if (!(amountOutWithFee <= 0n)) {
9048
- _context.n = 7;
8985
+ _context.n = 5;
9049
8986
  break;
9050
8987
  }
9051
8988
  throw new AmountTooLowError();
9052
- case 7:
8989
+ case 5:
9053
8990
  Logger.log('Amount out with fee:', amountOutWithFee);
9054
8991
  return _context.a(2, {
9055
8992
  tx: extrinsic[0],
9056
8993
  amountOut: amountOutWithFee
9057
8994
  });
9058
- case 8:
8995
+ case 6:
9059
8996
  Logger.log('Calculated amount out:', amountOut);
9060
8997
  return _context.a(2, {
9061
8998
  tx: extrinsic[0],
@@ -9064,48 +9001,27 @@ var BifrostExchange = /*#__PURE__*/function (_ExchangeChain) {
9064
9001
  }
9065
9002
  }, _callee, this);
9066
9003
  }));
9067
- function swapCurrency(_x, _x2) {
9068
- return _swapCurrency.apply(this, arguments);
9004
+ function buildSwap(_x, _x2) {
9005
+ return _buildSwap.apply(this, arguments);
9069
9006
  }
9070
- return swapCurrency;
9007
+ return buildSwap;
9071
9008
  }()
9072
9009
  }, {
9073
9010
  key: "getAmountOut",
9074
9011
  value: function () {
9075
9012
  var _getAmountOut = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
9076
- var apiPjs, assetFrom, assetTo, amount, origin, chainId, tokenMap, tokenWrappedFrom, tokenWrappedTo, tokenFrom, tokenTo, currencyCombinations, pairs, pctDestFee, amountWithoutFee, amountIn, trade;
9013
+ var apiPjs, assetFrom, assetTo, amount, origin, amountWithoutFee, _yield$this$findBestT2, amountOut;
9077
9014
  return _regenerator().w(function (_context2) {
9078
9015
  while (1) switch (_context2.n) {
9079
9016
  case 0:
9080
9017
  apiPjs = options.apiPjs, assetFrom = options.assetFrom, assetTo = options.assetTo, amount = options.amount, origin = options.origin;
9081
- chainId = getParaId(this.chain);
9082
- tokenMap = getTokenMap(this.chain, chainId);
9083
- tokenWrappedFrom = findToken(tokenMap, assetFrom.symbol);
9084
- if (!(tokenWrappedFrom === undefined)) {
9085
- _context2.n = 1;
9086
- break;
9087
- }
9088
- throw new RoutingResolutionError('Currency from not found');
9018
+ amountWithoutFee = padValueBy(amount, origin ? DEST_FEE_BUFFER_PCT : 0);
9019
+ _context2.n = 1;
9020
+ return this.findBestTrade(apiPjs, assetFrom.symbol, assetTo.symbol, amountWithoutFee);
9089
9021
  case 1:
9090
- tokenWrappedTo = findToken(tokenMap, assetTo.symbol);
9091
- if (!(tokenWrappedTo === undefined)) {
9092
- _context2.n = 2;
9093
- break;
9094
- }
9095
- throw new RoutingResolutionError('Currency to not found');
9096
- case 2:
9097
- tokenFrom = new Token(tokenWrappedFrom.wrapped);
9098
- tokenTo = new Token(tokenWrappedTo.wrapped);
9099
- currencyCombinations = getCurrencyCombinations(chainId, tokenFrom, tokenTo);
9100
- _context2.n = 3;
9101
- return getFilteredPairs(apiPjs, chainId, currencyCombinations);
9102
- case 3:
9103
- pairs = _context2.v;
9104
- pctDestFee = origin ? DEST_FEE_BUFFER_PCT : 0;
9105
- amountWithoutFee = padValueBy(amount, pctDestFee);
9106
- amountIn = Amount.fromRawAmount(tokenFrom, amountWithoutFee.toString());
9107
- trade = getBestTrade(chainId, pairs, amountIn, tokenTo);
9108
- return _context2.a(2, parseUnits(trade.outputAmount.toFixed(), tokenTo.decimals));
9022
+ _yield$this$findBestT2 = _context2.v;
9023
+ amountOut = _yield$this$findBestT2.amountOut;
9024
+ return _context2.a(2, amountOut);
9109
9025
  }
9110
9026
  }, _callee2, this);
9111
9027
  }));
@@ -9130,6 +9046,50 @@ var BifrostExchange = /*#__PURE__*/function (_ExchangeChain) {
9130
9046
  }
9131
9047
  return getDexConfig$1;
9132
9048
  }()
9049
+ }, {
9050
+ key: "findBestTrade",
9051
+ value: function () {
9052
+ var _findBestTrade = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(api, symbolFrom, symbolTo, amountIn) {
9053
+ var chainId, tokenMap, tokenInfoFrom, tokenInfoTo, tokenFrom, tokenTo, pairs, trade;
9054
+ return _regenerator().w(function (_context4) {
9055
+ while (1) switch (_context4.n) {
9056
+ case 0:
9057
+ chainId = getParaId(this.chain);
9058
+ tokenMap = getTokenMap(this.chain, chainId);
9059
+ tokenInfoFrom = findToken(tokenMap, symbolFrom);
9060
+ if (tokenInfoFrom) {
9061
+ _context4.n = 1;
9062
+ break;
9063
+ }
9064
+ throw new RoutingResolutionError('Currency from not found');
9065
+ case 1:
9066
+ tokenInfoTo = findToken(tokenMap, symbolTo);
9067
+ if (tokenInfoTo) {
9068
+ _context4.n = 2;
9069
+ break;
9070
+ }
9071
+ throw new RoutingResolutionError('Currency to not found');
9072
+ case 2:
9073
+ tokenFrom = new Token(tokenInfoFrom);
9074
+ tokenTo = new Token(tokenInfoTo);
9075
+ _context4.n = 3;
9076
+ return getFilteredPairs(api, chainId, getCurrencyCombinations(chainId, tokenFrom, tokenTo));
9077
+ case 3:
9078
+ pairs = _context4.v;
9079
+ trade = getBestTrade(chainId, pairs, Amount.fromRawAmount(tokenFrom, amountIn.toString()), tokenTo);
9080
+ return _context4.a(2, {
9081
+ trade: trade,
9082
+ tokenTo: tokenTo,
9083
+ amountOut: parseUnits(trade.outputAmount.toFixed(), tokenTo.decimals)
9084
+ });
9085
+ }
9086
+ }, _callee4, this);
9087
+ }));
9088
+ function findBestTrade(_x5, _x6, _x7, _x8) {
9089
+ return _findBestTrade.apply(this, arguments);
9090
+ }
9091
+ return findBestTrade;
9092
+ }()
9133
9093
  }]);
9134
9094
  }(ExchangeChain);
9135
9095
 
@@ -9223,9 +9183,9 @@ var HydrationExchange = /*#__PURE__*/function (_ExchangeChain) {
9223
9183
  }
9224
9184
  _inherits(HydrationExchange, _ExchangeChain);
9225
9185
  return _createClass(HydrationExchange, [{
9226
- key: "swapCurrency",
9186
+ key: "buildSwap",
9227
9187
  value: function () {
9228
- var _swapCurrency = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
9188
+ var _buildSwap = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options, toDestTransactionFee) {
9229
9189
  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;
9230
9190
  return _regenerator().w(function (_context) {
9231
9191
  while (1) switch (_context.p = _context.n) {
@@ -9335,10 +9295,10 @@ var HydrationExchange = /*#__PURE__*/function (_ExchangeChain) {
9335
9295
  }
9336
9296
  }, _callee, this, [[2,, 16, 17]]);
9337
9297
  }));
9338
- function swapCurrency(_x, _x2) {
9339
- return _swapCurrency.apply(this, arguments);
9298
+ function buildSwap(_x, _x2) {
9299
+ return _buildSwap.apply(this, arguments);
9340
9300
  }
9341
- return swapCurrency;
9301
+ return buildSwap;
9342
9302
  }()
9343
9303
  }, {
9344
9304
  key: "getAmountOut",
@@ -9843,7 +9803,7 @@ var selectBestExchangeCommon = /*#__PURE__*/function () {
9843
9803
  return _context.a(2, 0);
9844
9804
  case 5:
9845
9805
  config = api.config;
9846
- parsedAmount = applyDecimalAbstraction(options.amount, assetFromExchange === null || assetFromExchange === void 0 ? void 0 : assetFromExchange.decimals, !(isConfig(config) && config.abstractDecimals === false)).toString();
9806
+ parsedAmount = applyDecimalAbstraction(options.amount, assetFromExchange.decimals, !(isConfig(config) && config.abstractDecimals === false)).toString();
9847
9807
  _context.p = 6;
9848
9808
  _context.n = 7;
9849
9809
  return computeAmountOut(dex, assetFromExchange, assetTo, options, parsedAmount);
@@ -10019,6 +9979,9 @@ var selectBestExchange = /*#__PURE__*/function () {
10019
9979
  modifiedOptions = _objectSpread2(_objectSpread2({}, options), {}, {
10020
9980
  amount: BigInt(parsedAmount),
10021
9981
  exchange: exchangeInfo,
9982
+ // Recipient does not affect the quote. Use the sender so swapCurrency does not reject
9983
+ // exchanges without supportsRecipient, which may still swap via execute
9984
+ recipient: options.sender,
10022
9985
  feeCalcAddress: determineFeeCalcAddress(options.sender, options.recipient)
10023
9986
  });
10024
9987
  _context.n = 3;
@@ -10144,7 +10107,8 @@ var prepareTransformedOptions = /*#__PURE__*/function () {
10144
10107
  _t0,
10145
10108
  _t1,
10146
10109
  _t10,
10147
- _t11;
10110
+ _t11,
10111
+ _t12;
10148
10112
  return _regenerator().w(function (_context) {
10149
10113
  while (1) switch (_context.n) {
10150
10114
  case 0:
@@ -10197,7 +10161,7 @@ var prepareTransformedOptions = /*#__PURE__*/function () {
10197
10161
  _t4 = _objectSpread2({}, options);
10198
10162
  _t5 = {};
10199
10163
  _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);
10200
- _t7 = originSpecified && assetFromOrigin && originApi ? {
10164
+ _t7 = originSpecified && assetFromOrigin ? {
10201
10165
  api: originApi.api,
10202
10166
  chain: from,
10203
10167
  assetFrom: assetFromOrigin,
@@ -10220,13 +10184,15 @@ var prepareTransformedOptions = /*#__PURE__*/function () {
10220
10184
  chain: to,
10221
10185
  address: recipient
10222
10186
  } : undefined;
10223
- _t11 = determineFeeCalcAddress(sender, recipient);
10187
+ _t11 = recipient !== null && recipient !== void 0 ? recipient : sender;
10188
+ _t12 = determineFeeCalcAddress(sender, recipient);
10224
10189
  transformed = _t3(_t4, _t5, {
10225
10190
  amount: _t6,
10226
10191
  origin: _t7,
10227
10192
  exchange: _t1,
10228
10193
  destination: _t10,
10229
- feeCalcAddress: _t11
10194
+ recipient: _t11,
10195
+ feeCalcAddress: _t12
10230
10196
  });
10231
10197
  return _context.a(2, {
10232
10198
  dex: dex,
@@ -10300,10 +10266,11 @@ var buildSwapExecuteTx = /*#__PURE__*/function () {
10300
10266
  sender: evmSenderAddress !== null && evmSenderAddress !== void 0 ? evmSenderAddress : sender,
10301
10267
  recipient: recipient !== null && recipient !== void 0 ? recipient : sender,
10302
10268
  calculateMinAmountOut: function calculateMinAmountOut(amountIn, assetTo) {
10269
+ var slippagePct = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : minAmountOutSlippage;
10303
10270
  return callDexAmountOut(dex, options, {
10304
10271
  amount: amountIn,
10305
10272
  assetTo: assetTo,
10306
- slippagePct: minAmountOutSlippage
10273
+ slippagePct: slippagePct
10307
10274
  });
10308
10275
  }
10309
10276
  });
@@ -10978,7 +10945,7 @@ var getRouterFees = /*#__PURE__*/function () {
10978
10945
  mergedHops = [].concat(_toConsumableArray((_sendingChain$hops = sendingChain === null || sendingChain === void 0 ? void 0 : sendingChain.hops) !== null && _sendingChain$hops !== void 0 ? _sendingChain$hops : []), _toConsumableArray(sendingChain && receivingChain ? [{
10979
10946
  chain: exchange.chain,
10980
10947
  result: _objectSpread2(_objectSpread2({}, swapChain), {}, {
10981
- fee: swapChain.fee + ((_receivingChain$origi = receivingChain === null || receivingChain === void 0 ? void 0 : receivingChain.origin.fee) !== null && _receivingChain$origi !== void 0 ? _receivingChain$origi : 0n),
10948
+ fee: swapChain.fee + ((_receivingChain$origi = receivingChain.origin.fee) !== null && _receivingChain$origi !== void 0 ? _receivingChain$origi : 0n),
10982
10949
  isExchange: true
10983
10950
  })
10984
10951
  }] : []), _toConsumableArray((_receivingChain$hops = receivingChain === null || receivingChain === void 0 ? void 0 : receivingChain.hops) !== null && _receivingChain$hops !== void 0 ? _receivingChain$hops : []));
@@ -11166,7 +11133,6 @@ var getBestAmountOut = /*#__PURE__*/function () {
11166
11133
 
11167
11134
  var computeExchangeMinAmount = /*#__PURE__*/function () {
11168
11135
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(dex, options) {
11169
- var _result$fee;
11170
11136
  var exchange, existentialDeposit, nativeSymbol, isNativeAsset, _yield$getSwapFee, result, swapFee;
11171
11137
  return _regenerator().w(function (_context) {
11172
11138
  while (1) switch (_context.n) {
@@ -11188,7 +11154,7 @@ var computeExchangeMinAmount = /*#__PURE__*/function () {
11188
11154
  case 2:
11189
11155
  _yield$getSwapFee = _context.v;
11190
11156
  result = _yield$getSwapFee.result;
11191
- swapFee = (_result$fee = result.fee) !== null && _result$fee !== void 0 ? _result$fee : 0n;
11157
+ swapFee = result.fee;
11192
11158
  return _context.a(2, existentialDeposit + swapFee + 1n);
11193
11159
  }
11194
11160
  }, _callee);
@@ -11236,7 +11202,7 @@ var getMinTransferableAmount = /*#__PURE__*/function () {
11236
11202
 
11237
11203
  var computeLocalTransferableAmount = /*#__PURE__*/function () {
11238
11204
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(dex, options) {
11239
- var exchange, sender, currency, balance, existentialDeposit, swapFee, nativeSymbol, _result$fee, _yield$getSwapFee, result, transferable;
11205
+ var exchange, sender, currency, balance, existentialDeposit, swapFee, nativeSymbol, _yield$getSwapFee, result, transferable;
11240
11206
  return _regenerator().w(function (_context) {
11241
11207
  while (1) switch (_context.n) {
11242
11208
  case 0:
@@ -11265,7 +11231,7 @@ var computeLocalTransferableAmount = /*#__PURE__*/function () {
11265
11231
  case 2:
11266
11232
  _yield$getSwapFee = _context.v;
11267
11233
  result = _yield$getSwapFee.result;
11268
- swapFee = (_result$fee = result.fee) !== null && _result$fee !== void 0 ? _result$fee : 0n;
11234
+ swapFee = result.fee;
11269
11235
  case 3:
11270
11236
  transferable = balance - existentialDeposit - swapFee;
11271
11237
  return _context.a(2, transferable > 0n ? transferable : 0n);
@@ -11653,7 +11619,7 @@ var buildExchangeDestInfo = function buildExchangeDestInfo(exchange, recipient,
11653
11619
  var getSwapInfo = /*#__PURE__*/function () {
11654
11620
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(initialOptions) {
11655
11621
  var _origin$chain, _destination$chain, _sendingInfo$hops, _receivingInfo$hops, _sendingInfo$origin, _receivingInfo$destin;
11656
- 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;
11622
+ 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;
11657
11623
  return _regenerator().w(function (_context2) {
11658
11624
  while (1) switch (_context2.p = _context2.n) {
11659
11625
  case 0:
@@ -11683,7 +11649,7 @@ var getSwapInfo = /*#__PURE__*/function () {
11683
11649
  throw _t;
11684
11650
  case 5:
11685
11651
  validateRoutedFeeAsset(options);
11686
- api = options.api, origin = options.origin, exchange = options.exchange, currencyTo = options.currencyTo, destination = options.destination, amount = options.amount, sender = options.sender, evmSenderAddress = options.evmSenderAddress;
11652
+ 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;
11687
11653
  assertCurrencyCore(currencyTo);
11688
11654
  senderAddress = evmSenderAddress !== null && evmSenderAddress !== void 0 ? evmSenderAddress : sender;
11689
11655
  if (!(origin && origin.chain !== exchange.chain)) {
@@ -11770,7 +11736,7 @@ var getSwapInfo = /*#__PURE__*/function () {
11770
11736
  break;
11771
11737
  case 16:
11772
11738
  _context2.n = 17;
11773
- return buildExchangeDestInfo(exchange, sender, amountOut, currencyTo);
11739
+ return buildExchangeDestInfo(exchange, recipient, amountOut, currencyTo);
11774
11740
  case 17:
11775
11741
  _t5 = _context2.v;
11776
11742
  case 18:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paraspell/swap",
3
- "version": "14.5.3",
3
+ "version": "14.6.0",
4
4
  "description": "Tool for XCM cross-chain asset exchanging across Polkadot and Kusama ecosystems",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,19 +29,19 @@
29
29
  "@acala-network/sdk": "4.1.15",
30
30
  "@acala-network/sdk-core": "4.1.15",
31
31
  "@acala-network/sdk-swap": "4.1.15",
32
- "@crypto-dex-sdk/amm": "^0.1.5",
33
- "@crypto-dex-sdk/chain": "^0.1.4",
34
- "@crypto-dex-sdk/currency": "^0.1.5",
35
- "@crypto-dex-sdk/format": "^0.1.4",
36
- "@crypto-dex-sdk/math": "^0.1.5",
37
- "@crypto-dex-sdk/parachains-bifrost": "^0.1.5",
38
- "@crypto-dex-sdk/token-lists": "^0.1.4",
39
- "@galacticcouncil/descriptors": "^2.9.0",
40
- "@galacticcouncil/sdk-next": "^2.5.0",
41
- "@paraspell/sdk": "14.5.3",
42
- "@paraspell/sdk-common": "14.5.3",
43
- "@paraspell/sdk-core": "14.5.3",
44
- "@paraspell/sdk-pjs": "14.5.3",
32
+ "@crypto-dex-sdk/amm": "^0.1.7",
33
+ "@crypto-dex-sdk/chain": "^0.1.7",
34
+ "@crypto-dex-sdk/currency": "^0.1.7",
35
+ "@crypto-dex-sdk/format": "^0.1.7",
36
+ "@crypto-dex-sdk/math": "^0.1.7",
37
+ "@crypto-dex-sdk/parachains-bifrost": "^0.1.7",
38
+ "@crypto-dex-sdk/token-lists": "^0.1.7",
39
+ "@galacticcouncil/descriptors": "^2.11.0",
40
+ "@galacticcouncil/sdk-next": "^2.7.0",
41
+ "@paraspell/sdk": "14.6.0",
42
+ "@paraspell/sdk-common": "14.6.0",
43
+ "@paraspell/sdk-core": "14.6.0",
44
+ "@paraspell/sdk-pjs": "14.6.0",
45
45
  "@polkadot/api": "^17.0.2",
46
46
  "@polkadot/types": "^17.0.2",
47
47
  "@polkadot/util": "^14.0.3",
@@ -54,14 +54,14 @@
54
54
  },
55
55
  "devDependencies": {
56
56
  "@babel/plugin-syntax-import-attributes": "^7.29.7",
57
- "@babel/preset-env": "^7.29.7",
57
+ "@babel/preset-env": "^7.29.14",
58
58
  "@codecov/rollup-plugin": "^2.0.1",
59
59
  "@rollup/plugin-babel": "^7.1.0",
60
60
  "@rollup/plugin-json": "^6.1.0",
61
61
  "@rollup/plugin-typescript": "^12.3.1",
62
- "@vitest/coverage-v8": "^5.0.5",
63
- "dotenv": "^18.0.9",
64
- "rollup": "^4.66.0",
62
+ "@vitest/coverage-v8": "^5.1.2",
63
+ "dotenv": "^18.0.10",
64
+ "rollup": "^4.68.8",
65
65
  "rollup-plugin-dts": "^6.5.1",
66
66
  "tsx": "^4.23.15"
67
67
  },