@paraspell/sdk-core 11.2.0 → 11.2.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.
Files changed (4) hide show
  1. package/dist/index.cjs +1545 -1703
  2. package/dist/index.d.ts +257 -256
  3. package/dist/index.mjs +1546 -1704
  4. package/package.json +4 -4
package/dist/index.cjs CHANGED
@@ -3473,6 +3473,86 @@ var createDirectExecuteXcm = function createDirectExecuteXcm(options) {
3473
3473
  return addXcmVersionHeader(fullXcm, version);
3474
3474
  };
3475
3475
 
3476
+ var resolveFeeAsset = function resolveFeeAsset(feeAsset, origin, destination, currency) {
3477
+ var asset = assets.findAssetInfo(origin, feeAsset, !sdkCommon.isTLocation(destination) ? destination : null);
3478
+ var usesRawOverriddenMultiAssets = Array.isArray(currency) && currency.every(assets.isTAsset);
3479
+ if (!asset && !usesRawOverriddenMultiAssets) {
3480
+ throwUnsupportedCurrency(feeAsset, origin);
3481
+ }
3482
+ return asset !== null && asset !== void 0 ? asset : undefined;
3483
+ };
3484
+
3485
+ var isAssetHub = function isAssetHub(chain) {
3486
+ return chain === 'AssetHubPolkadot' || chain === 'AssetHubKusama';
3487
+ };
3488
+ var isBridgeHub = function isBridgeHub(chain) {
3489
+ return chain === 'BridgeHubPolkadot' || chain === 'BridgeHubKusama';
3490
+ };
3491
+ var isPeople = function isPeople(chain) {
3492
+ return chain === 'PeoplePolkadot' || chain === 'PeopleKusama';
3493
+ };
3494
+ var isSystemPara = function isSystemPara(chain) {
3495
+ return isAssetHub(chain) || isBridgeHub(chain) || isPeople(chain);
3496
+ };
3497
+ var mul = function mul(v, num) {
3498
+ var den = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1n;
3499
+ return v * num / den;
3500
+ };
3501
+ var padFee = function padFee(raw, origin, dest, side) {
3502
+ var relayOrigin = sdkCommon.isRelayChain(origin);
3503
+ var relayDest = sdkCommon.isRelayChain(dest);
3504
+ var sysParaOrigin = isSystemPara(origin);
3505
+ var sysParaDest = isSystemPara(dest);
3506
+ var relayToPara = relayOrigin && !relayDest;
3507
+ var sysParaToPara = sysParaOrigin && !sysParaDest;
3508
+ var paraToPara = !relayOrigin && !sysParaOrigin;
3509
+ if (sysParaToPara) return raw * 40n;
3510
+ if (relayToPara) return side === 'origin' ? mul(raw, 320n, 100n) : mul(raw, 3000n, 100n);
3511
+ if (paraToPara && side == 'origin' && origin === 'Mythos' && dest !== 'Ethereum') {
3512
+ return 150000000000000000n;
3513
+ }
3514
+ if (paraToPara) return mul(raw, 130n, 100n);
3515
+ // apply default 30% padding
3516
+ return mul(raw, 130n, 100n);
3517
+ };
3518
+ var padFeeBy = function padFeeBy(amount, percent) {
3519
+ return mul(amount, BigInt(100 + percent), 100n);
3520
+ };
3521
+
3522
+ var determineAddress = function determineAddress(chain, address, senderAddress) {
3523
+ if (assets.isChainEvm(chain)) {
3524
+ return viem.isAddress(address) ? address : senderAddress;
3525
+ }
3526
+ return viem.isAddress(address) ? senderAddress : address;
3527
+ };
3528
+ var getReverseTxFee = /*#__PURE__*/function () {
3529
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref, currencyInput) {
3530
+ var api, origin, destination, senderAddress, address, currency, toAddress, fromAddress, tx, rawFee;
3531
+ return _regenerator().w(function (_context) {
3532
+ while (1) switch (_context.n) {
3533
+ case 0:
3534
+ api = _ref.api, origin = _ref.origin, destination = _ref.destination, senderAddress = _ref.senderAddress, address = _ref.address, currency = _ref.currency;
3535
+ toAddress = determineAddress(origin, address, senderAddress);
3536
+ fromAddress = determineAddress(destination, address, senderAddress);
3537
+ _context.n = 1;
3538
+ return Builder(api).from(destination).to(origin).address(toAddress).senderAddress(fromAddress).currency(_objectSpread2(_objectSpread2({}, currencyInput), {}, {
3539
+ amount: currency.amount
3540
+ })).build();
3541
+ case 1:
3542
+ tx = _context.v;
3543
+ _context.n = 2;
3544
+ return api.calculateTransactionFee(tx, fromAddress);
3545
+ case 2:
3546
+ rawFee = _context.v;
3547
+ return _context.a(2, padFee(rawFee, origin, destination, 'destination'));
3548
+ }
3549
+ }, _callee);
3550
+ }));
3551
+ return function getReverseTxFee(_x, _x2) {
3552
+ return _ref2.apply(this, arguments);
3553
+ };
3554
+ }();
3555
+
3476
3556
  var AssetsPallet = /*#__PURE__*/function () {
3477
3557
  function AssetsPallet() {
3478
3558
  _classCallCheck(this, AssetsPallet);
@@ -4009,331 +4089,88 @@ var getOriginFeeDetails = /*#__PURE__*/function () {
4009
4089
  };
4010
4090
  }();
4011
4091
 
4012
- var resolveFeeAsset = function resolveFeeAsset(feeAsset, origin, destination, currency) {
4013
- var asset = assets.findAssetInfo(origin, feeAsset, !sdkCommon.isTLocation(destination) ? destination : null);
4014
- var usesRawOverriddenMultiAssets = Array.isArray(currency) && currency.every(assets.isTAsset);
4015
- if (!asset && !usesRawOverriddenMultiAssets) {
4016
- throwUnsupportedCurrency(feeAsset, origin);
4017
- }
4018
- return asset !== null && asset !== void 0 ? asset : undefined;
4019
- };
4020
-
4021
- var isAssetHub = function isAssetHub(chain) {
4022
- return chain === 'AssetHubPolkadot' || chain === 'AssetHubKusama';
4023
- };
4024
- var isBridgeHub = function isBridgeHub(chain) {
4025
- return chain === 'BridgeHubPolkadot' || chain === 'BridgeHubKusama';
4026
- };
4027
- var isPeople = function isPeople(chain) {
4028
- return chain === 'PeoplePolkadot' || chain === 'PeopleKusama';
4029
- };
4030
- var isSystemPara = function isSystemPara(chain) {
4031
- return isAssetHub(chain) || isBridgeHub(chain) || isPeople(chain);
4032
- };
4033
- var mul = function mul(v, num) {
4034
- var den = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1n;
4035
- return v * num / den;
4036
- };
4037
- var padFee = function padFee(raw, origin, dest, side) {
4038
- var relayOrigin = sdkCommon.isRelayChain(origin);
4039
- var relayDest = sdkCommon.isRelayChain(dest);
4040
- var sysParaOrigin = isSystemPara(origin);
4041
- var sysParaDest = isSystemPara(dest);
4042
- var relayToPara = relayOrigin && !relayDest;
4043
- var sysParaToPara = sysParaOrigin && !sysParaDest;
4044
- var paraToPara = !relayOrigin && !sysParaOrigin;
4045
- if (sysParaToPara) return raw * 40n;
4046
- if (relayToPara) return side === 'origin' ? mul(raw, 320n, 100n) : mul(raw, 3000n, 100n);
4047
- if (paraToPara && side == 'origin' && origin === 'Mythos' && dest !== 'Ethereum') {
4048
- return 150000000000000000n;
4049
- }
4050
- if (paraToPara) return mul(raw, 130n, 100n);
4051
- // apply default 30% padding
4052
- return mul(raw, 130n, 100n);
4053
- };
4054
- var padFeeBy = function padFeeBy(amount, percent) {
4055
- return mul(amount, BigInt(100 + percent), 100n);
4056
- };
4057
-
4058
- var determineAddress = function determineAddress(chain, address, senderAddress) {
4059
- if (assets.isChainEvm(chain)) {
4060
- return viem.isAddress(address) ? address : senderAddress;
4061
- }
4062
- return viem.isAddress(address) ? senderAddress : address;
4063
- };
4064
- var getReverseTxFee = /*#__PURE__*/function () {
4065
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref, currencyInput) {
4066
- var api, origin, destination, senderAddress, address, currency, toAddress, fromAddress, tx, rawFee;
4092
+ var dryRun = /*#__PURE__*/function () {
4093
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
4094
+ var api, senderAddress, origin;
4067
4095
  return _regenerator().w(function (_context) {
4068
- while (1) switch (_context.n) {
4096
+ while (1) switch (_context.p = _context.n) {
4069
4097
  case 0:
4070
- api = _ref.api, origin = _ref.origin, destination = _ref.destination, senderAddress = _ref.senderAddress, address = _ref.address, currency = _ref.currency;
4071
- toAddress = determineAddress(origin, address, senderAddress);
4072
- fromAddress = determineAddress(destination, address, senderAddress);
4098
+ api = options.api, senderAddress = options.senderAddress, origin = options.origin;
4099
+ validateAddress(senderAddress, origin, false);
4073
4100
  _context.n = 1;
4074
- return Builder(api).from(destination).to(origin).address(toAddress).senderAddress(fromAddress).currency(_objectSpread2(_objectSpread2({}, currencyInput), {}, {
4075
- amount: currency.amount
4076
- })).build();
4101
+ return api.init(origin, DRY_RUN_CLIENT_TIMEOUT_MS);
4077
4102
  case 1:
4078
- tx = _context.v;
4103
+ _context.p = 1;
4079
4104
  _context.n = 2;
4080
- return api.calculateTransactionFee(tx, fromAddress);
4105
+ return dryRunInternal(options);
4081
4106
  case 2:
4082
- rawFee = _context.v;
4083
- return _context.a(2, padFee(rawFee, origin, destination, 'destination'));
4107
+ return _context.a(2, _context.v);
4108
+ case 3:
4109
+ _context.p = 3;
4110
+ _context.n = 4;
4111
+ return api.disconnect();
4112
+ case 4:
4113
+ return _context.f(3);
4114
+ case 5:
4115
+ return _context.a(2);
4084
4116
  }
4085
- }, _callee);
4117
+ }, _callee, null, [[1,, 3, 5]]);
4086
4118
  }));
4087
- return function getReverseTxFee(_x, _x2) {
4088
- return _ref2.apply(this, arguments);
4119
+ return function dryRun(_x) {
4120
+ return _ref.apply(this, arguments);
4089
4121
  };
4090
4122
  }();
4091
4123
 
4092
- var isSufficientOrigin = /*#__PURE__*/function () {
4093
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(api, origin, destination, senderAddress, feeNative, currency, asset, feeAsset) {
4094
- var edNative, balanceNative, isNativeAssetToOrigin, isNativeAssetToDest, isSufficientNative, balanceAsset, edAsset, isSufficientAsset;
4124
+ var dryRunOrigin = /*#__PURE__*/function () {
4125
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
4126
+ var api, chain, address, result;
4095
4127
  return _regenerator().w(function (_context) {
4096
- while (1) switch (_context.n) {
4128
+ while (1) switch (_context.p = _context.n) {
4097
4129
  case 0:
4098
- if (!feeAsset) {
4099
- _context.n = 1;
4100
- break;
4101
- }
4102
- return _context.a(2, undefined);
4130
+ api = options.api, chain = options.chain, address = options.address;
4131
+ validateAddress(address, chain, false);
4132
+ _context.n = 1;
4133
+ return api.init(chain, DRY_RUN_CLIENT_TIMEOUT_MS);
4103
4134
  case 1:
4104
- edNative = assets.getExistentialDepositOrThrow(origin);
4135
+ _context.p = 1;
4105
4136
  _context.n = 2;
4106
- return getBalanceNativeInternal({
4107
- api: api,
4108
- chain: origin,
4109
- address: senderAddress
4110
- });
4137
+ return api.getDryRunCall(options);
4111
4138
  case 2:
4112
- balanceNative = _context.v;
4113
- isNativeAssetToOrigin = assets.isSymbolMatch(asset.symbol, assets.getNativeAssetSymbol(origin));
4114
- isNativeAssetToDest = assets.isSymbolMatch(asset.symbol, assets.getNativeAssetSymbol(destination));
4115
- if (!(isNativeAssetToOrigin && isNativeAssetToDest)) {
4139
+ result = _context.v;
4140
+ if (!result.success) {
4116
4141
  _context.n = 3;
4117
4142
  break;
4118
4143
  }
4119
- return _context.a(2, balanceNative - edNative - feeNative - BigInt(currency.amount) > 0n);
4144
+ return _context.a(2, _objectSpread2(_objectSpread2({}, result), {}, {
4145
+ currency: assets.getNativeAssetSymbol(chain)
4146
+ }));
4120
4147
  case 3:
4121
- if (isNativeAssetToOrigin) {
4122
- _context.n = 5;
4123
- break;
4124
- }
4125
- isSufficientNative = balanceNative - edNative - feeNative > 0n;
4126
- _context.n = 4;
4127
- return getAssetBalance({
4128
- api: api,
4129
- chain: origin,
4130
- address: senderAddress,
4131
- currency: currency
4132
- });
4148
+ return _context.a(2, result);
4133
4149
  case 4:
4134
- balanceAsset = _context.v;
4135
- edAsset = assets.getExistentialDepositOrThrow(origin, currency);
4136
- isSufficientAsset = balanceAsset - edAsset > 0n;
4137
- return _context.a(2, isSufficientNative && isSufficientAsset);
4150
+ _context.p = 4;
4151
+ _context.n = 5;
4152
+ return api.disconnect();
4138
4153
  case 5:
4139
- return _context.a(2, balanceNative - edNative - feeNative - BigInt(currency.amount) > 0n);
4154
+ return _context.f(4);
4140
4155
  case 6:
4141
4156
  return _context.a(2);
4142
4157
  }
4143
- }, _callee);
4158
+ }, _callee, null, [[1,, 4, 6]]);
4144
4159
  }));
4145
- return function isSufficientOrigin(_x, _x2, _x3, _x4, _x5, _x6, _x7, _x8) {
4160
+ return function dryRunOrigin(_x) {
4146
4161
  return _ref.apply(this, arguments);
4147
4162
  };
4148
4163
  }();
4149
- var isSufficientDestination = /*#__PURE__*/function () {
4150
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(api, destination, address, amount, asset, feeNative) {
4151
- var isNativeAsset, existentialDeposit, nativeBalance;
4152
- return _regenerator().w(function (_context2) {
4153
- while (1) switch (_context2.n) {
4164
+
4165
+ var getParaEthTransferFees = /*#__PURE__*/function () {
4166
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(ahApi) {
4167
+ var DEFAULT_FEE, feeStorageItem, leFeeHex, bytes, reversedHex, validReversedHex, leFee, transferBridgeFee, finalBridgeFee, finalAssethubExecutionFee;
4168
+ return _regenerator().w(function (_context) {
4169
+ while (1) switch (_context.n) {
4154
4170
  case 0:
4155
- isNativeAsset = assets.isSymbolMatch(asset.symbol, assets.getNativeAssetSymbol(destination));
4156
- if (isNativeAsset) {
4157
- _context2.n = 1;
4158
- break;
4159
- }
4160
- return _context2.a(2, undefined);
4161
- case 1:
4162
- existentialDeposit = assets.getExistentialDepositOrThrow(destination);
4163
- _context2.n = 2;
4164
- return getBalanceNativeInternal({
4165
- api: api,
4166
- chain: destination,
4167
- address: address
4168
- });
4169
- case 2:
4170
- nativeBalance = _context2.v;
4171
- return _context2.a(2, nativeBalance + amount - existentialDeposit - feeNative > 0n);
4172
- }
4173
- }, _callee2);
4174
- }));
4175
- return function isSufficientDestination(_x9, _x0, _x1, _x10, _x11, _x12) {
4176
- return _ref2.apply(this, arguments);
4177
- };
4178
- }();
4179
-
4180
- var createOriginLocation = function createOriginLocation(origin, destination) {
4181
- if (sdkCommon.isRelayChain(origin)) return DOT_LOCATION;
4182
- return {
4183
- parents: sdkCommon.isRelayChain(destination) ? sdkCommon.Parents.ZERO : sdkCommon.Parents.ONE,
4184
- interior: {
4185
- X1: [{
4186
- Parachain: getParaId(origin)
4187
- }]
4188
- }
4189
- };
4190
- };
4191
- var getDestXcmFee = /*#__PURE__*/function () {
4192
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
4193
- var api, origin, hopChain, destination, currency, forwardedXcms, asset, address, feeAsset, originFee, disableFallback, resolvedFeeAsset, calcPaymentInfoFee, _fee, sufficient, dryRunResult, _fee2, fee, newForwardedXcms, destParaId;
4194
- return _regenerator().w(function (_context2) {
4195
- while (1) switch (_context2.n) {
4196
- case 0:
4197
- api = options.api, origin = options.origin, hopChain = options.prevChain, destination = options.destination, currency = options.currency, forwardedXcms = options.forwardedXcms, asset = options.asset, address = options.address, feeAsset = options.feeAsset, originFee = options.originFee, disableFallback = options.disableFallback;
4198
- resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, origin, destination, currency) : undefined;
4199
- calcPaymentInfoFee = /*#__PURE__*/function () {
4200
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
4201
- var originAsset, _t;
4202
- return _regenerator().w(function (_context) {
4203
- while (1) switch (_context.p = _context.n) {
4204
- case 0:
4205
- if (!(destination === 'Ethereum')) {
4206
- _context.n = 1;
4207
- break;
4208
- }
4209
- return _context.a(2, 0n);
4210
- case 1:
4211
- originAsset = assets.findAssetInfoOrThrow(origin, currency, destination);
4212
- if (!originAsset.location) {
4213
- _context.n = 7;
4214
- break;
4215
- }
4216
- _context.p = 2;
4217
- _context.n = 3;
4218
- return getReverseTxFee(_objectSpread2(_objectSpread2({}, options), {}, {
4219
- destination: destination
4220
- }), {
4221
- location: originAsset.location
4222
- });
4223
- case 3:
4224
- return _context.a(2, _context.v);
4225
- case 4:
4226
- _context.p = 4;
4227
- _t = _context.v;
4228
- if (!(_t instanceof assets.InvalidCurrencyError)) {
4229
- _context.n = 6;
4230
- break;
4231
- }
4232
- _context.n = 5;
4233
- return getReverseTxFee(_objectSpread2(_objectSpread2({}, options), {}, {
4234
- destination: destination
4235
- }), {
4236
- symbol: originAsset.symbol
4237
- });
4238
- case 5:
4239
- return _context.a(2, _context.v);
4240
- case 6:
4241
- throw _t;
4242
- case 7:
4243
- _context.n = 8;
4244
- return getReverseTxFee(_objectSpread2(_objectSpread2({}, options), {}, {
4245
- destination: destination
4246
- }), {
4247
- symbol: originAsset.symbol
4248
- });
4249
- case 8:
4250
- return _context.a(2, _context.v);
4251
- }
4252
- }, _callee, null, [[2, 4]]);
4253
- }));
4254
- return function calcPaymentInfoFee() {
4255
- return _ref2.apply(this, arguments);
4256
- };
4257
- }();
4258
- if (!(!assets.hasDryRunSupport(destination) || !forwardedXcms || destination === 'Ethereum')) {
4259
- _context2.n = 3;
4260
- break;
4261
- }
4262
- _context2.n = 1;
4263
- return calcPaymentInfoFee();
4264
- case 1:
4265
- _fee = _context2.v;
4266
- _context2.n = 2;
4267
- return isSufficientDestination(api, destination, address, BigInt(currency.amount), asset, _fee);
4268
- case 2:
4269
- sufficient = _context2.v;
4270
- return _context2.a(2, {
4271
- fee: _fee,
4272
- feeType: 'paymentInfo',
4273
- sufficient: !assets.hasDryRunSupport(destination) ? sufficient : false
4274
- });
4275
- case 3:
4276
- _context2.n = 4;
4277
- return api.getDryRunXcm({
4278
- originLocation: addXcmVersionHeader(createOriginLocation(hopChain, destination), sdkCommon.Version.V4),
4279
- xcm: forwardedXcms[1][0],
4280
- chain: destination,
4281
- origin: origin,
4282
- asset: asset,
4283
- originFee: originFee,
4284
- feeAsset: resolvedFeeAsset,
4285
- amount: BigInt(currency.amount) < 2n ? 2n : BigInt(currency.amount)
4286
- });
4287
- case 4:
4288
- dryRunResult = _context2.v;
4289
- if (dryRunResult.success) {
4290
- _context2.n = 7;
4291
- break;
4292
- }
4293
- if (!disableFallback) {
4294
- _context2.n = 5;
4295
- break;
4296
- }
4297
- return _context2.a(2, {
4298
- dryRunError: dryRunResult.failureReason
4299
- });
4300
- case 5:
4301
- _context2.n = 6;
4302
- return calcPaymentInfoFee();
4303
- case 6:
4304
- _fee2 = _context2.v;
4305
- return _context2.a(2, {
4306
- fee: _fee2,
4307
- feeType: 'paymentInfo',
4308
- dryRunError: dryRunResult.failureReason,
4309
- sufficient: false
4310
- });
4311
- case 7:
4312
- fee = dryRunResult.fee, newForwardedXcms = dryRunResult.forwardedXcms, destParaId = dryRunResult.destParaId;
4313
- return _context2.a(2, {
4314
- fee: fee,
4315
- feeType: 'dryRun',
4316
- sufficient: true,
4317
- forwardedXcms: newForwardedXcms,
4318
- destParaId: destParaId
4319
- });
4320
- }
4321
- }, _callee2);
4322
- }));
4323
- return function getDestXcmFee(_x) {
4324
- return _ref.apply(this, arguments);
4325
- };
4326
- }();
4327
-
4328
- var getParaEthTransferFees = /*#__PURE__*/function () {
4329
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(ahApi) {
4330
- var DEFAULT_FEE, feeStorageItem, leFeeHex, bytes, reversedHex, validReversedHex, leFee, transferBridgeFee, finalBridgeFee, finalAssethubExecutionFee;
4331
- return _regenerator().w(function (_context) {
4332
- while (1) switch (_context.n) {
4333
- case 0:
4334
- DEFAULT_FEE = 2750872500000n;
4335
- _context.n = 1;
4336
- return ahApi.getFromRpc('state', 'getStorage', '0x5fbc5c7ba58845ad1f1a9a7c5bc12fad');
4171
+ DEFAULT_FEE = 2750872500000n;
4172
+ _context.n = 1;
4173
+ return ahApi.getFromRpc('state', 'getStorage', '0x5fbc5c7ba58845ad1f1a9a7c5bc12fad');
4337
4174
  case 1:
4338
4175
  feeStorageItem = _context.v;
4339
4176
  leFeeHex = feeStorageItem.replace('0x', '');
@@ -5055,340 +4892,63 @@ var addEthereumBridgeFees = /*#__PURE__*/function () {
5055
4892
  };
5056
4893
  }();
5057
4894
 
5058
- var getFailureInfo$1 = function getFailureInfo(results, hops) {
5059
- // Check standard chains first for backwards compatibility
5060
- for (var _i = 0, _arr = ['destination', 'assetHub', 'bridgeHub']; _i < _arr.length; _i++) {
5061
- var chain = _arr[_i];
5062
- var res = results[chain];
5063
- if (res && !res.success && res.failureReason) {
5064
- return {
5065
- failureReason: res.failureReason,
5066
- failureChain: chain
5067
- };
4895
+ var AssetManagerPallet = /*#__PURE__*/function () {
4896
+ function AssetManagerPallet() {
4897
+ _classCallCheck(this, AssetManagerPallet);
4898
+ }
4899
+ return _createClass(AssetManagerPallet, [{
4900
+ key: "setBalance",
4901
+ value: function setBalance(address, asset) {
4902
+ assertHasId(asset);
4903
+ var assetId = asset.assetId,
4904
+ amount = asset.amount;
4905
+ return Promise.resolve({
4906
+ balanceTx: {
4907
+ module: 'AssetManager',
4908
+ method: 'updateBalance',
4909
+ parameters: {
4910
+ who: {
4911
+ Id: address
4912
+ },
4913
+ currency_id: {
4914
+ ForeignAsset: assetId
4915
+ },
4916
+ amount: amount
4917
+ }
4918
+ }
4919
+ });
5068
4920
  }
4921
+ }]);
4922
+ }();
4923
+
4924
+ var BalancesPallet = /*#__PURE__*/function () {
4925
+ function BalancesPallet() {
4926
+ _classCallCheck(this, BalancesPallet);
5069
4927
  }
5070
- var _iterator = _createForOfIteratorHelper(hops),
5071
- _step;
5072
- try {
5073
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
5074
- var hop = _step.value;
5075
- if (!hop.result.success && hop.result.failureReason) {
5076
- return {
5077
- failureReason: hop.result.failureReason,
5078
- failureChain: hop.chain
5079
- };
5080
- }
4928
+ return _createClass(BalancesPallet, [{
4929
+ key: "setBalance",
4930
+ value: function setBalance(address, assetInfo, chain) {
4931
+ var amount = assetInfo.amount;
4932
+ var notUseId = chain.startsWith('Hydration') || chain === 'Basilisk' || assets.isChainEvm(chain);
4933
+ return Promise.resolve({
4934
+ balanceTx: {
4935
+ module: 'Balances',
4936
+ method: 'force_set_balance',
4937
+ parameters: {
4938
+ who: notUseId ? address : {
4939
+ Id: address
4940
+ },
4941
+ new_free: amount
4942
+ }
4943
+ }
4944
+ });
5081
4945
  }
5082
- } catch (err) {
5083
- _iterator.e(err);
5084
- } finally {
5085
- _iterator.f();
5086
- }
5087
- return {};
5088
- };
5089
- var dryRunInternal = /*#__PURE__*/function () {
5090
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
5091
- var _traversalResult$brid, _traversalResult$asse, _traversalResult$dest;
5092
- var api, origin, destination, currency, tx, senderAddress, feeAsset, swapConfig, resolvedFeeAsset, asset, amount, originDryRun, initialForwardedXcms, initialDestParaId, processHop, traversalResult, assetHubChain, bridgeHubChain, processedBridgeHub, bridgeHubHopIndex, originWithCurrency, assetHubWithCurrency, bridgeHubWithCurrency, destinationWithCurrency, _getFailureInfo, failureReason, failureChain, _t;
5093
- return _regenerator().w(function (_context2) {
5094
- while (1) switch (_context2.n) {
5095
- case 0:
5096
- api = options.api, origin = options.origin, destination = options.destination, currency = options.currency, tx = options.tx, senderAddress = options.senderAddress, feeAsset = options.feeAsset, swapConfig = options.swapConfig;
5097
- resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, origin, destination, currency) : undefined;
5098
- asset = assets.findAssetInfoOrThrow(origin, currency, destination);
5099
- amount = abstractDecimals(currency.amount, asset.decimals, api);
5100
- _context2.n = 1;
5101
- return api.getDryRunCall({
5102
- tx: tx,
5103
- chain: origin,
5104
- address: senderAddress,
5105
- asset: _objectSpread2(_objectSpread2({}, asset), {}, {
5106
- amount: amount
5107
- }),
5108
- feeAsset: resolvedFeeAsset
5109
- });
5110
- case 1:
5111
- originDryRun = _context2.v;
5112
- if (originDryRun.success) {
5113
- _context2.n = 2;
5114
- break;
5115
- }
5116
- return _context2.a(2, {
5117
- failureReason: originDryRun.failureReason,
5118
- failureChain: 'origin',
5119
- origin: originDryRun,
5120
- hops: []
5121
- });
5122
- case 2:
5123
- initialForwardedXcms = originDryRun.forwardedXcms, initialDestParaId = originDryRun.destParaId;
5124
- processHop = /*#__PURE__*/function () {
5125
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(params) {
5126
- var hopApi, currentChain, currentOrigin, currentAsset, forwardedXcms, hasPassedExchange, isDestination, hopDryRun, hopCurrency;
5127
- return _regenerator().w(function (_context) {
5128
- while (1) switch (_context.n) {
5129
- case 0:
5130
- hopApi = params.api, currentChain = params.currentChain, currentOrigin = params.currentOrigin, currentAsset = params.currentAsset, forwardedXcms = params.forwardedXcms, hasPassedExchange = params.hasPassedExchange, isDestination = params.isDestination;
5131
- if (assets.hasDryRunSupport(currentChain)) {
5132
- _context.n = 1;
5133
- break;
5134
- }
5135
- return _context.a(2, {
5136
- success: false,
5137
- failureReason: "DryRunApi is not available on chain ".concat(currentChain)
5138
- });
5139
- case 1:
5140
- _context.n = 2;
5141
- return hopApi.getDryRunXcm({
5142
- originLocation: addXcmVersionHeader(createOriginLocation(currentOrigin, currentChain), sdkCommon.Version.V4),
5143
- xcm: forwardedXcms[1][0],
5144
- chain: currentChain,
5145
- origin: currentOrigin,
5146
- asset: currentAsset,
5147
- feeAsset: resolvedFeeAsset,
5148
- originFee: originDryRun.fee,
5149
- amount: amount
5150
- });
5151
- case 2:
5152
- hopDryRun = _context.v;
5153
- if (!hopDryRun.success) {
5154
- _context.n = 3;
5155
- break;
5156
- }
5157
- if (destination === 'Ethereum' && (currentChain.includes('AssetHub') || currentChain.includes('BridgeHub'))) {
5158
- hopCurrency = assets.getNativeAssetSymbol(currentChain);
5159
- } else if (hasPassedExchange && swapConfig && currentChain !== swapConfig.exchangeChain) {
5160
- hopCurrency = assets.findAssetOnDestOrThrow(swapConfig.exchangeChain, currentChain, swapConfig.currencyTo).symbol;
5161
- } else if (isDestination) {
5162
- hopCurrency = assets.findAssetOnDestOrThrow(origin, currentChain, currency).symbol;
5163
- } else {
5164
- hopCurrency = asset.symbol;
5165
- }
5166
- return _context.a(2, _objectSpread2(_objectSpread2({}, hopDryRun), {}, {
5167
- currency: hopCurrency
5168
- }));
5169
- case 3:
5170
- return _context.a(2, hopDryRun);
5171
- }
5172
- }, _callee);
5173
- }));
5174
- return function processHop(_x2) {
5175
- return _ref2.apply(this, arguments);
5176
- };
5177
- }();
5178
- _context2.n = 3;
5179
- return traverseXcmHops({
5180
- api: api,
5181
- origin: origin,
5182
- destination: destination,
5183
- currency: currency,
5184
- initialForwardedXcms: initialForwardedXcms,
5185
- initialDestParaId: initialDestParaId,
5186
- swapConfig: swapConfig,
5187
- processHop: processHop,
5188
- shouldContinue: function shouldContinue(hopResult) {
5189
- return hopResult.success;
5190
- },
5191
- extractNextHopData: function extractNextHopData(hopResult) {
5192
- return {
5193
- forwardedXcms: hopResult.success ? hopResult.forwardedXcms : undefined,
5194
- destParaId: hopResult.success ? hopResult.destParaId : undefined
5195
- };
5196
- }
5197
- });
5198
- case 3:
5199
- traversalResult = _context2.v;
5200
- // Process Ethereum bridge fees
5201
- assetHubChain = "AssetHub".concat(getRelayChainOf(origin));
5202
- bridgeHubChain = "BridgeHub".concat(getRelayChainOf(origin));
5203
- if (!((_traversalResult$brid = traversalResult.bridgeHub) !== null && _traversalResult$brid !== void 0 && _traversalResult$brid.success)) {
5204
- _context2.n = 5;
5205
- break;
5206
- }
5207
- _context2.n = 4;
5208
- return addEthereumBridgeFees(api, traversalResult.bridgeHub, destination, assetHubChain);
5209
- case 4:
5210
- _t = _context2.v;
5211
- _context2.n = 6;
5212
- break;
5213
- case 5:
5214
- _t = traversalResult.bridgeHub;
5215
- case 6:
5216
- processedBridgeHub = _t;
5217
- // Update bridge hub in hops if needed
5218
- if (processedBridgeHub && processedBridgeHub.success && traversalResult.bridgeHub && traversalResult.bridgeHub.success && processedBridgeHub.fee !== traversalResult.bridgeHub.fee) {
5219
- bridgeHubHopIndex = traversalResult.hops.findIndex(function (hop) {
5220
- return hop.chain === bridgeHubChain;
5221
- });
5222
- if (bridgeHubHopIndex !== -1 && traversalResult.hops[bridgeHubHopIndex].result.success) {
5223
- traversalResult.hops[bridgeHubHopIndex].result = _objectSpread2(_objectSpread2({}, traversalResult.hops[bridgeHubHopIndex].result), {}, {
5224
- fee: processedBridgeHub.fee
5225
- });
5226
- }
5227
- }
5228
- originWithCurrency = originDryRun.success ? _objectSpread2(_objectSpread2({}, originDryRun), {}, {
5229
- currency: resolvedFeeAsset ? resolvedFeeAsset.symbol : assets.getNativeAssetSymbol(origin)
5230
- }) : originDryRun;
5231
- assetHubWithCurrency = (_traversalResult$asse = traversalResult.assetHub) !== null && _traversalResult$asse !== void 0 && _traversalResult$asse.success ? _objectSpread2(_objectSpread2({}, traversalResult.assetHub), {}, {
5232
- currency: resolvedFeeAsset ? resolvedFeeAsset.symbol : assets.getNativeAssetSymbol(assetHubChain)
5233
- }) : traversalResult.assetHub;
5234
- bridgeHubWithCurrency = processedBridgeHub !== null && processedBridgeHub !== void 0 && processedBridgeHub.success ? _objectSpread2(_objectSpread2({}, processedBridgeHub), {}, {
5235
- currency: assets.getNativeAssetSymbol(bridgeHubChain)
5236
- }) : processedBridgeHub;
5237
- destinationWithCurrency = (_traversalResult$dest = traversalResult.destination) !== null && _traversalResult$dest !== void 0 && _traversalResult$dest.success ? _objectSpread2(_objectSpread2({}, traversalResult.destination), {}, {
5238
- currency: asset === null || asset === void 0 ? void 0 : asset.symbol
5239
- }) : traversalResult.destination;
5240
- _getFailureInfo = getFailureInfo$1({
5241
- destination: destinationWithCurrency,
5242
- assetHub: assetHubWithCurrency,
5243
- bridgeHub: bridgeHubWithCurrency
5244
- }, traversalResult.hops), failureReason = _getFailureInfo.failureReason, failureChain = _getFailureInfo.failureChain;
5245
- return _context2.a(2, {
5246
- failureReason: failureReason,
5247
- failureChain: failureChain,
5248
- origin: originWithCurrency,
5249
- assetHub: assetHubWithCurrency,
5250
- bridgeHub: bridgeHubWithCurrency,
5251
- destination: destinationWithCurrency,
5252
- hops: traversalResult.hops
5253
- });
5254
- }
5255
- }, _callee2);
5256
- }));
5257
- return function dryRunInternal(_x) {
5258
- return _ref.apply(this, arguments);
5259
- };
5260
- }();
5261
-
5262
- var dryRun = /*#__PURE__*/function () {
5263
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
5264
- var api, senderAddress, origin;
5265
- return _regenerator().w(function (_context) {
5266
- while (1) switch (_context.p = _context.n) {
5267
- case 0:
5268
- api = options.api, senderAddress = options.senderAddress, origin = options.origin;
5269
- validateAddress(senderAddress, origin, false);
5270
- _context.n = 1;
5271
- return api.init(origin, DRY_RUN_CLIENT_TIMEOUT_MS);
5272
- case 1:
5273
- _context.p = 1;
5274
- _context.n = 2;
5275
- return dryRunInternal(options);
5276
- case 2:
5277
- return _context.a(2, _context.v);
5278
- case 3:
5279
- _context.p = 3;
5280
- _context.n = 4;
5281
- return api.disconnect();
5282
- case 4:
5283
- return _context.f(3);
5284
- case 5:
5285
- return _context.a(2);
5286
- }
5287
- }, _callee, null, [[1,, 3, 5]]);
5288
- }));
5289
- return function dryRun(_x) {
5290
- return _ref.apply(this, arguments);
5291
- };
5292
- }();
5293
-
5294
- var dryRunOrigin = /*#__PURE__*/function () {
5295
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
5296
- var api, chain, address, result;
5297
- return _regenerator().w(function (_context) {
5298
- while (1) switch (_context.p = _context.n) {
5299
- case 0:
5300
- api = options.api, chain = options.chain, address = options.address;
5301
- validateAddress(address, chain, false);
5302
- _context.n = 1;
5303
- return api.init(chain, DRY_RUN_CLIENT_TIMEOUT_MS);
5304
- case 1:
5305
- _context.p = 1;
5306
- _context.n = 2;
5307
- return api.getDryRunCall(options);
5308
- case 2:
5309
- result = _context.v;
5310
- if (!result.success) {
5311
- _context.n = 3;
5312
- break;
5313
- }
5314
- return _context.a(2, _objectSpread2(_objectSpread2({}, result), {}, {
5315
- currency: assets.getNativeAssetSymbol(chain)
5316
- }));
5317
- case 3:
5318
- return _context.a(2, result);
5319
- case 4:
5320
- _context.p = 4;
5321
- _context.n = 5;
5322
- return api.disconnect();
5323
- case 5:
5324
- return _context.f(4);
5325
- case 6:
5326
- return _context.a(2);
5327
- }
5328
- }, _callee, null, [[1,, 4, 6]]);
5329
- }));
5330
- return function dryRunOrigin(_x) {
5331
- return _ref.apply(this, arguments);
5332
- };
5333
- }();
5334
-
5335
- var AssetManagerPallet = /*#__PURE__*/function () {
5336
- function AssetManagerPallet() {
5337
- _classCallCheck(this, AssetManagerPallet);
5338
- }
5339
- return _createClass(AssetManagerPallet, [{
5340
- key: "setBalance",
5341
- value: function setBalance(address, asset) {
5342
- assertHasId(asset);
5343
- var assetId = asset.assetId,
5344
- amount = asset.amount;
5345
- return Promise.resolve({
5346
- balanceTx: {
5347
- module: 'AssetManager',
5348
- method: 'updateBalance',
5349
- parameters: {
5350
- who: {
5351
- Id: address
5352
- },
5353
- currency_id: {
5354
- ForeignAsset: assetId
5355
- },
5356
- amount: amount
5357
- }
5358
- }
5359
- });
5360
- }
5361
- }]);
5362
- }();
5363
-
5364
- var BalancesPallet = /*#__PURE__*/function () {
5365
- function BalancesPallet() {
5366
- _classCallCheck(this, BalancesPallet);
5367
- }
5368
- return _createClass(BalancesPallet, [{
5369
- key: "setBalance",
5370
- value: function setBalance(address, assetInfo, chain) {
5371
- var amount = assetInfo.amount;
5372
- var notUseId = chain.startsWith('Hydration') || chain === 'Basilisk' || assets.isChainEvm(chain);
5373
- return Promise.resolve({
5374
- balanceTx: {
5375
- module: 'Balances',
5376
- method: 'force_set_balance',
5377
- parameters: {
5378
- who: notUseId ? address : {
5379
- Id: address
5380
- },
5381
- new_free: amount
5382
- }
5383
- }
5384
- });
5385
- }
5386
- }]);
5387
- }();
5388
-
5389
- var CurrenciesPallet = /*#__PURE__*/function () {
5390
- function CurrenciesPallet() {
5391
- _classCallCheck(this, CurrenciesPallet);
4946
+ }]);
4947
+ }();
4948
+
4949
+ var CurrenciesPallet = /*#__PURE__*/function () {
4950
+ function CurrenciesPallet() {
4951
+ _classCallCheck(this, CurrenciesPallet);
5392
4952
  }
5393
4953
  return _createClass(CurrenciesPallet, [{
5394
4954
  key: "setBalance",
@@ -5976,7 +5536,7 @@ var getOriginXcmFeeEstimate = /*#__PURE__*/function () {
5976
5536
  };
5977
5537
  }();
5978
5538
 
5979
- var getFailureInfo = function getFailureInfo(chains, hops) {
5539
+ var getFailureInfo$1 = function getFailureInfo(chains, hops) {
5980
5540
  var _chains$origin, _chains$assetHub, _chains$bridgeHub, _chains$destination;
5981
5541
  // Check standard chains first for backwards compatibility
5982
5542
  if ((_chains$origin = chains.origin) !== null && _chains$origin !== void 0 && _chains$origin.dryRunError) return {
@@ -6103,7 +5663,7 @@ var getXcmFeeInternal = /*#__PURE__*/function () {
6103
5663
  }),
6104
5664
  hops: []
6105
5665
  };
6106
- _getFailureInfo = getFailureInfo({
5666
+ _getFailureInfo = getFailureInfo$1({
6107
5667
  origin: _result.origin,
6108
5668
  destination: _result.destination
6109
5669
  }, []), _failureChain = _getFailureInfo.failureChain, _failureReason = _getFailureInfo.failureReason;
@@ -6315,7 +5875,7 @@ var getXcmFeeInternal = /*#__PURE__*/function () {
6315
5875
  };
6316
5876
  })
6317
5877
  });
6318
- _getFailureInfo2 = getFailureInfo({
5878
+ _getFailureInfo2 = getFailureInfo$1({
6319
5879
  origin: result.origin,
6320
5880
  assetHub: result.assetHub,
6321
5881
  bridgeHub: result.bridgeHub,
@@ -6512,1162 +6072,1526 @@ var transferRelayToPara = /*#__PURE__*/function () {
6512
6072
  };
6513
6073
  }();
6514
6074
 
6515
- var send = /*#__PURE__*/function () {
6516
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
6517
- var api, origin, currency, feeAsset, address, destination, paraIdTo, version, senderAddress, ahAddress, pallet, method, isBridge, assetCheckEnabled, asset, resolvedFeeAsset, amount, finalAmount, originVersion, destVersion, resolvedVersion, isLocalTransfer, overriddenAsset, resolvedAsset, finalAsset, finalVersion, normalizedAsset;
6075
+ var send = /*#__PURE__*/function () {
6076
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
6077
+ var api, origin, currency, feeAsset, address, destination, paraIdTo, version, senderAddress, ahAddress, pallet, method, isBridge, assetCheckEnabled, asset, resolvedFeeAsset, amount, finalAmount, originVersion, destVersion, resolvedVersion, isLocalTransfer, overriddenAsset, resolvedAsset, finalAsset, finalVersion, normalizedAsset;
6078
+ return _regenerator().w(function (_context) {
6079
+ while (1) switch (_context.n) {
6080
+ case 0:
6081
+ api = options.api, origin = options.from, currency = options.currency, feeAsset = options.feeAsset, address = options.address, destination = options.to, paraIdTo = options.paraIdTo, version = options.version, senderAddress = options.senderAddress, ahAddress = options.ahAddress, pallet = options.pallet, method = options.method;
6082
+ validateCurrency(currency, feeAsset);
6083
+ validateDestination(origin, destination);
6084
+ validateDestinationAddress(address, destination);
6085
+ if (senderAddress) validateAddress(senderAddress, origin, false);
6086
+ isBridge = !sdkCommon.isTLocation(destination) && sdkCommon.isDotKsmBridge(origin, destination);
6087
+ assetCheckEnabled = shouldPerformAssetCheck(origin, currency);
6088
+ validateAssetSpecifiers(assetCheckEnabled, currency);
6089
+ asset = resolveAsset(currency, origin, destination, assetCheckEnabled);
6090
+ resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, origin, destination, currency) : undefined;
6091
+ validateAssetSupport(options, assetCheckEnabled, isBridge, asset);
6092
+ amount = Array.isArray(currency) ? 0n : abstractDecimals(currency.amount, asset === null || asset === void 0 ? void 0 : asset.decimals, api); // Ensure amount is at least 2 to avoid Rust panic (only for non-array currencies)
6093
+ finalAmount = !Array.isArray(currency) && amount < 2n ? 2n : amount;
6094
+ originVersion = getChainVersion(origin);
6095
+ destVersion = !sdkCommon.isTLocation(destination) ? getChainVersion(destination) : undefined;
6096
+ resolvedVersion = selectXcmVersion(version, originVersion, destVersion);
6097
+ if (!sdkCommon.isRelayChain(origin)) {
6098
+ _context.n = 6;
6099
+ break;
6100
+ }
6101
+ if (!(destination === 'Ethereum')) {
6102
+ _context.n = 1;
6103
+ break;
6104
+ }
6105
+ throw new InvalidParameterError('Transfers from relay chain to Ethereum are not supported.');
6106
+ case 1:
6107
+ if (asset) {
6108
+ _context.n = 2;
6109
+ break;
6110
+ }
6111
+ throw new InvalidParameterError('Asset is required for relay chain to relay chain transfers.');
6112
+ case 2:
6113
+ isLocalTransfer = origin === destination;
6114
+ if (!isLocalTransfer) {
6115
+ _context.n = 5;
6116
+ break;
6117
+ }
6118
+ if (!sdkCommon.isTLocation(address)) {
6119
+ _context.n = 3;
6120
+ break;
6121
+ }
6122
+ throw new InvalidAddressError('Location address is not supported for local transfers.');
6123
+ case 3:
6124
+ _context.n = 4;
6125
+ return api.init(origin, TX_CLIENT_TIMEOUT_MS);
6126
+ case 4:
6127
+ return _context.a(2, api.callTxMethod({
6128
+ module: 'Balances',
6129
+ method: 'transfer_keep_alive',
6130
+ parameters: {
6131
+ dest: {
6132
+ Id: address
6133
+ },
6134
+ value: finalAmount
6135
+ }
6136
+ }));
6137
+ case 5:
6138
+ return _context.a(2, transferRelayToPara({
6139
+ api: api,
6140
+ origin: origin,
6141
+ destination: destination,
6142
+ address: address,
6143
+ assetInfo: _objectSpread2(_objectSpread2({}, asset), {}, {
6144
+ amount: finalAmount
6145
+ }),
6146
+ paraIdTo: paraIdTo,
6147
+ version: resolvedVersion,
6148
+ pallet: pallet,
6149
+ method: method
6150
+ }));
6151
+ case 6:
6152
+ overriddenAsset = resolveOverriddenAsset(options, isBridge, assetCheckEnabled, resolvedFeeAsset);
6153
+ _context.n = 7;
6154
+ return api.init(origin, TX_CLIENT_TIMEOUT_MS);
6155
+ case 7:
6156
+ // In case asset check is disabled, we create asset object from currency symbol
6157
+ resolvedAsset = asset !== null && asset !== void 0 ? asset : {
6158
+ symbol: 'symbol' in currency ? currency.symbol : undefined
6159
+ };
6160
+ finalAsset = Array.isArray(currency) ? _objectSpread2(_objectSpread2({}, resolvedAsset), {}, {
6161
+ amount: 0n,
6162
+ assetId: '1'
6163
+ }) : _objectSpread2(_objectSpread2({}, resolvedAsset), {}, {
6164
+ amount: finalAmount
6165
+ });
6166
+ finalVersion = selectXcmVersion(version, originVersion, destVersion);
6167
+ normalizedAsset = finalAsset.location ? _objectSpread2(_objectSpread2({}, finalAsset), {}, {
6168
+ location: assets.normalizeLocation(finalAsset.location, finalVersion)
6169
+ }) : finalAsset;
6170
+ return _context.a(2, getChain(origin).transfer({
6171
+ api: api,
6172
+ assetInfo: normalizedAsset,
6173
+ currency: currency,
6174
+ feeAsset: resolvedFeeAsset,
6175
+ feeCurrency: feeAsset,
6176
+ address: address,
6177
+ to: destination,
6178
+ paraIdTo: paraIdTo,
6179
+ overriddenAsset: overriddenAsset,
6180
+ version: finalVersion,
6181
+ senderAddress: senderAddress,
6182
+ ahAddress: ahAddress,
6183
+ pallet: pallet,
6184
+ method: method
6185
+ }));
6186
+ }
6187
+ }, _callee);
6188
+ }));
6189
+ return function send(_x) {
6190
+ return _ref.apply(this, arguments);
6191
+ };
6192
+ }();
6193
+
6194
+ var buildTypeAndThenCall = function buildTypeAndThenCall(_ref, isDotAsset, customXcm, assets) {
6195
+ var origin = _ref.origin,
6196
+ reserve = _ref.reserve,
6197
+ dest = _ref.dest,
6198
+ assetInfo = _ref.assetInfo,
6199
+ version = _ref.options.version;
6200
+ var feeAssetLocation = !isDotAsset ? RELAY_LOCATION : assetInfo.location;
6201
+ var finalDest = origin.chain === reserve.chain ? dest.chain : reserve.chain;
6202
+ var destLocation = createDestination(version, origin.chain, finalDest, getParaId(finalDest));
6203
+ var reserveType = origin.chain === reserve.chain ? 'LocalReserve' : 'DestinationReserve';
6204
+ var feeMultiAsset = createAsset(version, assetInfo.amount, feeAssetLocation);
6205
+ return {
6206
+ module: 'PolkadotXcm',
6207
+ method: 'transfer_assets_using_type_and_then',
6208
+ parameters: {
6209
+ dest: addXcmVersionHeader(destLocation, version),
6210
+ assets: addXcmVersionHeader(assets, version),
6211
+ assets_transfer_type: reserveType,
6212
+ remote_fees_id: addXcmVersionHeader(feeMultiAsset.id, version),
6213
+ fees_transfer_type: reserveType,
6214
+ custom_xcm_on_dest: addXcmVersionHeader(customXcm, version),
6215
+ weight_limit: 'Unlimited'
6216
+ }
6217
+ };
6218
+ };
6219
+
6220
+ var FEE_PADDING_PERCENTAGE$2 = 20;
6221
+ var FEE_PADDING_HYDRATION = 150;
6222
+ var computeInstructionFee = /*#__PURE__*/function () {
6223
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref, version, xcm) {
6224
+ var chain, api, _t, _t2, _t3;
6225
+ return _regenerator().w(function (_context) {
6226
+ while (1) switch (_context.n) {
6227
+ case 0:
6228
+ chain = _ref.chain, api = _ref.api;
6229
+ _t = padFeeBy;
6230
+ _context.n = 1;
6231
+ return api.getXcmPaymentApiFee(chain, addXcmVersionHeader(xcm, version), {
6232
+ location: DOT_LOCATION
6233
+ }, true);
6234
+ case 1:
6235
+ _t2 = _context.v;
6236
+ _t3 = chain === 'Hydration' ? FEE_PADDING_HYDRATION : FEE_PADDING_PERCENTAGE$2;
6237
+ return _context.a(2, _t(_t2, _t3));
6238
+ }
6239
+ }, _callee);
6240
+ }));
6241
+ return function computeInstructionFee(_x, _x2, _x3) {
6242
+ return _ref2.apply(this, arguments);
6243
+ };
6244
+ }();
6245
+ var computeAllFees = /*#__PURE__*/function () {
6246
+ var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(_ref3, customXcm, isDotAsset, refundInstruction) {
6247
+ var reserve, dest, version, _t4, _t5, _t6, _t7, _t8, _t9, _t0, _t1, _t10;
6248
+ return _regenerator().w(function (_context2) {
6249
+ while (1) switch (_context2.n) {
6250
+ case 0:
6251
+ reserve = _ref3.reserve, dest = _ref3.dest, version = _ref3.options.version;
6252
+ if (!('DepositReserveAsset' in customXcm)) {
6253
+ _context2.n = 6;
6254
+ break;
6255
+ }
6256
+ _context2.n = 1;
6257
+ return computeInstructionFee(reserve, version, [customXcm]);
6258
+ case 1:
6259
+ _t5 = _context2.v;
6260
+ if (!refundInstruction) {
6261
+ _context2.n = 3;
6262
+ break;
6263
+ }
6264
+ _context2.n = 2;
6265
+ return computeInstructionFee(reserve, version, [refundInstruction]);
6266
+ case 2:
6267
+ _t6 = _context2.v;
6268
+ _context2.n = 4;
6269
+ break;
6270
+ case 3:
6271
+ _t6 = 0n;
6272
+ case 4:
6273
+ _t7 = _t6;
6274
+ _context2.n = 5;
6275
+ return computeInstructionFee(assets.hasXcmPaymentApiSupport(dest.chain) ? dest : reserve, version, customXcm.DepositReserveAsset.xcm);
6276
+ case 5:
6277
+ _t8 = _context2.v;
6278
+ _t4 = {
6279
+ reserveFee: _t5,
6280
+ refundFee: _t7,
6281
+ destFee: _t8
6282
+ };
6283
+ _context2.n = 13;
6284
+ break;
6285
+ case 6:
6286
+ if (isDotAsset) {
6287
+ _context2.n = 8;
6288
+ break;
6289
+ }
6290
+ _context2.n = 7;
6291
+ return computeInstructionFee(assets.hasXcmPaymentApiSupport(dest.chain) ? dest : reserve, version, [customXcm]);
6292
+ case 7:
6293
+ _t9 = _context2.v;
6294
+ _context2.n = 9;
6295
+ break;
6296
+ case 8:
6297
+ _t9 = 0n;
6298
+ case 9:
6299
+ _t0 = _t9;
6300
+ if (isDotAsset) {
6301
+ _context2.n = 11;
6302
+ break;
6303
+ }
6304
+ _context2.n = 10;
6305
+ return computeInstructionFee(assets.hasXcmPaymentApiSupport(reserve.chain) ? reserve : dest, version, [refundInstruction]);
6306
+ case 10:
6307
+ _t1 = _context2.v;
6308
+ _context2.n = 12;
6309
+ break;
6310
+ case 11:
6311
+ _t1 = 0n;
6312
+ case 12:
6313
+ _t10 = _t1;
6314
+ _t4 = {
6315
+ reserveFee: 0n,
6316
+ destFee: _t0,
6317
+ refundFee: _t10
6318
+ };
6319
+ case 13:
6320
+ return _context2.a(2, _t4);
6321
+ }
6322
+ }, _callee2);
6323
+ }));
6324
+ return function computeAllFees(_x4, _x5, _x6, _x7) {
6325
+ return _ref4.apply(this, arguments);
6326
+ };
6327
+ }();
6328
+
6329
+ var createTypeAndThenCallContext = /*#__PURE__*/function () {
6330
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(chain, options) {
6331
+ var api, paraIdTo, assetInfo, destChain, reserveChain, destApi, reserveApi;
6518
6332
  return _regenerator().w(function (_context) {
6519
6333
  while (1) switch (_context.n) {
6520
6334
  case 0:
6521
- api = options.api, origin = options.from, currency = options.currency, feeAsset = options.feeAsset, address = options.address, destination = options.to, paraIdTo = options.paraIdTo, version = options.version, senderAddress = options.senderAddress, ahAddress = options.ahAddress, pallet = options.pallet, method = options.method;
6522
- validateCurrency(currency, feeAsset);
6523
- validateDestination(origin, destination);
6524
- validateDestinationAddress(address, destination);
6525
- if (senderAddress) validateAddress(senderAddress, origin, false);
6526
- isBridge = !sdkCommon.isTLocation(destination) && sdkCommon.isDotKsmBridge(origin, destination);
6527
- assetCheckEnabled = shouldPerformAssetCheck(origin, currency);
6528
- validateAssetSpecifiers(assetCheckEnabled, currency);
6529
- asset = resolveAsset(currency, origin, destination, assetCheckEnabled);
6530
- resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, origin, destination, currency) : undefined;
6531
- validateAssetSupport(options, assetCheckEnabled, isBridge, asset);
6532
- amount = Array.isArray(currency) ? 0n : abstractDecimals(currency.amount, asset === null || asset === void 0 ? void 0 : asset.decimals, api); // Ensure amount is at least 2 to avoid Rust panic (only for non-array currencies)
6533
- finalAmount = !Array.isArray(currency) && amount < 2n ? 2n : amount;
6534
- originVersion = getChainVersion(origin);
6535
- destVersion = !sdkCommon.isTLocation(destination) ? getChainVersion(destination) : undefined;
6536
- resolvedVersion = selectXcmVersion(version, originVersion, destVersion);
6537
- if (!sdkCommon.isRelayChain(origin)) {
6538
- _context.n = 6;
6539
- break;
6540
- }
6541
- if (!(destination === 'Ethereum')) {
6542
- _context.n = 1;
6543
- break;
6544
- }
6545
- throw new InvalidParameterError('Transfers from relay chain to Ethereum are not supported.');
6335
+ api = options.api, paraIdTo = options.paraIdTo, assetInfo = options.assetInfo;
6336
+ assertHasLocation(assetInfo);
6337
+ destChain = getTChain(paraIdTo, getRelayChainOf(chain));
6338
+ reserveChain = sdkCommon.isRelayChain(destChain) ? destChain : getAssetReserveChain(chain, chain, assetInfo.location);
6339
+ destApi = api.clone();
6340
+ _context.n = 1;
6341
+ return destApi.init(destChain);
6546
6342
  case 1:
6547
- if (asset) {
6548
- _context.n = 2;
6549
- break;
6550
- }
6551
- throw new InvalidParameterError('Asset is required for relay chain to relay chain transfers.');
6343
+ reserveApi = reserveChain !== chain ? api.clone() : destApi;
6344
+ _context.n = 2;
6345
+ return reserveApi.init(reserveChain);
6552
6346
  case 2:
6553
- isLocalTransfer = origin === destination;
6554
- if (!isLocalTransfer) {
6555
- _context.n = 5;
6556
- break;
6557
- }
6558
- if (!sdkCommon.isTLocation(address)) {
6559
- _context.n = 3;
6560
- break;
6561
- }
6562
- throw new InvalidAddressError('Location address is not supported for local transfers.');
6563
- case 3:
6564
- _context.n = 4;
6565
- return api.init(origin, TX_CLIENT_TIMEOUT_MS);
6566
- case 4:
6567
- return _context.a(2, api.callTxMethod({
6568
- module: 'Balances',
6569
- method: 'transfer_keep_alive',
6570
- parameters: {
6571
- dest: {
6572
- Id: address
6573
- },
6574
- value: finalAmount
6575
- }
6576
- }));
6577
- case 5:
6578
- return _context.a(2, transferRelayToPara({
6579
- api: api,
6580
- origin: origin,
6581
- destination: destination,
6582
- address: address,
6583
- assetInfo: _objectSpread2(_objectSpread2({}, asset), {}, {
6584
- amount: finalAmount
6585
- }),
6586
- paraIdTo: paraIdTo,
6587
- version: resolvedVersion,
6588
- pallet: pallet,
6589
- method: method
6590
- }));
6591
- case 6:
6592
- overriddenAsset = resolveOverriddenAsset(options, isBridge, assetCheckEnabled, resolvedFeeAsset);
6593
- _context.n = 7;
6594
- return api.init(origin, TX_CLIENT_TIMEOUT_MS);
6595
- case 7:
6596
- // In case asset check is disabled, we create asset object from currency symbol
6597
- resolvedAsset = asset !== null && asset !== void 0 ? asset : {
6598
- symbol: 'symbol' in currency ? currency.symbol : undefined
6599
- };
6600
- finalAsset = Array.isArray(currency) ? _objectSpread2(_objectSpread2({}, resolvedAsset), {}, {
6601
- amount: 0n,
6602
- assetId: '1'
6603
- }) : _objectSpread2(_objectSpread2({}, resolvedAsset), {}, {
6604
- amount: finalAmount
6347
+ return _context.a(2, {
6348
+ origin: {
6349
+ api: api,
6350
+ chain: chain
6351
+ },
6352
+ dest: {
6353
+ api: destApi,
6354
+ chain: destChain
6355
+ },
6356
+ reserve: {
6357
+ api: reserveApi,
6358
+ chain: reserveChain
6359
+ },
6360
+ assetInfo: assetInfo,
6361
+ options: options
6605
6362
  });
6606
- finalVersion = selectXcmVersion(version, originVersion, destVersion);
6607
- normalizedAsset = finalAsset.location ? _objectSpread2(_objectSpread2({}, finalAsset), {}, {
6608
- location: assets.normalizeLocation(finalAsset.location, finalVersion)
6609
- }) : finalAsset;
6610
- return _context.a(2, getChain(origin).transfer({
6611
- api: api,
6612
- assetInfo: normalizedAsset,
6613
- currency: currency,
6614
- feeAsset: resolvedFeeAsset,
6615
- feeCurrency: feeAsset,
6616
- address: address,
6617
- to: destination,
6618
- paraIdTo: paraIdTo,
6619
- overriddenAsset: overriddenAsset,
6620
- version: finalVersion,
6621
- senderAddress: senderAddress,
6622
- ahAddress: ahAddress,
6623
- pallet: pallet,
6624
- method: method
6625
- }));
6626
6363
  }
6627
6364
  }, _callee);
6628
6365
  }));
6629
- return function send(_x) {
6366
+ return function createTypeAndThenCallContext(_x, _x2) {
6630
6367
  return _ref.apply(this, arguments);
6631
6368
  };
6632
6369
  }();
6633
6370
 
6634
- var buildTypeAndThenCall = function buildTypeAndThenCall(_ref, isDotAsset, customXcm, assets) {
6371
+ var createCustomXcm = function createCustomXcm(_ref, isDotAsset) {
6635
6372
  var origin = _ref.origin,
6636
- reserve = _ref.reserve,
6637
6373
  dest = _ref.dest,
6374
+ reserve = _ref.reserve,
6638
6375
  assetInfo = _ref.assetInfo,
6639
- version = _ref.options.version;
6376
+ options = _ref.options;
6377
+ var fees = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
6378
+ reserveFee: MIN_FEE,
6379
+ refundFee: 0n,
6380
+ destFee: MIN_FEE
6381
+ };
6382
+ var destination = options.destination,
6383
+ version = options.version,
6384
+ address = options.address,
6385
+ paraIdTo = options.paraIdTo;
6386
+ var reserveFee = fees.reserveFee,
6387
+ refundFee = fees.refundFee,
6388
+ destFee = fees.destFee;
6640
6389
  var feeAssetLocation = !isDotAsset ? RELAY_LOCATION : assetInfo.location;
6641
- var finalDest = origin.chain === reserve.chain ? dest.chain : reserve.chain;
6642
- var destLocation = createDestination(version, origin.chain, finalDest, getParaId(finalDest));
6643
- var reserveType = origin.chain === reserve.chain ? 'LocalReserve' : 'DestinationReserve';
6644
- var feeMultiAsset = createAsset(version, assetInfo.amount, feeAssetLocation);
6645
- return {
6646
- module: 'PolkadotXcm',
6647
- method: 'transfer_assets_using_type_and_then',
6648
- parameters: {
6649
- dest: addXcmVersionHeader(destLocation, version),
6650
- assets: addXcmVersionHeader(assets, version),
6651
- assets_transfer_type: reserveType,
6652
- remote_fees_id: addXcmVersionHeader(feeMultiAsset.id, version),
6653
- fees_transfer_type: reserveType,
6654
- custom_xcm_on_dest: addXcmVersionHeader(customXcm, version),
6655
- weight_limit: 'Unlimited'
6390
+ var asset = createAsset(version, assetInfo.amount, localizeLocation(dest.chain, assetInfo.location));
6391
+ var depositInstruction = {
6392
+ DepositAsset: {
6393
+ assets: {
6394
+ Wild: {
6395
+ AllOf: {
6396
+ id: asset.id,
6397
+ fun: 'Fungible'
6398
+ }
6399
+ }
6400
+ },
6401
+ beneficiary: createBeneficiaryLocation({
6402
+ api: origin.api,
6403
+ address: address,
6404
+ version: version
6405
+ })
6406
+ }
6407
+ };
6408
+ var assetsFilter = [];
6409
+ if (!isDotAsset) assetsFilter.push(createAsset(version, reserveFee + destFee, localizeLocation(reserve.chain, RELAY_LOCATION)));
6410
+ assetsFilter.push(createAsset(version, assetInfo.amount, localizeLocation(reserve.chain, assetInfo.location)));
6411
+ return origin.chain !== reserve.chain && dest.chain !== reserve.chain ? {
6412
+ DepositReserveAsset: {
6413
+ assets: fees.destFee === MIN_FEE ? {
6414
+ Wild: 'All'
6415
+ } : {
6416
+ Definite: assetsFilter
6417
+ },
6418
+ dest: createDestination(version, origin.chain, destination, paraIdTo),
6419
+ xcm: [{
6420
+ BuyExecution: {
6421
+ fees: createAsset(version, !isDotAsset ? destFee : assetInfo.amount - reserveFee - refundFee, feeAssetLocation),
6422
+ weight_limit: 'Unlimited'
6423
+ }
6424
+ }, depositInstruction]
6656
6425
  }
6426
+ } : depositInstruction;
6427
+ };
6428
+
6429
+ var createRefundInstruction = function createRefundInstruction(api, senderAddress, version) {
6430
+ return {
6431
+ SetAppendix: [{
6432
+ DepositAsset: {
6433
+ assets: {
6434
+ Wild: 'All'
6435
+ },
6436
+ beneficiary: createBeneficiaryLocation({
6437
+ api: api,
6438
+ address: senderAddress,
6439
+ version: version
6440
+ })
6441
+ }
6442
+ }]
6443
+ };
6444
+ };
6445
+
6446
+ var buildAssets = function buildAssets(asset, feeAmount, isDotAsset, version) {
6447
+ var assets = [];
6448
+ if (!isDotAsset) {
6449
+ assets.push(createAsset(version, feeAmount, RELAY_LOCATION));
6450
+ }
6451
+ assets.push(createAsset(version, asset.amount, asset.location));
6452
+ return assets;
6453
+ };
6454
+ /**
6455
+ * Creates a type and then call for transferring assets using XCM. Works only for DOT and snowbridge assets so far.
6456
+ */
6457
+ var createTypeAndThenCall = /*#__PURE__*/function () {
6458
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(chain, options) {
6459
+ var api, senderAddress, version, context, assetInfo, isDotAsset, customXcm, refundInstruction, fees, finalCustomXcm, totalFee, assets;
6460
+ return _regenerator().w(function (_context) {
6461
+ while (1) switch (_context.n) {
6462
+ case 0:
6463
+ api = options.api, senderAddress = options.senderAddress, version = options.version;
6464
+ _context.n = 1;
6465
+ return createTypeAndThenCallContext(chain, options);
6466
+ case 1:
6467
+ context = _context.v;
6468
+ assetInfo = context.assetInfo;
6469
+ isDotAsset = sdkCommon.deepEqual(assetInfo.location, RELAY_LOCATION) || sdkCommon.deepEqual(assetInfo.location, {
6470
+ parents: 2,
6471
+ interior: {
6472
+ X1: [{
6473
+ GlobalConsensus: {
6474
+ Kusama: null
6475
+ }
6476
+ }]
6477
+ }
6478
+ });
6479
+ customXcm = createCustomXcm(context, isDotAsset);
6480
+ refundInstruction = senderAddress ? createRefundInstruction(api, senderAddress, version) : null;
6481
+ _context.n = 2;
6482
+ return computeAllFees(context, customXcm, isDotAsset, refundInstruction);
6483
+ case 2:
6484
+ fees = _context.v;
6485
+ finalCustomXcm = [];
6486
+ if (refundInstruction) finalCustomXcm.push(refundInstruction);
6487
+ finalCustomXcm.push(createCustomXcm(context, isDotAsset, fees));
6488
+ totalFee = fees.reserveFee + fees.destFee + fees.refundFee;
6489
+ assets = buildAssets(assetInfo, totalFee, isDotAsset, version);
6490
+ return _context.a(2, buildTypeAndThenCall(context, isDotAsset, finalCustomXcm, assets));
6491
+ }
6492
+ }, _callee);
6493
+ }));
6494
+ return function createTypeAndThenCall(_x, _x2) {
6495
+ return _ref.apply(this, arguments);
6657
6496
  };
6658
- };
6497
+ }();
6659
6498
 
6660
- var FEE_PADDING_PERCENTAGE$2 = 20;
6661
- var FEE_PADDING_HYDRATION = 150;
6662
- var computeInstructionFee = /*#__PURE__*/function () {
6663
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref, version, xcm) {
6664
- var chain, api, _t, _t2, _t3;
6499
+ var getTransferableAmountInternal = /*#__PURE__*/function () {
6500
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
6501
+ var api, senderAddress, chain, destination, currency, tx, feeAsset, resolvedFeeAsset, asset, amount, balance, ed, isNativeAsset, shouldSubstractFee, feeToSubtract, _yield$getOriginXcmFe, fee, transferable;
6665
6502
  return _regenerator().w(function (_context) {
6666
6503
  while (1) switch (_context.n) {
6667
6504
  case 0:
6668
- chain = _ref.chain, api = _ref.api;
6669
- _t = padFeeBy;
6505
+ api = _ref.api, senderAddress = _ref.senderAddress, chain = _ref.origin, destination = _ref.destination, currency = _ref.currency, tx = _ref.tx, feeAsset = _ref.feeAsset;
6506
+ validateAddress(senderAddress, chain, false);
6507
+ resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, chain, destination, currency) : undefined;
6508
+ asset = assets.findAssetInfoOrThrow(chain, currency, null);
6509
+ amount = abstractDecimals(currency.amount, asset.decimals, api);
6670
6510
  _context.n = 1;
6671
- return api.getXcmPaymentApiFee(chain, addXcmVersionHeader(xcm, version), {
6672
- location: DOT_LOCATION
6673
- }, true);
6511
+ return getAssetBalanceInternal({
6512
+ api: api,
6513
+ address: senderAddress,
6514
+ chain: chain,
6515
+ currency: currency
6516
+ });
6674
6517
  case 1:
6675
- _t2 = _context.v;
6676
- _t3 = chain === 'Hydration' ? FEE_PADDING_HYDRATION : FEE_PADDING_PERCENTAGE$2;
6677
- return _context.a(2, _t(_t2, _t3));
6518
+ balance = _context.v;
6519
+ ed = assets.getExistentialDepositOrThrow(chain, currency);
6520
+ isNativeAsset = assets.getNativeAssetSymbol(chain) === asset.symbol;
6521
+ shouldSubstractFee = isNativeAsset || chain === 'AssetHubPolkadot' && resolvedFeeAsset && assets.isAssetEqual(resolvedFeeAsset, asset);
6522
+ feeToSubtract = 0n;
6523
+ if (!shouldSubstractFee) {
6524
+ _context.n = 4;
6525
+ break;
6526
+ }
6527
+ _context.n = 2;
6528
+ return getOriginXcmFee({
6529
+ api: api,
6530
+ tx: tx,
6531
+ origin: chain,
6532
+ destination: chain,
6533
+ senderAddress: senderAddress,
6534
+ feeAsset: feeAsset,
6535
+ currency: _objectSpread2(_objectSpread2({}, currency), {}, {
6536
+ amount: amount
6537
+ }),
6538
+ disableFallback: false
6539
+ });
6540
+ case 2:
6541
+ _yield$getOriginXcmFe = _context.v;
6542
+ fee = _yield$getOriginXcmFe.fee;
6543
+ if (!(fee === undefined)) {
6544
+ _context.n = 3;
6545
+ break;
6546
+ }
6547
+ throw new InvalidParameterError("Cannot get origin xcm fee for currency ".concat(JSON.stringify(currency, sdkCommon.replaceBigInt), " on chain ").concat(chain, "."));
6548
+ case 3:
6549
+ feeToSubtract = fee;
6550
+ case 4:
6551
+ transferable = balance - ed - feeToSubtract;
6552
+ return _context.a(2, transferable > 0n ? transferable : 0n);
6678
6553
  }
6679
6554
  }, _callee);
6680
6555
  }));
6681
- return function computeInstructionFee(_x, _x2, _x3) {
6556
+ return function getTransferableAmountInternal(_x) {
6682
6557
  return _ref2.apply(this, arguments);
6683
6558
  };
6684
6559
  }();
6685
- var computeAllFees = /*#__PURE__*/function () {
6686
- var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(_ref3, customXcm, isDotAsset, refundInstruction) {
6687
- var reserve, dest, version, _t4, _t5, _t6, _t7, _t8, _t9, _t0, _t1, _t10;
6560
+ var getTransferableAmount = /*#__PURE__*/function () {
6561
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
6562
+ var api;
6688
6563
  return _regenerator().w(function (_context2) {
6689
- while (1) switch (_context2.n) {
6564
+ while (1) switch (_context2.p = _context2.n) {
6690
6565
  case 0:
6691
- reserve = _ref3.reserve, dest = _ref3.dest, version = _ref3.options.version;
6692
- if (!('DepositReserveAsset' in customXcm)) {
6693
- _context2.n = 6;
6694
- break;
6695
- }
6696
- _context2.n = 1;
6697
- return computeInstructionFee(reserve, version, [customXcm]);
6698
- case 1:
6699
- _t5 = _context2.v;
6700
- if (!refundInstruction) {
6701
- _context2.n = 3;
6702
- break;
6703
- }
6566
+ api = options.api;
6567
+ api.setDisconnectAllowed(false);
6568
+ _context2.p = 1;
6704
6569
  _context2.n = 2;
6705
- return computeInstructionFee(reserve, version, [refundInstruction]);
6570
+ return getTransferableAmountInternal(options);
6706
6571
  case 2:
6707
- _t6 = _context2.v;
6708
- _context2.n = 4;
6709
- break;
6572
+ return _context2.a(2, _context2.v);
6710
6573
  case 3:
6711
- _t6 = 0n;
6574
+ _context2.p = 3;
6575
+ api.setDisconnectAllowed(true);
6576
+ _context2.n = 4;
6577
+ return api.disconnect();
6712
6578
  case 4:
6713
- _t7 = _t6;
6714
- _context2.n = 5;
6715
- return computeInstructionFee(assets.hasXcmPaymentApiSupport(dest.chain) ? dest : reserve, version, customXcm.DepositReserveAsset.xcm);
6579
+ return _context2.f(3);
6716
6580
  case 5:
6717
- _t8 = _context2.v;
6718
- _t4 = {
6719
- reserveFee: _t5,
6720
- refundFee: _t7,
6721
- destFee: _t8
6722
- };
6723
- _context2.n = 13;
6724
- break;
6725
- case 6:
6726
- if (isDotAsset) {
6727
- _context2.n = 8;
6728
- break;
6729
- }
6730
- _context2.n = 7;
6731
- return computeInstructionFee(assets.hasXcmPaymentApiSupport(dest.chain) ? dest : reserve, version, [customXcm]);
6732
- case 7:
6733
- _t9 = _context2.v;
6734
- _context2.n = 9;
6735
- break;
6736
- case 8:
6737
- _t9 = 0n;
6738
- case 9:
6739
- _t0 = _t9;
6740
- if (isDotAsset) {
6741
- _context2.n = 11;
6742
- break;
6743
- }
6744
- _context2.n = 10;
6745
- return computeInstructionFee(assets.hasXcmPaymentApiSupport(reserve.chain) ? reserve : dest, version, [refundInstruction]);
6746
- case 10:
6747
- _t1 = _context2.v;
6748
- _context2.n = 12;
6749
- break;
6750
- case 11:
6751
- _t1 = 0n;
6752
- case 12:
6753
- _t10 = _t1;
6754
- _t4 = {
6755
- reserveFee: 0n,
6756
- destFee: _t0,
6757
- refundFee: _t10
6758
- };
6759
- case 13:
6760
- return _context2.a(2, _t4);
6581
+ return _context2.a(2);
6761
6582
  }
6762
- }, _callee2);
6583
+ }, _callee2, null, [[1,, 3, 5]]);
6763
6584
  }));
6764
- return function computeAllFees(_x4, _x5, _x6, _x7) {
6765
- return _ref4.apply(this, arguments);
6585
+ return function getTransferableAmount(_x2) {
6586
+ return _ref3.apply(this, arguments);
6766
6587
  };
6767
6588
  }();
6768
6589
 
6769
- var createTypeAndThenCallContext = /*#__PURE__*/function () {
6770
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(chain, options) {
6771
- var api, paraIdTo, assetInfo, destChain, reserveChain, destApi, reserveApi;
6590
+ var buildDestInfo = /*#__PURE__*/function () {
6591
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
6592
+ var api, origin, destination, address, currency, originFee, isFeeAssetAh, destFeeDetail, assetHubFee, bridgeFee, destApi, destAsset, edDest, edDestBn, destCurrency, destBalance, destAmount, effectiveAmountForBalance, destBalanceSufficient, destBalanceSufficientResult, destBalanceAfter, destbalanceAfterResult, receivedAmount, isAssetHubToAssetHubRoute, nativeAssetOfOriginSymbol, isOriginAssetNative, destXcmFeeBalance, isDestFeeInNativeCurrency, destRecipientNativeBalance, destXcmFeeBalanceAfter;
6772
6593
  return _regenerator().w(function (_context) {
6773
6594
  while (1) switch (_context.n) {
6774
6595
  case 0:
6775
- api = options.api, paraIdTo = options.paraIdTo, assetInfo = options.assetInfo;
6776
- assertHasLocation(assetInfo);
6777
- destChain = getTChain(paraIdTo, getRelayChainOf(chain));
6778
- reserveChain = sdkCommon.isRelayChain(destChain) ? destChain : getAssetReserveChain(chain, chain, assetInfo.location);
6596
+ api = _ref.api, origin = _ref.origin, destination = _ref.destination, address = _ref.address, currency = _ref.currency, originFee = _ref.originFee, isFeeAssetAh = _ref.isFeeAssetAh, destFeeDetail = _ref.destFeeDetail, assetHubFee = _ref.assetHubFee, bridgeFee = _ref.bridgeFee;
6779
6597
  destApi = api.clone();
6598
+ if (!(destination !== 'Ethereum')) {
6599
+ _context.n = 1;
6600
+ break;
6601
+ }
6780
6602
  _context.n = 1;
6781
- return destApi.init(destChain);
6603
+ return destApi.init(destination);
6782
6604
  case 1:
6783
- reserveApi = reserveChain !== chain ? api.clone() : destApi;
6784
- _context.n = 2;
6785
- return reserveApi.init(reserveChain);
6605
+ destAsset = assets.findAssetOnDestOrThrow(origin, destination, currency);
6606
+ edDest = destAsset.existentialDeposit;
6607
+ if (edDest) {
6608
+ _context.n = 2;
6609
+ break;
6610
+ }
6611
+ throw new InvalidParameterError("Existential deposit not found for ".concat(destination, " with currency ").concat(JSON.stringify(currency, sdkCommon.replaceBigInt)));
6786
6612
  case 2:
6613
+ edDestBn = BigInt(edDest);
6614
+ destCurrency = destAsset.location ? {
6615
+ location: destAsset.location
6616
+ } : {
6617
+ symbol: destAsset.symbol
6618
+ };
6619
+ _context.n = 3;
6620
+ return getAssetBalanceInternal({
6621
+ api: destApi,
6622
+ address: address,
6623
+ chain: destination,
6624
+ currency: destCurrency
6625
+ });
6626
+ case 3:
6627
+ destBalance = _context.v;
6628
+ destAmount = isFeeAssetAh ? BigInt(currency.amount) - originFee : BigInt(currency.amount);
6629
+ effectiveAmountForBalance = destAmount;
6630
+ if (destination === 'Ethereum' && assetHubFee !== undefined) {
6631
+ effectiveAmountForBalance -= assetHubFee;
6632
+ }
6633
+ destBalanceSufficient = effectiveAmountForBalance - destFeeDetail.fee > (destBalance < edDestBn ? edDestBn : 0);
6634
+ destBalanceSufficientResult = destFeeDetail.currency !== destAsset.symbol && destination !== 'Ethereum' ? new UnableToComputeError('Unable to compute if dest balance will be sufficient. Fee currency is not the same') : destBalanceSufficient;
6635
+ destBalanceAfter = destBalance - (destFeeDetail.currency === destAsset.symbol ? destFeeDetail.fee : 0n) + effectiveAmountForBalance;
6636
+ destbalanceAfterResult = destFeeDetail.currency !== destAsset.symbol && destination !== 'Ethereum' ? new UnableToComputeError('Unable to compute if dest balance will be sufficient. Fee currency is not the same') : destBalanceAfter;
6637
+ isAssetHubToAssetHubRoute = origin === 'AssetHubKusama' && destination === 'AssetHubPolkadot' || origin === 'AssetHubPolkadot' && destination === 'AssetHubKusama';
6638
+ if (isAssetHubToAssetHubRoute) {
6639
+ nativeAssetOfOriginSymbol = assets.getNativeAssetSymbol(origin);
6640
+ isOriginAssetNative = false;
6641
+ if (destAsset.symbol === nativeAssetOfOriginSymbol) {
6642
+ isOriginAssetNative = true;
6643
+ }
6644
+ if (isOriginAssetNative) {
6645
+ if (bridgeFee === undefined) {
6646
+ receivedAmount = new UnableToComputeError("bridgeFee is required for native asset transfer from ".concat(origin, " to ").concat(destination, " but was not provided."));
6647
+ } else {
6648
+ receivedAmount = BigInt(currency.amount) - originFee - bridgeFee;
6649
+ }
6650
+ } else {
6651
+ receivedAmount = new UnableToComputeError("Unable to compute received amount: The transferred asset (".concat(destAsset.symbol, ") is not the native asset (").concat(nativeAssetOfOriginSymbol, ") of origin ").concat(origin, " for the ").concat(origin, "->").concat(destination, " route."));
6652
+ }
6653
+ } else {
6654
+ if (destbalanceAfterResult instanceof UnableToComputeError) {
6655
+ receivedAmount = destbalanceAfterResult;
6656
+ } else {
6657
+ receivedAmount = destbalanceAfterResult - destBalance;
6658
+ }
6659
+ }
6660
+ isDestFeeInNativeCurrency = destFeeDetail.currency === assets.getNativeAssetSymbol(destination);
6661
+ if (!isDestFeeInNativeCurrency) {
6662
+ _context.n = 5;
6663
+ break;
6664
+ }
6665
+ _context.n = 4;
6666
+ return getBalanceNativeInternal({
6667
+ address: address,
6668
+ chain: destination,
6669
+ api: destApi
6670
+ });
6671
+ case 4:
6672
+ destRecipientNativeBalance = _context.v;
6673
+ destXcmFeeBalance = destRecipientNativeBalance;
6674
+ _context.n = 6;
6675
+ break;
6676
+ case 5:
6677
+ destXcmFeeBalance = destBalance;
6678
+ case 6:
6679
+ destXcmFeeBalanceAfter = isFeeAssetAh ? destBalanceAfter : destXcmFeeBalance - destFeeDetail.fee + (destFeeDetail.currency === destAsset.symbol ? effectiveAmountForBalance : 0n);
6787
6680
  return _context.a(2, {
6788
- origin: {
6789
- api: api,
6790
- chain: chain
6791
- },
6792
- dest: {
6793
- api: destApi,
6794
- chain: destChain
6795
- },
6796
- reserve: {
6797
- api: reserveApi,
6798
- chain: reserveChain
6681
+ receivedCurrency: {
6682
+ sufficient: destBalanceSufficientResult,
6683
+ receivedAmount: receivedAmount,
6684
+ balance: destBalance,
6685
+ balanceAfter: destbalanceAfterResult,
6686
+ currencySymbol: destAsset.symbol,
6687
+ existentialDeposit: edDestBn
6799
6688
  },
6800
- assetInfo: assetInfo,
6801
- options: options
6689
+ xcmFee: {
6690
+ fee: destFeeDetail.fee,
6691
+ balance: destXcmFeeBalance,
6692
+ balanceAfter: destXcmFeeBalanceAfter,
6693
+ currencySymbol: destFeeDetail.currency
6694
+ }
6802
6695
  });
6803
6696
  }
6804
6697
  }, _callee);
6805
6698
  }));
6806
- return function createTypeAndThenCallContext(_x, _x2) {
6807
- return _ref.apply(this, arguments);
6699
+ return function buildDestInfo(_x) {
6700
+ return _ref2.apply(this, arguments);
6808
6701
  };
6809
6702
  }();
6810
6703
 
6811
- var createCustomXcm = function createCustomXcm(_ref, isDotAsset) {
6812
- var origin = _ref.origin,
6813
- dest = _ref.dest,
6814
- reserve = _ref.reserve,
6815
- assetInfo = _ref.assetInfo,
6816
- options = _ref.options;
6817
- var fees = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
6818
- reserveFee: MIN_FEE,
6819
- refundFee: 0n,
6820
- destFee: MIN_FEE
6821
- };
6822
- var destination = options.destination,
6823
- version = options.version,
6824
- address = options.address,
6825
- paraIdTo = options.paraIdTo;
6826
- var reserveFee = fees.reserveFee,
6827
- refundFee = fees.refundFee,
6828
- destFee = fees.destFee;
6829
- var feeAssetLocation = !isDotAsset ? RELAY_LOCATION : assetInfo.location;
6830
- var asset = createAsset(version, assetInfo.amount, localizeLocation(dest.chain, assetInfo.location));
6831
- var depositInstruction = {
6832
- DepositAsset: {
6833
- assets: {
6834
- Wild: {
6835
- AllOf: {
6836
- id: asset.id,
6837
- fun: 'Fungible'
6838
- }
6839
- }
6840
- },
6841
- beneficiary: createBeneficiaryLocation({
6842
- api: origin.api,
6843
- address: address,
6844
- version: version
6845
- })
6846
- }
6847
- };
6848
- var assetsFilter = [];
6849
- if (!isDotAsset) assetsFilter.push(createAsset(version, reserveFee + destFee, localizeLocation(reserve.chain, RELAY_LOCATION)));
6850
- assetsFilter.push(createAsset(version, assetInfo.amount, localizeLocation(reserve.chain, assetInfo.location)));
6851
- return origin.chain !== reserve.chain && dest.chain !== reserve.chain ? {
6852
- DepositReserveAsset: {
6853
- assets: fees.destFee === MIN_FEE ? {
6854
- Wild: 'All'
6855
- } : {
6856
- Definite: assetsFilter
6857
- },
6858
- dest: createDestination(version, origin.chain, destination, paraIdTo),
6859
- xcm: [{
6860
- BuyExecution: {
6861
- fees: createAsset(version, !isDotAsset ? destFee : assetInfo.amount - reserveFee - refundFee, feeAssetLocation),
6862
- weight_limit: 'Unlimited'
6863
- }
6864
- }, depositInstruction]
6865
- }
6866
- } : depositInstruction;
6867
- };
6868
-
6869
- var createRefundInstruction = function createRefundInstruction(api, senderAddress, version) {
6870
- return {
6871
- SetAppendix: [{
6872
- DepositAsset: {
6873
- assets: {
6874
- Wild: 'All'
6875
- },
6876
- beneficiary: createBeneficiaryLocation({
6877
- api: api,
6878
- address: senderAddress,
6879
- version: version
6880
- })
6881
- }
6882
- }]
6883
- };
6884
- };
6885
-
6886
- var buildAssets = function buildAssets(asset, feeAmount, isDotAsset, version) {
6887
- var assets = [];
6888
- if (!isDotAsset) {
6889
- assets.push(createAsset(version, feeAmount, RELAY_LOCATION));
6890
- }
6891
- assets.push(createAsset(version, asset.amount, asset.location));
6892
- return assets;
6893
- };
6894
- /**
6895
- * Creates a type and then call for transferring assets using XCM. Works only for DOT and snowbridge assets so far.
6896
- */
6897
- var createTypeAndThenCall = /*#__PURE__*/function () {
6898
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(chain, options) {
6899
- var api, senderAddress, version, context, assetInfo, isDotAsset, customXcm, refundInstruction, fees, finalCustomXcm, totalFee, assets;
6704
+ var buildHopInfo = /*#__PURE__*/function () {
6705
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
6706
+ var api, chain, feeData, originChain, currency, senderAddress, ahAddress, hopApi, resolvedAddress, nativeBalanceOnHop, nativeAssetSymbolOnHop, xcmFeeDetails, isBridgeHub, hopAsset, hopCurrencyPayload, balance, ed;
6900
6707
  return _regenerator().w(function (_context) {
6901
- while (1) switch (_context.n) {
6708
+ while (1) switch (_context.p = _context.n) {
6902
6709
  case 0:
6903
- api = options.api, senderAddress = options.senderAddress, version = options.version;
6710
+ api = _ref.api, chain = _ref.chain, feeData = _ref.feeData, originChain = _ref.originChain, currency = _ref.currency, senderAddress = _ref.senderAddress, ahAddress = _ref.ahAddress;
6711
+ hopApi = api.clone();
6904
6712
  _context.n = 1;
6905
- return createTypeAndThenCallContext(chain, options);
6713
+ return hopApi.init(chain);
6906
6714
  case 1:
6907
- context = _context.v;
6908
- assetInfo = context.assetInfo;
6909
- isDotAsset = sdkCommon.deepEqual(assetInfo.location, RELAY_LOCATION) || sdkCommon.deepEqual(assetInfo.location, {
6910
- parents: 2,
6911
- interior: {
6912
- X1: [{
6913
- GlobalConsensus: {
6914
- Kusama: null
6915
- }
6916
- }]
6917
- }
6715
+ hopApi.setDisconnectAllowed(false);
6716
+ _context.p = 2;
6717
+ resolvedAddress = assets.isChainEvm(originChain) && ahAddress ? ahAddress : senderAddress;
6718
+ _context.n = 3;
6719
+ return getBalanceNativeInternal({
6720
+ api: hopApi,
6721
+ address: resolvedAddress,
6722
+ chain: chain
6918
6723
  });
6919
- customXcm = createCustomXcm(context, isDotAsset);
6920
- refundInstruction = senderAddress ? createRefundInstruction(api, senderAddress, version) : null;
6921
- _context.n = 2;
6922
- return computeAllFees(context, customXcm, isDotAsset, refundInstruction);
6923
- case 2:
6924
- fees = _context.v;
6925
- finalCustomXcm = [];
6926
- if (refundInstruction) finalCustomXcm.push(refundInstruction);
6927
- finalCustomXcm.push(createCustomXcm(context, isDotAsset, fees));
6928
- totalFee = fees.reserveFee + fees.destFee + fees.refundFee;
6929
- assets = buildAssets(assetInfo, totalFee, isDotAsset, version);
6930
- return _context.a(2, buildTypeAndThenCall(context, isDotAsset, finalCustomXcm, assets));
6724
+ case 3:
6725
+ nativeBalanceOnHop = _context.v;
6726
+ nativeAssetSymbolOnHop = assets.getNativeAssetSymbol(chain);
6727
+ xcmFeeDetails = {
6728
+ fee: feeData.fee,
6729
+ balance: nativeBalanceOnHop,
6730
+ currencySymbol: nativeAssetSymbolOnHop
6731
+ };
6732
+ isBridgeHub = chain.includes('BridgeHub');
6733
+ if (!isBridgeHub) {
6734
+ _context.n = 4;
6735
+ break;
6736
+ }
6737
+ return _context.a(2, {
6738
+ currencySymbol: assets.getNativeAssetSymbol(chain),
6739
+ xcmFee: xcmFeeDetails
6740
+ });
6741
+ case 4:
6742
+ hopAsset = assets.findAssetOnDestOrThrow(originChain, chain, currency);
6743
+ hopCurrencyPayload = hopAsset.location ? {
6744
+ location: hopAsset.location
6745
+ } : {
6746
+ symbol: hopAsset.symbol
6747
+ };
6748
+ _context.n = 5;
6749
+ return getAssetBalanceInternal({
6750
+ api: hopApi,
6751
+ address: resolvedAddress,
6752
+ chain: chain,
6753
+ currency: hopCurrencyPayload
6754
+ });
6755
+ case 5:
6756
+ balance = _context.v;
6757
+ ed = assets.getExistentialDeposit(chain, hopCurrencyPayload);
6758
+ if (ed) {
6759
+ _context.n = 6;
6760
+ break;
6761
+ }
6762
+ throw new InvalidParameterError("Existential deposit not found for chain ".concat(chain, " with currency ").concat(JSON.stringify(hopCurrencyPayload)));
6763
+ case 6:
6764
+ return _context.a(2, {
6765
+ balance: balance,
6766
+ currencySymbol: hopAsset.symbol,
6767
+ existentialDeposit: BigInt(ed),
6768
+ xcmFee: xcmFeeDetails
6769
+ });
6770
+ case 7:
6771
+ _context.p = 7;
6772
+ hopApi.setDisconnectAllowed(true);
6773
+ _context.n = 8;
6774
+ return hopApi.disconnect();
6775
+ case 8:
6776
+ return _context.f(7);
6777
+ case 9:
6778
+ return _context.a(2);
6931
6779
  }
6932
- }, _callee);
6780
+ }, _callee, null, [[2,, 7, 9]]);
6933
6781
  }));
6934
- return function createTypeAndThenCall(_x, _x2) {
6935
- return _ref.apply(this, arguments);
6782
+ return function buildHopInfo(_x) {
6783
+ return _ref2.apply(this, arguments);
6936
6784
  };
6937
6785
  }();
6938
6786
 
6939
- var attemptDryRunFee = /*#__PURE__*/function () {
6940
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
6941
- var origin, currency, builder, reductionPcts, lastReduction, result, _i, _reductionPcts, percentage, modifiedBuilder, _t, _t2, _t3, _t4, _t5, _t6, _t7;
6942
- return _regenerator().w(function (_context) {
6943
- while (1) switch (_context.n) {
6787
+ var getTransferInfo = /*#__PURE__*/function () {
6788
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(_ref) {
6789
+ var api, tx, origin, destination, senderAddress, ahAddress, address, currency, feeAsset, resolvedFeeAsset, originAsset, amount, originBalanceFee, originBalance, edOrigin, _yield$getXcmFee, _yield$getXcmFee$orig, originFee, originFeeCurrency, assetHubFeeResult, bridgeHubFeeResult, destFeeDetail, hops, isFeeAssetAh, originBalanceAfter, originBalanceFeeAfter, originBalanceNativeSufficient, originBalanceSufficient, assetHub, bridgeHub, bridgeHubChain, builtHops, destinationInfo, _t;
6790
+ return _regenerator().w(function (_context2) {
6791
+ while (1) switch (_context2.p = _context2.n) {
6944
6792
  case 0:
6945
- origin = options.origin, currency = options.currency, builder = options.builder;
6946
- reductionPcts = [0, 10, 20, 30, 40, 50];
6947
- lastReduction = reductionPcts[reductionPcts.length - 1];
6948
- result = null;
6949
- _i = 0, _reductionPcts = reductionPcts;
6950
- case 1:
6951
- if (!(_i < _reductionPcts.length)) {
6952
- _context.n = 5;
6793
+ api = _ref.api, tx = _ref.tx, origin = _ref.origin, destination = _ref.destination, senderAddress = _ref.senderAddress, ahAddress = _ref.ahAddress, address = _ref.address, currency = _ref.currency, feeAsset = _ref.feeAsset;
6794
+ if (!(assets.isChainEvm(origin) && !ahAddress)) {
6795
+ _context2.n = 1;
6953
6796
  break;
6954
6797
  }
6955
- percentage = _reductionPcts[_i];
6956
- modifiedBuilder = builder.currency(_objectSpread2(_objectSpread2({}, currency), {}, {
6957
- amount: padFeeBy(BigInt(currency.amount), -percentage)
6958
- }));
6959
- _t = getOriginXcmFeeInternal;
6960
- _t2 = _objectSpread2;
6961
- _t3 = _objectSpread2({}, options);
6962
- _t4 = {};
6963
- _context.n = 2;
6964
- return modifiedBuilder.build();
6798
+ throw new InvalidParameterError("ahAddress is required for EVM origin ".concat(origin, "."));
6799
+ case 1:
6800
+ resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, origin, destination, currency) : undefined;
6801
+ _context2.n = 2;
6802
+ return api.init(origin);
6965
6803
  case 2:
6966
- _t5 = _context.v;
6967
- _t6 = {
6968
- tx: _t5
6969
- };
6970
- _t7 = _t2(_t3, _t4, _t6);
6971
- _context.n = 3;
6972
- return _t(_t7);
6973
- case 3:
6974
- result = _context.v;
6975
- if (!(result.feeType === 'dryRun' || percentage === lastReduction || !assets.hasDryRunSupport(origin))) {
6976
- _context.n = 4;
6804
+ api.setDisconnectAllowed(false);
6805
+ _context2.p = 3;
6806
+ originAsset = assets.findAssetInfoOrThrow(origin, currency, destination);
6807
+ amount = abstractDecimals(currency.amount, originAsset.decimals, api);
6808
+ if (!(feeAsset && resolvedFeeAsset)) {
6809
+ _context2.n = 5;
6977
6810
  break;
6978
6811
  }
6979
- return _context.a(2, result);
6812
+ _context2.n = 4;
6813
+ return getAssetBalanceInternal({
6814
+ api: api,
6815
+ address: senderAddress,
6816
+ chain: origin,
6817
+ currency: feeAsset
6818
+ });
6980
6819
  case 4:
6981
- _i++;
6982
- _context.n = 1;
6820
+ _t = _context2.v;
6821
+ _context2.n = 7;
6983
6822
  break;
6984
6823
  case 5:
6985
- return _context.a(2, result);
6986
- }
6987
- }, _callee);
6988
- }));
6989
- return function attemptDryRunFee(_x) {
6990
- return _ref.apply(this, arguments);
6991
- };
6992
- }();
6993
-
6994
- var getTransferableAmountInternal = /*#__PURE__*/function () {
6995
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
6996
- var api, senderAddress, chain, destination, currency, builder, feeAsset, resolvedFeeAsset, asset, amount, balance, ed, isNativeAsset, shouldSubstractFee, feeToSubtract, _yield$attemptDryRunF, fee, transferable;
6997
- return _regenerator().w(function (_context) {
6998
- while (1) switch (_context.n) {
6999
- case 0:
7000
- api = _ref.api, senderAddress = _ref.senderAddress, chain = _ref.origin, destination = _ref.destination, currency = _ref.currency, builder = _ref.builder, feeAsset = _ref.feeAsset;
7001
- validateAddress(senderAddress, chain, false);
7002
- resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, chain, destination, currency) : undefined;
7003
- asset = assets.findAssetInfoOrThrow(chain, currency, null);
7004
- amount = abstractDecimals(currency.amount, asset.decimals, api);
7005
- _context.n = 1;
6824
+ _context2.n = 6;
6825
+ return getBalanceNativeInternal({
6826
+ api: api,
6827
+ address: senderAddress,
6828
+ chain: origin
6829
+ });
6830
+ case 6:
6831
+ _t = _context2.v;
6832
+ case 7:
6833
+ originBalanceFee = _t;
6834
+ _context2.n = 8;
7006
6835
  return getAssetBalanceInternal({
7007
6836
  api: api,
7008
6837
  address: senderAddress,
7009
- chain: chain,
6838
+ chain: origin,
7010
6839
  currency: currency
7011
6840
  });
7012
- case 1:
7013
- balance = _context.v;
7014
- ed = assets.getExistentialDepositOrThrow(chain, currency);
7015
- isNativeAsset = assets.getNativeAssetSymbol(chain) === asset.symbol;
7016
- shouldSubstractFee = isNativeAsset || chain === 'AssetHubPolkadot' && resolvedFeeAsset && assets.isAssetEqual(resolvedFeeAsset, asset);
7017
- feeToSubtract = 0n;
7018
- if (!shouldSubstractFee) {
7019
- _context.n = 4;
6841
+ case 8:
6842
+ originBalance = _context2.v;
6843
+ edOrigin = assets.getExistentialDepositOrThrow(origin, currency);
6844
+ _context2.n = 9;
6845
+ return getXcmFee({
6846
+ api: api,
6847
+ tx: tx,
6848
+ origin: origin,
6849
+ destination: destination,
6850
+ senderAddress: senderAddress,
6851
+ address: address,
6852
+ currency: currency,
6853
+ feeAsset: feeAsset,
6854
+ disableFallback: false
6855
+ });
6856
+ case 9:
6857
+ _yield$getXcmFee = _context2.v;
6858
+ _yield$getXcmFee$orig = _yield$getXcmFee.origin;
6859
+ originFee = _yield$getXcmFee$orig.fee;
6860
+ originFeeCurrency = _yield$getXcmFee$orig.currency;
6861
+ assetHubFeeResult = _yield$getXcmFee.assetHub;
6862
+ bridgeHubFeeResult = _yield$getXcmFee.bridgeHub;
6863
+ destFeeDetail = _yield$getXcmFee.destination;
6864
+ hops = _yield$getXcmFee.hops;
6865
+ isFeeAssetAh = origin === 'AssetHubPolkadot' && resolvedFeeAsset && assets.isAssetEqual(resolvedFeeAsset, originAsset);
6866
+ originBalanceAfter = originBalance - amount;
6867
+ originBalanceFeeAfter = isFeeAssetAh ? originBalanceFee - amount : originBalanceFee - originFee;
6868
+ originBalanceNativeSufficient = originBalanceFee >= originFee;
6869
+ originBalanceSufficient = originBalanceAfter >= edOrigin;
6870
+ if (!assetHubFeeResult) {
6871
+ _context2.n = 11;
7020
6872
  break;
7021
6873
  }
7022
- _context.n = 2;
7023
- return attemptDryRunFee({
6874
+ _context2.n = 10;
6875
+ return buildHopInfo({
7024
6876
  api: api,
7025
- builder: builder,
7026
- origin: chain,
7027
- destination: chain,
6877
+ chain: "AssetHub".concat(getRelayChainOf(origin)),
6878
+ feeData: assetHubFeeResult,
6879
+ originChain: origin,
6880
+ currency: currency,
7028
6881
  senderAddress: senderAddress,
7029
- feeAsset: feeAsset,
6882
+ ahAddress: ahAddress
6883
+ });
6884
+ case 10:
6885
+ assetHub = _context2.v;
6886
+ case 11:
6887
+ if (!bridgeHubFeeResult) {
6888
+ _context2.n = 13;
6889
+ break;
6890
+ }
6891
+ bridgeHubChain = "BridgeHub".concat(getRelayChainOf(origin));
6892
+ _context2.n = 12;
6893
+ return buildHopInfo({
6894
+ api: api,
6895
+ chain: bridgeHubChain,
6896
+ feeData: bridgeHubFeeResult,
6897
+ originChain: origin,
6898
+ currency: currency,
6899
+ senderAddress: senderAddress,
6900
+ ahAddress: ahAddress
6901
+ });
6902
+ case 12:
6903
+ bridgeHub = _context2.v;
6904
+ case 13:
6905
+ builtHops = [];
6906
+ if (!(hops && hops.length > 0)) {
6907
+ _context2.n = 15;
6908
+ break;
6909
+ }
6910
+ _context2.n = 14;
6911
+ return Promise.all(hops.map(/*#__PURE__*/function () {
6912
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(hop) {
6913
+ var result;
6914
+ return _regenerator().w(function (_context) {
6915
+ while (1) switch (_context.n) {
6916
+ case 0:
6917
+ _context.n = 1;
6918
+ return buildHopInfo({
6919
+ api: api,
6920
+ chain: hop.chain,
6921
+ feeData: hop.result,
6922
+ originChain: origin,
6923
+ currency: currency,
6924
+ senderAddress: senderAddress,
6925
+ ahAddress: ahAddress
6926
+ });
6927
+ case 1:
6928
+ result = _context.v;
6929
+ return _context.a(2, {
6930
+ chain: hop.chain,
6931
+ result: result
6932
+ });
6933
+ }
6934
+ }, _callee);
6935
+ }));
6936
+ return function (_x2) {
6937
+ return _ref3.apply(this, arguments);
6938
+ };
6939
+ }()));
6940
+ case 14:
6941
+ builtHops = _context2.v;
6942
+ case 15:
6943
+ _context2.n = 16;
6944
+ return buildDestInfo({
6945
+ api: api,
6946
+ origin: origin,
6947
+ destination: destination,
6948
+ address: address,
7030
6949
  currency: _objectSpread2(_objectSpread2({}, currency), {}, {
7031
6950
  amount: amount
7032
6951
  }),
7033
- disableFallback: false
6952
+ originFee: originFee,
6953
+ isFeeAssetAh: !!isFeeAssetAh,
6954
+ destFeeDetail: destFeeDetail,
6955
+ assetHubFee: assetHubFeeResult === null || assetHubFeeResult === void 0 ? void 0 : assetHubFeeResult.fee,
6956
+ bridgeFee: bridgeHubFeeResult === null || bridgeHubFeeResult === void 0 ? void 0 : bridgeHubFeeResult.fee
7034
6957
  });
7035
- case 2:
7036
- _yield$attemptDryRunF = _context.v;
7037
- fee = _yield$attemptDryRunF.fee;
7038
- if (!(fee === undefined)) {
7039
- _context.n = 3;
7040
- break;
7041
- }
7042
- throw new InvalidParameterError("Cannot get origin xcm fee for currency ".concat(JSON.stringify(currency, sdkCommon.replaceBigInt), " on chain ").concat(chain, "."));
7043
- case 3:
7044
- feeToSubtract = fee;
7045
- case 4:
7046
- transferable = balance - ed - feeToSubtract;
7047
- return _context.a(2, transferable > 0n ? transferable : 0n);
7048
- }
7049
- }, _callee);
7050
- }));
7051
- return function getTransferableAmountInternal(_x) {
7052
- return _ref2.apply(this, arguments);
7053
- };
7054
- }();
7055
- var getTransferableAmount = /*#__PURE__*/function () {
7056
- var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
7057
- var api;
7058
- return _regenerator().w(function (_context2) {
7059
- while (1) switch (_context2.p = _context2.n) {
7060
- case 0:
7061
- api = options.api;
7062
- api.setDisconnectAllowed(false);
7063
- _context2.p = 1;
7064
- _context2.n = 2;
7065
- return getTransferableAmountInternal(options);
7066
- case 2:
7067
- return _context2.a(2, _context2.v);
7068
- case 3:
7069
- _context2.p = 3;
6958
+ case 16:
6959
+ destinationInfo = _context2.v;
6960
+ return _context2.a(2, {
6961
+ chain: {
6962
+ origin: origin,
6963
+ destination: destination,
6964
+ ecosystem: assets.getRelayChainSymbol(origin)
6965
+ },
6966
+ origin: {
6967
+ selectedCurrency: {
6968
+ sufficient: originBalanceSufficient,
6969
+ balance: originBalance,
6970
+ balanceAfter: originBalanceAfter,
6971
+ currencySymbol: originAsset.symbol,
6972
+ existentialDeposit: edOrigin
6973
+ },
6974
+ xcmFee: {
6975
+ sufficient: originBalanceNativeSufficient,
6976
+ fee: originFee,
6977
+ balance: originBalanceFee,
6978
+ balanceAfter: originBalanceFeeAfter,
6979
+ currencySymbol: originFeeCurrency
6980
+ }
6981
+ },
6982
+ assetHub: assetHub,
6983
+ bridgeHub: bridgeHub,
6984
+ hops: builtHops,
6985
+ destination: destinationInfo
6986
+ });
6987
+ case 17:
6988
+ _context2.p = 17;
7070
6989
  api.setDisconnectAllowed(true);
7071
- _context2.n = 4;
6990
+ _context2.n = 18;
7072
6991
  return api.disconnect();
7073
- case 4:
7074
- return _context2.f(3);
7075
- case 5:
6992
+ case 18:
6993
+ return _context2.f(17);
6994
+ case 19:
7076
6995
  return _context2.a(2);
7077
6996
  }
7078
- }, _callee2, null, [[1,, 3, 5]]);
6997
+ }, _callee2, null, [[3,, 17, 19]]);
7079
6998
  }));
7080
- return function getTransferableAmount(_x2) {
7081
- return _ref3.apply(this, arguments);
6999
+ return function getTransferInfo(_x) {
7000
+ return _ref2.apply(this, arguments);
7082
7001
  };
7083
7002
  }();
7084
7003
 
7085
- var buildDestInfo = /*#__PURE__*/function () {
7004
+ var calculateTotalXcmFee = function calculateTotalXcmFee(feeResult) {
7005
+ var _feeResult$assetHub;
7006
+ var totalFee = 0n;
7007
+ if (((_feeResult$assetHub = feeResult.assetHub) === null || _feeResult$assetHub === void 0 ? void 0 : _feeResult$assetHub.fee) !== undefined) {
7008
+ totalFee += feeResult.assetHub.fee;
7009
+ }
7010
+ if (feeResult.destination.fee !== undefined) {
7011
+ totalFee += feeResult.destination.fee;
7012
+ }
7013
+ return totalFee;
7014
+ };
7015
+ var verifyEdOnDestinationInternal = /*#__PURE__*/function () {
7086
7016
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
7087
- var api, origin, destination, address, currency, originFee, isFeeAssetAh, destFeeDetail, assetHubFee, bridgeFee, destApi, destAsset, edDest, edDestBn, destCurrency, destBalance, destAmount, effectiveAmountForBalance, destBalanceSufficient, destBalanceSufficientResult, destBalanceAfter, destbalanceAfterResult, receivedAmount, isAssetHubToAssetHubRoute, nativeAssetOfOriginSymbol, isOriginAssetNative, destXcmFeeBalance, isDestFeeInNativeCurrency, destRecipientNativeBalance, destXcmFeeBalanceAfter;
7017
+ var api, tx, origin, destination, address, senderAddress, feeAsset, currency, destApi, asset, amount, destCurrency, ed, balance, xcmFeeResult, dryRunError, assetHubFeeResult, bridgeHubFeeResult, _xcmFeeResult$destina, destFee, destFeeCurrency, destDryRunError, hopDryRunError, totalFee, method, feeToSubtract;
7088
7018
  return _regenerator().w(function (_context) {
7089
7019
  while (1) switch (_context.n) {
7090
7020
  case 0:
7091
- api = _ref.api, origin = _ref.origin, destination = _ref.destination, address = _ref.address, currency = _ref.currency, originFee = _ref.originFee, isFeeAssetAh = _ref.isFeeAssetAh, destFeeDetail = _ref.destFeeDetail, assetHubFee = _ref.assetHubFee, bridgeFee = _ref.bridgeFee;
7092
- destApi = api.clone();
7093
- if (!(destination !== 'Ethereum')) {
7021
+ api = _ref.api, tx = _ref.tx, origin = _ref.origin, destination = _ref.destination, address = _ref.address, senderAddress = _ref.senderAddress, feeAsset = _ref.feeAsset, currency = _ref.currency;
7022
+ if (!(destination === 'Ethereum')) {
7094
7023
  _context.n = 1;
7095
7024
  break;
7096
7025
  }
7097
- _context.n = 1;
7098
- return destApi.init(destination);
7026
+ return _context.a(2, true);
7099
7027
  case 1:
7100
- destAsset = assets.findAssetOnDestOrThrow(origin, destination, currency);
7101
- edDest = destAsset.existentialDeposit;
7102
- if (edDest) {
7028
+ validateAddress(address, destination);
7029
+ if (!(origin === 'AssetHubPolkadot' && destination === 'AssetHubKusama')) {
7103
7030
  _context.n = 2;
7104
7031
  break;
7105
7032
  }
7106
- throw new InvalidParameterError("Existential deposit not found for ".concat(destination, " with currency ").concat(JSON.stringify(currency, sdkCommon.replaceBigInt)));
7033
+ throw new InvalidParameterError('Kusama is outside of Polkadot ecosystem, thus function is unable to verify the existential deposit for it.');
7107
7034
  case 2:
7108
- edDestBn = BigInt(edDest);
7109
- destCurrency = destAsset.location ? {
7110
- location: destAsset.location
7035
+ if (!(origin === 'AssetHubKusama' && destination === 'AssetHubPolkadot')) {
7036
+ _context.n = 3;
7037
+ break;
7038
+ }
7039
+ throw new InvalidParameterError('Polkadot is outside of Kusama ecosystem, thus function is unable to verify the existential deposit for it.');
7040
+ case 3:
7041
+ destApi = api.clone();
7042
+ _context.n = 4;
7043
+ return destApi.init(destination);
7044
+ case 4:
7045
+ asset = assets.findAssetOnDestOrThrow(origin, destination, currency);
7046
+ amount = abstractDecimals(currency.amount, asset.decimals, api);
7047
+ destCurrency = asset.location ? {
7048
+ location: asset.location
7111
7049
  } : {
7112
- symbol: destAsset.symbol
7050
+ symbol: asset.symbol
7113
7051
  };
7114
- _context.n = 3;
7052
+ ed = assets.getExistentialDepositOrThrow(destination, destCurrency);
7053
+ _context.n = 5;
7115
7054
  return getAssetBalanceInternal({
7116
- api: destApi,
7117
7055
  address: address,
7118
7056
  chain: destination,
7057
+ api: destApi,
7119
7058
  currency: destCurrency
7120
7059
  });
7121
- case 3:
7122
- destBalance = _context.v;
7123
- destAmount = isFeeAssetAh ? BigInt(currency.amount) - originFee : BigInt(currency.amount);
7124
- effectiveAmountForBalance = destAmount;
7125
- if (destination === 'Ethereum' && assetHubFee !== undefined) {
7126
- effectiveAmountForBalance -= assetHubFee;
7060
+ case 5:
7061
+ balance = _context.v;
7062
+ _context.n = 6;
7063
+ return getXcmFee({
7064
+ api: api,
7065
+ tx: tx,
7066
+ origin: origin,
7067
+ destination: destination,
7068
+ senderAddress: senderAddress,
7069
+ address: address,
7070
+ currency: _objectSpread2(_objectSpread2({}, currency), {}, {
7071
+ amount: amount
7072
+ }),
7073
+ feeAsset: feeAsset,
7074
+ disableFallback: false
7075
+ });
7076
+ case 6:
7077
+ xcmFeeResult = _context.v;
7078
+ dryRunError = xcmFeeResult.origin.dryRunError, assetHubFeeResult = xcmFeeResult.assetHub, bridgeHubFeeResult = xcmFeeResult.bridgeHub, _xcmFeeResult$destina = xcmFeeResult.destination, destFee = _xcmFeeResult$destina.fee, destFeeCurrency = _xcmFeeResult$destina.currency, destDryRunError = _xcmFeeResult$destina.dryRunError;
7079
+ if (!(destFee === undefined)) {
7080
+ _context.n = 7;
7081
+ break;
7127
7082
  }
7128
- destBalanceSufficient = effectiveAmountForBalance - destFeeDetail.fee > (destBalance < edDestBn ? edDestBn : 0);
7129
- destBalanceSufficientResult = destFeeDetail.currency !== destAsset.symbol && destination !== 'Ethereum' ? new UnableToComputeError('Unable to compute if dest balance will be sufficient. Fee currency is not the same') : destBalanceSufficient;
7130
- destBalanceAfter = destBalance - (destFeeDetail.currency === destAsset.symbol ? destFeeDetail.fee : 0n) + effectiveAmountForBalance;
7131
- destbalanceAfterResult = destFeeDetail.currency !== destAsset.symbol && destination !== 'Ethereum' ? new UnableToComputeError('Unable to compute if dest balance will be sufficient. Fee currency is not the same') : destBalanceAfter;
7132
- isAssetHubToAssetHubRoute = origin === 'AssetHubKusama' && destination === 'AssetHubPolkadot' || origin === 'AssetHubPolkadot' && destination === 'AssetHubKusama';
7133
- if (isAssetHubToAssetHubRoute) {
7134
- nativeAssetOfOriginSymbol = assets.getNativeAssetSymbol(origin);
7135
- isOriginAssetNative = false;
7136
- if (destAsset.symbol === nativeAssetOfOriginSymbol) {
7137
- isOriginAssetNative = true;
7138
- }
7139
- if (isOriginAssetNative) {
7140
- if (bridgeFee === undefined) {
7141
- receivedAmount = new UnableToComputeError("bridgeFee is required for native asset transfer from ".concat(origin, " to ").concat(destination, " but was not provided."));
7142
- } else {
7143
- receivedAmount = BigInt(currency.amount) - originFee - bridgeFee;
7144
- }
7145
- } else {
7146
- receivedAmount = new UnableToComputeError("Unable to compute received amount: The transferred asset (".concat(destAsset.symbol, ") is not the native asset (").concat(nativeAssetOfOriginSymbol, ") of origin ").concat(origin, " for the ").concat(origin, "->").concat(destination, " route."));
7147
- }
7148
- } else {
7149
- if (destbalanceAfterResult instanceof UnableToComputeError) {
7150
- receivedAmount = destbalanceAfterResult;
7151
- } else {
7152
- receivedAmount = destbalanceAfterResult - destBalance;
7153
- }
7083
+ throw new InvalidParameterError("Cannot get destination xcm fee for currency ".concat(JSON.stringify(currency, sdkCommon.replaceBigInt), " on chain ").concat(destination, "."));
7084
+ case 7:
7085
+ if (!dryRunError) {
7086
+ _context.n = 8;
7087
+ break;
7154
7088
  }
7155
- isDestFeeInNativeCurrency = destFeeDetail.currency === assets.getNativeAssetSymbol(destination);
7156
- if (!isDestFeeInNativeCurrency) {
7157
- _context.n = 5;
7089
+ throw new DryRunFailedError(dryRunError, 'origin');
7090
+ case 8:
7091
+ hopDryRunError = (assetHubFeeResult === null || assetHubFeeResult === void 0 ? void 0 : assetHubFeeResult.dryRunError) || (bridgeHubFeeResult === null || bridgeHubFeeResult === void 0 ? void 0 : bridgeHubFeeResult.dryRunError);
7092
+ if (!hopDryRunError) {
7093
+ _context.n = 9;
7158
7094
  break;
7159
7095
  }
7160
- _context.n = 4;
7161
- return getBalanceNativeInternal({
7162
- address: address,
7163
- chain: destination,
7164
- api: destApi
7165
- });
7166
- case 4:
7167
- destRecipientNativeBalance = _context.v;
7168
- destXcmFeeBalance = destRecipientNativeBalance;
7169
- _context.n = 6;
7170
- break;
7171
- case 5:
7172
- destXcmFeeBalance = destBalance;
7173
- case 6:
7174
- destXcmFeeBalanceAfter = isFeeAssetAh ? destBalanceAfter : destXcmFeeBalance - destFeeDetail.fee + (destFeeDetail.currency === destAsset.symbol ? effectiveAmountForBalance : 0n);
7175
- return _context.a(2, {
7176
- receivedCurrency: {
7177
- sufficient: destBalanceSufficientResult,
7178
- receivedAmount: receivedAmount,
7179
- balance: destBalance,
7180
- balanceAfter: destbalanceAfterResult,
7181
- currencySymbol: destAsset.symbol,
7182
- existentialDeposit: edDestBn
7183
- },
7184
- xcmFee: {
7185
- fee: destFeeDetail.fee,
7186
- balance: destXcmFeeBalance,
7187
- balanceAfter: destXcmFeeBalanceAfter,
7188
- currencySymbol: destFeeDetail.currency
7189
- }
7190
- });
7096
+ throw new DryRunFailedError(hopDryRunError, assetHubFeeResult !== null && assetHubFeeResult !== void 0 && assetHubFeeResult.dryRunError ? 'assetHub' : 'bridgeHub');
7097
+ case 9:
7098
+ if (!destDryRunError) {
7099
+ _context.n = 10;
7100
+ break;
7101
+ }
7102
+ throw new UnableToComputeError("Unable to compute fee for the destination asset. Destination dry run error: ".concat(destDryRunError));
7103
+ case 10:
7104
+ if (!(assets.normalizeSymbol(asset.symbol) !== assets.normalizeSymbol(destFeeCurrency))) {
7105
+ _context.n = 11;
7106
+ break;
7107
+ }
7108
+ throw new UnableToComputeError("The XCM fee could not be calculated because the origin or destination chain does not support DryRun.\n As a result, fee estimation is only available through PaymentInfo, which provides the cost in the native asset.\n This limitation restricts support to transfers involving the native asset of the Destination chain only.");
7109
+ case 11:
7110
+ totalFee = calculateTotalXcmFee(xcmFeeResult);
7111
+ method = api.getMethod(tx);
7112
+ if (method === 'transfer_assets_using_type_and_then' || method === 'transferAssetsUsingTypeAndThen') {
7113
+ feeToSubtract = totalFee;
7114
+ } else {
7115
+ feeToSubtract = destFee;
7116
+ }
7117
+ return _context.a(2, amount - feeToSubtract > (balance < ed ? ed : 0));
7191
7118
  }
7192
7119
  }, _callee);
7193
7120
  }));
7194
- return function buildDestInfo(_x) {
7121
+ return function verifyEdOnDestinationInternal(_x) {
7195
7122
  return _ref2.apply(this, arguments);
7196
7123
  };
7197
7124
  }();
7198
7125
 
7199
- var buildHopInfo = /*#__PURE__*/function () {
7200
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
7201
- var api, chain, feeData, originChain, currency, senderAddress, ahAddress, hopApi, resolvedAddress, nativeBalanceOnHop, nativeAssetSymbolOnHop, xcmFeeDetails, isBridgeHub, hopAsset, hopCurrencyPayload, balance, ed;
7126
+ var verifyEdOnDestination = /*#__PURE__*/function () {
7127
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
7128
+ var api;
7202
7129
  return _regenerator().w(function (_context) {
7203
7130
  while (1) switch (_context.p = _context.n) {
7204
7131
  case 0:
7205
- api = _ref.api, chain = _ref.chain, feeData = _ref.feeData, originChain = _ref.originChain, currency = _ref.currency, senderAddress = _ref.senderAddress, ahAddress = _ref.ahAddress;
7206
- hopApi = api.clone();
7207
- _context.n = 1;
7208
- return hopApi.init(chain);
7209
- case 1:
7210
- hopApi.setDisconnectAllowed(false);
7211
- _context.p = 2;
7212
- resolvedAddress = assets.isChainEvm(originChain) && ahAddress ? ahAddress : senderAddress;
7213
- _context.n = 3;
7214
- return getBalanceNativeInternal({
7215
- api: hopApi,
7216
- address: resolvedAddress,
7217
- chain: chain
7218
- });
7132
+ api = options.api;
7133
+ _context.p = 1;
7134
+ _context.n = 2;
7135
+ return verifyEdOnDestinationInternal(options);
7136
+ case 2:
7137
+ return _context.a(2, _context.v);
7219
7138
  case 3:
7220
- nativeBalanceOnHop = _context.v;
7221
- nativeAssetSymbolOnHop = assets.getNativeAssetSymbol(chain);
7222
- xcmFeeDetails = {
7223
- fee: feeData.fee,
7224
- balance: nativeBalanceOnHop,
7225
- currencySymbol: nativeAssetSymbolOnHop
7226
- };
7227
- isBridgeHub = chain.includes('BridgeHub');
7228
- if (!isBridgeHub) {
7229
- _context.n = 4;
7230
- break;
7231
- }
7232
- return _context.a(2, {
7233
- currencySymbol: assets.getNativeAssetSymbol(chain),
7234
- xcmFee: xcmFeeDetails
7235
- });
7139
+ _context.p = 3;
7140
+ _context.n = 4;
7141
+ return api.disconnect();
7236
7142
  case 4:
7237
- hopAsset = assets.findAssetOnDestOrThrow(originChain, chain, currency);
7238
- hopCurrencyPayload = hopAsset.location ? {
7239
- location: hopAsset.location
7240
- } : {
7241
- symbol: hopAsset.symbol
7242
- };
7243
- _context.n = 5;
7244
- return getAssetBalanceInternal({
7245
- api: hopApi,
7246
- address: resolvedAddress,
7247
- chain: chain,
7248
- currency: hopCurrencyPayload
7249
- });
7143
+ return _context.f(3);
7250
7144
  case 5:
7251
- balance = _context.v;
7252
- ed = assets.getExistentialDeposit(chain, hopCurrencyPayload);
7253
- if (ed) {
7254
- _context.n = 6;
7255
- break;
7256
- }
7257
- throw new InvalidParameterError("Existential deposit not found for chain ".concat(chain, " with currency ").concat(JSON.stringify(hopCurrencyPayload)));
7258
- case 6:
7259
- return _context.a(2, {
7260
- balance: balance,
7261
- currencySymbol: hopAsset.symbol,
7262
- existentialDeposit: BigInt(ed),
7263
- xcmFee: xcmFeeDetails
7264
- });
7265
- case 7:
7266
- _context.p = 7;
7267
- hopApi.setDisconnectAllowed(true);
7268
- _context.n = 8;
7269
- return hopApi.disconnect();
7270
- case 8:
7271
- return _context.f(7);
7272
- case 9:
7273
7145
  return _context.a(2);
7274
7146
  }
7275
- }, _callee, null, [[2,, 7, 9]]);
7147
+ }, _callee, null, [[1,, 3, 5]]);
7276
7148
  }));
7277
- return function buildHopInfo(_x) {
7278
- return _ref2.apply(this, arguments);
7279
- };
7280
- }();
7281
-
7282
- var getTransferInfo = /*#__PURE__*/function () {
7283
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(_ref) {
7284
- var api, tx, origin, destination, senderAddress, ahAddress, address, currency, feeAsset, resolvedFeeAsset, originAsset, amount, originBalanceFee, originBalance, edOrigin, _yield$getXcmFee, _yield$getXcmFee$orig, originFee, originFeeCurrency, assetHubFeeResult, bridgeHubFeeResult, destFeeDetail, hops, isFeeAssetAh, originBalanceAfter, originBalanceFeeAfter, originBalanceNativeSufficient, originBalanceSufficient, assetHub, bridgeHub, bridgeHubChain, builtHops, destinationInfo, _t;
7285
- return _regenerator().w(function (_context2) {
7286
- while (1) switch (_context2.p = _context2.n) {
7149
+ return function verifyEdOnDestination(_x) {
7150
+ return _ref.apply(this, arguments);
7151
+ };
7152
+ }();
7153
+
7154
+ var isSufficientOrigin = /*#__PURE__*/function () {
7155
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(api, origin, destination, senderAddress, feeNative, currency, asset, feeAsset) {
7156
+ var edNative, balanceNative, isNativeAssetToOrigin, isNativeAssetToDest, isSufficientNative, balanceAsset, edAsset, isSufficientAsset;
7157
+ return _regenerator().w(function (_context) {
7158
+ while (1) switch (_context.n) {
7287
7159
  case 0:
7288
- api = _ref.api, tx = _ref.tx, origin = _ref.origin, destination = _ref.destination, senderAddress = _ref.senderAddress, ahAddress = _ref.ahAddress, address = _ref.address, currency = _ref.currency, feeAsset = _ref.feeAsset;
7289
- if (!(assets.isChainEvm(origin) && !ahAddress)) {
7290
- _context2.n = 1;
7160
+ if (!feeAsset) {
7161
+ _context.n = 1;
7291
7162
  break;
7292
7163
  }
7293
- throw new InvalidParameterError("ahAddress is required for EVM origin ".concat(origin, "."));
7164
+ return _context.a(2, undefined);
7294
7165
  case 1:
7295
- resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, origin, destination, currency) : undefined;
7296
- _context2.n = 2;
7297
- return api.init(origin);
7298
- case 2:
7299
- api.setDisconnectAllowed(false);
7300
- _context2.p = 3;
7301
- originAsset = assets.findAssetInfoOrThrow(origin, currency, destination);
7302
- amount = abstractDecimals(currency.amount, originAsset.decimals, api);
7303
- if (!(feeAsset && resolvedFeeAsset)) {
7304
- _context2.n = 5;
7305
- break;
7306
- }
7307
- _context2.n = 4;
7308
- return getAssetBalanceInternal({
7309
- api: api,
7310
- address: senderAddress,
7311
- chain: origin,
7312
- currency: feeAsset
7313
- });
7314
- case 4:
7315
- _t = _context2.v;
7316
- _context2.n = 7;
7317
- break;
7318
- case 5:
7319
- _context2.n = 6;
7166
+ edNative = assets.getExistentialDepositOrThrow(origin);
7167
+ _context.n = 2;
7320
7168
  return getBalanceNativeInternal({
7321
7169
  api: api,
7322
- address: senderAddress,
7323
- chain: origin
7324
- });
7325
- case 6:
7326
- _t = _context2.v;
7327
- case 7:
7328
- originBalanceFee = _t;
7329
- _context2.n = 8;
7330
- return getAssetBalanceInternal({
7331
- api: api,
7332
- address: senderAddress,
7333
7170
  chain: origin,
7334
- currency: currency
7335
- });
7336
- case 8:
7337
- originBalance = _context2.v;
7338
- edOrigin = assets.getExistentialDepositOrThrow(origin, currency);
7339
- _context2.n = 9;
7340
- return getXcmFee({
7341
- api: api,
7342
- tx: tx,
7343
- origin: origin,
7344
- destination: destination,
7345
- senderAddress: senderAddress,
7346
- address: address,
7347
- currency: currency,
7348
- feeAsset: feeAsset,
7349
- disableFallback: false
7171
+ address: senderAddress
7350
7172
  });
7351
- case 9:
7352
- _yield$getXcmFee = _context2.v;
7353
- _yield$getXcmFee$orig = _yield$getXcmFee.origin;
7354
- originFee = _yield$getXcmFee$orig.fee;
7355
- originFeeCurrency = _yield$getXcmFee$orig.currency;
7356
- assetHubFeeResult = _yield$getXcmFee.assetHub;
7357
- bridgeHubFeeResult = _yield$getXcmFee.bridgeHub;
7358
- destFeeDetail = _yield$getXcmFee.destination;
7359
- hops = _yield$getXcmFee.hops;
7360
- if (!(originFee === undefined)) {
7361
- _context2.n = 10;
7173
+ case 2:
7174
+ balanceNative = _context.v;
7175
+ isNativeAssetToOrigin = assets.isSymbolMatch(asset.symbol, assets.getNativeAssetSymbol(origin));
7176
+ isNativeAssetToDest = assets.isSymbolMatch(asset.symbol, assets.getNativeAssetSymbol(destination));
7177
+ if (!(isNativeAssetToOrigin && isNativeAssetToDest)) {
7178
+ _context.n = 3;
7362
7179
  break;
7363
7180
  }
7364
- throw new InvalidParameterError("Cannot get origin xcm fee for currency ".concat(JSON.stringify(currency, sdkCommon.replaceBigInt), " on chain ").concat(origin, "."));
7365
- case 10:
7366
- isFeeAssetAh = origin === 'AssetHubPolkadot' && resolvedFeeAsset && assets.isAssetEqual(resolvedFeeAsset, originAsset);
7367
- originBalanceAfter = originBalance - amount;
7368
- originBalanceFeeAfter = isFeeAssetAh ? originBalanceFee - amount : originBalanceFee - originFee;
7369
- originBalanceNativeSufficient = originBalanceFee >= originFee;
7370
- originBalanceSufficient = originBalanceAfter >= edOrigin;
7371
- if (!assetHubFeeResult) {
7372
- _context2.n = 12;
7181
+ return _context.a(2, balanceNative - edNative - feeNative - BigInt(currency.amount) > 0n);
7182
+ case 3:
7183
+ if (isNativeAssetToOrigin) {
7184
+ _context.n = 5;
7373
7185
  break;
7374
7186
  }
7375
- _context2.n = 11;
7376
- return buildHopInfo({
7187
+ isSufficientNative = balanceNative - edNative - feeNative > 0n;
7188
+ _context.n = 4;
7189
+ return getAssetBalance({
7377
7190
  api: api,
7378
- chain: "AssetHub".concat(getRelayChainOf(origin)),
7379
- feeData: assetHubFeeResult,
7380
- originChain: origin,
7381
- currency: currency,
7382
- senderAddress: senderAddress,
7383
- ahAddress: ahAddress
7191
+ chain: origin,
7192
+ address: senderAddress,
7193
+ currency: currency
7384
7194
  });
7385
- case 11:
7386
- assetHub = _context2.v;
7387
- case 12:
7388
- if (!bridgeHubFeeResult) {
7389
- _context2.n = 14;
7195
+ case 4:
7196
+ balanceAsset = _context.v;
7197
+ edAsset = assets.getExistentialDepositOrThrow(origin, currency);
7198
+ isSufficientAsset = balanceAsset - edAsset > 0n;
7199
+ return _context.a(2, isSufficientNative && isSufficientAsset);
7200
+ case 5:
7201
+ return _context.a(2, balanceNative - edNative - feeNative - BigInt(currency.amount) > 0n);
7202
+ case 6:
7203
+ return _context.a(2);
7204
+ }
7205
+ }, _callee);
7206
+ }));
7207
+ return function isSufficientOrigin(_x, _x2, _x3, _x4, _x5, _x6, _x7, _x8) {
7208
+ return _ref.apply(this, arguments);
7209
+ };
7210
+ }();
7211
+ var isSufficientDestination = /*#__PURE__*/function () {
7212
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(api, destination, address, amount, asset, feeNative) {
7213
+ var isNativeAsset, existentialDeposit, nativeBalance;
7214
+ return _regenerator().w(function (_context2) {
7215
+ while (1) switch (_context2.n) {
7216
+ case 0:
7217
+ isNativeAsset = assets.isSymbolMatch(asset.symbol, assets.getNativeAssetSymbol(destination));
7218
+ if (isNativeAsset) {
7219
+ _context2.n = 1;
7390
7220
  break;
7391
7221
  }
7392
- bridgeHubChain = "BridgeHub".concat(getRelayChainOf(origin));
7393
- _context2.n = 13;
7394
- return buildHopInfo({
7222
+ return _context2.a(2, undefined);
7223
+ case 1:
7224
+ existentialDeposit = assets.getExistentialDepositOrThrow(destination);
7225
+ _context2.n = 2;
7226
+ return getBalanceNativeInternal({
7395
7227
  api: api,
7396
- chain: bridgeHubChain,
7397
- feeData: bridgeHubFeeResult,
7398
- originChain: origin,
7399
- currency: currency,
7400
- senderAddress: senderAddress,
7401
- ahAddress: ahAddress
7228
+ chain: destination,
7229
+ address: address
7402
7230
  });
7403
- case 13:
7404
- bridgeHub = _context2.v;
7405
- case 14:
7406
- builtHops = [];
7407
- if (!(hops && hops.length > 0)) {
7408
- _context2.n = 16;
7409
- break;
7410
- }
7411
- _context2.n = 15;
7412
- return Promise.all(hops.map(/*#__PURE__*/function () {
7413
- var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(hop) {
7414
- var result;
7231
+ case 2:
7232
+ nativeBalance = _context2.v;
7233
+ return _context2.a(2, nativeBalance + amount - existentialDeposit - feeNative > 0n);
7234
+ }
7235
+ }, _callee2);
7236
+ }));
7237
+ return function isSufficientDestination(_x9, _x0, _x1, _x10, _x11, _x12) {
7238
+ return _ref2.apply(this, arguments);
7239
+ };
7240
+ }();
7241
+
7242
+ var createOriginLocation = function createOriginLocation(origin, destination) {
7243
+ if (sdkCommon.isRelayChain(origin)) return DOT_LOCATION;
7244
+ return {
7245
+ parents: sdkCommon.isRelayChain(destination) ? sdkCommon.Parents.ZERO : sdkCommon.Parents.ONE,
7246
+ interior: {
7247
+ X1: [{
7248
+ Parachain: getParaId(origin)
7249
+ }]
7250
+ }
7251
+ };
7252
+ };
7253
+ var getDestXcmFee = /*#__PURE__*/function () {
7254
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
7255
+ var api, origin, hopChain, destination, currency, forwardedXcms, asset, address, feeAsset, originFee, disableFallback, resolvedFeeAsset, calcPaymentInfoFee, _fee, sufficient, dryRunResult, _fee2, fee, newForwardedXcms, destParaId;
7256
+ return _regenerator().w(function (_context2) {
7257
+ while (1) switch (_context2.n) {
7258
+ case 0:
7259
+ api = options.api, origin = options.origin, hopChain = options.prevChain, destination = options.destination, currency = options.currency, forwardedXcms = options.forwardedXcms, asset = options.asset, address = options.address, feeAsset = options.feeAsset, originFee = options.originFee, disableFallback = options.disableFallback;
7260
+ resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, origin, destination, currency) : undefined;
7261
+ calcPaymentInfoFee = /*#__PURE__*/function () {
7262
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
7263
+ var originAsset, _t;
7415
7264
  return _regenerator().w(function (_context) {
7416
- while (1) switch (_context.n) {
7265
+ while (1) switch (_context.p = _context.n) {
7417
7266
  case 0:
7418
- _context.n = 1;
7419
- return buildHopInfo({
7420
- api: api,
7421
- chain: hop.chain,
7422
- feeData: hop.result,
7423
- originChain: origin,
7424
- currency: currency,
7425
- senderAddress: senderAddress,
7426
- ahAddress: ahAddress
7427
- });
7267
+ if (!(destination === 'Ethereum')) {
7268
+ _context.n = 1;
7269
+ break;
7270
+ }
7271
+ return _context.a(2, 0n);
7428
7272
  case 1:
7429
- result = _context.v;
7430
- return _context.a(2, {
7431
- chain: hop.chain,
7432
- result: result
7273
+ originAsset = assets.findAssetInfoOrThrow(origin, currency, destination);
7274
+ if (!originAsset.location) {
7275
+ _context.n = 7;
7276
+ break;
7277
+ }
7278
+ _context.p = 2;
7279
+ _context.n = 3;
7280
+ return getReverseTxFee(_objectSpread2(_objectSpread2({}, options), {}, {
7281
+ destination: destination
7282
+ }), {
7283
+ location: originAsset.location
7284
+ });
7285
+ case 3:
7286
+ return _context.a(2, _context.v);
7287
+ case 4:
7288
+ _context.p = 4;
7289
+ _t = _context.v;
7290
+ if (!(_t instanceof assets.InvalidCurrencyError)) {
7291
+ _context.n = 6;
7292
+ break;
7293
+ }
7294
+ _context.n = 5;
7295
+ return getReverseTxFee(_objectSpread2(_objectSpread2({}, options), {}, {
7296
+ destination: destination
7297
+ }), {
7298
+ symbol: originAsset.symbol
7299
+ });
7300
+ case 5:
7301
+ return _context.a(2, _context.v);
7302
+ case 6:
7303
+ throw _t;
7304
+ case 7:
7305
+ _context.n = 8;
7306
+ return getReverseTxFee(_objectSpread2(_objectSpread2({}, options), {}, {
7307
+ destination: destination
7308
+ }), {
7309
+ symbol: originAsset.symbol
7433
7310
  });
7311
+ case 8:
7312
+ return _context.a(2, _context.v);
7434
7313
  }
7435
- }, _callee);
7314
+ }, _callee, null, [[2, 4]]);
7436
7315
  }));
7437
- return function (_x2) {
7438
- return _ref3.apply(this, arguments);
7316
+ return function calcPaymentInfoFee() {
7317
+ return _ref2.apply(this, arguments);
7439
7318
  };
7440
- }()));
7441
- case 15:
7442
- builtHops = _context2.v;
7443
- case 16:
7444
- _context2.n = 17;
7445
- return buildDestInfo({
7446
- api: api,
7319
+ }();
7320
+ if (!(!assets.hasDryRunSupport(destination) || !forwardedXcms || destination === 'Ethereum')) {
7321
+ _context2.n = 3;
7322
+ break;
7323
+ }
7324
+ _context2.n = 1;
7325
+ return calcPaymentInfoFee();
7326
+ case 1:
7327
+ _fee = _context2.v;
7328
+ _context2.n = 2;
7329
+ return isSufficientDestination(api, destination, address, BigInt(currency.amount), asset, _fee);
7330
+ case 2:
7331
+ sufficient = _context2.v;
7332
+ return _context2.a(2, {
7333
+ fee: _fee,
7334
+ feeType: 'paymentInfo',
7335
+ sufficient: !assets.hasDryRunSupport(destination) ? sufficient : false
7336
+ });
7337
+ case 3:
7338
+ _context2.n = 4;
7339
+ return api.getDryRunXcm({
7340
+ originLocation: addXcmVersionHeader(createOriginLocation(hopChain, destination), sdkCommon.Version.V4),
7341
+ xcm: forwardedXcms[1][0],
7342
+ chain: destination,
7447
7343
  origin: origin,
7448
- destination: destination,
7449
- address: address,
7450
- currency: _objectSpread2(_objectSpread2({}, currency), {}, {
7451
- amount: amount
7452
- }),
7344
+ asset: asset,
7453
7345
  originFee: originFee,
7454
- isFeeAssetAh: !!isFeeAssetAh,
7455
- destFeeDetail: destFeeDetail,
7456
- assetHubFee: assetHubFeeResult === null || assetHubFeeResult === void 0 ? void 0 : assetHubFeeResult.fee,
7457
- bridgeFee: bridgeHubFeeResult === null || bridgeHubFeeResult === void 0 ? void 0 : bridgeHubFeeResult.fee
7346
+ feeAsset: resolvedFeeAsset,
7347
+ amount: BigInt(currency.amount) < 2n ? 2n : BigInt(currency.amount)
7458
7348
  });
7459
- case 17:
7460
- destinationInfo = _context2.v;
7349
+ case 4:
7350
+ dryRunResult = _context2.v;
7351
+ if (dryRunResult.success) {
7352
+ _context2.n = 7;
7353
+ break;
7354
+ }
7355
+ if (!disableFallback) {
7356
+ _context2.n = 5;
7357
+ break;
7358
+ }
7461
7359
  return _context2.a(2, {
7462
- chain: {
7463
- origin: origin,
7464
- destination: destination,
7465
- ecosystem: assets.getRelayChainSymbol(origin)
7466
- },
7467
- origin: {
7468
- selectedCurrency: {
7469
- sufficient: originBalanceSufficient,
7470
- balance: originBalance,
7471
- balanceAfter: originBalanceAfter,
7472
- currencySymbol: originAsset.symbol,
7473
- existentialDeposit: edOrigin
7474
- },
7475
- xcmFee: {
7476
- sufficient: originBalanceNativeSufficient,
7477
- fee: originFee,
7478
- balance: originBalanceFee,
7479
- balanceAfter: originBalanceFeeAfter,
7480
- currencySymbol: originFeeCurrency
7481
- }
7482
- },
7483
- assetHub: assetHub,
7484
- bridgeHub: bridgeHub,
7485
- hops: builtHops,
7486
- destination: destinationInfo
7360
+ dryRunError: dryRunResult.failureReason
7361
+ });
7362
+ case 5:
7363
+ _context2.n = 6;
7364
+ return calcPaymentInfoFee();
7365
+ case 6:
7366
+ _fee2 = _context2.v;
7367
+ return _context2.a(2, {
7368
+ fee: _fee2,
7369
+ feeType: 'paymentInfo',
7370
+ dryRunError: dryRunResult.failureReason,
7371
+ sufficient: false
7372
+ });
7373
+ case 7:
7374
+ fee = dryRunResult.fee, newForwardedXcms = dryRunResult.forwardedXcms, destParaId = dryRunResult.destParaId;
7375
+ return _context2.a(2, {
7376
+ fee: fee,
7377
+ feeType: 'dryRun',
7378
+ sufficient: true,
7379
+ forwardedXcms: newForwardedXcms,
7380
+ destParaId: destParaId
7487
7381
  });
7488
- case 18:
7489
- _context2.p = 18;
7490
- api.setDisconnectAllowed(true);
7491
- _context2.n = 19;
7492
- return api.disconnect();
7493
- case 19:
7494
- return _context2.f(18);
7495
- case 20:
7496
- return _context2.a(2);
7497
7382
  }
7498
- }, _callee2, null, [[3,, 18, 20]]);
7383
+ }, _callee2);
7499
7384
  }));
7500
- return function getTransferInfo(_x) {
7501
- return _ref2.apply(this, arguments);
7385
+ return function getDestXcmFee(_x) {
7386
+ return _ref.apply(this, arguments);
7502
7387
  };
7503
7388
  }();
7504
7389
 
7505
- var calculateTotalXcmFee = function calculateTotalXcmFee(feeResult) {
7506
- var _feeResult$assetHub;
7507
- var totalFee = 0n;
7508
- if (((_feeResult$assetHub = feeResult.assetHub) === null || _feeResult$assetHub === void 0 ? void 0 : _feeResult$assetHub.fee) !== undefined) {
7509
- totalFee += feeResult.assetHub.fee;
7390
+ var getFailureInfo = function getFailureInfo(results, hops) {
7391
+ // Check standard chains first for backwards compatibility
7392
+ for (var _i = 0, _arr = ['destination', 'assetHub', 'bridgeHub']; _i < _arr.length; _i++) {
7393
+ var chain = _arr[_i];
7394
+ var res = results[chain];
7395
+ if (res && !res.success && res.failureReason) {
7396
+ return {
7397
+ failureReason: res.failureReason,
7398
+ failureChain: chain
7399
+ };
7400
+ }
7510
7401
  }
7511
- if (feeResult.destination.fee !== undefined) {
7512
- totalFee += feeResult.destination.fee;
7402
+ var _iterator = _createForOfIteratorHelper(hops),
7403
+ _step;
7404
+ try {
7405
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
7406
+ var hop = _step.value;
7407
+ if (!hop.result.success && hop.result.failureReason) {
7408
+ return {
7409
+ failureReason: hop.result.failureReason,
7410
+ failureChain: hop.chain
7411
+ };
7412
+ }
7413
+ }
7414
+ } catch (err) {
7415
+ _iterator.e(err);
7416
+ } finally {
7417
+ _iterator.f();
7513
7418
  }
7514
- return totalFee;
7419
+ return {};
7515
7420
  };
7516
- var verifyEdOnDestinationInternal = /*#__PURE__*/function () {
7517
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
7518
- var api, tx, origin, destination, address, senderAddress, feeAsset, currency, destApi, asset, amount, destCurrency, ed, balance, xcmFeeResult, dryRunError, assetHubFeeResult, bridgeHubFeeResult, _xcmFeeResult$destina, destFee, destFeeCurrency, destDryRunError, hopDryRunError, totalFee, method, feeToSubtract;
7519
- return _regenerator().w(function (_context) {
7520
- while (1) switch (_context.n) {
7421
+ var dryRunInternal = /*#__PURE__*/function () {
7422
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(options) {
7423
+ var _traversalResult$brid, _traversalResult$asse, _traversalResult$dest;
7424
+ var api, origin, destination, currency, tx, senderAddress, feeAsset, swapConfig, _options$useRootOrigi, useRootOrigin, resolvedFeeAsset, asset, amount, originDryRun, initialForwardedXcms, initialDestParaId, processHop, traversalResult, assetHubChain, bridgeHubChain, processedBridgeHub, bridgeHubHopIndex, originWithCurrency, assetHubWithCurrency, bridgeHubWithCurrency, destinationWithCurrency, _getFailureInfo, failureReason, failureChain, _t;
7425
+ return _regenerator().w(function (_context2) {
7426
+ while (1) switch (_context2.n) {
7521
7427
  case 0:
7522
- api = _ref.api, tx = _ref.tx, origin = _ref.origin, destination = _ref.destination, address = _ref.address, senderAddress = _ref.senderAddress, feeAsset = _ref.feeAsset, currency = _ref.currency;
7523
- if (!(destination === 'Ethereum')) {
7524
- _context.n = 1;
7525
- break;
7526
- }
7527
- return _context.a(2, true);
7428
+ api = options.api, origin = options.origin, destination = options.destination, currency = options.currency, tx = options.tx, senderAddress = options.senderAddress, feeAsset = options.feeAsset, swapConfig = options.swapConfig, _options$useRootOrigi = options.useRootOrigin, useRootOrigin = _options$useRootOrigi === void 0 ? false : _options$useRootOrigi;
7429
+ resolvedFeeAsset = feeAsset ? resolveFeeAsset(feeAsset, origin, destination, currency) : undefined;
7430
+ asset = assets.findAssetInfoOrThrow(origin, currency, destination);
7431
+ amount = abstractDecimals(currency.amount, asset.decimals, api);
7432
+ _context2.n = 1;
7433
+ return api.getDryRunCall({
7434
+ tx: tx,
7435
+ chain: origin,
7436
+ address: senderAddress,
7437
+ asset: _objectSpread2(_objectSpread2({}, asset), {}, {
7438
+ amount: amount
7439
+ }),
7440
+ feeAsset: resolvedFeeAsset,
7441
+ useRootOrigin: useRootOrigin
7442
+ });
7528
7443
  case 1:
7529
- validateAddress(address, destination);
7530
- if (!(origin === 'AssetHubPolkadot' && destination === 'AssetHubKusama')) {
7531
- _context.n = 2;
7532
- break;
7533
- }
7534
- throw new InvalidParameterError('Kusama is outside of Polkadot ecosystem, thus function is unable to verify the existential deposit for it.');
7535
- case 2:
7536
- if (!(origin === 'AssetHubKusama' && destination === 'AssetHubPolkadot')) {
7537
- _context.n = 3;
7444
+ originDryRun = _context2.v;
7445
+ if (originDryRun.success) {
7446
+ _context2.n = 2;
7538
7447
  break;
7539
7448
  }
7540
- throw new InvalidParameterError('Polkadot is outside of Kusama ecosystem, thus function is unable to verify the existential deposit for it.');
7541
- case 3:
7542
- destApi = api.clone();
7543
- _context.n = 4;
7544
- return destApi.init(destination);
7545
- case 4:
7546
- asset = assets.findAssetOnDestOrThrow(origin, destination, currency);
7547
- amount = abstractDecimals(currency.amount, asset.decimals, api);
7548
- destCurrency = asset.location ? {
7549
- location: asset.location
7550
- } : {
7551
- symbol: asset.symbol
7552
- };
7553
- ed = assets.getExistentialDepositOrThrow(destination, destCurrency);
7554
- _context.n = 5;
7555
- return getAssetBalanceInternal({
7556
- address: address,
7557
- chain: destination,
7558
- api: destApi,
7559
- currency: destCurrency
7449
+ return _context2.a(2, {
7450
+ failureReason: originDryRun.failureReason,
7451
+ failureChain: 'origin',
7452
+ origin: originDryRun,
7453
+ hops: []
7560
7454
  });
7561
- case 5:
7562
- balance = _context.v;
7563
- _context.n = 6;
7564
- return getXcmFee({
7455
+ case 2:
7456
+ initialForwardedXcms = originDryRun.forwardedXcms, initialDestParaId = originDryRun.destParaId;
7457
+ processHop = /*#__PURE__*/function () {
7458
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(params) {
7459
+ var hopApi, currentChain, currentOrigin, currentAsset, forwardedXcms, hasPassedExchange, isDestination, hopDryRun, hopCurrency;
7460
+ return _regenerator().w(function (_context) {
7461
+ while (1) switch (_context.n) {
7462
+ case 0:
7463
+ hopApi = params.api, currentChain = params.currentChain, currentOrigin = params.currentOrigin, currentAsset = params.currentAsset, forwardedXcms = params.forwardedXcms, hasPassedExchange = params.hasPassedExchange, isDestination = params.isDestination;
7464
+ if (assets.hasDryRunSupport(currentChain)) {
7465
+ _context.n = 1;
7466
+ break;
7467
+ }
7468
+ return _context.a(2, {
7469
+ success: false,
7470
+ failureReason: "DryRunApi is not available on chain ".concat(currentChain)
7471
+ });
7472
+ case 1:
7473
+ _context.n = 2;
7474
+ return hopApi.getDryRunXcm({
7475
+ originLocation: addXcmVersionHeader(createOriginLocation(currentOrigin, currentChain), sdkCommon.Version.V4),
7476
+ xcm: forwardedXcms[1][0],
7477
+ chain: currentChain,
7478
+ origin: currentOrigin,
7479
+ asset: currentAsset,
7480
+ feeAsset: resolvedFeeAsset,
7481
+ originFee: originDryRun.fee,
7482
+ amount: amount
7483
+ });
7484
+ case 2:
7485
+ hopDryRun = _context.v;
7486
+ if (!hopDryRun.success) {
7487
+ _context.n = 3;
7488
+ break;
7489
+ }
7490
+ if (destination === 'Ethereum' && (currentChain.includes('AssetHub') || currentChain.includes('BridgeHub'))) {
7491
+ hopCurrency = assets.getNativeAssetSymbol(currentChain);
7492
+ } else if (hasPassedExchange && swapConfig && currentChain !== swapConfig.exchangeChain) {
7493
+ hopCurrency = assets.findAssetOnDestOrThrow(swapConfig.exchangeChain, currentChain, swapConfig.currencyTo).symbol;
7494
+ } else if (isDestination) {
7495
+ hopCurrency = assets.findAssetOnDestOrThrow(origin, currentChain, currency).symbol;
7496
+ } else {
7497
+ hopCurrency = asset.symbol;
7498
+ }
7499
+ return _context.a(2, _objectSpread2(_objectSpread2({}, hopDryRun), {}, {
7500
+ currency: hopCurrency
7501
+ }));
7502
+ case 3:
7503
+ return _context.a(2, hopDryRun);
7504
+ }
7505
+ }, _callee);
7506
+ }));
7507
+ return function processHop(_x2) {
7508
+ return _ref2.apply(this, arguments);
7509
+ };
7510
+ }();
7511
+ _context2.n = 3;
7512
+ return traverseXcmHops({
7565
7513
  api: api,
7566
- tx: tx,
7567
7514
  origin: origin,
7568
7515
  destination: destination,
7569
- senderAddress: senderAddress,
7570
- address: address,
7571
- currency: _objectSpread2(_objectSpread2({}, currency), {}, {
7572
- amount: amount
7573
- }),
7574
- feeAsset: feeAsset,
7575
- disableFallback: false
7516
+ currency: currency,
7517
+ initialForwardedXcms: initialForwardedXcms,
7518
+ initialDestParaId: initialDestParaId,
7519
+ swapConfig: swapConfig,
7520
+ processHop: processHop,
7521
+ shouldContinue: function shouldContinue(hopResult) {
7522
+ return hopResult.success;
7523
+ },
7524
+ extractNextHopData: function extractNextHopData(hopResult) {
7525
+ return {
7526
+ forwardedXcms: hopResult.success ? hopResult.forwardedXcms : undefined,
7527
+ destParaId: hopResult.success ? hopResult.destParaId : undefined
7528
+ };
7529
+ }
7576
7530
  });
7577
- case 6:
7578
- xcmFeeResult = _context.v;
7579
- dryRunError = xcmFeeResult.origin.dryRunError, assetHubFeeResult = xcmFeeResult.assetHub, bridgeHubFeeResult = xcmFeeResult.bridgeHub, _xcmFeeResult$destina = xcmFeeResult.destination, destFee = _xcmFeeResult$destina.fee, destFeeCurrency = _xcmFeeResult$destina.currency, destDryRunError = _xcmFeeResult$destina.dryRunError;
7580
- if (!(destFee === undefined)) {
7581
- _context.n = 7;
7582
- break;
7583
- }
7584
- throw new InvalidParameterError("Cannot get destination xcm fee for currency ".concat(JSON.stringify(currency, sdkCommon.replaceBigInt), " on chain ").concat(destination, "."));
7585
- case 7:
7586
- if (!dryRunError) {
7587
- _context.n = 8;
7588
- break;
7589
- }
7590
- throw new DryRunFailedError(dryRunError, 'origin');
7591
- case 8:
7592
- hopDryRunError = (assetHubFeeResult === null || assetHubFeeResult === void 0 ? void 0 : assetHubFeeResult.dryRunError) || (bridgeHubFeeResult === null || bridgeHubFeeResult === void 0 ? void 0 : bridgeHubFeeResult.dryRunError);
7593
- if (!hopDryRunError) {
7594
- _context.n = 9;
7595
- break;
7596
- }
7597
- throw new DryRunFailedError(hopDryRunError, assetHubFeeResult !== null && assetHubFeeResult !== void 0 && assetHubFeeResult.dryRunError ? 'assetHub' : 'bridgeHub');
7598
- case 9:
7599
- if (!destDryRunError) {
7600
- _context.n = 10;
7601
- break;
7602
- }
7603
- throw new UnableToComputeError("Unable to compute fee for the destination asset. Destination dry run error: ".concat(destDryRunError));
7604
- case 10:
7605
- if (!(assets.normalizeSymbol(asset.symbol) !== assets.normalizeSymbol(destFeeCurrency))) {
7606
- _context.n = 11;
7531
+ case 3:
7532
+ traversalResult = _context2.v;
7533
+ // Process Ethereum bridge fees
7534
+ assetHubChain = "AssetHub".concat(getRelayChainOf(origin));
7535
+ bridgeHubChain = "BridgeHub".concat(getRelayChainOf(origin));
7536
+ if (!((_traversalResult$brid = traversalResult.bridgeHub) !== null && _traversalResult$brid !== void 0 && _traversalResult$brid.success)) {
7537
+ _context2.n = 5;
7607
7538
  break;
7608
7539
  }
7609
- throw new UnableToComputeError("The XCM fee could not be calculated because the origin or destination chain does not support DryRun.\n As a result, fee estimation is only available through PaymentInfo, which provides the cost in the native asset.\n This limitation restricts support to transfers involving the native asset of the Destination chain only.");
7610
- case 11:
7611
- totalFee = calculateTotalXcmFee(xcmFeeResult);
7612
- method = api.getMethod(tx);
7613
- if (method === 'transfer_assets_using_type_and_then' || method === 'transferAssetsUsingTypeAndThen') {
7614
- feeToSubtract = totalFee;
7615
- } else {
7616
- feeToSubtract = destFee;
7617
- }
7618
- return _context.a(2, amount - feeToSubtract > (balance < ed ? ed : 0));
7619
- }
7620
- }, _callee);
7621
- }));
7622
- return function verifyEdOnDestinationInternal(_x) {
7623
- return _ref2.apply(this, arguments);
7624
- };
7625
- }();
7626
-
7627
- var verifyEdOnDestination = /*#__PURE__*/function () {
7628
- var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(options) {
7629
- var api;
7630
- return _regenerator().w(function (_context) {
7631
- while (1) switch (_context.p = _context.n) {
7632
- case 0:
7633
- api = options.api;
7634
- _context.p = 1;
7635
- _context.n = 2;
7636
- return verifyEdOnDestinationInternal(options);
7637
- case 2:
7638
- return _context.a(2, _context.v);
7639
- case 3:
7640
- _context.p = 3;
7641
- _context.n = 4;
7642
- return api.disconnect();
7540
+ _context2.n = 4;
7541
+ return addEthereumBridgeFees(api, traversalResult.bridgeHub, destination, assetHubChain);
7643
7542
  case 4:
7644
- return _context.f(3);
7543
+ _t = _context2.v;
7544
+ _context2.n = 6;
7545
+ break;
7645
7546
  case 5:
7646
- return _context.a(2);
7547
+ _t = traversalResult.bridgeHub;
7548
+ case 6:
7549
+ processedBridgeHub = _t;
7550
+ // Update bridge hub in hops if needed
7551
+ if (processedBridgeHub && processedBridgeHub.success && traversalResult.bridgeHub && traversalResult.bridgeHub.success && processedBridgeHub.fee !== traversalResult.bridgeHub.fee) {
7552
+ bridgeHubHopIndex = traversalResult.hops.findIndex(function (hop) {
7553
+ return hop.chain === bridgeHubChain;
7554
+ });
7555
+ if (bridgeHubHopIndex !== -1 && traversalResult.hops[bridgeHubHopIndex].result.success) {
7556
+ traversalResult.hops[bridgeHubHopIndex].result = _objectSpread2(_objectSpread2({}, traversalResult.hops[bridgeHubHopIndex].result), {}, {
7557
+ fee: processedBridgeHub.fee
7558
+ });
7559
+ }
7560
+ }
7561
+ originWithCurrency = originDryRun.success ? _objectSpread2(_objectSpread2({}, originDryRun), {}, {
7562
+ currency: resolvedFeeAsset ? resolvedFeeAsset.symbol : assets.getNativeAssetSymbol(origin)
7563
+ }) : originDryRun;
7564
+ assetHubWithCurrency = (_traversalResult$asse = traversalResult.assetHub) !== null && _traversalResult$asse !== void 0 && _traversalResult$asse.success ? _objectSpread2(_objectSpread2({}, traversalResult.assetHub), {}, {
7565
+ currency: resolvedFeeAsset ? resolvedFeeAsset.symbol : assets.getNativeAssetSymbol(assetHubChain)
7566
+ }) : traversalResult.assetHub;
7567
+ bridgeHubWithCurrency = processedBridgeHub !== null && processedBridgeHub !== void 0 && processedBridgeHub.success ? _objectSpread2(_objectSpread2({}, processedBridgeHub), {}, {
7568
+ currency: assets.getNativeAssetSymbol(bridgeHubChain)
7569
+ }) : processedBridgeHub;
7570
+ destinationWithCurrency = (_traversalResult$dest = traversalResult.destination) !== null && _traversalResult$dest !== void 0 && _traversalResult$dest.success ? _objectSpread2(_objectSpread2({}, traversalResult.destination), {}, {
7571
+ currency: asset === null || asset === void 0 ? void 0 : asset.symbol
7572
+ }) : traversalResult.destination;
7573
+ _getFailureInfo = getFailureInfo({
7574
+ destination: destinationWithCurrency,
7575
+ assetHub: assetHubWithCurrency,
7576
+ bridgeHub: bridgeHubWithCurrency
7577
+ }, traversalResult.hops), failureReason = _getFailureInfo.failureReason, failureChain = _getFailureInfo.failureChain;
7578
+ return _context2.a(2, {
7579
+ failureReason: failureReason,
7580
+ failureChain: failureChain,
7581
+ origin: originWithCurrency,
7582
+ assetHub: assetHubWithCurrency,
7583
+ bridgeHub: bridgeHubWithCurrency,
7584
+ destination: destinationWithCurrency,
7585
+ hops: traversalResult.hops
7586
+ });
7647
7587
  }
7648
- }, _callee, null, [[1,, 3, 5]]);
7588
+ }, _callee2);
7649
7589
  }));
7650
- return function verifyEdOnDestination(_x) {
7590
+ return function dryRunInternal(_x) {
7651
7591
  return _ref.apply(this, arguments);
7652
7592
  };
7653
7593
  }();
7654
7594
 
7655
- var validateHops = function validateHops(hops) {
7656
- var _iterator = _createForOfIteratorHelper(hops),
7657
- _step;
7658
- try {
7659
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
7660
- var hop = _step.value;
7661
- if (!hop.result.success) {
7662
- throw new DryRunFailedError("Dry run failed on an intermediate hop (".concat(hop.chain, "). Reason: ").concat(hop.result.failureReason || 'Unknown'));
7663
- }
7664
- }
7665
- } catch (err) {
7666
- _iterator.e(err);
7667
- } finally {
7668
- _iterator.f();
7669
- }
7670
- };
7671
7595
  var getReserveFeeFromHops = function getReserveFeeFromHops(hops) {
7672
7596
  if (!hops || hops.length === 0 || !hops[0].result.success) {
7673
7597
  return MIN_FEE;
@@ -7677,7 +7601,7 @@ var getReserveFeeFromHops = function getReserveFeeFromHops(hops) {
7677
7601
  var FEE_PADDING_PERCENTAGE$1 = 40;
7678
7602
  var handleExecuteTransfer = /*#__PURE__*/function () {
7679
7603
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(chain, options) {
7680
- var api, senderAddress, paraIdTo, assetInfo, currency, feeCurrency, address, feeAssetInfo, version, feeAssetBalance, checkAmount, destChain, internalOptions, call, dryRunResult, originFeeEstimate, originFee, reserveFeeEstimate, reserveFee, xcm, weight, _t;
7604
+ var api, senderAddress, paraIdTo, assetInfo, currency, feeCurrency, address, feeAssetInfo, version, checkAmount, destChain, internalOptions, FEE_ASSET_AMOUNT, feeAssetAmount, call, dryRunResult, originFeeEstimate, originFee, reserveFeeEstimate, reserveFee, xcm, weight;
7681
7605
  return _regenerator().w(function (_context) {
7682
7606
  while (1) switch (_context.n) {
7683
7607
  case 0:
@@ -7689,25 +7613,6 @@ var handleExecuteTransfer = /*#__PURE__*/function () {
7689
7613
  throw new InvalidParameterError('Please provide senderAddress');
7690
7614
  case 1:
7691
7615
  assertAddressIsString(address);
7692
- if (!(feeCurrency && feeAssetInfo && !assets.isAssetEqual(assetInfo, feeAssetInfo))) {
7693
- _context.n = 3;
7694
- break;
7695
- }
7696
- _context.n = 2;
7697
- return getAssetBalanceInternal({
7698
- api: api,
7699
- address: senderAddress,
7700
- chain: chain,
7701
- currency: feeCurrency
7702
- });
7703
- case 2:
7704
- _t = _context.v;
7705
- _context.n = 4;
7706
- break;
7707
- case 3:
7708
- _t = undefined;
7709
- case 4:
7710
- feeAssetBalance = _t;
7711
7616
  checkAmount = function checkAmount(fee) {
7712
7617
  if (assetInfo.amount <= fee) {
7713
7618
  throw new InvalidParameterError("Asset amount is too low, please increase the amount or use a different fee asset.");
@@ -7728,14 +7633,16 @@ var handleExecuteTransfer = /*#__PURE__*/function () {
7728
7633
  senderAddress: senderAddress,
7729
7634
  version: version,
7730
7635
  paraIdTo: paraIdTo
7731
- };
7636
+ }; // We mint 1000 units of feeAsset and use 100
7637
+ FEE_ASSET_AMOUNT = 100;
7638
+ feeAssetAmount = feeAssetInfo ? viem.parseUnits(FEE_ASSET_AMOUNT.toString(), feeAssetInfo.decimals) : MIN_FEE;
7732
7639
  call = createExecuteCall(chain, createDirectExecuteXcm(_objectSpread2(_objectSpread2({}, internalOptions), {}, {
7733
7640
  fees: {
7734
- originFee: feeAssetBalance && feeAssetBalance > 1n ? feeAssetBalance : MIN_FEE,
7641
+ originFee: feeAssetAmount,
7735
7642
  reserveFee: MIN_FEE
7736
7643
  }
7737
7644
  })), MAX_WEIGHT);
7738
- _context.n = 5;
7645
+ _context.n = 2;
7739
7646
  return dryRunInternal({
7740
7647
  api: api,
7741
7648
  tx: api.callTxMethod(call),
@@ -7744,17 +7651,17 @@ var handleExecuteTransfer = /*#__PURE__*/function () {
7744
7651
  senderAddress: senderAddress,
7745
7652
  address: address,
7746
7653
  currency: currency,
7747
- feeAsset: feeCurrency
7654
+ feeAsset: feeCurrency,
7655
+ useRootOrigin: true
7748
7656
  });
7749
- case 5:
7657
+ case 2:
7750
7658
  dryRunResult = _context.v;
7751
7659
  if (dryRunResult.origin.success) {
7752
- _context.n = 6;
7660
+ _context.n = 3;
7753
7661
  break;
7754
7662
  }
7755
7663
  throw new DryRunFailedError(dryRunResult.failureReason);
7756
- case 6:
7757
- validateHops(dryRunResult.hops);
7664
+ case 3:
7758
7665
  originFeeEstimate = dryRunResult.origin.fee;
7759
7666
  originFee = padFeeBy(originFeeEstimate, FEE_PADDING_PERCENTAGE$1);
7760
7667
  reserveFeeEstimate = getReserveFeeFromHops(dryRunResult.hops);
@@ -7766,9 +7673,9 @@ var handleExecuteTransfer = /*#__PURE__*/function () {
7766
7673
  reserveFee: reserveFee
7767
7674
  }
7768
7675
  }));
7769
- _context.n = 7;
7676
+ _context.n = 4;
7770
7677
  return api.getXcmWeight(xcm);
7771
- case 7:
7678
+ case 4:
7772
7679
  weight = _context.v;
7773
7680
  return _context.a(2, createExecuteCall(chain, xcm, weight));
7774
7681
  }
@@ -8059,7 +7966,7 @@ var createXcmAndCall = /*#__PURE__*/function () {
8059
7966
  }();
8060
7967
  var handleSwapExecuteTransfer = /*#__PURE__*/function () {
8061
7968
  var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(options) {
8062
- var api, chain, exchangeChain, destChain, assetFrom, assetTo, currencyTo, senderAddress, recipientAddress, calculateMinAmountOut, version, internalOptions, dryRunParams, _yield$createXcmAndCa, initialCall, firstDryRunResult, exchangeHopIndex, hasOriginReserveHop, requireExchangeSuccess, extractedFees, exchangeHop, totalFeesInFromAsset, updatedAssetTo, amountAvailableForSwap, recalculatedMinAmountOut, _yield$createXcmAndCa2, secondCall, secondDryRunResult, finalFees, hasHopsInSecondRun, isOnExchangeChain, finalExchangeHopIndex, finalTotalFeesInFromAsset, finalAmountAvailableForSwap, finalMinAmountOut, finalAssetTo, _yield$createXcmAndCa3, finalCall;
7969
+ var api, chain, exchangeChain, destChain, assetFrom, assetTo, currencyTo, senderAddress, recipientAddress, calculateMinAmountOut, version, internalOptions, dryRunParams, _yield$createXcmAndCa, initialCall, firstDryRunResult, exchangeHopIndex, extractedFees, totalFeesInFromAsset, updatedAssetTo, amountAvailableForSwap, recalculatedMinAmountOut, _yield$createXcmAndCa2, finalCall;
8063
7970
  return _regenerator().w(function (_context3) {
8064
7971
  while (1) switch (_context3.n) {
8065
7972
  case 0:
@@ -8086,7 +7993,8 @@ var handleSwapExecuteTransfer = /*#__PURE__*/function () {
8086
7993
  swapConfig: {
8087
7994
  currencyTo: currencyTo,
8088
7995
  exchangeChain: exchangeChain
8089
- }
7996
+ },
7997
+ useRootOrigin: true
8090
7998
  }; // First dry run with dummy fees to extract actual fees
8091
7999
  _context3.n = 2;
8092
8000
  return createXcmAndCall(_objectSpread2(_objectSpread2({}, internalOptions), {}, {
@@ -8105,38 +8013,8 @@ var handleSwapExecuteTransfer = /*#__PURE__*/function () {
8105
8013
  }));
8106
8014
  case 3:
8107
8015
  firstDryRunResult = _context3.v;
8108
- exchangeHopIndex = findExchangeHopIndex(chain, firstDryRunResult.hops, exchangeChain, destChain); // Check if there's a hop before exchange (origin reserve hop)
8109
- hasOriginReserveHop = destChain ? exchangeHopIndex > 0 : firstDryRunResult.hops.length > 0; // If there's no origin reserve hop, we need the exchange hop/destination to succeed to get fees
8110
- requireExchangeSuccess = !hasOriginReserveHop;
8111
- extractedFees = extractFeesFromDryRun(chain, firstDryRunResult, exchangeHopIndex, destChain, false); // If we need exchange to succeed but it failed, throw error
8112
- if (!(requireExchangeSuccess && extractedFees.exchangeFee === 0n)) {
8113
- _context3.n = 6;
8114
- break;
8115
- }
8116
- if (!destChain) {
8117
- _context3.n = 5;
8118
- break;
8119
- }
8120
- if (!chain) {
8121
- _context3.n = 4;
8122
- break;
8123
- }
8124
- exchangeHop = firstDryRunResult.hops[exchangeHopIndex];
8125
- if (exchangeHop.result.success) {
8126
- _context3.n = 4;
8127
- break;
8128
- }
8129
- throw new DryRunFailedError("Exchange hop failed when no origin reserve exists: ".concat(exchangeHop.result.failureReason || 'Unknown reason'));
8130
- case 4:
8131
- _context3.n = 6;
8132
- break;
8133
- case 5:
8134
- if (!(firstDryRunResult.destination && !firstDryRunResult.destination.success)) {
8135
- _context3.n = 6;
8136
- break;
8137
- }
8138
- throw new DryRunFailedError("Exchange (destination) failed when no origin reserve exists: ".concat(firstDryRunResult.destination.failureReason || 'Unknown reason'));
8139
- case 6:
8016
+ exchangeHopIndex = findExchangeHopIndex(chain, firstDryRunResult.hops, exchangeChain, destChain);
8017
+ extractedFees = extractFeesFromDryRun(chain, firstDryRunResult, exchangeHopIndex, destChain, false);
8140
8018
  if (extractedFees.exchangeFee === 0n) {
8141
8019
  // We set the exchange fee to non-zero value to prevent creating dummy tx
8142
8020
  extractedFees.exchangeFee = MIN_FEE;
@@ -8146,67 +8024,27 @@ var handleSwapExecuteTransfer = /*#__PURE__*/function () {
8146
8024
  validateAmount(BigInt(assetFrom.amount), totalFeesInFromAsset);
8147
8025
  updatedAssetTo = assetTo;
8148
8026
  if (!chain) {
8149
- _context3.n = 8;
8027
+ _context3.n = 5;
8150
8028
  break;
8151
8029
  }
8152
8030
  amountAvailableForSwap = BigInt(assetFrom.amount) - totalFeesInFromAsset;
8153
- _context3.n = 7;
8031
+ _context3.n = 4;
8154
8032
  return calculateMinAmountOut(amountAvailableForSwap);
8155
- case 7:
8033
+ case 4:
8156
8034
  recalculatedMinAmountOut = _context3.v;
8157
8035
  updatedAssetTo = _objectSpread2(_objectSpread2({}, assetTo), {}, {
8158
8036
  amount: recalculatedMinAmountOut
8159
8037
  });
8160
- case 8:
8161
- _context3.n = 9;
8038
+ case 5:
8039
+ _context3.n = 6;
8162
8040
  return createXcmAndCall(_objectSpread2(_objectSpread2({}, internalOptions), {}, {
8163
8041
  assetInfoTo: updatedAssetTo,
8164
8042
  fees: extractedFees
8165
8043
  }), firstDryRunResult.origin.success ? firstDryRunResult.origin.weight : undefined);
8166
- case 9:
8044
+ case 6:
8167
8045
  _yield$createXcmAndCa2 = _context3.v;
8168
- secondCall = _yield$createXcmAndCa2.call;
8169
- _context3.n = 10;
8170
- return executeDryRun(_objectSpread2(_objectSpread2({}, dryRunParams), {}, {
8171
- tx: api.callTxMethod(secondCall)
8172
- }));
8173
- case 10:
8174
- secondDryRunResult = _context3.v;
8175
- hasHopsInSecondRun = secondDryRunResult.hops && secondDryRunResult.hops.length > 0;
8176
- isOnExchangeChain = chain === exchangeChain;
8177
- if (hasHopsInSecondRun && !isOnExchangeChain) {
8178
- finalExchangeHopIndex = findExchangeHopIndex(chain, secondDryRunResult.hops, exchangeChain, destChain);
8179
- finalFees = extractFeesFromDryRun(chain, secondDryRunResult, finalExchangeHopIndex, destChain, true);
8180
- } else {
8181
- finalFees = extractFeesFromDryRun(chain, secondDryRunResult, 0, destChain, true);
8182
- }
8183
- // Validate that we have enough after accounting for final fees
8184
- finalTotalFeesInFromAsset = chain ? finalFees.originReserveFee + finalFees.exchangeFee : 0n;
8185
- validateAmount(BigInt(assetFrom.amount), finalTotalFeesInFromAsset);
8186
- // If the final fees are different, we might need one more iteration
8187
- if (!(finalFees.exchangeFee !== extractedFees.exchangeFee || finalFees.originReserveFee !== extractedFees.originReserveFee)) {
8188
- _context3.n = 13;
8189
- break;
8190
- }
8191
- finalAmountAvailableForSwap = BigInt(assetFrom.amount) - finalTotalFeesInFromAsset;
8192
- _context3.n = 11;
8193
- return calculateMinAmountOut(finalAmountAvailableForSwap);
8194
- case 11:
8195
- finalMinAmountOut = _context3.v;
8196
- finalAssetTo = _objectSpread2(_objectSpread2({}, assetTo), {}, {
8197
- amount: finalMinAmountOut
8198
- });
8199
- _context3.n = 12;
8200
- return createXcmAndCall(_objectSpread2(_objectSpread2({}, internalOptions), {}, {
8201
- assetInfoTo: finalAssetTo,
8202
- fees: finalFees
8203
- }), secondDryRunResult.origin.success ? secondDryRunResult.origin.weight : undefined);
8204
- case 12:
8205
- _yield$createXcmAndCa3 = _context3.v;
8206
- finalCall = _yield$createXcmAndCa3.call;
8046
+ finalCall = _yield$createXcmAndCa2.call;
8207
8047
  return _context3.a(2, api.callTxMethod(finalCall));
8208
- case 13:
8209
- return _context3.a(2, api.callTxMethod(secondCall));
8210
8048
  }
8211
8049
  }, _callee3);
8212
8050
  }));
@@ -12672,15 +12510,19 @@ var GeneralBuilder = /*#__PURE__*/function () {
12672
12510
  key: "getTransferableAmount",
12673
12511
  value: (function () {
12674
12512
  var _getTransferableAmount2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
12675
- var _this$_options7, from, to, senderAddress, currency, feeAsset;
12513
+ var _this$_options7, from, to, senderAddress, currency, feeAsset, tx;
12676
12514
  return _regenerator().w(function (_context8) {
12677
12515
  while (1) switch (_context8.n) {
12678
12516
  case 0:
12679
12517
  _this$_options7 = this._options, from = _this$_options7.from, to = _this$_options7.to, senderAddress = _this$_options7.senderAddress, currency = _this$_options7.currency, feeAsset = _this$_options7.feeAsset;
12680
12518
  assertToIsString(to);
12519
+ _context8.n = 1;
12520
+ return this.build();
12521
+ case 1:
12522
+ tx = _context8.v;
12681
12523
  return _context8.a(2, getTransferableAmount({
12682
12524
  api: this.api,
12683
- builder: this,
12525
+ tx: tx,
12684
12526
  origin: from,
12685
12527
  destination: to,
12686
12528
  senderAddress: senderAddress,