@paraspell/sdk-core 8.12.1 → 8.13.1

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.cjs CHANGED
@@ -954,25 +954,6 @@ var ScenarioNotSupportedError = /*#__PURE__*/function (_Error) {
954
954
  return _createClass(ScenarioNotSupportedError);
955
955
  }(/*#__PURE__*/_wrapNativeSuper(Error));
956
956
 
957
- var DEFAULT_FEE_ASSET = 0;
958
- var ETH_PARA_ID = 1;
959
- var ETH_CHAIN_ID = BigInt(ETH_PARA_ID);
960
- var ETHEREUM_JUNCTION = {
961
- GlobalConsensus: {
962
- Ethereum: {
963
- chainId: ETH_CHAIN_ID
964
- }
965
- }
966
- };
967
- var DOT_MULTILOCATION = {
968
- parents: sdkCommon.Parents.ONE,
969
- interior: 'Here'
970
- };
971
- var SYSTEM_NODES_POLKADOT = ['PeoplePolkadot', 'CoretimePolkadot', 'Collectives'];
972
- var SYSTEM_NODES_KUSAMA = ['PeopleKusama', 'CoretimeKusama'];
973
- var ASSET_HUB_EXECUTION_FEE = 2200000000n; // 0.22 DOT
974
- // 30% padding
975
-
976
957
  var AssetHubPolkadot$1 = {
977
958
  name: "AssetHub",
978
959
  info: "PolkadotAssetHub",
@@ -1226,14 +1207,14 @@ var Centrifuge$1 = {
1226
1207
  info: "centrifuge",
1227
1208
  paraId: 2031,
1228
1209
  providers: [
1229
- {
1230
- name: "Centrifuge",
1231
- endpoint: "wss://fullnode.centrifuge.io"
1232
- },
1233
1210
  {
1234
1211
  name: "LuckyFriday",
1235
1212
  endpoint: "wss://rpc-centrifuge.luckyfriday.io"
1236
1213
  },
1214
+ {
1215
+ name: "Centrifuge",
1216
+ endpoint: "wss://fullnode.centrifuge.io"
1217
+ },
1237
1218
  {
1238
1219
  name: "OnFinality",
1239
1220
  endpoint: "wss://centrifuge-parachain.api.onfinality.io/public-ws"
@@ -2300,57 +2281,24 @@ var getNodeProviders = function getNodeProviders(node) {
2300
2281
  });
2301
2282
  };
2302
2283
 
2303
- /**
2304
- * Retrieves the parachain ID for a specified node.
2305
- *
2306
- * @param node - The node for which to get the paraId.
2307
- * @returns The parachain ID of the node.
2308
- */
2309
- var getParaId = function getParaId(node) {
2310
- if (node === 'Ethereum') {
2311
- return ETH_PARA_ID;
2312
- }
2313
- return getNodeConfig(node).paraId;
2314
- };
2315
-
2316
- var shuffleArray = function shuffleArray(array) {
2317
- var copy = _toConsumableArray(array);
2318
- for (var i = copy.length - 1; i > 0; i--) {
2319
- var j = Math.floor(Math.random() * (i + 1));
2320
- var _ref = [copy[j], copy[i]];
2321
- copy[i] = _ref[0];
2322
- copy[j] = _ref[1];
2284
+ var DEFAULT_FEE_ASSET = 0;
2285
+ var ETH_PARA_ID = 1;
2286
+ var ETH_CHAIN_ID = BigInt(ETH_PARA_ID);
2287
+ var ETHEREUM_JUNCTION = {
2288
+ GlobalConsensus: {
2289
+ Ethereum: {
2290
+ chainId: ETH_CHAIN_ID
2291
+ }
2323
2292
  }
2324
- return copy;
2325
2293
  };
2326
-
2327
- var shuffleWsProviders = function shuffleWsProviders(node, wsProviders) {
2328
- var ALLOWED_NODES = ['Hydration', 'Acala'];
2329
- if (ALLOWED_NODES.includes(node)) {
2330
- return shuffleArray(wsProviders);
2331
- }
2332
- return wsProviders;
2294
+ var DOT_MULTILOCATION = {
2295
+ parents: sdkCommon.Parents.ONE,
2296
+ interior: 'Here'
2333
2297
  };
2334
-
2335
- var createApiInstanceForNode = /*#__PURE__*/function () {
2336
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(api, node) {
2337
- var wsUrl, resolvedWsUrl;
2338
- return _regeneratorRuntime().wrap(function _callee$(_context) {
2339
- while (1) switch (_context.prev = _context.next) {
2340
- case 0:
2341
- wsUrl = getNodeProviders(node);
2342
- resolvedWsUrl = Array.isArray(wsUrl) ? shuffleWsProviders(node, wsUrl) : wsUrl;
2343
- return _context.abrupt("return", api.createApiInstance(resolvedWsUrl));
2344
- case 3:
2345
- case "end":
2346
- return _context.stop();
2347
- }
2348
- }, _callee);
2349
- }));
2350
- return function createApiInstanceForNode(_x, _x2) {
2351
- return _ref.apply(this, arguments);
2352
- };
2353
- }();
2298
+ var SYSTEM_NODES_POLKADOT = ['PeoplePolkadot', 'CoretimePolkadot', 'Collectives'];
2299
+ var SYSTEM_NODES_KUSAMA = ['PeopleKusama', 'CoretimeKusama'];
2300
+ var ASSET_HUB_EXECUTION_FEE = 2200000000n; // 0.22 DOT
2301
+ // 30% padding
2354
2302
 
2355
2303
  var createX1Payload = function createX1Payload(version, junction) {
2356
2304
  return version === exports.Version.V4 ? {
@@ -2513,6 +2461,169 @@ var throwUnsupportedCurrency = function throwUnsupportedCurrency(currency, node)
2513
2461
  throw new assets.InvalidCurrencyError("".concat(isDestination ? 'Destination' : 'Origin', " node ").concat(node, " does not support currency ").concat(JSON.stringify(currency), "."));
2514
2462
  };
2515
2463
 
2464
+ var buildMultiLocation = function buildMultiLocation(_ref) {
2465
+ var paraIdTo = _ref.paraIdTo,
2466
+ asset = _ref.asset,
2467
+ origin = _ref.origin,
2468
+ destination = _ref.destination;
2469
+ if (!assets.isForeignAsset(asset)) {
2470
+ if (_typeof(destination) === 'object') {
2471
+ return destination;
2472
+ }
2473
+ // ParaToRelay scenario
2474
+ if (sdkCommon.isRelayChain(destination)) {
2475
+ return DOT_MULTILOCATION;
2476
+ }
2477
+ // If it is a native asset, search by symbol on AssetHub and use the multiLocation from there
2478
+ var assetHubAsset = assets.getOtherAssets(destination).find(function (ahAsset) {
2479
+ var _ahAsset$symbol, _asset$symbol;
2480
+ return ((_ahAsset$symbol = ahAsset.symbol) === null || _ahAsset$symbol === void 0 ? void 0 : _ahAsset$symbol.toLowerCase()) === ((_asset$symbol = asset.symbol) === null || _asset$symbol === void 0 ? void 0 : _asset$symbol.toLowerCase());
2481
+ });
2482
+ if (assetHubAsset === undefined) {
2483
+ throw new assets.InvalidCurrencyError("Asset ".concat(asset.symbol, " not found in AssetHub"));
2484
+ }
2485
+ if (assetHubAsset.multiLocation) {
2486
+ return assetHubAsset.multiLocation;
2487
+ }
2488
+ throw new assets.InvalidCurrencyError("Asset ".concat(asset.symbol, " has no multiLocation"));
2489
+ }
2490
+ var createDefaultMultiLocation = function createDefaultMultiLocation(assetId) {
2491
+ return {
2492
+ parents: sdkCommon.Parents.ONE,
2493
+ interior: {
2494
+ X3: [{
2495
+ Parachain: paraIdTo
2496
+ }, {
2497
+ PalletInstance: '50'
2498
+ }, {
2499
+ GeneralIndex: BigInt(assetId)
2500
+ }]
2501
+ }
2502
+ };
2503
+ };
2504
+ var isBifrostOrigin = origin === 'BifrostPolkadot' || origin === 'BifrostKusama';
2505
+ if (isBifrostOrigin) {
2506
+ return createDefaultMultiLocation(asset.assetId);
2507
+ }
2508
+ if (asset.multiLocation) {
2509
+ return asset.multiLocation;
2510
+ } else {
2511
+ return createDefaultMultiLocation(asset.assetId);
2512
+ }
2513
+ };
2514
+ var getModifiedCurrencySelection = function getModifiedCurrencySelection(version, input) {
2515
+ var amount = input.asset.amount,
2516
+ overriddenAsset = input.overriddenAsset;
2517
+ if (overriddenAsset) {
2518
+ return addXcmVersionHeader(maybeOverrideMultiAssets(version, amount, [], overriddenAsset), version);
2519
+ }
2520
+ var multiLocation = buildMultiLocation(input);
2521
+ var multiAsset = createMultiAsset(version, amount, multiLocation);
2522
+ return addXcmVersionHeader(multiAsset, version);
2523
+ };
2524
+
2525
+ var getCurrencySelection = function getCurrencySelection(input, useMultiAssets, currencySelection) {
2526
+ var origin = input.origin;
2527
+ var _getNode = getNode(origin),
2528
+ version = _getNode.version;
2529
+ if (useMultiAssets) {
2530
+ return getModifiedCurrencySelection(version, input);
2531
+ }
2532
+ return currencySelection;
2533
+ };
2534
+
2535
+ var getXTokensParameters = function getXTokensParameters(isMultiAssetTransfer, currencySelection, addressSelection, amount, fees, overriddenAsset) {
2536
+ if (isMultiAssetTransfer) {
2537
+ var isOverridenMultiAssets = overriddenAsset && !sdkCommon.isTMultiLocation(overriddenAsset);
2538
+ var feeAssetIndex = isOverridenMultiAssets ? overriddenAsset.findIndex(function (asset) {
2539
+ return asset.isFeeAsset;
2540
+ }) : undefined;
2541
+ return _objectSpread2(_objectSpread2(_defineProperty({}, isOverridenMultiAssets ? 'assets' : 'asset', currencySelection), isOverridenMultiAssets && {
2542
+ fee_item: feeAssetIndex
2543
+ }), {}, {
2544
+ dest: addressSelection,
2545
+ dest_weight_limit: fees
2546
+ });
2547
+ }
2548
+ return {
2549
+ currency_id: currencySelection,
2550
+ amount: BigInt(amount),
2551
+ dest: addressSelection,
2552
+ dest_weight_limit: fees
2553
+ };
2554
+ };
2555
+
2556
+ var XTokensTransferImpl = /*#__PURE__*/function () {
2557
+ function XTokensTransferImpl() {
2558
+ _classCallCheck(this, XTokensTransferImpl);
2559
+ }
2560
+ return _createClass(XTokensTransferImpl, null, [{
2561
+ key: "transferXTokens",
2562
+ value: function transferXTokens(input, currencySelection) {
2563
+ var fees = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'Unlimited';
2564
+ var api = input.api,
2565
+ origin = input.origin,
2566
+ asset = input.asset,
2567
+ addressSelection = input.addressSelection,
2568
+ destination = input.destination,
2569
+ scenario = input.scenario,
2570
+ overriddenAsset = input.overriddenAsset,
2571
+ pallet = input.pallet,
2572
+ method = input.method,
2573
+ _input$useMultiAssetT = input.useMultiAssetTransfer,
2574
+ useMultiAssetTransfer = _input$useMultiAssetT === void 0 ? false : _input$useMultiAssetT;
2575
+ var isMultiLocationDestination = _typeof(destination) === 'object';
2576
+ if (isMultiLocationDestination) {
2577
+ throw new Error('Multilocation destinations are not supported for specific transfer you are trying to create. In special cases such as xTokens or xTransfer pallet try using address multilocation instead (for both destination and address in same multilocation set (eg. X2 - Parachain, Address). For further assistance please open issue in our repository.');
2578
+ }
2579
+ var isBifrostOrigin = origin === 'BifrostPolkadot' || origin === 'BifrostKusama';
2580
+ var isAssetHubDest = destination === 'AssetHubPolkadot' || destination === 'AssetHubKusama';
2581
+ var isAstarOrShidenToRelay = scenario === 'ParaToRelay' && (origin === 'Astar' || origin === 'Shiden');
2582
+ var isOverridenMultiAssets = overriddenAsset && !sdkCommon.isTMultiLocation(overriddenAsset);
2583
+ var shouldUseMultiasset = useMultiAssetTransfer || isAstarOrShidenToRelay || isAssetHubDest && !isBifrostOrigin || !!isOverridenMultiAssets;
2584
+ var modifiedCurrencySelection = getCurrencySelection(input, shouldUseMultiasset, currencySelection);
2585
+ var section = shouldUseMultiasset ? isOverridenMultiAssets ? 'transfer_multiassets' : 'transfer_multiasset' : 'transfer';
2586
+ var parameters = getXTokensParameters(shouldUseMultiasset, modifiedCurrencySelection, addressSelection, asset.amount, fees, overriddenAsset);
2587
+ var call = {
2588
+ module: pallet !== null && pallet !== void 0 ? pallet : 'XTokens',
2589
+ section: method !== null && method !== void 0 ? method : section,
2590
+ parameters: parameters
2591
+ };
2592
+ return api.callTxMethod(call);
2593
+ }
2594
+ }]);
2595
+ }();
2596
+
2597
+ var getBridgeStatus = /*#__PURE__*/function () {
2598
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(api) {
2599
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2600
+ while (1) switch (_context.prev = _context.next) {
2601
+ case 0:
2602
+ _context.next = 2;
2603
+ return api.init('BridgeHubPolkadot');
2604
+ case 2:
2605
+ _context.prev = 2;
2606
+ _context.next = 5;
2607
+ return api.getBridgeStatus();
2608
+ case 5:
2609
+ return _context.abrupt("return", _context.sent);
2610
+ case 6:
2611
+ _context.prev = 6;
2612
+ _context.next = 9;
2613
+ return api.disconnect();
2614
+ case 9:
2615
+ return _context.finish(6);
2616
+ case 10:
2617
+ case "end":
2618
+ return _context.stop();
2619
+ }
2620
+ }, _callee, null, [[2,, 6, 10]]);
2621
+ }));
2622
+ return function getBridgeStatus(_x) {
2623
+ return _ref.apply(this, arguments);
2624
+ };
2625
+ }();
2626
+
2516
2627
  var createBeneficiaryMultiLocation = function createBeneficiaryMultiLocation(_ref) {
2517
2628
  var api = _ref.api,
2518
2629
  scenario = _ref.scenario,
@@ -2605,548 +2716,159 @@ var transformMultiLocation = function transformMultiLocation(multiLocation) {
2605
2716
  });
2606
2717
  };
2607
2718
 
2608
- var createVersionedBeneficiary = function createVersionedBeneficiary(options) {
2609
- var version = options.version;
2610
- var multiLocation = createBeneficiaryMultiLocation(options);
2611
- return addXcmVersionHeader(multiLocation, version);
2612
- };
2613
-
2614
- var getMultiLocationTokenId = function getMultiLocationTokenId(location, node) {
2615
- if (location.interior.type === 'Here') {
2616
- // native token
2617
- return assets.getNativeAssetSymbol(node);
2719
+ var createCustomXcmOnDest = function createCustomXcmOnDest(_ref, origin, version, messageId) {
2720
+ var api = _ref.api,
2721
+ address = _ref.address,
2722
+ asset = _ref.asset,
2723
+ scenario = _ref.scenario,
2724
+ senderAddress = _ref.senderAddress,
2725
+ ahAddress = _ref.ahAddress;
2726
+ if (!assets.isForeignAsset(asset)) {
2727
+ throw new assets.InvalidCurrencyError("Asset ".concat(JSON.stringify(asset), " is not a foreign asset"));
2618
2728
  }
2619
- var foreignAssets = assets.getOtherAssets(node);
2620
- if (location.interior.type === 'X2') {
2621
- if (location.interior.value[0].type === 'PalletInstance' && location.interior.value[0].value === 50 && location.interior.value[1].type === 'GeneralIndex') {
2622
- var _foreignAssets$find$a, _foreignAssets$find;
2623
- var assetId = location.interior.value[1].value;
2624
- return (_foreignAssets$find$a = (_foreignAssets$find = foreignAssets.find(function (asset) {
2625
- return asset.assetId === String(assetId);
2626
- })) === null || _foreignAssets$find === void 0 ? void 0 : _foreignAssets$find.assetId) !== null && _foreignAssets$find$a !== void 0 ? _foreignAssets$find$a : null;
2627
- }
2729
+ if (!asset.multiLocation) {
2730
+ throw new assets.InvalidCurrencyError("Asset ".concat(JSON.stringify(asset), " has no multiLocation"));
2628
2731
  }
2629
- return null;
2630
- };
2631
-
2632
- var computeFeeFromDryRun = function computeFeeFromDryRun(dryRun, node, executionFee) {
2633
- // Extract delivery fees from emitted events
2634
- var deliveryFees = [];
2635
- var _iterator = _createForOfIteratorHelper(dryRun.value.emitted_events),
2636
- _step;
2637
- try {
2638
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
2639
- var e = _step.value;
2640
- var isXcmEvent = e.type === 'XcmPallet' || e.type === 'PolkadotXcm' || e.type === 'CumulusXcm';
2641
- var isFeesPaid = e.value.type === 'FeesPaid';
2642
- if (isXcmEvent && isFeesPaid && e.value.value.fees) {
2643
- var _iterator2 = _createForOfIteratorHelper(e.value.value.fees),
2644
- _step2;
2645
- try {
2646
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
2647
- var feeItem = _step2.value;
2648
- if (feeItem.fun.type === 'NonFungible') continue;
2649
- var plancks = feeItem.fun.value;
2650
- var tokenSymbol = getMultiLocationTokenId(feeItem.id, node);
2651
- if (!tokenSymbol || !plancks) continue;
2652
- deliveryFees.push({
2653
- plancks: plancks,
2654
- tokenSymbol: tokenSymbol
2655
- });
2656
- }
2657
- } catch (err) {
2658
- _iterator2.e(err);
2659
- } finally {
2660
- _iterator2.f();
2661
- }
2662
- }
2663
- }
2664
- } catch (err) {
2665
- _iterator.e(err);
2666
- } finally {
2667
- _iterator.f();
2732
+ if (!senderAddress) {
2733
+ throw new InvalidParameterError("Please provide senderAddress");
2668
2734
  }
2669
- var nativeAssetSymbol = assets.getNativeAssetSymbol(node);
2670
- // Sum the fees that match the feeToken
2671
- var totalDeliveryFees = deliveryFees.filter(function (df) {
2672
- return df.tokenSymbol === nativeAssetSymbol;
2673
- }).reduce(function (acc, df) {
2674
- return acc + df.plancks;
2675
- }, 0n);
2676
- return totalDeliveryFees + executionFee;
2677
- };
2678
-
2679
- var getMultiLocationTokenIdPjs = function getMultiLocationTokenIdPjs(location, node) {
2680
- if (location.interior === 'Here') {
2681
- // native token
2682
- return assets.getNativeAssetSymbol(node);
2735
+ if (assets.isNodeEvm(origin) && !ahAddress) {
2736
+ throw new InvalidParameterError("Please provide ahAddress");
2683
2737
  }
2684
- var foreignAssets = assets.getOtherAssets(node);
2685
- if (Object.keys(location.interior)[0] === 'X2') {
2686
- var junctions = Object.values(location.interior)[0];
2687
- if (Object.keys(junctions[0])[0] === 'PalletInstance' && junctions[0].PalletInstance === '50' && Object.keys(junctions[1])[0] === 'GeneralIndex') {
2688
- var _foreignAssets$find$s, _foreignAssets$find;
2689
- var assetId = junctions[1].GeneralIndex;
2690
- return (_foreignAssets$find$s = (_foreignAssets$find = foreignAssets.find(function (asset) {
2691
- return asset.assetId === String(assetId);
2692
- })) === null || _foreignAssets$find === void 0 ? void 0 : _foreignAssets$find.symbol) !== null && _foreignAssets$find$s !== void 0 ? _foreignAssets$find$s : null;
2693
- }
2738
+ var ethAsset = assets.findAssetByMultiLocation(assets.getOtherAssets('Ethereum'), asset.multiLocation);
2739
+ if (!ethAsset) {
2740
+ throw new assets.InvalidCurrencyError("Could not obtain Ethereum asset address for ".concat(JSON.stringify(asset)));
2694
2741
  }
2695
- return null;
2696
- };
2697
-
2698
- var computeFeeFromDryRunPjs = function computeFeeFromDryRunPjs(dryRun, node, executionFee) {
2699
- // Extract delivery fees from emitted events
2700
- var deliveryFees = [];
2701
- var _iterator = _createForOfIteratorHelper(dryRun.Ok.emittedEvents),
2702
- _step;
2703
- try {
2704
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
2705
- var e = _step.value;
2706
- var isXcmEvent = e.section === 'xcmPallet' || e.section === 'polkadotXcm' || e.section === 'cumulusXcm';
2707
- var isFeesPaid = e.method === 'FeesPaid';
2708
- if (isXcmEvent && isFeesPaid && e.data.fees) {
2709
- var _iterator2 = _createForOfIteratorHelper(e.data.fees),
2710
- _step2;
2711
- try {
2712
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
2713
- var feeItem = _step2.value;
2714
- if (feeItem.fun.NonFungible) continue;
2715
- var plancks = BigInt(feeItem.fun.Fungible.replace(/,/g, ''));
2716
- var tokenSymbol = getMultiLocationTokenIdPjs(feeItem.id, node);
2717
- if (!tokenSymbol || !plancks) continue;
2718
- deliveryFees.push({
2719
- plancks: plancks,
2720
- tokenSymbol: tokenSymbol
2721
- });
2742
+ var interiorSb = ethAsset.symbol === 'ETH' ? {
2743
+ Here: null
2744
+ } : {
2745
+ X1: [{
2746
+ AccountKey20: {
2747
+ network: null,
2748
+ key: ethAsset.assetId
2749
+ }
2750
+ }]
2751
+ };
2752
+ return _defineProperty({}, version, [{
2753
+ SetAppendix: [{
2754
+ DepositAsset: {
2755
+ assets: {
2756
+ Wild: 'All'
2757
+ },
2758
+ beneficiary: createBeneficiaryMultiLocation({
2759
+ api: api,
2760
+ scenario: scenario,
2761
+ pallet: 'PolkadotXcm',
2762
+ recipientAddress: assets.isNodeEvm(origin) ? ahAddress : senderAddress,
2763
+ version: version
2764
+ })
2765
+ }
2766
+ }]
2767
+ }, {
2768
+ InitiateReserveWithdraw: {
2769
+ assets: {
2770
+ Wild: {
2771
+ AllOf: {
2772
+ id: asset.multiLocation,
2773
+ fun: 'Fungible'
2722
2774
  }
2723
- } catch (err) {
2724
- _iterator2.e(err);
2725
- } finally {
2726
- _iterator2.f();
2727
2775
  }
2728
- }
2729
- }
2730
- } catch (err) {
2731
- _iterator.e(err);
2732
- } finally {
2733
- _iterator.f();
2734
- }
2735
- var nativeAssetSymbol = assets.getNativeAssetSymbol(node);
2736
- // Sum the fees that match the feeToken
2737
- var totalDeliveryFees = deliveryFees.filter(function (df) {
2738
- return df.tokenSymbol === nativeAssetSymbol;
2739
- }).reduce(function (acc, df) {
2740
- return acc + df.plancks;
2741
- }, 0n);
2742
- return totalDeliveryFees + executionFee;
2743
- };
2744
-
2745
- var resolveModuleError = function resolveModuleError(node, error) {
2746
- var palletDetails = pallets.getSupportedPalletsDetails(node).find(function (p) {
2747
- return p.index === Number(error.index);
2748
- });
2749
- if (!palletDetails) {
2750
- throw new Error("Pallet with index ".concat(error.index, " not found"));
2751
- }
2752
- // Use only the first byte of the error to get the error index
2753
- var errorIndex = Number(error.error.slice(0, 4));
2754
- var name = palletDetails.name;
2755
- if (name !== 'XTokens' && name !== 'PolkadotXcm' && name !== 'XcmPallet') {
2756
- throw new Error("Pallet ".concat(name, " is not supported"));
2757
- }
2758
- var failureReason = name === 'XTokens' ? Object.values(exports.XTokensError)[errorIndex] : Object.values(exports.PolkadotXcmError)[errorIndex];
2759
- if (!failureReason) {
2760
- throw new Error("Error index ".concat(errorIndex, " not found in ").concat(name, " pallet"));
2761
- }
2762
- return failureReason;
2763
- };
2764
-
2765
- var generateAddressMultiLocationV4 = function generateAddressMultiLocationV4(api, address) {
2766
- var isMultiLocation = _typeof(address) === 'object';
2767
- if (isMultiLocation) {
2768
- return _defineProperty({}, exports.Version.V4, address);
2769
- }
2770
- var isEthAddress = ethers.ethers.isAddress(address);
2771
- return addXcmVersionHeader({
2772
- parents: sdkCommon.Parents.ZERO,
2773
- interior: {
2774
- X1: [isEthAddress ? {
2775
- AccountKey20: {
2776
- key: address
2776
+ },
2777
+ reserve: {
2778
+ parents: sdkCommon.Parents.TWO,
2779
+ interior: {
2780
+ X1: [ETHEREUM_JUNCTION]
2777
2781
  }
2778
- } : {
2779
- AccountId32: {
2780
- id: api.accountToHex(address),
2781
- network: null
2782
+ },
2783
+ xcm: [{
2784
+ BuyExecution: {
2785
+ fees: {
2786
+ id: {
2787
+ parents: sdkCommon.Parents.ZERO,
2788
+ interior: interiorSb
2789
+ },
2790
+ fun: {
2791
+ Fungible: 1n
2792
+ }
2793
+ },
2794
+ weight_limit: 'Unlimited'
2795
+ }
2796
+ }, {
2797
+ DepositAsset: {
2798
+ assets: {
2799
+ Wild: {
2800
+ AllCounted: 1
2801
+ }
2802
+ },
2803
+ beneficiary: {
2804
+ parents: sdkCommon.Parents.ZERO,
2805
+ interior: {
2806
+ X1: [{
2807
+ AccountKey20: {
2808
+ network: null,
2809
+ key: address
2810
+ }
2811
+ }]
2812
+ }
2813
+ }
2782
2814
  }
2815
+ }, {
2816
+ SetTopic: messageId
2783
2817
  }]
2784
2818
  }
2785
- }, exports.Version.V4);
2819
+ }, {
2820
+ SetTopic: messageId
2821
+ }]);
2786
2822
  };
2787
2823
 
2788
- var getFees = function getFees(scenario) {
2789
- if (scenario === 'ParaToRelay') {
2790
- return 4600000000;
2791
- } else if (scenario === 'ParaToPara') {
2792
- return 399600000000;
2793
- }
2794
- throw new Error("Fees for scenario ".concat(scenario, " are not defined."));
2795
- };
2796
-
2797
- /**
2798
- * Retrieves the node instance for a given node.
2799
- *
2800
- * @param node - The node identifier.
2801
- * @returns The node instance
2802
- */
2803
- var getNode = function getNode(node) {
2804
- var nodeMap = nodes();
2805
- return nodeMap[node];
2806
- };
2807
-
2808
- var resolveParaId = function resolveParaId(paraId, destination) {
2809
- if (sdkCommon.isTMultiLocation(destination) || sdkCommon.isRelayChain(destination) || destination === 'Ethereum') {
2810
- return undefined;
2811
- }
2812
- return paraId !== null && paraId !== void 0 ? paraId : getParaId(destination);
2813
- };
2814
-
2815
- var validateAddress = function validateAddress(address, node) {
2816
- var isDestination = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
2817
- var isEvm = assets.isNodeEvm(node);
2818
- var isEthereumAddress = ethers.ethers.isAddress(address);
2819
- if (isEvm) {
2820
- if (!isEthereumAddress) {
2821
- throw new InvalidAddressError("".concat(isDestination ? 'Destination node' : 'Node', " is an EVM chain, but the address provided is not a valid Ethereum address."));
2822
- }
2823
- } else {
2824
- if (isEthereumAddress) {
2825
- throw new InvalidAddressError("EVM address provided but ".concat(isDestination ? 'destination ' : '', "node is not an EVM chain."));
2826
- }
2827
- }
2828
- };
2829
-
2830
- // Contains important call creation utils (Selection of fees,formating of header and more.. )
2831
- /**
2832
- * Determines the relay chain for a given node.
2833
- *
2834
- * @param node - The node for which to determine the relay chain.
2835
- * @returns 'Kusama' if the node's relay chain symbol is 'KSM'; otherwise, 'Polkadot'.
2836
- */
2837
- var determineRelayChain = function determineRelayChain(node) {
2838
- return assets.getRelayChainSymbol(node) === 'KSM' ? 'Kusama' : 'Polkadot';
2839
- };
2840
-
2841
- var buildMultiLocation = function buildMultiLocation(_ref) {
2842
- var paraIdTo = _ref.paraIdTo,
2843
- asset = _ref.asset,
2844
- origin = _ref.origin,
2845
- destination = _ref.destination;
2846
- if (!assets.isForeignAsset(asset)) {
2847
- if (_typeof(destination) === 'object') {
2848
- return destination;
2849
- }
2850
- // ParaToRelay scenario
2851
- if (sdkCommon.isRelayChain(destination)) {
2852
- return DOT_MULTILOCATION;
2853
- }
2854
- // If it is a native asset, search by symbol on AssetHub and use the multiLocation from there
2855
- var assetHubAsset = assets.getOtherAssets(destination).find(function (ahAsset) {
2856
- var _ahAsset$symbol, _asset$symbol;
2857
- return ((_ahAsset$symbol = ahAsset.symbol) === null || _ahAsset$symbol === void 0 ? void 0 : _ahAsset$symbol.toLowerCase()) === ((_asset$symbol = asset.symbol) === null || _asset$symbol === void 0 ? void 0 : _asset$symbol.toLowerCase());
2858
- });
2859
- if (assetHubAsset === undefined) {
2860
- throw new assets.InvalidCurrencyError("Asset ".concat(asset.symbol, " not found in AssetHub"));
2861
- }
2862
- if (assetHubAsset.multiLocation) {
2863
- return assetHubAsset.multiLocation;
2864
- }
2865
- throw new assets.InvalidCurrencyError("Asset ".concat(asset.symbol, " has no multiLocation"));
2866
- }
2867
- var createDefaultMultiLocation = function createDefaultMultiLocation(assetId) {
2868
- return {
2869
- parents: sdkCommon.Parents.ONE,
2870
- interior: {
2871
- X3: [{
2872
- Parachain: paraIdTo
2873
- }, {
2874
- PalletInstance: '50'
2875
- }, {
2876
- GeneralIndex: BigInt(assetId)
2877
- }]
2878
- }
2879
- };
2880
- };
2881
- var isBifrostOrigin = origin === 'BifrostPolkadot' || origin === 'BifrostKusama';
2882
- if (isBifrostOrigin) {
2883
- return createDefaultMultiLocation(asset.assetId);
2884
- }
2885
- if (asset.multiLocation) {
2886
- return asset.multiLocation;
2887
- } else {
2888
- return createDefaultMultiLocation(asset.assetId);
2889
- }
2890
- };
2891
- var getModifiedCurrencySelection = function getModifiedCurrencySelection(version, input) {
2892
- var amount = input.asset.amount,
2893
- overriddenAsset = input.overriddenAsset;
2894
- if (overriddenAsset) {
2895
- return addXcmVersionHeader(maybeOverrideMultiAssets(version, amount, [], overriddenAsset), version);
2896
- }
2897
- var multiLocation = buildMultiLocation(input);
2898
- var multiAsset = createMultiAsset(version, amount, multiLocation);
2899
- return addXcmVersionHeader(multiAsset, version);
2900
- };
2901
-
2902
- var getCurrencySelection = function getCurrencySelection(input, useMultiAssets, currencySelection) {
2903
- var origin = input.origin;
2904
- var _getNode = getNode(origin),
2905
- version = _getNode.version;
2906
- if (useMultiAssets) {
2907
- return getModifiedCurrencySelection(version, input);
2908
- }
2909
- return currencySelection;
2910
- };
2911
-
2912
- var getXTokensParameters = function getXTokensParameters(isMultiAssetTransfer, currencySelection, addressSelection, amount, fees, overriddenAsset) {
2913
- if (isMultiAssetTransfer) {
2914
- var isOverridenMultiAssets = overriddenAsset && !sdkCommon.isTMultiLocation(overriddenAsset);
2915
- var feeAssetIndex = isOverridenMultiAssets ? overriddenAsset.findIndex(function (asset) {
2916
- return asset.isFeeAsset;
2917
- }) : undefined;
2918
- return _objectSpread2(_objectSpread2(_defineProperty({}, isOverridenMultiAssets ? 'assets' : 'asset', currencySelection), isOverridenMultiAssets && {
2919
- fee_item: feeAssetIndex
2920
- }), {}, {
2921
- dest: addressSelection,
2922
- dest_weight_limit: fees
2923
- });
2924
- }
2925
- return {
2926
- currency_id: currencySelection,
2927
- amount: BigInt(amount),
2928
- dest: addressSelection,
2929
- dest_weight_limit: fees
2930
- };
2931
- };
2932
-
2933
- var XTokensTransferImpl = /*#__PURE__*/function () {
2934
- function XTokensTransferImpl() {
2935
- _classCallCheck(this, XTokensTransferImpl);
2936
- }
2937
- return _createClass(XTokensTransferImpl, null, [{
2938
- key: "transferXTokens",
2939
- value: function transferXTokens(input, currencySelection) {
2940
- var fees = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'Unlimited';
2941
- var api = input.api,
2942
- origin = input.origin,
2943
- asset = input.asset,
2944
- addressSelection = input.addressSelection,
2945
- destination = input.destination,
2946
- scenario = input.scenario,
2947
- overriddenAsset = input.overriddenAsset,
2948
- pallet = input.pallet,
2949
- method = input.method,
2950
- _input$useMultiAssetT = input.useMultiAssetTransfer,
2951
- useMultiAssetTransfer = _input$useMultiAssetT === void 0 ? false : _input$useMultiAssetT;
2952
- var isMultiLocationDestination = _typeof(destination) === 'object';
2953
- if (isMultiLocationDestination) {
2954
- throw new Error('Multilocation destinations are not supported for specific transfer you are trying to create. In special cases such as xTokens or xTransfer pallet try using address multilocation instead (for both destination and address in same multilocation set (eg. X2 - Parachain, Address). For further assistance please open issue in our repository.');
2955
- }
2956
- var isBifrostOrigin = origin === 'BifrostPolkadot' || origin === 'BifrostKusama';
2957
- var isAssetHubDest = destination === 'AssetHubPolkadot' || destination === 'AssetHubKusama';
2958
- var isAstarOrShidenToRelay = scenario === 'ParaToRelay' && (origin === 'Astar' || origin === 'Shiden');
2959
- var isOverridenMultiAssets = overriddenAsset && !sdkCommon.isTMultiLocation(overriddenAsset);
2960
- var shouldUseMultiasset = useMultiAssetTransfer || isAstarOrShidenToRelay || isAssetHubDest && !isBifrostOrigin || !!isOverridenMultiAssets;
2961
- var modifiedCurrencySelection = getCurrencySelection(input, shouldUseMultiasset, currencySelection);
2962
- var section = shouldUseMultiasset ? isOverridenMultiAssets ? 'transfer_multiassets' : 'transfer_multiasset' : 'transfer';
2963
- var parameters = getXTokensParameters(shouldUseMultiasset, modifiedCurrencySelection, addressSelection, asset.amount, fees, overriddenAsset);
2964
- var call = {
2965
- module: pallet !== null && pallet !== void 0 ? pallet : 'XTokens',
2966
- section: method !== null && method !== void 0 ? method : section,
2967
- parameters: parameters
2968
- };
2969
- return api.callTxMethod(call);
2970
- }
2971
- }]);
2972
- }();
2973
-
2974
- var getBridgeStatus = /*#__PURE__*/function () {
2975
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(api) {
2824
+ var generateMessageId = /*#__PURE__*/function () {
2825
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(api, senderAddress, sourceParaId, tokenAddress, recipientAddress, amount) {
2826
+ var accountNextId, sourceAccountHex, entropy;
2976
2827
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2977
2828
  while (1) switch (_context.prev = _context.next) {
2978
2829
  case 0:
2979
2830
  _context.next = 2;
2980
- return api.init('BridgeHubPolkadot');
2831
+ return api.getFromRpc('system', 'accountNextIndex', senderAddress);
2981
2832
  case 2:
2982
- _context.prev = 2;
2983
- _context.next = 5;
2984
- return api.getBridgeStatus();
2985
- case 5:
2986
- return _context.abrupt("return", _context.sent);
2833
+ accountNextId = _context.sent;
2834
+ sourceAccountHex = api.accountToHex(senderAddress);
2835
+ entropy = new Uint8Array([].concat(_toConsumableArray(api.stringToUint8a(sourceParaId.toString())), _toConsumableArray(api.hexToUint8a(sourceAccountHex)), _toConsumableArray(api.stringToUint8a(accountNextId)), _toConsumableArray(api.hexToUint8a(tokenAddress)), _toConsumableArray(api.stringToUint8a(recipientAddress)), _toConsumableArray(api.stringToUint8a(amount.toString()))));
2836
+ return _context.abrupt("return", api.blake2AsHex(entropy));
2987
2837
  case 6:
2988
- _context.prev = 6;
2989
- _context.next = 9;
2990
- return api.disconnect();
2991
- case 9:
2992
- return _context.finish(6);
2993
- case 10:
2994
2838
  case "end":
2995
2839
  return _context.stop();
2996
2840
  }
2997
- }, _callee, null, [[2,, 6, 10]]);
2841
+ }, _callee);
2998
2842
  }));
2999
- return function getBridgeStatus(_x) {
2843
+ return function generateMessageId(_x, _x2, _x3, _x4, _x5, _x6) {
3000
2844
  return _ref.apply(this, arguments);
3001
2845
  };
3002
2846
  }();
3003
2847
 
3004
- var createCustomXcmOnDest = function createCustomXcmOnDest(_ref, origin, version, messageId) {
3005
- var api = _ref.api,
3006
- address = _ref.address,
3007
- asset = _ref.asset,
3008
- scenario = _ref.scenario,
3009
- senderAddress = _ref.senderAddress,
3010
- ahAddress = _ref.ahAddress;
3011
- if (!assets.isForeignAsset(asset)) {
3012
- throw new assets.InvalidCurrencyError("Asset ".concat(JSON.stringify(asset), " is not a foreign asset"));
3013
- }
3014
- if (!asset.multiLocation) {
3015
- throw new assets.InvalidCurrencyError("Asset ".concat(JSON.stringify(asset), " has no multiLocation"));
3016
- }
3017
- if (!senderAddress) {
3018
- throw new InvalidParameterError("Please provide senderAddress");
3019
- }
3020
- if (assets.isNodeEvm(origin) && !ahAddress) {
3021
- throw new InvalidParameterError("Please provide ahAddress");
2848
+ var resolveParaId = function resolveParaId(paraId, destination) {
2849
+ if (sdkCommon.isTMultiLocation(destination) || sdkCommon.isRelayChain(destination) || destination === 'Ethereum') {
2850
+ return undefined;
3022
2851
  }
3023
- var ethAsset = assets.findAssetByMultiLocation(assets.getOtherAssets('Ethereum'), asset.multiLocation);
3024
- if (!ethAsset) {
3025
- throw new assets.InvalidCurrencyError("Could not obtain Ethereum asset address for ".concat(JSON.stringify(asset)));
3026
- }
3027
- var interiorSb = ethAsset.symbol === 'ETH' ? {
3028
- Here: null
3029
- } : {
3030
- X1: [{
3031
- AccountKey20: {
3032
- network: null,
3033
- key: ethAsset.assetId
3034
- }
3035
- }]
3036
- };
3037
- return _defineProperty({}, version, [{
3038
- SetAppendix: [{
3039
- DepositAsset: {
3040
- assets: {
3041
- Wild: 'All'
3042
- },
3043
- beneficiary: createBeneficiaryMultiLocation({
3044
- api: api,
3045
- scenario: scenario,
3046
- pallet: 'PolkadotXcm',
3047
- recipientAddress: assets.isNodeEvm(origin) ? ahAddress : senderAddress,
3048
- version: version
3049
- })
3050
- }
3051
- }]
3052
- }, {
3053
- InitiateReserveWithdraw: {
3054
- assets: {
3055
- Wild: {
3056
- AllOf: {
3057
- id: asset.multiLocation,
3058
- fun: 'Fungible'
3059
- }
3060
- }
3061
- },
3062
- reserve: {
3063
- parents: sdkCommon.Parents.TWO,
3064
- interior: {
3065
- X1: [ETHEREUM_JUNCTION]
3066
- }
3067
- },
3068
- xcm: [{
3069
- BuyExecution: {
3070
- fees: {
3071
- id: {
3072
- parents: sdkCommon.Parents.ZERO,
3073
- interior: interiorSb
3074
- },
3075
- fun: {
3076
- Fungible: 1n
3077
- }
3078
- },
3079
- weight_limit: 'Unlimited'
3080
- }
3081
- }, {
3082
- DepositAsset: {
3083
- assets: {
3084
- Wild: {
3085
- AllCounted: 1
3086
- }
3087
- },
3088
- beneficiary: {
3089
- parents: sdkCommon.Parents.ZERO,
3090
- interior: {
3091
- X1: [{
3092
- AccountKey20: {
3093
- network: null,
3094
- key: address
3095
- }
3096
- }]
3097
- }
3098
- }
3099
- }
3100
- }, {
3101
- SetTopic: messageId
3102
- }]
3103
- }
3104
- }, {
3105
- SetTopic: messageId
3106
- }]);
3107
- };
3108
-
3109
- var generateMessageId = /*#__PURE__*/function () {
3110
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(api, senderAddress, sourceParaId, tokenAddress, recipientAddress, amount) {
3111
- var accountNextId, sourceAccountHex, entropy;
3112
- return _regeneratorRuntime().wrap(function _callee$(_context) {
3113
- while (1) switch (_context.prev = _context.next) {
3114
- case 0:
3115
- _context.next = 2;
3116
- return api.getFromRpc('system', 'accountNextIndex', senderAddress);
3117
- case 2:
3118
- accountNextId = _context.sent;
3119
- sourceAccountHex = api.accountToHex(senderAddress);
3120
- entropy = new Uint8Array([].concat(_toConsumableArray(api.stringToUint8a(sourceParaId.toString())), _toConsumableArray(api.hexToUint8a(sourceAccountHex)), _toConsumableArray(api.stringToUint8a(accountNextId)), _toConsumableArray(api.hexToUint8a(tokenAddress)), _toConsumableArray(api.stringToUint8a(recipientAddress)), _toConsumableArray(api.stringToUint8a(amount.toString()))));
3121
- return _context.abrupt("return", api.blake2AsHex(entropy));
3122
- case 6:
3123
- case "end":
3124
- return _context.stop();
3125
- }
3126
- }, _callee);
3127
- }));
3128
- return function generateMessageId(_x, _x2, _x3, _x4, _x5, _x6) {
3129
- return _ref.apply(this, arguments);
3130
- };
3131
- }();
3132
-
3133
- var supportsXTokens = function supportsXTokens(obj) {
3134
- return _typeof(obj) === 'object' && obj !== null && 'transferXTokens' in obj;
3135
- };
3136
- var supportsXTransfer = function supportsXTransfer(obj) {
3137
- return _typeof(obj) === 'object' && obj !== null && 'transferXTransfer' in obj;
3138
- };
3139
- var supportsPolkadotXCM = function supportsPolkadotXCM(obj) {
3140
- return _typeof(obj) === 'object' && obj !== null && 'transferPolkadotXCM' in obj;
3141
- };
3142
- var ParachainNode = /*#__PURE__*/function () {
3143
- function ParachainNode(node, info, type, version) {
3144
- _classCallCheck(this, ParachainNode);
3145
- this._assetCheckEnabled = true;
3146
- this._info = info;
3147
- this._type = type;
3148
- this._node = node;
3149
- this._version = version;
2852
+ return paraId !== null && paraId !== void 0 ? paraId : getParaId(destination);
2853
+ };
2854
+
2855
+ var supportsXTokens = function supportsXTokens(obj) {
2856
+ return _typeof(obj) === 'object' && obj !== null && 'transferXTokens' in obj;
2857
+ };
2858
+ var supportsXTransfer = function supportsXTransfer(obj) {
2859
+ return _typeof(obj) === 'object' && obj !== null && 'transferXTransfer' in obj;
2860
+ };
2861
+ var supportsPolkadotXCM = function supportsPolkadotXCM(obj) {
2862
+ return _typeof(obj) === 'object' && obj !== null && 'transferPolkadotXCM' in obj;
2863
+ };
2864
+ var ParachainNode = /*#__PURE__*/function () {
2865
+ function ParachainNode(node, info, type, version) {
2866
+ _classCallCheck(this, ParachainNode);
2867
+ this._assetCheckEnabled = true;
2868
+ this._info = info;
2869
+ this._type = type;
2870
+ this._node = node;
2871
+ this._version = version;
3150
2872
  }
3151
2873
  return _createClass(ParachainNode, [{
3152
2874
  key: "info",
@@ -3455,17 +3177,17 @@ var ParachainNode = /*#__PURE__*/function () {
3455
3177
  return Builder(api.clone()).from('AssetHubPolkadot').to(destination).currency({
3456
3178
  symbol: 'DOT',
3457
3179
  amount: AH_EXECUTION_FEE_PADDED
3458
- }).address(address).dryRun(senderAddress);
3180
+ }).address(address).senderAddress(senderAddress).dryRun();
3459
3181
  case 12:
3460
3182
  dryRunResult = _context2.sent;
3461
- if (dryRunResult.success) {
3183
+ if (dryRunResult.origin.success) {
3462
3184
  _context2.next = 15;
3463
3185
  break;
3464
3186
  }
3465
- throw new DryRunFailedError(dryRunResult.failureReason);
3187
+ throw new DryRunFailedError(dryRunResult.origin.failureReason);
3466
3188
  case 15:
3467
3189
  // Pad fee by 50%
3468
- dryRunFeePadded = BigInt(dryRunResult.fee) * BigInt(3) / BigInt(2);
3190
+ dryRunFeePadded = BigInt(dryRunResult.origin.fee) * BigInt(3) / BigInt(2);
3469
3191
  destWithHeader = createVersionedDestination(scenario, version, destination, paraIdTo);
3470
3192
  _extractVersionFromHe = extractVersionFromHeader(destWithHeader), _extractVersionFromHe2 = _slicedToArray(_extractVersionFromHe, 2), _extractVersionFromHe2[0], dest = _extractVersionFromHe2[1];
3471
3193
  call = {
@@ -3956,6 +3678,12 @@ var AssetHubKusama = /*#__PURE__*/function (_ParachainNode) {
3956
3678
  }]);
3957
3679
  }(ParachainNode);
3958
3680
 
3681
+ var createVersionedBeneficiary = function createVersionedBeneficiary(options) {
3682
+ var version = options.version;
3683
+ var multiLocation = createBeneficiaryMultiLocation(options);
3684
+ return addXcmVersionHeader(multiLocation, version);
3685
+ };
3686
+
3959
3687
  var createExecuteXcm = function createExecuteXcm(input, weight, executionFee) {
3960
3688
  var api = input.api,
3961
3689
  _input$version = input.version,
@@ -4055,6 +3783,44 @@ var createExecuteXcm = function createExecuteXcm(input, weight, executionFee) {
4055
3783
  return api.callTxMethod(call);
4056
3784
  };
4057
3785
 
3786
+ var generateAddressMultiLocationV4 = function generateAddressMultiLocationV4(api, address) {
3787
+ var isMultiLocation = _typeof(address) === 'object';
3788
+ if (isMultiLocation) {
3789
+ return _defineProperty({}, exports.Version.V4, address);
3790
+ }
3791
+ var isEthAddress = ethers.ethers.isAddress(address);
3792
+ return addXcmVersionHeader({
3793
+ parents: sdkCommon.Parents.ZERO,
3794
+ interior: {
3795
+ X1: [isEthAddress ? {
3796
+ AccountKey20: {
3797
+ key: address
3798
+ }
3799
+ } : {
3800
+ AccountId32: {
3801
+ id: api.accountToHex(address),
3802
+ network: null
3803
+ }
3804
+ }]
3805
+ }
3806
+ }, exports.Version.V4);
3807
+ };
3808
+
3809
+ var validateAddress = function validateAddress(address, node) {
3810
+ var isDestination = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
3811
+ var isEvm = assets.isNodeEvm(node);
3812
+ var isEthereumAddress = ethers.ethers.isAddress(address);
3813
+ if (isEvm) {
3814
+ if (!isEthereumAddress) {
3815
+ throw new InvalidAddressError("".concat(isDestination ? 'Destination node' : 'Node', " is an EVM chain, but the address provided is not a valid Ethereum address."));
3816
+ }
3817
+ } else {
3818
+ if (isEthereumAddress) {
3819
+ throw new InvalidAddressError("EVM address provided but ".concat(isDestination ? 'destination ' : '', "node is not an EVM chain."));
3820
+ }
3821
+ }
3822
+ };
3823
+
4058
3824
  var createCustomXcmToBifrost = function createCustomXcmToBifrost(_ref, version) {
4059
3825
  var api = _ref.api,
4060
3826
  address = _ref.address,
@@ -6549,7 +6315,618 @@ var nodes = function nodes() {
6549
6315
  Peaq: new Peaq(),
6550
6316
  Polimec: new Polimec()
6551
6317
  };
6552
- };
6318
+ };
6319
+
6320
+ /**
6321
+ * Retrieves the parachain ID for a specified node.
6322
+ *
6323
+ * @param node - The node for which to get the paraId.
6324
+ * @returns The parachain ID of the node.
6325
+ */
6326
+ var getParaId = function getParaId(node) {
6327
+ if (node === 'Ethereum') {
6328
+ return ETH_PARA_ID;
6329
+ }
6330
+ return getNodeConfig(node).paraId;
6331
+ };
6332
+
6333
+ var shuffleArray = function shuffleArray(array) {
6334
+ var copy = _toConsumableArray(array);
6335
+ for (var i = copy.length - 1; i > 0; i--) {
6336
+ var j = Math.floor(Math.random() * (i + 1));
6337
+ var _ref = [copy[j], copy[i]];
6338
+ copy[i] = _ref[0];
6339
+ copy[j] = _ref[1];
6340
+ }
6341
+ return copy;
6342
+ };
6343
+
6344
+ var shuffleWsProviders = function shuffleWsProviders(node, wsProviders) {
6345
+ var ALLOWED_NODES = ['Hydration', 'Acala'];
6346
+ if (ALLOWED_NODES.includes(node)) {
6347
+ return shuffleArray(wsProviders);
6348
+ }
6349
+ return wsProviders;
6350
+ };
6351
+
6352
+ var createApiInstanceForNode = /*#__PURE__*/function () {
6353
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(api, node) {
6354
+ var wsUrl, resolvedWsUrl;
6355
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
6356
+ while (1) switch (_context.prev = _context.next) {
6357
+ case 0:
6358
+ wsUrl = getNodeProviders(node);
6359
+ resolvedWsUrl = Array.isArray(wsUrl) ? shuffleWsProviders(node, wsUrl) : wsUrl;
6360
+ return _context.abrupt("return", api.createApiInstance(resolvedWsUrl));
6361
+ case 3:
6362
+ case "end":
6363
+ return _context.stop();
6364
+ }
6365
+ }, _callee);
6366
+ }));
6367
+ return function createApiInstanceForNode(_x, _x2) {
6368
+ return _ref.apply(this, arguments);
6369
+ };
6370
+ }();
6371
+
6372
+ var getMultiLocationTokenId = function getMultiLocationTokenId(location, node) {
6373
+ if (location.interior.type === 'Here') {
6374
+ // native token
6375
+ return assets.getNativeAssetSymbol(node);
6376
+ }
6377
+ var foreignAssets = assets.getOtherAssets(node);
6378
+ if (location.interior.type === 'X2') {
6379
+ if (location.interior.value[0].type === 'PalletInstance' && location.interior.value[0].value === 50 && location.interior.value[1].type === 'GeneralIndex') {
6380
+ var _foreignAssets$find$a, _foreignAssets$find;
6381
+ var assetId = location.interior.value[1].value;
6382
+ return (_foreignAssets$find$a = (_foreignAssets$find = foreignAssets.find(function (asset) {
6383
+ return asset.assetId === String(assetId);
6384
+ })) === null || _foreignAssets$find === void 0 ? void 0 : _foreignAssets$find.assetId) !== null && _foreignAssets$find$a !== void 0 ? _foreignAssets$find$a : null;
6385
+ }
6386
+ }
6387
+ return null;
6388
+ };
6389
+
6390
+ var computeFeeFromDryRun = function computeFeeFromDryRun(dryRun, node, executionFee) {
6391
+ // Extract delivery fees from emitted events
6392
+ var deliveryFees = [];
6393
+ var _iterator = _createForOfIteratorHelper(dryRun.value.emitted_events),
6394
+ _step;
6395
+ try {
6396
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
6397
+ var e = _step.value;
6398
+ var isXcmEvent = e.type === 'XcmPallet' || e.type === 'PolkadotXcm' || e.type === 'CumulusXcm';
6399
+ var isFeesPaid = e.value.type === 'FeesPaid';
6400
+ if (isXcmEvent && isFeesPaid && e.value.value.fees) {
6401
+ var _iterator2 = _createForOfIteratorHelper(e.value.value.fees),
6402
+ _step2;
6403
+ try {
6404
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
6405
+ var feeItem = _step2.value;
6406
+ if (feeItem.fun.type === 'NonFungible') continue;
6407
+ var plancks = feeItem.fun.value;
6408
+ var tokenSymbol = getMultiLocationTokenId(feeItem.id, node);
6409
+ if (!tokenSymbol || !plancks) continue;
6410
+ deliveryFees.push({
6411
+ plancks: plancks,
6412
+ tokenSymbol: tokenSymbol
6413
+ });
6414
+ }
6415
+ } catch (err) {
6416
+ _iterator2.e(err);
6417
+ } finally {
6418
+ _iterator2.f();
6419
+ }
6420
+ }
6421
+ }
6422
+ } catch (err) {
6423
+ _iterator.e(err);
6424
+ } finally {
6425
+ _iterator.f();
6426
+ }
6427
+ var nativeAssetSymbol = assets.getNativeAssetSymbol(node);
6428
+ // Sum the fees that match the feeToken
6429
+ var totalDeliveryFees = deliveryFees.filter(function (df) {
6430
+ return df.tokenSymbol === nativeAssetSymbol;
6431
+ }).reduce(function (acc, df) {
6432
+ return acc + df.plancks;
6433
+ }, 0n);
6434
+ return totalDeliveryFees + executionFee;
6435
+ };
6436
+
6437
+ var getMultiLocationTokenIdPjs = function getMultiLocationTokenIdPjs(location, node) {
6438
+ if (location.interior === 'Here') {
6439
+ // native token
6440
+ return assets.getNativeAssetSymbol(node);
6441
+ }
6442
+ var foreignAssets = assets.getOtherAssets(node);
6443
+ if (Object.keys(location.interior)[0] === 'X2') {
6444
+ var junctions = Object.values(location.interior)[0];
6445
+ if (Object.keys(junctions[0])[0] === 'PalletInstance' && junctions[0].PalletInstance === '50' && Object.keys(junctions[1])[0] === 'GeneralIndex') {
6446
+ var _foreignAssets$find$s, _foreignAssets$find;
6447
+ var assetId = junctions[1].GeneralIndex;
6448
+ return (_foreignAssets$find$s = (_foreignAssets$find = foreignAssets.find(function (asset) {
6449
+ return asset.assetId === String(assetId);
6450
+ })) === null || _foreignAssets$find === void 0 ? void 0 : _foreignAssets$find.symbol) !== null && _foreignAssets$find$s !== void 0 ? _foreignAssets$find$s : null;
6451
+ }
6452
+ }
6453
+ return null;
6454
+ };
6455
+
6456
+ var computeFeeFromDryRunPjs = function computeFeeFromDryRunPjs(dryRun, node, executionFee) {
6457
+ // Extract delivery fees from emitted events
6458
+ var deliveryFees = [];
6459
+ var _iterator = _createForOfIteratorHelper(dryRun.Ok.emittedEvents),
6460
+ _step;
6461
+ try {
6462
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
6463
+ var e = _step.value;
6464
+ var isXcmEvent = e.section === 'xcmPallet' || e.section === 'polkadotXcm' || e.section === 'cumulusXcm';
6465
+ var isFeesPaid = e.method === 'FeesPaid';
6466
+ if (isXcmEvent && isFeesPaid && e.data.fees) {
6467
+ var _iterator2 = _createForOfIteratorHelper(e.data.fees),
6468
+ _step2;
6469
+ try {
6470
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
6471
+ var feeItem = _step2.value;
6472
+ if (feeItem.fun.NonFungible) continue;
6473
+ var plancks = BigInt(feeItem.fun.Fungible.replace(/,/g, ''));
6474
+ var tokenSymbol = getMultiLocationTokenIdPjs(feeItem.id, node);
6475
+ if (!tokenSymbol || !plancks) continue;
6476
+ deliveryFees.push({
6477
+ plancks: plancks,
6478
+ tokenSymbol: tokenSymbol
6479
+ });
6480
+ }
6481
+ } catch (err) {
6482
+ _iterator2.e(err);
6483
+ } finally {
6484
+ _iterator2.f();
6485
+ }
6486
+ }
6487
+ }
6488
+ } catch (err) {
6489
+ _iterator.e(err);
6490
+ } finally {
6491
+ _iterator.f();
6492
+ }
6493
+ var nativeAssetSymbol = assets.getNativeAssetSymbol(node);
6494
+ // Sum the fees that match the feeToken
6495
+ var totalDeliveryFees = deliveryFees.filter(function (df) {
6496
+ return df.tokenSymbol === nativeAssetSymbol;
6497
+ }).reduce(function (acc, df) {
6498
+ return acc + df.plancks;
6499
+ }, 0n);
6500
+ return totalDeliveryFees + executionFee;
6501
+ };
6502
+
6503
+ var resolveModuleError = function resolveModuleError(node, error) {
6504
+ var palletDetails = pallets.getSupportedPalletsDetails(node).find(function (p) {
6505
+ return p.index === Number(error.index);
6506
+ });
6507
+ if (!palletDetails) {
6508
+ throw new Error("Pallet with index ".concat(error.index, " not found"));
6509
+ }
6510
+ // Use only the first byte of the error to get the error index
6511
+ var errorIndex = Number(error.error.slice(0, 4));
6512
+ var name = palletDetails.name;
6513
+ if (name !== 'XTokens' && name !== 'PolkadotXcm' && name !== 'XcmPallet') {
6514
+ throw new Error("Pallet ".concat(name, " is not supported"));
6515
+ }
6516
+ var failureReason = name === 'XTokens' ? Object.values(exports.XTokensError)[errorIndex] : Object.values(exports.PolkadotXcmError)[errorIndex];
6517
+ if (!failureReason) {
6518
+ throw new Error("Error index ".concat(errorIndex, " not found in ").concat(name, " pallet"));
6519
+ }
6520
+ return failureReason;
6521
+ };
6522
+
6523
+ var getFees = function getFees(scenario) {
6524
+ if (scenario === 'ParaToRelay') {
6525
+ return 4600000000;
6526
+ } else if (scenario === 'ParaToPara') {
6527
+ return 399600000000;
6528
+ }
6529
+ throw new Error("Fees for scenario ".concat(scenario, " are not defined."));
6530
+ };
6531
+
6532
+ /**
6533
+ * Retrieves the node instance for a given node.
6534
+ *
6535
+ * @param node - The node identifier.
6536
+ * @returns The node instance
6537
+ */
6538
+ var getNode = function getNode(node) {
6539
+ var nodeMap = nodes();
6540
+ return nodeMap[node];
6541
+ };
6542
+
6543
+ // Contains important call creation utils (Selection of fees,formating of header and more.. )
6544
+ /**
6545
+ * Determines the relay chain for a given node.
6546
+ *
6547
+ * @param node - The node for which to determine the relay chain.
6548
+ * @returns 'Kusama' if the node's relay chain symbol is 'KSM'; otherwise, 'Polkadot'.
6549
+ */
6550
+ var determineRelayChain = function determineRelayChain(node) {
6551
+ return assets.getRelayChainSymbol(node) === 'KSM' ? 'Kusama' : 'Polkadot';
6552
+ };
6553
+
6554
+ /**
6555
+ * Retrieves the node name corresponding to a specified parachain ID.
6556
+ *
6557
+ * @param paraId - The parachain ID.
6558
+ * @returns The node name if found; otherwise, null.
6559
+ */
6560
+ var getTNode = function getTNode(paraId, ecosystem) {
6561
+ var _NODE_NAMES_DOT_KSM$f;
6562
+ if (paraId === 0) {
6563
+ return ecosystem === 'polkadot' ? 'Polkadot' : 'Kusama';
6564
+ }
6565
+ if (paraId === 1) {
6566
+ return 'Ethereum';
6567
+ }
6568
+ return (_NODE_NAMES_DOT_KSM$f = sdkCommon.NODE_NAMES_DOT_KSM.find(function (nodeName) {
6569
+ return getNode(nodeName).type === ecosystem && getParaId(nodeName) === paraId;
6570
+ })) !== null && _NODE_NAMES_DOT_KSM$f !== void 0 ? _NODE_NAMES_DOT_KSM$f : null;
6571
+ };
6572
+
6573
+ var isAssetHub = function isAssetHub(chain) {
6574
+ return chain === 'AssetHubPolkadot' || chain === 'AssetHubKusama';
6575
+ };
6576
+ var isBridgeHub = function isBridgeHub(chain) {
6577
+ return chain === 'BridgeHubPolkadot' || chain === 'BridgeHubKusama';
6578
+ };
6579
+ var isPeople = function isPeople(chain) {
6580
+ return chain === 'PeoplePolkadot' || chain === 'PeopleKusama';
6581
+ };
6582
+ var isSystemPara = function isSystemPara(chain) {
6583
+ return isAssetHub(chain) || isBridgeHub(chain) || isPeople(chain);
6584
+ };
6585
+ var mul = function mul(v, num) {
6586
+ var den = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1n;
6587
+ return v * num / den;
6588
+ };
6589
+ var padFee = function padFee(raw, origin, dest, side) {
6590
+ var relayOrigin = sdkCommon.isRelayChain(origin);
6591
+ var relayDest = sdkCommon.isRelayChain(dest);
6592
+ var sysParaOrigin = isSystemPara(origin);
6593
+ var sysParaDest = isSystemPara(dest);
6594
+ var relayToPara = relayOrigin && !relayDest;
6595
+ var sysParaToPara = sysParaOrigin && !sysParaDest;
6596
+ var paraToPara = !relayOrigin && !sysParaOrigin;
6597
+ if (sysParaToPara) return raw * 40n;
6598
+ if (relayToPara) return side === 'origin' ? mul(raw, 320n, 100n) : mul(raw, 3000n, 100n);
6599
+ if (paraToPara) return mul(raw, 130n, 100n);
6600
+ // apply default 30% padding
6601
+ return mul(raw, 130n, 100n);
6602
+ };
6603
+
6604
+ var createOriginLocation = function createOriginLocation(origin, destination) {
6605
+ if (sdkCommon.isRelayChain(origin)) return DOT_MULTILOCATION;
6606
+ return {
6607
+ parents: sdkCommon.isRelayChain(destination) ? sdkCommon.Parents.ZERO : sdkCommon.Parents.ONE,
6608
+ interior: {
6609
+ X1: [{
6610
+ Parachain: getParaId(origin)
6611
+ }]
6612
+ }
6613
+ };
6614
+ };
6615
+ var getFeeForDestNode = /*#__PURE__*/function () {
6616
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref) {
6617
+ var api, forwardedXcms, origin, destination, address, senderAddress, currency, disableFallback, calcPaymentInfoFee, dryRunResult, fee, newForwardedXcms, destParaId;
6618
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
6619
+ while (1) switch (_context2.prev = _context2.next) {
6620
+ case 0:
6621
+ api = _ref.api, forwardedXcms = _ref.forwardedXcms, origin = _ref.origin, destination = _ref.destination, address = _ref.address, senderAddress = _ref.senderAddress, currency = _ref.currency, disableFallback = _ref.disableFallback;
6622
+ calcPaymentInfoFee = /*#__PURE__*/function () {
6623
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
6624
+ var originAsset, currencyInput, tx, rawFee;
6625
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
6626
+ while (1) switch (_context.prev = _context.next) {
6627
+ case 0:
6628
+ if (!('multiasset' in currency)) {
6629
+ _context.next = 2;
6630
+ break;
6631
+ }
6632
+ throw new assets.InvalidCurrencyError('Multi-assets are not yet supported for XCM fee calculation.');
6633
+ case 2:
6634
+ originAsset = assets.findAsset(origin, currency, destination);
6635
+ if (originAsset) {
6636
+ _context.next = 5;
6637
+ break;
6638
+ }
6639
+ throw new assets.InvalidCurrencyError("Currency ".concat(JSON.stringify(currency), " not found in ").concat(origin));
6640
+ case 5:
6641
+ currencyInput = originAsset.multiLocation ? {
6642
+ multilocation: originAsset.multiLocation
6643
+ } : {
6644
+ symbol: originAsset.symbol
6645
+ };
6646
+ _context.next = 8;
6647
+ return Builder(api).from(destination).to(origin).address(senderAddress).senderAddress(address).currency(_objectSpread2(_objectSpread2({}, currencyInput), {}, {
6648
+ amount: currency.amount
6649
+ })).build();
6650
+ case 8:
6651
+ tx = _context.sent;
6652
+ _context.next = 11;
6653
+ return api.calculateTransactionFee(tx, address);
6654
+ case 11:
6655
+ rawFee = _context.sent;
6656
+ return _context.abrupt("return", padFee(rawFee, origin, destination, 'destination'));
6657
+ case 13:
6658
+ case "end":
6659
+ return _context.stop();
6660
+ }
6661
+ }, _callee);
6662
+ }));
6663
+ return function calcPaymentInfoFee() {
6664
+ return _ref3.apply(this, arguments);
6665
+ };
6666
+ }();
6667
+ if (!(!assets.hasDryRunSupport(destination) || !forwardedXcms)) {
6668
+ _context2.next = 7;
6669
+ break;
6670
+ }
6671
+ _context2.next = 5;
6672
+ return calcPaymentInfoFee();
6673
+ case 5:
6674
+ _context2.t0 = _context2.sent;
6675
+ return _context2.abrupt("return", {
6676
+ fee: _context2.t0,
6677
+ feeType: 'paymentInfo'
6678
+ });
6679
+ case 7:
6680
+ _context2.next = 9;
6681
+ return api.getDryRunXcm({
6682
+ originLocation: addXcmVersionHeader(createOriginLocation(origin, destination), exports.Version.V4),
6683
+ xcm: forwardedXcms[1][0],
6684
+ node: destination,
6685
+ origin: origin
6686
+ });
6687
+ case 9:
6688
+ dryRunResult = _context2.sent;
6689
+ if (dryRunResult.success) {
6690
+ _context2.next = 18;
6691
+ break;
6692
+ }
6693
+ if (!disableFallback) {
6694
+ _context2.next = 13;
6695
+ break;
6696
+ }
6697
+ return _context2.abrupt("return", {
6698
+ dryRunError: dryRunResult.failureReason
6699
+ });
6700
+ case 13:
6701
+ _context2.next = 15;
6702
+ return calcPaymentInfoFee();
6703
+ case 15:
6704
+ _context2.t1 = _context2.sent;
6705
+ _context2.t2 = dryRunResult.failureReason;
6706
+ return _context2.abrupt("return", {
6707
+ fee: _context2.t1,
6708
+ feeType: 'paymentInfo',
6709
+ dryRunError: _context2.t2
6710
+ });
6711
+ case 18:
6712
+ fee = dryRunResult.fee, newForwardedXcms = dryRunResult.forwardedXcms, destParaId = dryRunResult.destParaId;
6713
+ return _context2.abrupt("return", {
6714
+ fee: fee,
6715
+ feeType: 'dryRun',
6716
+ forwardedXcms: newForwardedXcms,
6717
+ destParaId: destParaId
6718
+ });
6719
+ case 20:
6720
+ case "end":
6721
+ return _context2.stop();
6722
+ }
6723
+ }, _callee2);
6724
+ }));
6725
+ return function getFeeForDestNode(_x) {
6726
+ return _ref2.apply(this, arguments);
6727
+ };
6728
+ }();
6729
+
6730
+ var dryRunInternal = /*#__PURE__*/function () {
6731
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(options) {
6732
+ var _findAsset, _intermediateFees$ass, _intermediateFees$bri, _destinationDryRun;
6733
+ var origin, destination, currency, api, tx, senderAddress, asset, originDryRun, initialForwardedXcms, initialDestParaId, currentOrigin, forwardedXcms, nextParaId, intermediateFees, destinationDryRun, nextChain, hopApi, hopDryRun, newXcms, destParaId;
6734
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
6735
+ while (1) switch (_context.prev = _context.next) {
6736
+ case 0:
6737
+ origin = options.origin, destination = options.destination, currency = options.currency, api = options.api, tx = options.tx, senderAddress = options.senderAddress;
6738
+ asset = (_findAsset = assets.findAsset(origin, currency, destination)) !== null && _findAsset !== void 0 ? _findAsset : origin === 'AssetHubPolkadot' ? assets.findAsset('Ethereum', currency, null) : null;
6739
+ if (asset) {
6740
+ _context.next = 4;
6741
+ break;
6742
+ }
6743
+ throw new assets.InvalidCurrencyError("Asset ".concat(JSON.stringify(currency), " not found on ").concat(origin));
6744
+ case 4:
6745
+ _context.next = 6;
6746
+ return api.getDryRunCall({
6747
+ tx: tx,
6748
+ node: origin,
6749
+ address: senderAddress
6750
+ });
6751
+ case 6:
6752
+ originDryRun = _context.sent;
6753
+ if (originDryRun.success) {
6754
+ _context.next = 9;
6755
+ break;
6756
+ }
6757
+ return _context.abrupt("return", {
6758
+ origin: originDryRun
6759
+ });
6760
+ case 9:
6761
+ initialForwardedXcms = originDryRun.forwardedXcms, initialDestParaId = originDryRun.destParaId;
6762
+ currentOrigin = origin;
6763
+ forwardedXcms = initialForwardedXcms;
6764
+ nextParaId = initialDestParaId;
6765
+ intermediateFees = {};
6766
+ case 14:
6767
+ if (!(Array.isArray(forwardedXcms) && forwardedXcms.length && forwardedXcms[1].length && ('disconnect' in api.getApi() ? Object.values(forwardedXcms[1][0]).length : forwardedXcms[1][0].value.length) > 0 && nextParaId !== undefined)) {
6768
+ _context.next = 41;
6769
+ break;
6770
+ }
6771
+ nextChain = getTNode(nextParaId, determineRelayChain(origin) === 'Polkadot' ? 'polkadot' : 'kusama');
6772
+ if (nextChain) {
6773
+ _context.next = 18;
6774
+ break;
6775
+ }
6776
+ throw new Error("Unable to find TNode for paraId ".concat(nextParaId));
6777
+ case 18:
6778
+ hopApi = api.clone();
6779
+ _context.prev = 19;
6780
+ if (assets.hasDryRunSupport(nextChain)) {
6781
+ _context.next = 23;
6782
+ break;
6783
+ }
6784
+ if (nextChain === destination) {
6785
+ destinationDryRun = {
6786
+ success: false,
6787
+ failureReason: "DryRunApi is not available on node ".concat(nextChain)
6788
+ };
6789
+ }
6790
+ return _context.abrupt("break", 41);
6791
+ case 23:
6792
+ _context.next = 25;
6793
+ return hopApi.init(nextChain);
6794
+ case 25:
6795
+ _context.next = 27;
6796
+ return hopApi.getDryRunXcm({
6797
+ originLocation: addXcmVersionHeader(createOriginLocation(currentOrigin, nextChain), exports.Version.V4),
6798
+ xcm: forwardedXcms[1][0],
6799
+ node: nextChain,
6800
+ origin: currentOrigin
6801
+ });
6802
+ case 27:
6803
+ hopDryRun = _context.sent;
6804
+ if (nextChain === destination || sdkCommon.isRelayChain(nextChain) && !sdkCommon.isRelayChain(destination)) {
6805
+ destinationDryRun = hopDryRun;
6806
+ } else if (nextChain === 'AssetHubPolkadot') {
6807
+ intermediateFees.assetHub = hopDryRun;
6808
+ } else if (nextChain === 'BridgeHubPolkadot') {
6809
+ intermediateFees.bridgeHub = hopDryRun;
6810
+ }
6811
+ if (hopDryRun.success) {
6812
+ _context.next = 31;
6813
+ break;
6814
+ }
6815
+ return _context.abrupt("break", 41);
6816
+ case 31:
6817
+ newXcms = hopDryRun.forwardedXcms, destParaId = hopDryRun.destParaId;
6818
+ forwardedXcms = newXcms;
6819
+ nextParaId = destParaId;
6820
+ currentOrigin = nextChain;
6821
+ case 35:
6822
+ _context.prev = 35;
6823
+ _context.next = 38;
6824
+ return hopApi.disconnect();
6825
+ case 38:
6826
+ return _context.finish(35);
6827
+ case 39:
6828
+ _context.next = 14;
6829
+ break;
6830
+ case 41:
6831
+ return _context.abrupt("return", {
6832
+ origin: originDryRun.success ? _objectSpread2(_objectSpread2({}, originDryRun), {}, {
6833
+ currency: assets.getNativeAssetSymbol(origin)
6834
+ }) : originDryRun,
6835
+ assetHub: (_intermediateFees$ass = intermediateFees.assetHub) !== null && _intermediateFees$ass !== void 0 && _intermediateFees$ass.success ? _objectSpread2(_objectSpread2({}, intermediateFees.assetHub), {}, {
6836
+ currency: assets.getNativeAssetSymbol('AssetHubPolkadot')
6837
+ }) : intermediateFees.assetHub,
6838
+ bridgeHub: (_intermediateFees$bri = intermediateFees.bridgeHub) !== null && _intermediateFees$bri !== void 0 && _intermediateFees$bri.success ? _objectSpread2(_objectSpread2({}, intermediateFees.bridgeHub), {}, {
6839
+ currency: assets.getNativeAssetSymbol('BridgeHubPolkadot')
6840
+ }) : intermediateFees.bridgeHub,
6841
+ destination: (_destinationDryRun = destinationDryRun) !== null && _destinationDryRun !== void 0 && _destinationDryRun.success ? _objectSpread2(_objectSpread2({}, destinationDryRun), {}, {
6842
+ currency: asset.symbol
6843
+ }) : destinationDryRun
6844
+ });
6845
+ case 42:
6846
+ case "end":
6847
+ return _context.stop();
6848
+ }
6849
+ }, _callee, null, [[19,, 35, 39]]);
6850
+ }));
6851
+ return function dryRunInternal(_x) {
6852
+ return _ref.apply(this, arguments);
6853
+ };
6854
+ }();
6855
+
6856
+ var dryRun = /*#__PURE__*/function () {
6857
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(options) {
6858
+ var api, senderAddress, origin;
6859
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
6860
+ while (1) switch (_context.prev = _context.next) {
6861
+ case 0:
6862
+ api = options.api, senderAddress = options.senderAddress, origin = options.origin;
6863
+ validateAddress(senderAddress, origin, false);
6864
+ _context.next = 4;
6865
+ return api.init(origin);
6866
+ case 4:
6867
+ _context.prev = 4;
6868
+ _context.next = 7;
6869
+ return dryRunInternal(options);
6870
+ case 7:
6871
+ return _context.abrupt("return", _context.sent);
6872
+ case 8:
6873
+ _context.prev = 8;
6874
+ _context.next = 11;
6875
+ return api.disconnect();
6876
+ case 11:
6877
+ return _context.finish(8);
6878
+ case 12:
6879
+ case "end":
6880
+ return _context.stop();
6881
+ }
6882
+ }, _callee, null, [[4,, 8, 12]]);
6883
+ }));
6884
+ return function dryRun(_x) {
6885
+ return _ref.apply(this, arguments);
6886
+ };
6887
+ }();
6888
+
6889
+ var dryRunOrigin = /*#__PURE__*/function () {
6890
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(options) {
6891
+ var api, node, address, result;
6892
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
6893
+ while (1) switch (_context.prev = _context.next) {
6894
+ case 0:
6895
+ api = options.api, node = options.node, address = options.address;
6896
+ validateAddress(address, node, false);
6897
+ _context.next = 4;
6898
+ return api.init(node);
6899
+ case 4:
6900
+ _context.prev = 4;
6901
+ _context.next = 7;
6902
+ return api.getDryRunCall(options);
6903
+ case 7:
6904
+ result = _context.sent;
6905
+ if (!result.success) {
6906
+ _context.next = 12;
6907
+ break;
6908
+ }
6909
+ return _context.abrupt("return", _objectSpread2(_objectSpread2({}, result), {}, {
6910
+ currency: assets.getNativeAssetSymbol(node)
6911
+ }));
6912
+ case 12:
6913
+ return _context.abrupt("return", result);
6914
+ case 13:
6915
+ _context.prev = 13;
6916
+ _context.next = 16;
6917
+ return api.disconnect();
6918
+ case 16:
6919
+ return _context.finish(13);
6920
+ case 17:
6921
+ case "end":
6922
+ return _context.stop();
6923
+ }
6924
+ }, _callee, null, [[4,, 13, 17]]);
6925
+ }));
6926
+ return function dryRunOrigin(_x) {
6927
+ return _ref.apply(this, arguments);
6928
+ };
6929
+ }();
6553
6930
 
6554
6931
  var getParaEthTransferFees = /*#__PURE__*/function () {
6555
6932
  var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(ahApi) {
@@ -6903,352 +7280,143 @@ var transferMoonbeamToEth = /*#__PURE__*/function () {
6903
7280
  _context.next = 5;
6904
7281
  return getBridgeStatus(api.clone());
6905
7282
  case 5:
6906
- bridgeStatus = _context.sent;
6907
- if (!(bridgeStatus !== 'Normal')) {
6908
- _context.next = 8;
6909
- break;
6910
- }
6911
- throw new BridgeHaltedError();
6912
- case 8:
6913
- if (!('multiasset' in currency)) {
6914
- _context.next = 10;
6915
- break;
6916
- }
6917
- throw new Error('Multiassets syntax is not supported for Evm transfers');
6918
- case 10:
6919
- if (!('multilocation' in currency && assets.isOverrideMultiLocationSpecifier(currency.multilocation))) {
6920
- _context.next = 12;
6921
- break;
6922
- }
6923
- throw new Error('Override multilocation is not supported for Evm transfers');
6924
- case 12:
6925
- foundAsset = assets.findAsset(from, currency, to);
6926
- if (!(foundAsset === null)) {
6927
- _context.next = 15;
6928
- break;
6929
- }
6930
- throw new assets.InvalidCurrencyError("Origin node ".concat(from, " does not support currency ").concat(JSON.stringify(currency), "."));
6931
- case 15:
6932
- if (!(!assets.isForeignAsset(foundAsset) || !foundAsset.multiLocation)) {
6933
- _context.next = 17;
6934
- break;
6935
- }
6936
- throw new assets.InvalidCurrencyError('Currency must be a foreign asset with valid multi-location');
6937
- case 17:
6938
- ethAsset = assets.findAssetByMultiLocation(assets.getOtherAssets('Ethereum'), foundAsset.multiLocation);
6939
- if (!(!ethAsset || !ethAsset.assetId)) {
6940
- _context.next = 20;
6941
- break;
6942
- }
6943
- throw new assets.InvalidCurrencyError("Could not obtain Ethereum asset address for ".concat(JSON.stringify(foundAsset)));
6944
- case 20:
6945
- contract = isEthersSigner(signer) ? new ethers.Contract(xcmInterfacePrecompile, abi, signer) : viem.getContract({
6946
- abi: abi,
6947
- address: xcmInterfacePrecompile,
6948
- client: {
6949
- "public": viem.createPublicClient({
6950
- chain: signer.chain,
6951
- transport: viem.http()
6952
- }),
6953
- wallet: signer
6954
- }
6955
- });
6956
- if (!isEthersSigner(signer)) {
6957
- _context.next = 27;
6958
- break;
6959
- }
6960
- _context.next = 24;
6961
- return signer.getAddress();
6962
- case 24:
6963
- _context.t0 = _context.sent;
6964
- _context.next = 28;
6965
- break;
6966
- case 27:
6967
- _context.t0 = (_signer$account = signer.account) === null || _signer$account === void 0 ? void 0 : _signer$account.address;
6968
- case 28:
6969
- senderAddress = _context.t0;
6970
- if (senderAddress) {
6971
- _context.next = 31;
6972
- break;
6973
- }
6974
- throw new Error('Unable to get sender address');
6975
- case 31:
6976
- _context.next = 33;
6977
- return api.init(from);
6978
- case 33:
6979
- _context.next = 35;
6980
- return generateMessageId(api, senderAddress, getParaId(from), ethAsset.assetId, address, currency.amount);
6981
- case 35:
6982
- messageId = _context.sent;
6983
- customXcm = createCustomXcmOnDest({
6984
- api: api,
6985
- address: address,
6986
- scenario: 'ParaToPara',
6987
- senderAddress: senderAddress,
6988
- ahAddress: ahAddress,
6989
- asset: _objectSpread2(_objectSpread2({}, foundAsset), {}, {
6990
- amount: currency.amount
6991
- })}, from, exports.Version.V4, messageId);
6992
- _context.next = 39;
6993
- return api.objectToHex(customXcm, 'XcmVersionedXcm');
6994
- case 39:
6995
- customXcmOnDest = _context.sent;
6996
- _context.next = 42;
6997
- return api.createApiForNode('AssetHubPolkadot');
6998
- case 42:
6999
- assetHubApi = _context.sent;
7000
- _context.next = 45;
7001
- return getParaEthTransferFees(assetHubApi);
7002
- case 45:
7003
- _yield$getParaEthTran = _context.sent;
7004
- _yield$getParaEthTran2 = _slicedToArray(_yield$getParaEthTran, 2);
7005
- bridgeFee = _yield$getParaEthTran2[0];
7006
- executionFee = _yield$getParaEthTran2[1];
7007
- transferFee = (bridgeFee + executionFee).toString(); // Partially inspired by Moonbeam XCM-SDK
7008
- // https://github.com/moonbeam-foundation/xcm-sdk/blob/ab835c15bf41612604b1c858d956a9f07705ed65/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts#L53
7009
- createTx = function createTx(func, args) {
7010
- if (isEthersContract(contract)) {
7011
- return contract[func].apply(contract, _toConsumableArray(args));
7012
- }
7013
- return contract.write[func](args);
7014
- };
7015
- numberToHex32 = function numberToHex32(num) {
7016
- return typeof num !== 'number' || isNaN(num) ? function () {
7017
- throw new Error('Input must be a valid number');
7018
- }() : "0x".concat((num >>> 0).toString(16).padStart(8, '0'));
7019
- }; // Execute the custom XCM message with the precompile
7020
- _context.next = 54;
7021
- return createTx(isEthersSigner(signer) ? 'transferAssetsUsingTypeAndThenAddress((uint8,bytes[]),(address,uint256)[],uint8,uint8,uint8,bytes)' : 'transferAssetsUsingTypeAndThenAddress', [
7022
- // This represents (1,X1(Parachain(1000)))
7023
- [1, ['0x00' + numberToHex32(getParaId('AssetHubPolkadot')).slice(2)]],
7024
- // Assets including fee and the ERC20 asset, with fee be the first
7025
- [[XCDOT, transferFee], [ethAsset.assetId, currency.amount.toString()]],
7026
- // The TransferType corresponding to asset being sent, 2 represents `DestinationReserve`
7027
- 2,
7028
- // index for the fee
7029
- 0,
7030
- // The TransferType corresponding to fee asset
7031
- 2, customXcmOnDest]);
7032
- case 54:
7033
- tx = _context.sent;
7034
- return _context.abrupt("return", _typeof(tx) === 'object' ? tx.hash : tx);
7035
- case 56:
7036
- case "end":
7037
- return _context.stop();
7038
- }
7039
- }, _callee);
7040
- }));
7041
- return function transferMoonbeamToEth(_x) {
7042
- return _ref2.apply(this, arguments);
7043
- };
7044
- }();
7045
-
7046
- /**
7047
- * Retrieves the node name corresponding to a specified parachain ID.
7048
- *
7049
- * @param paraId - The parachain ID.
7050
- * @returns The node name if found; otherwise, null.
7051
- */
7052
- var getTNode = function getTNode(paraId, ecosystem) {
7053
- var _NODE_NAMES_DOT_KSM$f;
7054
- if (paraId === 0) {
7055
- return ecosystem === 'polkadot' ? 'Polkadot' : 'Kusama';
7056
- }
7057
- if (paraId === 1) {
7058
- return 'Ethereum';
7059
- }
7060
- return (_NODE_NAMES_DOT_KSM$f = sdkCommon.NODE_NAMES_DOT_KSM.find(function (nodeName) {
7061
- return getNode(nodeName).type === ecosystem && getParaId(nodeName) === paraId;
7062
- })) !== null && _NODE_NAMES_DOT_KSM$f !== void 0 ? _NODE_NAMES_DOT_KSM$f : null;
7063
- };
7064
-
7065
- var isAssetHub = function isAssetHub(chain) {
7066
- return chain === 'AssetHubPolkadot' || chain === 'AssetHubKusama';
7067
- };
7068
- var isBridgeHub = function isBridgeHub(chain) {
7069
- return chain === 'BridgeHubPolkadot' || chain === 'BridgeHubKusama';
7070
- };
7071
- var isPeople = function isPeople(chain) {
7072
- return chain === 'PeoplePolkadot' || chain === 'PeopleKusama';
7073
- };
7074
- var isSystemPara = function isSystemPara(chain) {
7075
- return isAssetHub(chain) || isBridgeHub(chain) || isPeople(chain);
7076
- };
7077
- var mul = function mul(v, num) {
7078
- var den = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1n;
7079
- return v * num / den;
7080
- };
7081
- var padFee = function padFee(raw, origin, dest, side) {
7082
- var relayOrigin = sdkCommon.isRelayChain(origin);
7083
- var relayDest = sdkCommon.isRelayChain(dest);
7084
- var sysParaOrigin = isSystemPara(origin);
7085
- var sysParaDest = isSystemPara(dest);
7086
- var relayToPara = relayOrigin && !relayDest;
7087
- var sysParaToPara = sysParaOrigin && !sysParaDest;
7088
- var paraToPara = !relayOrigin && !sysParaOrigin;
7089
- if (sysParaToPara) return raw * 40n;
7090
- if (relayToPara) return side === 'origin' ? mul(raw, 320n, 100n) : mul(raw, 3000n, 100n);
7091
- if (paraToPara) return mul(raw, 130n, 100n);
7092
- // apply default 30% padding
7093
- return mul(raw, 130n, 100n);
7094
- };
7095
-
7096
- var createOriginLocation = function createOriginLocation(origin, destination) {
7097
- if (sdkCommon.isRelayChain(origin)) return DOT_MULTILOCATION;
7098
- return {
7099
- parents: sdkCommon.isRelayChain(destination) ? sdkCommon.Parents.ZERO : sdkCommon.Parents.ONE,
7100
- interior: {
7101
- X1: [{
7102
- Parachain: getParaId(origin)
7103
- }]
7104
- }
7105
- };
7106
- };
7107
- var getFeeForDestNode = /*#__PURE__*/function () {
7108
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref) {
7109
- var api, forwardedXcms, origin, destination, address, senderAddress, currency, disableFallback, calcPaymentInfoFee, dryRunResult, fee, newForwardedXcms, destParaId;
7110
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
7111
- while (1) switch (_context2.prev = _context2.next) {
7112
- case 0:
7113
- api = _ref.api, forwardedXcms = _ref.forwardedXcms, origin = _ref.origin, destination = _ref.destination, address = _ref.address, senderAddress = _ref.senderAddress, currency = _ref.currency, disableFallback = _ref.disableFallback;
7114
- calcPaymentInfoFee = /*#__PURE__*/function () {
7115
- var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
7116
- var originAsset, currencyInput, tx, rawFee;
7117
- return _regeneratorRuntime().wrap(function _callee$(_context) {
7118
- while (1) switch (_context.prev = _context.next) {
7119
- case 0:
7120
- if (!('multiasset' in currency)) {
7121
- _context.next = 2;
7122
- break;
7123
- }
7124
- throw new assets.InvalidCurrencyError('Multi-assets are not yet supported for XCM fee calculation.');
7125
- case 2:
7126
- originAsset = assets.findAsset(origin, currency, destination);
7127
- if (originAsset) {
7128
- _context.next = 5;
7129
- break;
7130
- }
7131
- throw new assets.InvalidCurrencyError("Currency ".concat(JSON.stringify(currency), " not found in ").concat(origin));
7132
- case 5:
7133
- currencyInput = originAsset.multiLocation ? {
7134
- multilocation: originAsset.multiLocation
7135
- } : {
7136
- symbol: originAsset.symbol
7137
- };
7138
- _context.next = 8;
7139
- return Builder(api).from(destination).to(origin).address(senderAddress, address).currency(_objectSpread2(_objectSpread2({}, currencyInput), {}, {
7140
- amount: currency.amount
7141
- })).build();
7142
- case 8:
7143
- tx = _context.sent;
7144
- _context.next = 11;
7145
- return api.calculateTransactionFee(tx, address);
7146
- case 11:
7147
- rawFee = _context.sent;
7148
- return _context.abrupt("return", padFee(rawFee, origin, destination, 'destination'));
7149
- case 13:
7150
- case "end":
7151
- return _context.stop();
7152
- }
7153
- }, _callee);
7154
- }));
7155
- return function calcPaymentInfoFee() {
7156
- return _ref3.apply(this, arguments);
7157
- };
7158
- }();
7159
- if (!(!assets.hasDryRunSupport(destination) || !forwardedXcms)) {
7160
- _context2.next = 7;
7283
+ bridgeStatus = _context.sent;
7284
+ if (!(bridgeStatus !== 'Normal')) {
7285
+ _context.next = 8;
7161
7286
  break;
7162
7287
  }
7163
- _context2.next = 5;
7164
- return calcPaymentInfoFee();
7165
- case 5:
7166
- _context2.t0 = _context2.sent;
7167
- return _context2.abrupt("return", {
7168
- fee: _context2.t0,
7169
- feeType: 'paymentInfo'
7170
- });
7171
- case 7:
7172
- _context2.next = 9;
7173
- return api.getDryRunXcm({
7174
- originLocation: addXcmVersionHeader(createOriginLocation(origin, destination), exports.Version.V4),
7175
- xcm: forwardedXcms[1][0],
7176
- node: destination,
7177
- origin: origin
7178
- });
7179
- case 9:
7180
- dryRunResult = _context2.sent;
7181
- if (dryRunResult.success) {
7182
- _context2.next = 18;
7288
+ throw new BridgeHaltedError();
7289
+ case 8:
7290
+ if (!('multiasset' in currency)) {
7291
+ _context.next = 10;
7183
7292
  break;
7184
7293
  }
7185
- if (!disableFallback) {
7186
- _context2.next = 13;
7294
+ throw new Error('Multiassets syntax is not supported for Evm transfers');
7295
+ case 10:
7296
+ if (!('multilocation' in currency && assets.isOverrideMultiLocationSpecifier(currency.multilocation))) {
7297
+ _context.next = 12;
7187
7298
  break;
7188
7299
  }
7189
- return _context2.abrupt("return", {
7190
- dryRunError: dryRunResult.failureReason
7191
- });
7192
- case 13:
7193
- _context2.next = 15;
7194
- return calcPaymentInfoFee();
7300
+ throw new Error('Override multilocation is not supported for Evm transfers');
7301
+ case 12:
7302
+ foundAsset = assets.findAsset(from, currency, to);
7303
+ if (!(foundAsset === null)) {
7304
+ _context.next = 15;
7305
+ break;
7306
+ }
7307
+ throw new assets.InvalidCurrencyError("Origin node ".concat(from, " does not support currency ").concat(JSON.stringify(currency), "."));
7195
7308
  case 15:
7196
- _context2.t1 = _context2.sent;
7197
- _context2.t2 = dryRunResult.failureReason;
7198
- return _context2.abrupt("return", {
7199
- fee: _context2.t1,
7200
- feeType: 'paymentInfo',
7201
- dryRunError: _context2.t2
7202
- });
7203
- case 18:
7204
- fee = dryRunResult.fee, newForwardedXcms = dryRunResult.forwardedXcms, destParaId = dryRunResult.destParaId;
7205
- return _context2.abrupt("return", {
7206
- fee: fee,
7207
- feeType: 'dryRun',
7208
- forwardedXcms: newForwardedXcms,
7209
- destParaId: destParaId
7210
- });
7309
+ if (!(!assets.isForeignAsset(foundAsset) || !foundAsset.multiLocation)) {
7310
+ _context.next = 17;
7311
+ break;
7312
+ }
7313
+ throw new assets.InvalidCurrencyError('Currency must be a foreign asset with valid multi-location');
7314
+ case 17:
7315
+ ethAsset = assets.findAssetByMultiLocation(assets.getOtherAssets('Ethereum'), foundAsset.multiLocation);
7316
+ if (!(!ethAsset || !ethAsset.assetId)) {
7317
+ _context.next = 20;
7318
+ break;
7319
+ }
7320
+ throw new assets.InvalidCurrencyError("Could not obtain Ethereum asset address for ".concat(JSON.stringify(foundAsset)));
7211
7321
  case 20:
7212
- case "end":
7213
- return _context2.stop();
7214
- }
7215
- }, _callee2);
7216
- }));
7217
- return function getFeeForDestNode(_x) {
7218
- return _ref2.apply(this, arguments);
7219
- };
7220
- }();
7221
-
7222
- var getDryRun = /*#__PURE__*/function () {
7223
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(options) {
7224
- var api, node, address;
7225
- return _regeneratorRuntime().wrap(function _callee$(_context) {
7226
- while (1) switch (_context.prev = _context.next) {
7227
- case 0:
7228
- api = options.api, node = options.node, address = options.address;
7229
- validateAddress(address, node, false);
7230
- _context.next = 4;
7231
- return api.init(node);
7232
- case 4:
7233
- _context.prev = 4;
7234
- _context.next = 7;
7235
- return api.getDryRunCall(options);
7236
- case 7:
7237
- return _context.abrupt("return", _context.sent);
7238
- case 8:
7239
- _context.prev = 8;
7240
- _context.next = 11;
7241
- return api.disconnect();
7242
- case 11:
7243
- return _context.finish(8);
7244
- case 12:
7322
+ contract = isEthersSigner(signer) ? new ethers.Contract(xcmInterfacePrecompile, abi, signer) : viem.getContract({
7323
+ abi: abi,
7324
+ address: xcmInterfacePrecompile,
7325
+ client: {
7326
+ "public": viem.createPublicClient({
7327
+ chain: signer.chain,
7328
+ transport: viem.http()
7329
+ }),
7330
+ wallet: signer
7331
+ }
7332
+ });
7333
+ if (!isEthersSigner(signer)) {
7334
+ _context.next = 27;
7335
+ break;
7336
+ }
7337
+ _context.next = 24;
7338
+ return signer.getAddress();
7339
+ case 24:
7340
+ _context.t0 = _context.sent;
7341
+ _context.next = 28;
7342
+ break;
7343
+ case 27:
7344
+ _context.t0 = (_signer$account = signer.account) === null || _signer$account === void 0 ? void 0 : _signer$account.address;
7345
+ case 28:
7346
+ senderAddress = _context.t0;
7347
+ if (senderAddress) {
7348
+ _context.next = 31;
7349
+ break;
7350
+ }
7351
+ throw new Error('Unable to get sender address');
7352
+ case 31:
7353
+ _context.next = 33;
7354
+ return api.init(from);
7355
+ case 33:
7356
+ _context.next = 35;
7357
+ return generateMessageId(api, senderAddress, getParaId(from), ethAsset.assetId, address, currency.amount);
7358
+ case 35:
7359
+ messageId = _context.sent;
7360
+ customXcm = createCustomXcmOnDest({
7361
+ api: api,
7362
+ address: address,
7363
+ scenario: 'ParaToPara',
7364
+ senderAddress: senderAddress,
7365
+ ahAddress: ahAddress,
7366
+ asset: _objectSpread2(_objectSpread2({}, foundAsset), {}, {
7367
+ amount: currency.amount
7368
+ })}, from, exports.Version.V4, messageId);
7369
+ _context.next = 39;
7370
+ return api.objectToHex(customXcm, 'XcmVersionedXcm');
7371
+ case 39:
7372
+ customXcmOnDest = _context.sent;
7373
+ _context.next = 42;
7374
+ return api.createApiForNode('AssetHubPolkadot');
7375
+ case 42:
7376
+ assetHubApi = _context.sent;
7377
+ _context.next = 45;
7378
+ return getParaEthTransferFees(assetHubApi);
7379
+ case 45:
7380
+ _yield$getParaEthTran = _context.sent;
7381
+ _yield$getParaEthTran2 = _slicedToArray(_yield$getParaEthTran, 2);
7382
+ bridgeFee = _yield$getParaEthTran2[0];
7383
+ executionFee = _yield$getParaEthTran2[1];
7384
+ transferFee = (bridgeFee + executionFee).toString(); // Partially inspired by Moonbeam XCM-SDK
7385
+ // https://github.com/moonbeam-foundation/xcm-sdk/blob/ab835c15bf41612604b1c858d956a9f07705ed65/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts#L53
7386
+ createTx = function createTx(func, args) {
7387
+ if (isEthersContract(contract)) {
7388
+ return contract[func].apply(contract, _toConsumableArray(args));
7389
+ }
7390
+ return contract.write[func](args);
7391
+ };
7392
+ numberToHex32 = function numberToHex32(num) {
7393
+ return typeof num !== 'number' || isNaN(num) ? function () {
7394
+ throw new Error('Input must be a valid number');
7395
+ }() : "0x".concat((num >>> 0).toString(16).padStart(8, '0'));
7396
+ }; // Execute the custom XCM message with the precompile
7397
+ _context.next = 54;
7398
+ return createTx(isEthersSigner(signer) ? 'transferAssetsUsingTypeAndThenAddress((uint8,bytes[]),(address,uint256)[],uint8,uint8,uint8,bytes)' : 'transferAssetsUsingTypeAndThenAddress', [
7399
+ // This represents (1,X1(Parachain(1000)))
7400
+ [1, ['0x00' + numberToHex32(getParaId('AssetHubPolkadot')).slice(2)]],
7401
+ // Assets including fee and the ERC20 asset, with fee be the first
7402
+ [[XCDOT, transferFee], [ethAsset.assetId, currency.amount.toString()]],
7403
+ // The TransferType corresponding to asset being sent, 2 represents `DestinationReserve`
7404
+ 2,
7405
+ // index for the fee
7406
+ 0,
7407
+ // The TransferType corresponding to fee asset
7408
+ 2, customXcmOnDest]);
7409
+ case 54:
7410
+ tx = _context.sent;
7411
+ return _context.abrupt("return", _typeof(tx) === 'object' ? tx.hash : tx);
7412
+ case 56:
7245
7413
  case "end":
7246
7414
  return _context.stop();
7247
7415
  }
7248
- }, _callee, null, [[4,, 8, 12]]);
7416
+ }, _callee);
7249
7417
  }));
7250
- return function getDryRun(_x) {
7251
- return _ref.apply(this, arguments);
7418
+ return function transferMoonbeamToEth(_x) {
7419
+ return _ref2.apply(this, arguments);
7252
7420
  };
7253
7421
  }();
7254
7422
 
@@ -7273,8 +7441,7 @@ var getFeeForOriginNode = /*#__PURE__*/function () {
7273
7441
  });
7274
7442
  case 6:
7275
7443
  _context.next = 8;
7276
- return getDryRun({
7277
- api: api,
7444
+ return api.getDryRunCall({
7278
7445
  tx: tx,
7279
7446
  node: origin,
7280
7447
  address: senderAddress
@@ -7413,7 +7580,7 @@ var getXcmFee = /*#__PURE__*/function () {
7413
7580
  destinationFeeType = 'paymentInfo';
7414
7581
  case 36:
7415
7582
  if (!(Array.isArray(forwardedXcms) && forwardedXcms.length > 0 && forwardedXcms[1].length > 0 && ('disconnect' in api.getApi() ? Object.values(forwardedXcms[1][0]).length : forwardedXcms[1][0].value.length) > 0 && nextParaId !== undefined)) {
7416
- _context.next = 69;
7583
+ _context.next = 68;
7417
7584
  break;
7418
7585
  }
7419
7586
  nextChain = getTNode(nextParaId, determineRelayChain(origin) === 'Polkadot' ? 'polkadot' : 'kusama');
@@ -7484,7 +7651,7 @@ var getXcmFee = /*#__PURE__*/function () {
7484
7651
  destinationFee = destFallback.fee;
7485
7652
  destinationFeeType = destFallback.feeType; // paymentInfo
7486
7653
  case 57:
7487
- return _context.abrupt("break", 69);
7654
+ return _context.abrupt("break", 68);
7488
7655
  case 58:
7489
7656
  if (nextChain === destination || sdkCommon.isRelayChain(nextChain) && !sdkCommon.isRelayChain(destination)) {
7490
7657
  destinationFee = hopResult.fee;
@@ -7507,15 +7674,14 @@ var getXcmFee = /*#__PURE__*/function () {
7507
7674
  currentOrigin = nextChain;
7508
7675
  case 62:
7509
7676
  _context.prev = 62;
7510
- hopApi.setDisconnectAllowed(true);
7511
- _context.next = 66;
7677
+ _context.next = 65;
7512
7678
  return hopApi.disconnect();
7513
- case 66:
7679
+ case 65:
7514
7680
  return _context.finish(62);
7515
- case 67:
7681
+ case 66:
7516
7682
  _context.next = 36;
7517
7683
  break;
7518
- case 69:
7684
+ case 68:
7519
7685
  return _context.abrupt("return", _objectSpread2(_objectSpread2({
7520
7686
  origin: _objectSpread2(_objectSpread2(_objectSpread2({}, originFee && {
7521
7687
  fee: originFee
@@ -7537,11 +7703,11 @@ var getXcmFee = /*#__PURE__*/function () {
7537
7703
  dryRunError: destinationDryRunError
7538
7704
  })
7539
7705
  }));
7540
- case 70:
7706
+ case 69:
7541
7707
  case "end":
7542
7708
  return _context.stop();
7543
7709
  }
7544
- }, _callee, null, [[17,, 25, 30], [41,, 62, 67]]);
7710
+ }, _callee, null, [[17,, 25, 30], [41,, 62, 66]]);
7545
7711
  }));
7546
7712
  return function getXcmFee(_x) {
7547
7713
  return _ref2.apply(this, arguments);
@@ -7615,7 +7781,7 @@ var getXcmFeeEstimate = /*#__PURE__*/function () {
7615
7781
  symbol: originAsset.symbol
7616
7782
  };
7617
7783
  _context.next = 20;
7618
- return Builder(destApi).from(destination).to(origin).address(senderAddress, address).currency(_objectSpread2(_objectSpread2({}, currencyInput), {}, {
7784
+ return Builder(destApi).from(destination).to(origin).address(senderAddress).senderAddress(address).currency(_objectSpread2(_objectSpread2({}, currencyInput), {}, {
7619
7785
  amount: currency.amount
7620
7786
  })).build();
7621
7787
  case 20:
@@ -8099,14 +8265,31 @@ var GeneralBuilder = /*#__PURE__*/function () {
8099
8265
  currency: _currency
8100
8266
  }));
8101
8267
  }
8268
+ /**
8269
+ * Sets the recipient address.
8270
+ *
8271
+ * @param address - The destination address.
8272
+ * @returns An instance of Builder
8273
+ */
8102
8274
  }, {
8103
8275
  key: "address",
8104
- value: function address(_address, senderAddress) {
8276
+ value: function address(_address) {
8105
8277
  return new GeneralBuilder(this.api, this.batchManager, _objectSpread2(_objectSpread2({}, this._options), {}, {
8106
8278
  address: _address
8107
- }, senderAddress ? {
8108
- senderAddress: senderAddress
8109
- } : {}));
8279
+ }));
8280
+ }
8281
+ /**
8282
+ * Sets the sender address.
8283
+ *
8284
+ * @param address - The sender address.
8285
+ * @returns
8286
+ */
8287
+ }, {
8288
+ key: "senderAddress",
8289
+ value: function senderAddress(address) {
8290
+ return new GeneralBuilder(this.api, this.batchManager, _objectSpread2(_objectSpread2({}, this._options), {}, {
8291
+ senderAddress: address
8292
+ }));
8110
8293
  }
8111
8294
  /**
8112
8295
  * Sets the asset hub address. This is used for transfers that go through the Asset Hub and originate from an EVM chain.
@@ -8245,33 +8428,53 @@ var GeneralBuilder = /*#__PURE__*/function () {
8245
8428
  }, {
8246
8429
  key: "dryRun",
8247
8430
  value: function () {
8248
- var _dryRun = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(senderAddress) {
8249
- var tx;
8431
+ var _dryRun2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
8432
+ var _this$_options2, to, address, senderAddress, tx;
8250
8433
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
8251
8434
  while (1) switch (_context3.prev = _context3.next) {
8252
8435
  case 0:
8253
- this.api.setDisconnectAllowed(false);
8436
+ _this$_options2 = this._options, to = _this$_options2.to, address = _this$_options2.address, senderAddress = _this$_options2.senderAddress;
8254
8437
  _context3.next = 3;
8255
8438
  return this.build();
8256
8439
  case 3:
8257
8440
  tx = _context3.sent;
8258
- this.api.setDisconnectAllowed(true);
8259
- return _context3.abrupt("return", getDryRun({
8441
+ if (!sdkCommon.isTMultiLocation(to)) {
8442
+ _context3.next = 6;
8443
+ break;
8444
+ }
8445
+ throw new InvalidParameterError('Multi-Location destination is not supported for XCM fee calculation.');
8446
+ case 6:
8447
+ if (!sdkCommon.isTMultiLocation(address)) {
8448
+ _context3.next = 8;
8449
+ break;
8450
+ }
8451
+ throw new InvalidParameterError('Multi-Location address is not supported for XCM fee calculation.');
8452
+ case 8:
8453
+ if (!(to === 'Ethereum')) {
8454
+ _context3.next = 10;
8455
+ break;
8456
+ }
8457
+ throw new InvalidParameterError('Ethereum destination is not yet supported for XCM fee calculation.');
8458
+ case 10:
8459
+ return _context3.abrupt("return", dryRun({
8260
8460
  api: this.api,
8261
8461
  tx: tx,
8262
8462
  address: senderAddress,
8263
- node: this._options.from
8463
+ origin: this._options.from,
8464
+ destination: to,
8465
+ currency: this._options.currency,
8466
+ senderAddress: this._options.senderAddress
8264
8467
  }));
8265
- case 6:
8468
+ case 11:
8266
8469
  case "end":
8267
8470
  return _context3.stop();
8268
8471
  }
8269
8472
  }, _callee3, this);
8270
8473
  }));
8271
- function dryRun(_x2) {
8272
- return _dryRun.apply(this, arguments);
8474
+ function dryRun$1() {
8475
+ return _dryRun2.apply(this, arguments);
8273
8476
  }
8274
- return dryRun;
8477
+ return dryRun$1;
8275
8478
  }()
8276
8479
  /**
8277
8480
  * Estimates the XCM fee for the transfer using paymentInfo function.
@@ -8282,11 +8485,11 @@ var GeneralBuilder = /*#__PURE__*/function () {
8282
8485
  key: "getXcmFeeEstimate",
8283
8486
  value: (function () {
8284
8487
  var _getXcmFeeEstimate2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
8285
- var _this$_options2, from, to, address, senderAddress, tx;
8488
+ var _this$_options3, from, to, address, senderAddress, tx;
8286
8489
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
8287
8490
  while (1) switch (_context4.prev = _context4.next) {
8288
8491
  case 0:
8289
- _this$_options2 = this._options, from = _this$_options2.from, to = _this$_options2.to, address = _this$_options2.address, senderAddress = _this$_options2.senderAddress;
8492
+ _this$_options3 = this._options, from = _this$_options3.from, to = _this$_options3.to, address = _this$_options3.address, senderAddress = _this$_options3.senderAddress;
8290
8493
  if (!sdkCommon.isTMultiLocation(to)) {
8291
8494
  _context4.next = 3;
8292
8495
  break;
@@ -8311,7 +8514,9 @@ var GeneralBuilder = /*#__PURE__*/function () {
8311
8514
  case 10:
8312
8515
  tx = _context4.sent;
8313
8516
  this.api.setDisconnectAllowed(true);
8314
- return _context4.abrupt("return", getXcmFeeEstimate({
8517
+ _context4.prev = 12;
8518
+ _context4.next = 15;
8519
+ return getXcmFeeEstimate({
8315
8520
  api: this.api,
8316
8521
  tx: tx,
8317
8522
  origin: from,
@@ -8319,12 +8524,20 @@ var GeneralBuilder = /*#__PURE__*/function () {
8319
8524
  address: address,
8320
8525
  senderAddress: senderAddress,
8321
8526
  currency: this._options.currency
8322
- }));
8323
- case 13:
8527
+ });
8528
+ case 15:
8529
+ return _context4.abrupt("return", _context4.sent);
8530
+ case 16:
8531
+ _context4.prev = 16;
8532
+ _context4.next = 19;
8533
+ return this.api.disconnect();
8534
+ case 19:
8535
+ return _context4.finish(16);
8536
+ case 20:
8324
8537
  case "end":
8325
8538
  return _context4.stop();
8326
8539
  }
8327
- }, _callee4, this);
8540
+ }, _callee4, this, [[12,, 16, 20]]);
8328
8541
  }));
8329
8542
  function getXcmFeeEstimate$1() {
8330
8543
  return _getXcmFeeEstimate2.apply(this, arguments);
@@ -8343,7 +8556,7 @@ var GeneralBuilder = /*#__PURE__*/function () {
8343
8556
  var _getXcmFee2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
8344
8557
  var _ref,
8345
8558
  disableFallback,
8346
- _this$_options3,
8559
+ _this$_options4,
8347
8560
  from,
8348
8561
  to,
8349
8562
  address,
@@ -8356,7 +8569,7 @@ var GeneralBuilder = /*#__PURE__*/function () {
8356
8569
  _ref = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : {
8357
8570
  disableFallback: false
8358
8571
  }, disableFallback = _ref.disableFallback;
8359
- _this$_options3 = this._options, from = _this$_options3.from, to = _this$_options3.to, address = _this$_options3.address, senderAddress = _this$_options3.senderAddress;
8572
+ _this$_options4 = this._options, from = _this$_options4.from, to = _this$_options4.to, address = _this$_options4.address, senderAddress = _this$_options4.senderAddress;
8360
8573
  if (!sdkCommon.isTMultiLocation(to)) {
8361
8574
  _context5.next = 4;
8362
8575
  break;
@@ -8379,8 +8592,9 @@ var GeneralBuilder = /*#__PURE__*/function () {
8379
8592
  return this.build();
8380
8593
  case 10:
8381
8594
  tx = _context5.sent;
8382
- this.api.setDisconnectAllowed(false);
8383
- return _context5.abrupt("return", getXcmFee({
8595
+ _context5.prev = 11;
8596
+ _context5.next = 14;
8597
+ return getXcmFee({
8384
8598
  api: this.api,
8385
8599
  tx: tx,
8386
8600
  origin: from,
@@ -8389,12 +8603,20 @@ var GeneralBuilder = /*#__PURE__*/function () {
8389
8603
  address: address,
8390
8604
  currency: this._options.currency,
8391
8605
  disableFallback: disableFallback
8392
- }));
8393
- case 13:
8606
+ });
8607
+ case 14:
8608
+ return _context5.abrupt("return", _context5.sent);
8609
+ case 15:
8610
+ _context5.prev = 15;
8611
+ _context5.next = 18;
8612
+ return this.api.disconnect();
8613
+ case 18:
8614
+ return _context5.finish(15);
8615
+ case 19:
8394
8616
  case "end":
8395
8617
  return _context5.stop();
8396
8618
  }
8397
- }, _callee5, this);
8619
+ }, _callee5, this, [[11,, 15, 19]]);
8398
8620
  }));
8399
8621
  function getXcmFee$1() {
8400
8622
  return _getXcmFee2.apply(this, arguments);
@@ -8436,6 +8658,7 @@ var Builder = function Builder(api) {
8436
8658
 
8437
8659
  var getBalanceForeignPolkadotXcm = /*#__PURE__*/function () {
8438
8660
  var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(api, node, address, asset) {
8661
+ var ASSETS_PALLET_ID, hasRequiredJunctions;
8439
8662
  return _regeneratorRuntime().wrap(function _callee$(_context) {
8440
8663
  while (1) switch (_context.prev = _context.next) {
8441
8664
  case 0:
@@ -8449,7 +8672,7 @@ var getBalanceForeignPolkadotXcm = /*#__PURE__*/function () {
8449
8672
  }
8450
8673
  throw new assets.InvalidCurrencyError("Asset ".concat(JSON.stringify(asset), " has no assetId"));
8451
8674
  case 3:
8452
- return _context.abrupt("return", api.getBalanceForeignAssetsAccount(address, BigInt(asset.assetId)));
8675
+ return _context.abrupt("return", api.getBalanceAssetsPallet(address, BigInt(asset.assetId)));
8453
8676
  case 4:
8454
8677
  if (!(node === 'Mythos')) {
8455
8678
  _context.next = 6;
@@ -8473,19 +8696,21 @@ var getBalanceForeignPolkadotXcm = /*#__PURE__*/function () {
8473
8696
  }
8474
8697
  throw new assets.InvalidCurrencyError("Asset ".concat(JSON.stringify(asset), " has no multi-location"));
8475
8698
  case 11:
8476
- return _context.abrupt("return", api.getAssetHubForeignBalance(address, asset.multiLocation));
8699
+ return _context.abrupt("return", api.getBalanceForeignAssetsPallet(address, asset.multiLocation));
8477
8700
  case 12:
8478
8701
  if (!(node === 'AssetHubPolkadot')) {
8479
8702
  _context.next = 18;
8480
8703
  break;
8481
8704
  }
8482
- if (!asset.multiLocation) {
8705
+ ASSETS_PALLET_ID = 50;
8706
+ hasRequiredJunctions = asset.multiLocation && sdkCommon.hasJunction(asset.multiLocation, 'PalletInstance', ASSETS_PALLET_ID) && sdkCommon.hasJunction(asset.multiLocation, 'GeneralIndex');
8707
+ if (!(!asset.multiLocation || hasRequiredJunctions)) {
8483
8708
  _context.next = 17;
8484
8709
  break;
8485
8710
  }
8486
- return _context.abrupt("return", api.getAssetHubForeignBalance(address, asset.multiLocation));
8711
+ return _context.abrupt("return", api.getBalanceAssetsPallet(address, Number(asset.assetId)));
8487
8712
  case 17:
8488
- return _context.abrupt("return", api.getBalanceForeignAssetsAccount(address, Number(asset.assetId)));
8713
+ return _context.abrupt("return", api.getBalanceForeignAssetsPallet(address, asset.multiLocation));
8489
8714
  case 18:
8490
8715
  return _context.abrupt("return", api.getBalanceForeignPolkadotXcm(address, asset.assetId));
8491
8716
  case 19:
@@ -8514,7 +8739,7 @@ var getBalanceForeignXTokens = /*#__PURE__*/function () {
8514
8739
  }
8515
8740
  throw new assets.InvalidCurrencyError("Asset ".concat(JSON.stringify(asset), " has no assetId"));
8516
8741
  case 3:
8517
- return _context.abrupt("return", api.getBalanceForeignAssetsAccount(address, BigInt(asset.assetId)));
8742
+ return _context.abrupt("return", api.getBalanceAssetsPallet(address, BigInt(asset.assetId)));
8518
8743
  case 4:
8519
8744
  if (!(node === 'BifrostPolkadot' || node === 'BifrostKusama')) {
8520
8745
  _context.next = 6;
@@ -8795,7 +9020,7 @@ var getOriginFeeDetailsInternal = /*#__PURE__*/function () {
8795
9020
  return api.init(origin);
8796
9021
  case 3:
8797
9022
  _context.next = 5;
8798
- return Builder(api).from(origin).to(destination).currency(currency).address(accountDestination, account).build();
9023
+ return Builder(api).from(origin).to(destination).currency(currency).address(accountDestination).senderAddress(account).build();
8799
9024
  case 5:
8800
9025
  tx = _context.sent;
8801
9026
  _context.next = 8;
@@ -9211,6 +9436,8 @@ exports.createBeneficiaryMultiLocation = createBeneficiaryMultiLocation;
9211
9436
  exports.createVersionedBeneficiary = createVersionedBeneficiary;
9212
9437
  exports.createX1Payload = createX1Payload;
9213
9438
  exports.determineRelayChain = determineRelayChain;
9439
+ exports.dryRun = dryRun;
9440
+ exports.dryRunOrigin = dryRunOrigin;
9214
9441
  exports.generateAddressMultiLocationV4 = generateAddressMultiLocationV4;
9215
9442
  exports.getAssetBalance = getAssetBalance;
9216
9443
  exports.getAssetBalanceInternal = getAssetBalanceInternal;
@@ -9219,7 +9446,6 @@ exports.getBalanceForeignInternal = getBalanceForeignInternal;
9219
9446
  exports.getBalanceNative = getBalanceNative;
9220
9447
  exports.getBalanceNativeInternal = getBalanceNativeInternal;
9221
9448
  exports.getBridgeStatus = getBridgeStatus;
9222
- exports.getDryRun = getDryRun;
9223
9449
  exports.getFees = getFees;
9224
9450
  exports.getMaxForeignTransferableAmount = getMaxForeignTransferableAmount;
9225
9451
  exports.getMaxNativeTransferableAmount = getMaxNativeTransferableAmount;