@subwallet/extension-base 1.3.29-1 → 1.3.30-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/background/KoniTypes.d.ts +4 -4
  2. package/background/errors/SwapError.js +1 -1
  3. package/cjs/background/errors/SwapError.js +1 -1
  4. package/cjs/constants/blocked-actions.js +2 -2
  5. package/cjs/constants/remind-notification-time.js +3 -3
  6. package/cjs/core/logic-validation/swap.js +63 -4
  7. package/cjs/core/utils.js +1 -1
  8. package/cjs/koni/background/handlers/Extension.js +5 -82
  9. package/cjs/packageInfo.js +1 -1
  10. package/cjs/services/balance-service/transfer/xcm/availBridge.js +6 -6
  11. package/cjs/services/earning-service/handlers/base.js +6 -3
  12. package/cjs/services/earning-service/handlers/native-staking/base.js +4 -1
  13. package/cjs/services/earning-service/handlers/native-staking/dtao.js +68 -50
  14. package/cjs/services/earning-service/handlers/native-staking/tao.js +12 -2
  15. package/cjs/services/earning-service/service.js +2 -1
  16. package/cjs/services/fee-service/utils/index.js +16 -4
  17. package/cjs/services/inapp-notification-service/index.js +19 -13
  18. package/cjs/services/swap-service/handler/asset-hub/handler.js +61 -314
  19. package/cjs/services/swap-service/handler/base-handler.js +393 -231
  20. package/cjs/services/swap-service/handler/chainflip-handler.js +18 -40
  21. package/cjs/services/swap-service/handler/hydradx-handler.js +77 -269
  22. package/cjs/services/swap-service/handler/simpleswap-handler.js +27 -48
  23. package/cjs/services/swap-service/handler/uniswap-handler.js +33 -54
  24. package/cjs/services/swap-service/index.js +154 -143
  25. package/cjs/services/swap-service/utils.js +107 -17
  26. package/cjs/services/transaction-service/index.js +1 -1
  27. package/cjs/types/swap/index.js +13 -1
  28. package/cjs/utils/swap.js +5 -1
  29. package/constants/blocked-actions.d.ts +1 -1
  30. package/constants/blocked-actions.js +1 -1
  31. package/constants/remind-notification-time.d.ts +1 -1
  32. package/constants/remind-notification-time.js +1 -1
  33. package/core/logic-validation/swap.d.ts +15 -0
  34. package/core/logic-validation/swap.js +60 -4
  35. package/core/utils.js +1 -1
  36. package/koni/background/handlers/Extension.d.ts +0 -1
  37. package/koni/background/handlers/Extension.js +6 -83
  38. package/package.json +6 -12
  39. package/packageInfo.js +1 -1
  40. package/services/balance-service/transfer/xcm/availBridge.js +6 -6
  41. package/services/base/types.d.ts +0 -4
  42. package/services/earning-service/handlers/base.d.ts +4 -3
  43. package/services/earning-service/handlers/base.js +6 -4
  44. package/services/earning-service/handlers/native-staking/base.js +4 -1
  45. package/services/earning-service/handlers/native-staking/dtao.d.ts +9 -6
  46. package/services/earning-service/handlers/native-staking/dtao.js +69 -48
  47. package/services/earning-service/handlers/native-staking/tao.js +12 -2
  48. package/services/earning-service/service.d.ts +2 -1
  49. package/services/earning-service/service.js +2 -1
  50. package/services/fee-service/utils/index.d.ts +1 -0
  51. package/services/fee-service/utils/index.js +14 -4
  52. package/services/inapp-notification-service/index.js +13 -7
  53. package/services/swap-service/handler/asset-hub/handler.d.ts +2 -9
  54. package/services/swap-service/handler/asset-hub/handler.js +64 -317
  55. package/services/swap-service/handler/base-handler.d.ts +6 -9
  56. package/services/swap-service/handler/base-handler.js +391 -229
  57. package/services/swap-service/handler/chainflip-handler.d.ts +2 -4
  58. package/services/swap-service/handler/chainflip-handler.js +15 -37
  59. package/services/swap-service/handler/hydradx-handler.d.ts +3 -10
  60. package/services/swap-service/handler/hydradx-handler.js +78 -270
  61. package/services/swap-service/handler/simpleswap-handler.d.ts +2 -4
  62. package/services/swap-service/handler/simpleswap-handler.js +24 -45
  63. package/services/swap-service/handler/uniswap-handler.d.ts +4 -6
  64. package/services/swap-service/handler/uniswap-handler.js +25 -46
  65. package/services/swap-service/index.d.ts +8 -14
  66. package/services/swap-service/index.js +141 -129
  67. package/services/swap-service/utils.d.ts +11 -3
  68. package/services/swap-service/utils.js +96 -15
  69. package/services/transaction-service/index.js +2 -2
  70. package/types/service-base.d.ts +2 -3
  71. package/types/swap/index.d.ts +25 -9
  72. package/types/swap/index.js +10 -0
  73. package/types/transaction/process.d.ts +19 -0
  74. package/types/yield/actions/join/submit.d.ts +4 -1
  75. package/types/yield/actions/others.d.ts +2 -0
  76. package/utils/swap.d.ts +3 -0
  77. package/utils/swap.js +3 -0
  78. package/cjs/services/swap-service/interface.js +0 -14
  79. package/services/swap-service/interface.d.ts +0 -9
  80. package/services/swap-service/interface.js +0 -8
@@ -8,7 +8,6 @@ exports.SwapBaseHandler = void 0;
8
8
  var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
9
9
  var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
10
10
  var _logicValidation = require("@subwallet/extension-base/core/logic-validation");
11
- var _swap = require("@subwallet/extension-base/core/logic-validation/swap");
12
11
  var _systemPallet = require("@subwallet/extension-base/core/substrate/system-pallet");
13
12
  var _xcmParser = require("@subwallet/extension-base/core/substrate/xcm-parser");
14
13
  var _utils = require("@subwallet/extension-base/core/utils");
@@ -16,11 +15,13 @@ var _utils2 = require("@subwallet/extension-base/services/chain-service/utils");
16
15
  var _utils3 = require("@subwallet/extension-base/services/swap-service/utils");
17
16
  var _types = require("@subwallet/extension-base/types");
18
17
  var _serviceBase = require("@subwallet/extension-base/types/service-base");
19
- var _swap2 = require("@subwallet/extension-base/types/swap");
18
+ var _swap = require("@subwallet/extension-base/types/swap");
20
19
  var _utils4 = require("@subwallet/extension-base/utils");
20
+ var _getId = require("@subwallet/extension-base/utils/getId");
21
21
  var _bignumber = _interopRequireDefault(require("bignumber.js"));
22
22
  var _i18next = require("i18next");
23
23
  var _utilCrypto = require("@polkadot/util-crypto");
24
+ var _xcm = require("../../balance-service/transfer/xcm");
24
25
  // Copyright 2019-2022 @subwallet/extension-base
25
26
  // SPDX-License-Identifier: Apache-2.0
26
27
 
@@ -39,30 +40,6 @@ class SwapBaseHandler {
39
40
  this.balanceService = balanceService;
40
41
  this.feeService = feeService;
41
42
  }
42
-
43
- // public abstract getSwapQuote(request: SwapRequest): Promise<SwapQuote | SwapError>;
44
- async generateOptimalProcess(params, genStepFuncList) {
45
- const result = {
46
- totalFee: [_serviceBase.MOCK_STEP_FEE],
47
- steps: [_serviceBase.DEFAULT_FIRST_STEP],
48
- path: []
49
- };
50
- try {
51
- for (const genStepFunc of genStepFuncList) {
52
- const step = await genStepFunc(params);
53
- if (step) {
54
- result.steps.push({
55
- id: result.steps.length,
56
- ...step[0]
57
- });
58
- result.totalFee.push(step[1]);
59
- }
60
- }
61
- return result;
62
- } catch (e) {
63
- return result;
64
- }
65
- }
66
43
  async generateOptimalProcessV2(params, genStepFuncList) {
67
44
  const result = {
68
45
  totalFee: [_serviceBase.MOCK_STEP_FEE],
@@ -70,8 +47,8 @@ class SwapBaseHandler {
70
47
  path: params.path
71
48
  };
72
49
  try {
73
- for (const genStepFunc of genStepFuncList) {
74
- const step = await genStepFunc(params);
50
+ for (const [i, genStepFunc] of genStepFuncList.entries()) {
51
+ const step = await genStepFunc(params, i);
75
52
  if (step) {
76
53
  result.steps.push({
77
54
  id: result.steps.length,
@@ -85,158 +62,168 @@ class SwapBaseHandler {
85
62
  return result;
86
63
  }
87
64
  }
88
- async validateXcmStep(params, stepIndex) {
89
- const bnAmount = new _bignumber.default(params.selectedQuote.fromAmount);
90
- const swapPair = params.selectedQuote.pair;
91
- const alternativeAssetSlug = (0, _utils3.getSwapAlternativeAsset)(swapPair);
92
- if (!alternativeAssetSlug) {
93
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
65
+ async getBridgeStep(params, stepIndex) {
66
+ // only xcm on substrate for now
67
+ const {
68
+ path,
69
+ request: {
70
+ address,
71
+ fromAmount,
72
+ recipient
73
+ },
74
+ selectedQuote
75
+ } = params;
76
+ if (stepIndex < 0 || stepIndex > params.path.length - 1) {
77
+ return undefined;
78
+ }
79
+ const bridgePairInfo = path[stepIndex];
80
+ if (bridgePairInfo.action !== _swap.DynamicSwapType.BRIDGE) {
81
+ return undefined;
82
+ }
83
+ if (!bridgePairInfo || !selectedQuote) {
84
+ return undefined;
85
+ }
86
+ const fromTokenInfo = this.chainService.getAssetBySlug(bridgePairInfo.pair.from);
87
+ const toTokenInfo = this.chainService.getAssetBySlug(bridgePairInfo.pair.to);
88
+ const fromChainInfo = this.chainService.getChainInfoByKey(fromTokenInfo.originChain);
89
+ const toChainInfo = this.chainService.getChainInfoByKey(toTokenInfo.originChain);
90
+ if (!fromChainInfo || !toChainInfo || !fromChainInfo || !toChainInfo) {
91
+ throw Error('Token or chain not found');
92
+ }
93
+ let recipientAddress;
94
+ const senderAddress = (0, _utils4._reformatAddressWithChain)(address, fromChainInfo);
95
+ if (stepIndex === 0) {
96
+ recipientAddress = (0, _utils4._reformatAddressWithChain)(address, toChainInfo);
97
+ } else {
98
+ // bridge after swap
99
+ recipientAddress = (0, _utils4._reformatAddressWithChain)(recipient || address, toChainInfo);
94
100
  }
95
- const alternativeAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
96
- const fromAsset = this.chainService.getAssetBySlug(swapPair.from);
97
- const [alternativeAssetBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTransferableBalance(params.address, alternativeAsset.originChain, alternativeAssetSlug), this.balanceService.getTransferableBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
98
- const bnAlternativeAssetBalance = new _bignumber.default(alternativeAssetBalance.value);
99
- const bnFromAssetBalance = new _bignumber.default(fromAssetBalance.value);
100
- const xcmFeeComponent = params.process.totalFee[stepIndex].feeComponent[0]; // todo: can do better than indexing
101
- const xcmFee = new _bignumber.default(xcmFeeComponent.amount || '0');
102
- let xcmAmount = bnAmount.minus(bnFromAssetBalance);
103
- let editedXcmFee = new _bignumber.default(0);
104
- if ((0, _utils2._isNativeToken)(alternativeAsset)) {
105
- xcmAmount = xcmAmount.plus(xcmFee);
106
- editedXcmFee = xcmFee.times(2);
107
- }
108
- if (!bnAlternativeAssetBalance.minus((0, _utils2._isNativeToken)(alternativeAsset) ? xcmAmount.plus(xcmFee) : xcmFee).gt(0)) {
109
- const maxBn = bnFromAssetBalance.plus(new _bignumber.default(alternativeAssetBalance.value)).minus((0, _utils2._isNativeToken)(alternativeAsset) ? editedXcmFee : xcmFee);
110
- const maxValue = (0, _utils4.formatNumber)(maxBn.toString(), fromAsset.decimals || 0);
111
- const altInputTokenInfo = this.chainService.getAssetBySlug(alternativeAssetSlug);
112
- const symbol = altInputTokenInfo.symbol;
113
- const alternativeChain = this.chainService.getChainInfoByKey(altInputTokenInfo.originChain);
114
- const chain = this.chainService.getChainInfoByKey(fromAsset.originChain);
115
- const inputNetworkName = chain.name;
116
- const altNetworkName = alternativeChain.name;
117
- const currentValue = (0, _utils4.formatNumber)(bnFromAssetBalance.toString(), fromAsset.decimals || 0);
118
- const bnMaxXCM = new _bignumber.default(alternativeAssetBalance.value).minus((0, _utils2._isNativeToken)(alternativeAsset) ? editedXcmFee : xcmFee);
119
- const maxXCMValue = (0, _utils4.formatNumber)(bnMaxXCM.toString(), fromAsset.decimals || 0);
120
- if (maxBn.lte(0) || bnFromAssetBalance.lte(0) || bnMaxXCM.lte(0)) {
121
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.NOT_ENOUGH_BALANCE, (0, _i18next.t)(`Insufficient balance. Deposit ${fromAsset.symbol} and try again.`))];
101
+ try {
102
+ if (!this.chainService.getChainStateByKey(toTokenInfo.originChain).active) {
103
+ await this.chainService.enableChain(toTokenInfo.originChain);
122
104
  }
123
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.NOT_ENOUGH_BALANCE, (0, _i18next.t)('You can only enter a maximum of {{maxValue}} {{symbol}}, which is {{currentValue}} {{symbol}} ({{inputNetworkName}}) and {{maxXCMValue}} {{symbol}} ({{altNetworkName}}). Lower your amount and try again.', {
124
- replace: {
125
- symbol,
126
- maxValue,
127
- inputNetworkName,
128
- altNetworkName,
129
- currentValue,
130
- maxXCMValue
131
- }
132
- }))];
133
- }
134
- return [];
135
- }
136
- async validateXcmStepV2(params, stepIndex) {
137
- var _currentFee$feeCompon, _params$recipient;
138
- const currentStep = params.process.steps[stepIndex];
139
- const currentFee = params.process.totalFee[stepIndex];
140
- const feeToken = currentFee.selectedFeeToken || currentFee.defaultFeeToken;
141
- const feeAmount = (_currentFee$feeCompon = currentFee.feeComponent.find(fee => fee.feeType === _swap2.SwapFeeType.NETWORK_FEE)) === null || _currentFee$feeCompon === void 0 ? void 0 : _currentFee$feeCompon.amount;
142
- if (!feeAmount) {
143
- throw new Error('Fee not found for XCM step');
144
- }
145
- const metadata = currentStep.metadata;
146
- const sendingAmount = metadata.sendingValue;
147
- const bnAmount = new _bignumber.default(sendingAmount);
148
- const fromAsset = metadata === null || metadata === void 0 ? void 0 : metadata.originTokenInfo;
149
- const toAsset = metadata === null || metadata === void 0 ? void 0 : metadata.destinationTokenInfo;
150
- if (!fromAsset || !toAsset) {
151
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
152
- }
153
- const fromChain = this.chainService.getChainInfoByKey(fromAsset.originChain);
154
- const toChain = this.chainService.getChainInfoByKey(toAsset.originChain);
155
- const toChainNativeAsset = this.chainService.getNativeTokenInfo(toAsset.originChain);
156
- const sender = (0, _utils4._reformatAddressWithChain)(params.address, fromChain);
157
- const receiver = (0, _utils4._reformatAddressWithChain)((_params$recipient = params.recipient) !== null && _params$recipient !== void 0 ? _params$recipient : sender, toChain);
158
-
159
- /* Get transferable balance */
160
- const [fromAssetBalance, feeTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(sender, fromAsset.originChain, fromAsset.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM), this.balanceService.getTransferableBalance(sender, fromAsset.originChain, feeToken, _KoniTypes.ExtrinsicType.TRANSFER_XCM)]);
161
- const bnFromAssetBalance = new _bignumber.default(fromAssetBalance.value);
162
- const bnFeeTokenBalance = new _bignumber.default(feeTokenBalance.value);
163
-
164
- /* Compare transferable balance with amount xcm */
165
- if (bnFromAssetBalance.lt(bnAmount)) {
166
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.NOT_ENOUGH_BALANCE, (0, _i18next.t)(`Insufficient balance. Deposit ${fromAsset.symbol} and try again.`))];
167
- }
168
-
169
- /**
170
- * Calculate fee token keep alive after xcm
171
- * If fee token is the same as from token, need to subtract sending amount
172
- * @TODO: Need to update logic if change fee token (multi with rate)
173
- * */
174
- const feeBalanceAfterTransfer = bnFeeTokenBalance.minus(feeAmount).minus(fromAsset.slug === feeToken ? bnAmount : 0);
175
-
176
- /**
177
- * Check fee token balance after transfer.
178
- * Because the balance had subtracted with existence deposit, so only need to check if it's less than 0
179
- * */
180
- if (feeBalanceAfterTransfer.lt(0)) {
181
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.NOT_ENOUGH_EXISTENTIAL_DEPOSIT, (0, _i18next.t)(`Insufficient balance. Deposit ${fromAsset.symbol} and try again.`))];
182
- }
183
- const destMinAmount = (0, _utils2._getTokenMinAmount)(toAsset);
184
- // TODO: Need to update with new logic, calculate fee to claim on dest chain
185
- const minSendingRequired = new _bignumber.default(destMinAmount).multipliedBy(_utils3.FEE_RATE_MULTIPLIER.high);
186
-
187
- // Check sending token ED for receiver
188
- if (bnAmount.lt(minSendingRequired)) {
189
- const atLeastStr = (0, _utils4.formatNumber)(minSendingRequired, (0, _utils2._getAssetDecimals)(toAsset), _utils4.balanceFormatter, {
190
- maxNumberFormat: (0, _utils2._getAssetDecimals)(toAsset) || 6
105
+ const substrateApi = await this.chainService.getSubstrateApi(fromTokenInfo.originChain).isReady;
106
+ const id = (0, _getId.getId)();
107
+ const [feeInfo, toTokenBalance] = await Promise.all([this.feeService.subscribeChainFee(id, fromTokenInfo.originChain, 'substrate'), this.balanceService.getTotalBalance(senderAddress, toTokenInfo.originChain, toTokenInfo.slug, _KoniTypes.ExtrinsicType.TRANSFER_BALANCE)]);
108
+ const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
109
+ originTokenInfo: fromTokenInfo,
110
+ destinationTokenInfo: toTokenInfo,
111
+ originChain: fromChainInfo,
112
+ destinationChain: toChainInfo,
113
+ substrateApi: substrateApi,
114
+ feeInfo,
115
+ // Mock sending value to get payment info
116
+ sendingValue: fromAmount,
117
+ sender: senderAddress,
118
+ recipient: recipientAddress
191
119
  });
192
- return [new _TransactionError.TransactionError(_types.TransferTxErrorType.RECEIVER_NOT_ENOUGH_EXISTENTIAL_DEPOSIT, (0, _i18next.t)('You must transfer at least {{amount}} {{symbol}} to keep the destination account alive', {
193
- replace: {
194
- amount: atLeastStr,
195
- symbol: fromAsset.symbol
196
- }
197
- }))];
198
- }
120
+ const _xcmFeeInfo = await xcmTransfer.paymentInfo(senderAddress);
121
+ const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
122
+ const estimatedBridgeFee = Math.ceil(xcmFeeInfo.partialFee * _utils3.FEE_RATE_MULTIPLIER.medium).toString();
123
+ const fee = {
124
+ feeComponent: [{
125
+ feeType: _swap.SwapFeeType.NETWORK_FEE,
126
+ amount: estimatedBridgeFee,
127
+ tokenSlug: (0, _utils2._getChainNativeTokenSlug)(fromChainInfo)
128
+ }],
129
+ defaultFeeToken: (0, _utils2._getChainNativeTokenSlug)(fromChainInfo),
130
+ feeOptions: [(0, _utils2._getChainNativeTokenSlug)(fromChainInfo)]
131
+ };
132
+ const isBridgeNativeToken = (0, _utils2._isNativeToken)(fromTokenInfo);
133
+ let bnSendingValue;
134
+ let expectedReceive;
135
+ const actionList = JSON.stringify(path.map(step => step.action));
136
+ const xcmSwapXcm = actionList === JSON.stringify([_swap.DynamicSwapType.BRIDGE, _swap.DynamicSwapType.SWAP, _swap.DynamicSwapType.BRIDGE]);
137
+ const swapXcm = actionList === JSON.stringify([_swap.DynamicSwapType.SWAP, _swap.DynamicSwapType.BRIDGE]);
138
+ const needEditAmount = swapXcm || xcmSwapXcm;
199
139
 
200
- // Check keepAlive on dest chain for receiver
201
- if (!(0, _utils2._isNativeToken)(toAsset)) {
202
- const toChainApi = this.chainService.getSubstrateApi(toAsset.originChain);
203
-
204
- // TODO: Need to update, currently only support substrate xcm
205
- if (!toChainApi) {
206
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR, (0, _i18next.t)('Destination chain is not active'))];
207
- }
208
- const isSendingTokenSufficient = await (0, _utils._isSufficientToken)(toAsset, toChainApi);
209
- if (!isSendingTokenSufficient) {
210
- const toChainNativeAssetBalance = await this.balanceService.getTotalBalance(receiver, toAsset.originChain, toChainNativeAsset.slug, _KoniTypes.ExtrinsicType.TRANSFER_BALANCE);
211
- const isReceiverAliveByNativeToken = (0, _systemPallet._isAccountActive)(toChainNativeAssetBalance.metadata);
212
- if (!isReceiverAliveByNativeToken) {
213
- // TODO: Update message
214
- return [new _TransactionError.TransactionError(_types.TransferTxErrorType.RECEIVER_NOT_ENOUGH_EXISTENTIAL_DEPOSIT, (0, _i18next.t)('The recipient account has less than {{amount}} {{nativeSymbol}}, which can lead to your {{localSymbol}} being lost. Change recipient account and try again', {
215
- replace: {
216
- amount: toChainNativeAssetBalance.value,
217
- nativeSymbol: toChainNativeAsset.symbol,
218
- localSymbol: toAsset.symbol
219
- }
220
- }))];
140
+ // todo: increase transfer amount when XCM local token
141
+ if (stepIndex === 0) {
142
+ expectedReceive = fromAmount;
143
+ bnSendingValue = (0, _bignumber.default)(fromAmount);
144
+ if (needEditAmount) {
145
+ bnSendingValue = bnSendingValue.multipliedBy(_utils3.DEFAULT_EXCESS_AMOUNT_WEIGHT);
146
+ expectedReceive = bnSendingValue.toFixed(0, 1);
147
+ }
148
+ if (isBridgeNativeToken) {
149
+ bnSendingValue = bnSendingValue.plus((0, _bignumber.default)(estimatedBridgeFee));
150
+ } else {
151
+ bnSendingValue = bnSendingValue.plus((0, _bignumber.default)((0, _utils2._getTokenMinAmount)(toTokenInfo)).multipliedBy(_utils3.FEE_RATE_MULTIPLIER.medium)).plus((0, _utils2._getTokenMinAmount)(toTokenInfo));
152
+ }
153
+ if ((0, _bignumber.default)(toTokenBalance.value).lte(0)) {
154
+ bnSendingValue = bnSendingValue.plus((0, _utils2._getTokenMinAmount)(toTokenInfo));
155
+ }
156
+ } else {
157
+ // bridge after swap
158
+ expectedReceive = selectedQuote.toAmount;
159
+ if (needEditAmount) {
160
+ bnSendingValue = (0, _bignumber.default)(selectedQuote.toAmount).multipliedBy(_utils3.DEFAULT_EXCESS_AMOUNT_WEIGHT); // need to round
161
+ } else {
162
+ bnSendingValue = (0, _bignumber.default)(selectedQuote.toAmount);
221
163
  }
222
164
  }
165
+ if (toTokenInfo.originChain === 'mythos' && (0, _utils2._isNativeToken)(toTokenInfo)) {
166
+ bnSendingValue = bnSendingValue.plus((0, _bignumber.default)(2.5).shiftedBy((0, _utils2._getAssetDecimals)(toTokenInfo)));
167
+ }
168
+ const step = {
169
+ // @ts-ignore
170
+ metadata: {
171
+ sendingValue: bnSendingValue.toFixed(0, 1),
172
+ expectedReceive,
173
+ originTokenInfo: fromTokenInfo,
174
+ destinationTokenInfo: toTokenInfo,
175
+ receiver: recipientAddress,
176
+ sender: senderAddress
177
+ },
178
+ name: `Transfer ${fromTokenInfo.symbol} from ${fromChainInfo.name}`,
179
+ type: _serviceBase.CommonStepType.XCM
180
+ };
181
+ return [step, fee];
182
+ } catch (e) {
183
+ console.error('Error creating xcm step', e);
184
+ return undefined;
223
185
  }
224
-
225
- // SKIP: BECAUSE CURRENTLY NOT SUPPORT SNOWBRIDGE FOR SWAP FEATURE
226
- // check native token ED on dest chain for receiver
227
- // const bnKeepAliveBalance = _isNativeToken(destinationTokenInfo) ? new BigN(receiverNativeBalance).plus(sendingAmount) : new BigN(receiverNativeBalance);
228
- //
229
- // if (isSnowBridge && bnKeepAliveBalance.lt(_getChainExistentialDeposit(destChainInfo))) {
230
- // const { decimals, symbol } = _getChainNativeTokenBasicInfo(destChainInfo);
231
- // const atLeastStr = formatNumber(_getChainExistentialDeposit(destChainInfo), decimals || 0, balanceFormatter, { maxNumberFormat: 6 });
232
- //
233
- // error = new TransactionError(TransferTxErrorType.RECEIVER_NOT_ENOUGH_EXISTENTIAL_DEPOSIT, t(' Insufficient {{symbol}} on {{chain}} to cover min balance ({{amount}} {{symbol}})', { replace: { amount: atLeastStr, symbol, chain: destChainInfo.name } }));
234
- // }
235
-
236
- return [];
237
186
  }
238
- async validateTokenApproveStep(params, stepIndex) {
239
- return Promise.resolve([]);
187
+ async handleBridgeStep(params) {
188
+ const briefXcmStep = params.process.steps[params.currentStep].metadata;
189
+ if (!briefXcmStep || !briefXcmStep.originTokenInfo || !briefXcmStep.destinationTokenInfo || !briefXcmStep.sendingValue) {
190
+ throw new Error('XCM metadata error');
191
+ }
192
+ const originAsset = briefXcmStep.originTokenInfo;
193
+ const destinationAsset = briefXcmStep.destinationTokenInfo;
194
+ const originChain = this.chainService.getChainInfoByKey(originAsset.originChain);
195
+ const destinationChain = this.chainService.getChainInfoByKey(destinationAsset.originChain);
196
+ const substrateApi = this.chainService.getSubstrateApi(originAsset.originChain);
197
+ const chainApi = await substrateApi.isReady;
198
+ const feeInfo = await this.feeService.subscribeChainFee((0, _getId.getId)(), originAsset.originChain, 'substrate');
199
+ const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
200
+ originTokenInfo: originAsset,
201
+ destinationTokenInfo: destinationAsset,
202
+ sendingValue: briefXcmStep.sendingValue,
203
+ recipient: briefXcmStep.receiver,
204
+ substrateApi: chainApi,
205
+ sender: briefXcmStep.sender,
206
+ destinationChain,
207
+ originChain,
208
+ feeInfo
209
+ });
210
+ const xcmData = {
211
+ originNetworkKey: originAsset.originChain,
212
+ destinationNetworkKey: destinationAsset.originChain,
213
+ from: briefXcmStep.sender,
214
+ to: briefXcmStep.receiver,
215
+ value: briefXcmStep.sendingValue,
216
+ tokenSlug: originAsset.slug,
217
+ showExtraWarning: true
218
+ };
219
+ return {
220
+ txChain: originAsset.originChain,
221
+ extrinsic: xcmTransfer,
222
+ transferNativeAmount: (0, _utils2._isNativeToken)(originAsset) ? briefXcmStep.sendingValue : '0',
223
+ extrinsicType: _KoniTypes.ExtrinsicType.TRANSFER_XCM,
224
+ chainType: _KoniTypes.ChainType.SUBSTRATE,
225
+ txData: xcmData
226
+ };
240
227
  }
241
228
  async validateSetFeeTokenStep(params, stepIndex) {
242
229
  if (!params.selectedQuote) {
@@ -253,46 +240,6 @@ class SwapBaseHandler {
253
240
  }
254
241
  return [];
255
242
  }
256
- async validateSwapStep(params, isXcmOk, stepIndex) {
257
- // check swap quote timestamp
258
- // check balance to pay transaction fee
259
- // check balance against spending amount
260
- if (!params.selectedQuote) {
261
- return Promise.resolve([new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)]);
262
- }
263
- const selectedQuote = params.selectedQuote;
264
- const currentTimestamp = +Date.now();
265
- if (selectedQuote.aliveUntil <= currentTimestamp) {
266
- return Promise.resolve([new _TransactionError.TransactionError(_swap2.SwapErrorType.QUOTE_TIMEOUT)]);
267
- }
268
- const stepFee = params.process.totalFee[stepIndex].feeComponent;
269
- const networkFee = stepFee.find(fee => fee.feeType === _swap2.SwapFeeType.NETWORK_FEE);
270
- if (!networkFee) {
271
- return Promise.resolve([new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)]);
272
- }
273
- const fromAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.from);
274
- const feeTokenInfo = this.chainService.getAssetBySlug(networkFee.tokenSlug);
275
- const feeTokenChain = this.chainService.getChainInfoByKey(feeTokenInfo.originChain);
276
- const {
277
- fromAmount,
278
- minSwap
279
- } = params.selectedQuote;
280
- const [feeTokenBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTransferableBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug), this.balanceService.getTransferableBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
281
- const balanceError = (0, _swap._validateBalanceToSwap)(fromAsset, feeTokenInfo, feeTokenChain, networkFee.amount, fromAssetBalance.value, feeTokenBalance.value, fromAmount, isXcmOk, minSwap);
282
- if (balanceError) {
283
- return Promise.resolve([balanceError]);
284
- }
285
- if (!params.recipient) {
286
- return Promise.resolve([]);
287
- }
288
- const toAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.to);
289
- const toAssetChain = this.chainService.getChainInfoByKey(toAsset.originChain);
290
- const recipientError = (0, _swap._validateSwapRecipient)(toAssetChain, params.recipient);
291
- if (recipientError) {
292
- return Promise.resolve([recipientError]);
293
- }
294
- return Promise.resolve([]);
295
- }
296
243
  async validateBridgeStep(receiver, fromToken, toToken, selectedFeeToken, toChainNativeToken, bnBridgeAmount, bnFromTokenBalance, bnBridgeFeeAmount, bnFeeTokenBalance, bnBridgeDeliveryFee) {
297
244
  const minBridgeAmountRequired = new _bignumber.default((0, _utils2._getTokenMinAmount)(toToken)).multipliedBy(_utils3.FEE_RATE_MULTIPLIER.high);
298
245
  const spendingAndFeePaymentValidation = (0, _logicValidation.validateSpendingAndFeePayment)(fromToken, selectedFeeToken, bnBridgeAmount, bnFromTokenBalance, bnBridgeFeeAmount, bnFeeTokenBalance);
@@ -343,7 +290,7 @@ class SwapBaseHandler {
343
290
  const atLeastStr = (0, _utils4.formatNumber)((0, _utils2._getTokenMinAmount)(receivingToken), (0, _utils2._getAssetDecimals)(receivingToken), _utils4.balanceFormatter, {
344
291
  maxNumberFormat: (0, _utils2._getAssetDecimals)(receivingToken) || 6
345
292
  });
346
- return [new _TransactionError.TransactionError(_swap2.SwapErrorType.NOT_MEET_MIN_SWAP, (0, _i18next.t)('You can\'t receive less than {{number}} {{symbol}}', {
293
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.NOT_MEET_MIN_SWAP, (0, _i18next.t)('You can\'t receive less than {{number}} {{symbol}}', {
347
294
  replace: {
348
295
  number: atLeastStr,
349
296
  symbol: (0, _utils2._getAssetSymbol)(receivingToken)
@@ -355,7 +302,7 @@ class SwapBaseHandler {
355
302
  const isEvmDestChain = (0, _utils2._isChainEvmCompatible)(swapToChain);
356
303
  if (isEvmAddress && !isEvmDestChain || !isEvmAddress && isEvmDestChain) {
357
304
  // todo: update this condition
358
- return [new _TransactionError.TransactionError(_swap2.SwapErrorType.INVALID_RECIPIENT)];
305
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.INVALID_RECIPIENT)];
359
306
  }
360
307
  }
361
308
  return [];
@@ -364,7 +311,7 @@ class SwapBaseHandler {
364
311
  const swapStepInfo = params.process.steps[swapIndex];
365
312
  const swapMetadata = swapStepInfo.metadata; // todo
366
313
  const swapFee = params.process.totalFee[swapIndex];
367
- if (!swapMetadata || !swapMetadata.destinationTokenInfo || !swapMetadata.originTokenInfo || !swapMetadata.sendingValue) {
314
+ if (!swapMetadata || !swapMetadata.destinationTokenInfo || !swapMetadata.originTokenInfo || !swapMetadata.sendingValue || !swapMetadata.expectedReceive) {
368
315
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
369
316
  }
370
317
 
@@ -373,32 +320,35 @@ class SwapBaseHandler {
373
320
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
374
321
  }
375
322
  if (params.selectedQuote.aliveUntil <= +Date.now()) {
376
- return [new _TransactionError.TransactionError(_swap2.SwapErrorType.QUOTE_TIMEOUT)];
323
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.QUOTE_TIMEOUT)];
377
324
  }
378
- const swapNetworkFee = swapFee.feeComponent.find(fee => fee.feeType === _swap2.SwapFeeType.NETWORK_FEE);
325
+ if (params.selectedQuote.toAmount !== swapMetadata.expectedReceive) {
326
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
327
+ }
328
+ const swapNetworkFee = swapFee.feeComponent.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE);
379
329
  if (!swapNetworkFee) {
380
330
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
381
331
  }
382
332
  const swapToken = swapMetadata.originTokenInfo;
383
333
  const swapReceivingToken = swapMetadata.destinationTokenInfo;
384
- const bnSwapReceivingAmount = (0, _bignumber.default)(params.selectedQuote.toAmount);
334
+ const bnSwapReceivingAmount = (0, _bignumber.default)(swapMetadata.expectedReceive);
385
335
  const bnSwapValue = (0, _bignumber.default)(swapMetadata.sendingValue);
386
336
  const bnSwapFeeAmount = (0, _bignumber.default)(swapNetworkFee.amount);
387
337
  const swapFeeToken = this.chainService.getAssetBySlug(swapFee.selectedFeeToken || swapFee.defaultFeeToken);
388
338
  const swapToChain = this.chainService.getChainInfoByKey(swapMetadata.destinationTokenInfo.originChain);
389
- const [swapFeeTokenBalance, swapFromTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(params.address, swapFeeToken.originChain, swapFeeToken.slug, _KoniTypes.ExtrinsicType.SWAP), this.balanceService.getTransferableBalance(params.address, swapToken.originChain, swapToken.slug, _KoniTypes.ExtrinsicType.SWAP)]);
339
+ const [swapFeeTokenBalance, swapFromTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(swapMetadata.sender, swapFeeToken.originChain, swapFeeToken.slug, _KoniTypes.ExtrinsicType.SWAP), this.balanceService.getTransferableBalance(swapMetadata.sender, swapToken.originChain, swapToken.slug, _KoniTypes.ExtrinsicType.SWAP)]);
390
340
  const bnSwapFromTokenBalance = (0, _bignumber.default)(swapFromTokenBalance.value);
391
341
  const bnSwapFeeTokenBalance = (0, _bignumber.default)(swapFeeTokenBalance.value);
392
- return this.validateSwapStepV2(swapToChain, swapToken, swapReceivingToken, swapFeeToken, bnSwapValue, bnSwapReceivingAmount, bnSwapFromTokenBalance, bnSwapFeeAmount, bnSwapFeeTokenBalance, params.recipient);
342
+ return this.validateSwapStepV2(swapToChain, swapToken, swapReceivingToken, swapFeeToken, bnSwapValue, bnSwapReceivingAmount, bnSwapFromTokenBalance, bnSwapFeeAmount, bnSwapFeeTokenBalance, swapMetadata.receiver);
393
343
  }
394
344
  async validateXcmSwapProcess(params, swapIndex, xcmIndex) {
395
- var _currentFee$feeCompon2, _params$recipient2;
345
+ var _currentFee$feeCompon, _xcmMetadata$receiver;
396
346
  // Bridge
397
347
  const currentStep = params.process.steps[xcmIndex];
398
348
  const xcmMetadata = currentStep.metadata;
399
349
  const currentFee = params.process.totalFee[xcmIndex];
400
- const bridgeFeeAmount = (_currentFee$feeCompon2 = currentFee.feeComponent.find(fee => fee.feeType === _swap2.SwapFeeType.NETWORK_FEE)) === null || _currentFee$feeCompon2 === void 0 ? void 0 : _currentFee$feeCompon2.amount;
401
- if (!xcmMetadata || !xcmMetadata.destinationTokenInfo || !xcmMetadata.originTokenInfo || !xcmMetadata.sendingValue) {
350
+ const bridgeFeeAmount = (_currentFee$feeCompon = currentFee.feeComponent.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE)) === null || _currentFee$feeCompon === void 0 ? void 0 : _currentFee$feeCompon.amount;
351
+ if (!xcmMetadata || !xcmMetadata.destinationTokenInfo || !xcmMetadata.originTokenInfo || !xcmMetadata.sendingValue || !xcmMetadata.expectedReceive) {
402
352
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
403
353
  }
404
354
  if (!bridgeFeeAmount) {
@@ -417,8 +367,8 @@ class SwapBaseHandler {
417
367
  const bridgeSelectedFeeToken = this.chainService.getAssetBySlug(currentFee.selectedFeeToken || currentFee.defaultFeeToken);
418
368
  const bnBridgeDeliveryFee = (0, _bignumber.default)(0); // todo
419
369
 
420
- const bridgeSender = (0, _utils4._reformatAddressWithChain)(params.address, this.chainService.getChainInfoByKey(bridgeFromToken.originChain));
421
- const bridgeReceiver = (0, _utils4._reformatAddressWithChain)((_params$recipient2 = params.recipient) !== null && _params$recipient2 !== void 0 ? _params$recipient2 : bridgeSender, this.chainService.getChainInfoByKey(bridgeToToken.originChain));
370
+ const bridgeSender = (0, _utils4._reformatAddressWithChain)(xcmMetadata.sender, this.chainService.getChainInfoByKey(bridgeFromToken.originChain));
371
+ const bridgeReceiver = (0, _utils4._reformatAddressWithChain)((_xcmMetadata$receiver = xcmMetadata.receiver) !== null && _xcmMetadata$receiver !== void 0 ? _xcmMetadata$receiver : bridgeSender, this.chainService.getChainInfoByKey(bridgeToToken.originChain));
422
372
  const [bridgeFromTokenBalance, bridgeFeeTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(bridgeSender, bridgeFromToken.originChain, bridgeFromToken.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM), this.balanceService.getTransferableBalance(bridgeSender, bridgeFromToken.originChain, bridgeSelectedFeeToken.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM)]);
423
373
 
424
374
  // Native token balance has already accounted for ED aka strict mode
@@ -433,7 +383,10 @@ class SwapBaseHandler {
433
383
  const swapStepInfo = params.process.steps[swapIndex];
434
384
  const swapMetadata = swapStepInfo.metadata; // todo
435
385
  const swapFee = params.process.totalFee[swapIndex];
436
- if (!swapMetadata || !swapMetadata.destinationTokenInfo || !swapMetadata.originTokenInfo || !swapMetadata.sendingValue) {
386
+ if (swapStepInfo.type !== _types.SwapStepType.SWAP) {
387
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
388
+ }
389
+ if (!swapMetadata || !swapMetadata.destinationTokenInfo || !swapMetadata.originTokenInfo || !swapMetadata.sendingValue || !swapMetadata.expectedReceive) {
437
390
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
438
391
  }
439
392
 
@@ -442,9 +395,9 @@ class SwapBaseHandler {
442
395
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
443
396
  }
444
397
  if (params.selectedQuote.aliveUntil <= +Date.now()) {
445
- return [new _TransactionError.TransactionError(_swap2.SwapErrorType.QUOTE_TIMEOUT)];
398
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.QUOTE_TIMEOUT)];
446
399
  }
447
- const swapNetworkFee = swapFee.feeComponent.find(fee => fee.feeType === _swap2.SwapFeeType.NETWORK_FEE);
400
+ const swapNetworkFee = swapFee.feeComponent.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE);
448
401
  if (!swapNetworkFee) {
449
402
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
450
403
  }
@@ -463,17 +416,226 @@ class SwapBaseHandler {
463
416
  const atLeastString = (0, _utils4.formatNumber)((0, _utils2._getTokenMinAmount)(swapToken), (0, _utils2._getAssetDecimals)(swapToken), _utils4.balanceFormatter, {
464
417
  maxNumberFormat: (0, _utils2._getAssetDecimals)(swapToken) || 6
465
418
  });
466
- return [new _TransactionError.TransactionError(_swap2.SwapErrorType.NOT_MEET_MIN_SWAP, (0, _i18next.t)(`Swap amount too small. Increase to more than ${atLeastString} ${(0, _utils2._getAssetSymbol)(swapToken)} and try again`))];
419
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.NOT_MEET_MIN_SWAP, (0, _i18next.t)(`Swap amount too small. Increase to more than ${atLeastString} ${(0, _utils2._getAssetSymbol)(swapToken)} and try again`))];
420
+ }
421
+ const swapFeeToken = this.chainService.getAssetBySlug(swapFee.selectedFeeToken || swapFee.defaultFeeToken);
422
+ const swapToChain = this.chainService.getChainInfoByKey(swapMetadata.destinationTokenInfo.originChain);
423
+ const [swapFeeTokenBalance, swapFromTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(swapMetadata.sender, swapFeeToken.originChain, swapFeeToken.slug, _KoniTypes.ExtrinsicType.SWAP), this.balanceService.getTransferableBalance(swapMetadata.sender, swapToken.originChain, swapToken.slug, _KoniTypes.ExtrinsicType.SWAP)]);
424
+ const bnSwapFromTokenBalance = (0, _bignumber.default)(swapFromTokenBalance.value).plus(bnBridgeAmount);
425
+ const bnSwapFeeTokenBalance = (0, _bignumber.default)(swapFeeTokenBalance.value);
426
+ const swapStepValidation = this.validateSwapStepV2(swapToChain, swapToken, swapReceivingToken, swapFeeToken, bnSwapValue, bnSwapReceivingAmount, bnSwapFromTokenBalance, bnSwapFeeAmount, bnSwapFeeTokenBalance, swapMetadata.receiver);
427
+ if (swapStepValidation.length > 0) {
428
+ return swapStepValidation;
429
+ }
430
+ return [];
431
+ }
432
+ async validateSwapXcmProcess(params, swapIndex, xcmIndex) {
433
+ var _currentFee$feeCompon2, _xcmMetadata$receiver2;
434
+ // Swap
435
+ const swapStepInfo = params.process.steps[swapIndex];
436
+ const swapMetadata = swapStepInfo.metadata; // todo
437
+ const swapFee = params.process.totalFee[swapIndex];
438
+ if (!swapMetadata || !swapMetadata.destinationTokenInfo || !swapMetadata.originTokenInfo || !swapMetadata.sendingValue || !swapMetadata.expectedReceive) {
439
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
440
+ }
441
+
442
+ // Validate quote
443
+ if (!params.selectedQuote) {
444
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
445
+ }
446
+ if (params.selectedQuote.aliveUntil <= +Date.now()) {
447
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.QUOTE_TIMEOUT)];
448
+ }
449
+ const swapNetworkFee = swapFee.feeComponent.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE);
450
+ if (!swapNetworkFee) {
451
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
452
+ }
453
+ const swapToken = swapMetadata.originTokenInfo;
454
+ const swapReceivingToken = swapMetadata.destinationTokenInfo;
455
+ const bnSwapReceivingAmount = (0, _bignumber.default)(swapMetadata.expectedReceive);
456
+ const bnSwapValue = (0, _bignumber.default)(swapMetadata.sendingValue);
457
+ const bnSwapFeeAmount = (0, _bignumber.default)(swapNetworkFee.amount);
458
+ if (bnSwapValue.lte((0, _utils2._getTokenMinAmount)(swapToken))) {
459
+ const atLeastString = (0, _utils4.formatNumber)((0, _utils2._getTokenMinAmount)(swapToken), (0, _utils2._getAssetDecimals)(swapToken), _utils4.balanceFormatter, {
460
+ maxNumberFormat: (0, _utils2._getAssetDecimals)(swapToken) || 6
461
+ });
462
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.NOT_MEET_MIN_SWAP, (0, _i18next.t)(`Swap amount too small. Increase to more than ${atLeastString} ${(0, _utils2._getAssetSymbol)(swapToken)} and try again`))];
463
+ }
464
+ const swapFeeToken = this.chainService.getAssetBySlug(swapFee.selectedFeeToken || swapFee.defaultFeeToken);
465
+ const swapToChain = this.chainService.getChainInfoByKey(swapMetadata.destinationTokenInfo.originChain);
466
+ const [swapFeeTokenBalance, swapFromTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(swapMetadata.sender, swapFeeToken.originChain, swapFeeToken.slug, _KoniTypes.ExtrinsicType.SWAP), this.balanceService.getTransferableBalance(swapMetadata.sender, swapToken.originChain, swapToken.slug, _KoniTypes.ExtrinsicType.SWAP)]);
467
+ const bnSwapFromTokenBalance = (0, _bignumber.default)(swapFromTokenBalance.value);
468
+ const bnSwapFeeTokenBalance = (0, _bignumber.default)(swapFeeTokenBalance.value);
469
+ const swapStepValidation = this.validateSwapStepV2(swapToChain, swapToken, swapReceivingToken, swapFeeToken, bnSwapValue, bnSwapReceivingAmount, bnSwapFromTokenBalance, bnSwapFeeAmount, bnSwapFeeTokenBalance, swapMetadata.receiver);
470
+ if (swapStepValidation.length > 0) {
471
+ return swapStepValidation;
472
+ }
473
+
474
+ // Bridge
475
+ const currentStep = params.process.steps[xcmIndex];
476
+ const xcmMetadata = currentStep.metadata;
477
+ const currentFee = params.process.totalFee[xcmIndex];
478
+ const bridgeFeeAmount = (_currentFee$feeCompon2 = currentFee.feeComponent.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE)) === null || _currentFee$feeCompon2 === void 0 ? void 0 : _currentFee$feeCompon2.amount;
479
+ if (!xcmMetadata || !xcmMetadata.destinationTokenInfo || !xcmMetadata.originTokenInfo || !xcmMetadata.sendingValue || !xcmMetadata.expectedReceive) {
480
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
481
+ }
482
+ if (!bridgeFeeAmount) {
483
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
484
+ }
485
+ const bridgeFromToken = xcmMetadata.originTokenInfo;
486
+ const bridgeToToken = xcmMetadata.destinationTokenInfo;
487
+ const fromChain = this.chainService.getChainInfoByKey(bridgeFromToken.originChain);
488
+ const toChain = this.chainService.getChainInfoByKey(bridgeToToken.originChain);
489
+ if (swapReceivingToken.slug !== bridgeFromToken.slug) {
490
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
491
+ }
492
+ if ((0, _xcmParser._isSnowBridgeXcm)(fromChain, toChain)) {
493
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED)];
494
+ }
495
+ const bnBridgeFeeAmount = (0, _bignumber.default)(bridgeFeeAmount);
496
+ const bnBridgeAmount = new _bignumber.default(xcmMetadata.sendingValue);
497
+ const bridgeToChainNativeToken = this.chainService.getNativeTokenInfo(bridgeToToken.originChain);
498
+ const bridgeSelectedFeeToken = this.chainService.getAssetBySlug(currentFee.selectedFeeToken || currentFee.defaultFeeToken);
499
+ const bnBridgeDeliveryFee = (0, _bignumber.default)(0); // todo
500
+
501
+ const bridgeSender = (0, _utils4._reformatAddressWithChain)(xcmMetadata.sender, this.chainService.getChainInfoByKey(bridgeFromToken.originChain));
502
+ const bridgeReceiver = (0, _utils4._reformatAddressWithChain)((_xcmMetadata$receiver2 = xcmMetadata.receiver) !== null && _xcmMetadata$receiver2 !== void 0 ? _xcmMetadata$receiver2 : bridgeSender, this.chainService.getChainInfoByKey(bridgeToToken.originChain));
503
+ const [bridgeFromTokenBalance, bridgeFeeTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(bridgeSender, bridgeFromToken.originChain, bridgeFromToken.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM), this.balanceService.getTransferableBalance(bridgeSender, bridgeFromToken.originChain, bridgeSelectedFeeToken.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM)]);
504
+
505
+ // Native token balance has already accounted for ED aka strict mode
506
+ const bnBridgeFromTokenBalance = new _bignumber.default(bridgeFromTokenBalance.value).plus(bnSwapReceivingAmount);
507
+ const bnBridgeFeeTokenBalance = new _bignumber.default(bridgeFeeTokenBalance.value);
508
+ const bridgeStepValidation = await this.validateBridgeStep(bridgeReceiver, bridgeFromToken, bridgeToToken, bridgeSelectedFeeToken, bridgeToChainNativeToken, bnBridgeAmount, bnBridgeFromTokenBalance, bnBridgeFeeAmount, bnBridgeFeeTokenBalance, bnBridgeDeliveryFee);
509
+ if (bridgeStepValidation.length > 0) {
510
+ return bridgeStepValidation;
511
+ }
512
+ return [];
513
+ }
514
+ async validateXcmSwapXcmProcess(params, swapIndex, xcmIndex, transitIndex) {
515
+ var _bridgeFee$feeCompone, _bridgeMetadata$recei, _transitTotalFee$feeC, _transitMetadata$rece;
516
+ // Bridge
517
+ const bridgeStep = params.process.steps[xcmIndex];
518
+ const bridgeMetadata = bridgeStep.metadata;
519
+ const bridgeFee = params.process.totalFee[xcmIndex];
520
+ const bridgeFeeAmount = (_bridgeFee$feeCompone = bridgeFee.feeComponent.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE)) === null || _bridgeFee$feeCompone === void 0 ? void 0 : _bridgeFee$feeCompone.amount;
521
+ if (!bridgeMetadata || !bridgeMetadata.destinationTokenInfo || !bridgeMetadata.originTokenInfo || !bridgeMetadata.sendingValue || !bridgeMetadata.expectedReceive) {
522
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
523
+ }
524
+ if (!bridgeFeeAmount) {
525
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
526
+ }
527
+ const bridgeFromToken = bridgeMetadata.originTokenInfo;
528
+ const bridgeToToken = bridgeMetadata.destinationTokenInfo;
529
+ const fromChain = this.chainService.getChainInfoByKey(bridgeFromToken.originChain);
530
+ const toChain = this.chainService.getChainInfoByKey(bridgeToToken.originChain);
531
+ if ((0, _xcmParser._isSnowBridgeXcm)(fromChain, toChain)) {
532
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED)];
533
+ }
534
+ const bnBridgeFeeAmount = (0, _bignumber.default)(bridgeFeeAmount);
535
+ const bnBridgeAmount = new _bignumber.default(bridgeMetadata.sendingValue);
536
+ const bridgeToChainNativeToken = this.chainService.getNativeTokenInfo(bridgeToToken.originChain);
537
+ const bridgeSelectedFeeToken = this.chainService.getAssetBySlug(bridgeFee.selectedFeeToken || bridgeFee.defaultFeeToken);
538
+ const bnBridgeDeliveryFee = (0, _bignumber.default)(0); // todo
539
+
540
+ const bridgeSender = (0, _utils4._reformatAddressWithChain)(bridgeMetadata.sender, this.chainService.getChainInfoByKey(bridgeFromToken.originChain));
541
+ const bridgeReceiver = (0, _utils4._reformatAddressWithChain)((_bridgeMetadata$recei = bridgeMetadata.receiver) !== null && _bridgeMetadata$recei !== void 0 ? _bridgeMetadata$recei : bridgeSender, this.chainService.getChainInfoByKey(bridgeToToken.originChain));
542
+ const [bridgeFromTokenBalance, bridgeFeeTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(bridgeSender, bridgeFromToken.originChain, bridgeFromToken.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM), this.balanceService.getTransferableBalance(bridgeSender, bridgeFromToken.originChain, bridgeSelectedFeeToken.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM)]);
543
+
544
+ // Native token balance has already accounted for ED aka strict mode
545
+ const bnBridgeFromTokenBalance = new _bignumber.default(bridgeFromTokenBalance.value);
546
+ const bnBridgeFeeTokenBalance = new _bignumber.default(bridgeFeeTokenBalance.value);
547
+ const bridgeStepValidation = await this.validateBridgeStep(bridgeReceiver, bridgeFromToken, bridgeToToken, bridgeSelectedFeeToken, bridgeToChainNativeToken, bnBridgeAmount, bnBridgeFromTokenBalance, bnBridgeFeeAmount, bnBridgeFeeTokenBalance, bnBridgeDeliveryFee);
548
+ if (bridgeStepValidation.length > 0) {
549
+ return bridgeStepValidation;
550
+ }
551
+
552
+ // Swap
553
+ const swapStepInfo = params.process.steps[swapIndex];
554
+ const swapMetadata = swapStepInfo.metadata; // todo
555
+ const swapFee = params.process.totalFee[swapIndex];
556
+ if (swapStepInfo.type !== _types.SwapStepType.SWAP) {
557
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
558
+ }
559
+ if (!swapMetadata || !swapMetadata.destinationTokenInfo || !swapMetadata.originTokenInfo || !swapMetadata.sendingValue || !swapMetadata.expectedReceive) {
560
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
561
+ }
562
+
563
+ // Validate quote
564
+ if (!params.selectedQuote) {
565
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
566
+ }
567
+ if (params.selectedQuote.aliveUntil <= +Date.now()) {
568
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.QUOTE_TIMEOUT)];
569
+ }
570
+ const swapNetworkFee = swapFee.feeComponent.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE);
571
+ if (!swapNetworkFee) {
572
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
573
+ }
574
+ const swapToken = swapMetadata.originTokenInfo;
575
+ const swapReceivingToken = swapMetadata.destinationTokenInfo;
576
+ const bnSwapReceivingAmount = (0, _bignumber.default)(swapMetadata.expectedReceive);
577
+ if (swapToken.slug !== bridgeToToken.slug) {
578
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
579
+ }
580
+ const bnSwapValue = (0, _bignumber.default)(swapMetadata.sendingValue);
581
+ const bnSwapFeeAmount = (0, _bignumber.default)(swapNetworkFee.amount);
582
+ if (bnSwapValue.gt(bnBridgeAmount)) {
583
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
584
+ }
585
+ if (bnSwapValue.lte((0, _utils2._getTokenMinAmount)(swapToken))) {
586
+ const atLeastString = (0, _utils4.formatNumber)((0, _utils2._getTokenMinAmount)(swapToken), (0, _utils2._getAssetDecimals)(swapToken), _utils4.balanceFormatter, {
587
+ maxNumberFormat: (0, _utils2._getAssetDecimals)(swapToken) || 6
588
+ });
589
+ return [new _TransactionError.TransactionError(_swap.SwapErrorType.NOT_MEET_MIN_SWAP, (0, _i18next.t)(`Swap amount too small. Increase to more than ${atLeastString} ${(0, _utils2._getAssetSymbol)(swapToken)} and try again`))];
467
590
  }
468
591
  const swapFeeToken = this.chainService.getAssetBySlug(swapFee.selectedFeeToken || swapFee.defaultFeeToken);
469
592
  const swapToChain = this.chainService.getChainInfoByKey(swapMetadata.destinationTokenInfo.originChain);
470
- const [swapFeeTokenBalance, swapFromTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(params.address, swapFeeToken.originChain, swapFeeToken.slug, _KoniTypes.ExtrinsicType.SWAP), this.balanceService.getTransferableBalance(params.address, swapToken.originChain, swapToken.slug, _KoniTypes.ExtrinsicType.SWAP)]);
593
+ const [swapFeeTokenBalance, swapFromTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(swapMetadata.sender, swapFeeToken.originChain, swapFeeToken.slug, _KoniTypes.ExtrinsicType.SWAP), this.balanceService.getTransferableBalance(swapMetadata.sender, swapToken.originChain, swapToken.slug, _KoniTypes.ExtrinsicType.SWAP)]);
471
594
  const bnSwapFromTokenBalance = (0, _bignumber.default)(swapFromTokenBalance.value).plus(bnBridgeAmount);
472
595
  const bnSwapFeeTokenBalance = (0, _bignumber.default)(swapFeeTokenBalance.value);
473
- const swapStepValidation = this.validateSwapStepV2(swapToChain, swapToken, swapReceivingToken, swapFeeToken, bnSwapValue, bnSwapReceivingAmount, bnSwapFromTokenBalance, bnSwapFeeAmount, bnSwapFeeTokenBalance, params.recipient);
596
+ const swapStepValidation = this.validateSwapStepV2(swapToChain, swapToken, swapReceivingToken, swapFeeToken, bnSwapValue, bnSwapReceivingAmount, bnSwapFromTokenBalance, bnSwapFeeAmount, bnSwapFeeTokenBalance, swapMetadata.receiver);
474
597
  if (swapStepValidation.length > 0) {
475
598
  return swapStepValidation;
476
599
  }
600
+
601
+ // Bridge again
602
+ const transitStep = params.process.steps[transitIndex];
603
+ const transitMetadata = transitStep.metadata;
604
+ const transitTotalFee = params.process.totalFee[transitIndex];
605
+ const transitFee = (_transitTotalFee$feeC = transitTotalFee.feeComponent.find(fee => fee.feeType === _swap.SwapFeeType.NETWORK_FEE)) === null || _transitTotalFee$feeC === void 0 ? void 0 : _transitTotalFee$feeC.amount;
606
+ if (!transitMetadata || !transitMetadata.destinationTokenInfo || !transitMetadata.originTokenInfo || !transitMetadata.sendingValue || !transitMetadata.expectedReceive) {
607
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
608
+ }
609
+ if (!transitFee) {
610
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
611
+ }
612
+ const transitFromToken = transitMetadata.originTokenInfo;
613
+ const transitToToken = transitMetadata.destinationTokenInfo;
614
+ const fromTransitChain = this.chainService.getChainInfoByKey(transitFromToken.originChain);
615
+ const toTransitChain = this.chainService.getChainInfoByKey(transitToToken.originChain);
616
+ if (swapReceivingToken.slug !== transitFromToken.slug) {
617
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
618
+ }
619
+ if ((0, _xcmParser._isSnowBridgeXcm)(fromTransitChain, toTransitChain)) {
620
+ return [new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED)];
621
+ }
622
+ const bnTransitFeeAmount = (0, _bignumber.default)(transitFee);
623
+ const bnTransitAmount = new _bignumber.default(transitMetadata.sendingValue);
624
+ const transitToChainNativeToken = this.chainService.getNativeTokenInfo(transitToToken.originChain);
625
+ const transitSelectedFeeToken = this.chainService.getAssetBySlug(transitTotalFee.selectedFeeToken || transitTotalFee.defaultFeeToken);
626
+ const bnTransitDeliveryFee = (0, _bignumber.default)(0); // todo
627
+
628
+ const transitSender = (0, _utils4._reformatAddressWithChain)(transitMetadata.sender, this.chainService.getChainInfoByKey(transitFromToken.originChain));
629
+ const transitReceiver = (0, _utils4._reformatAddressWithChain)((_transitMetadata$rece = transitMetadata.receiver) !== null && _transitMetadata$rece !== void 0 ? _transitMetadata$rece : transitSender, this.chainService.getChainInfoByKey(transitToToken.originChain));
630
+ const [transitFromTokenBalance, transitFeeTokenBalance] = await Promise.all([this.balanceService.getTransferableBalance(transitSender, transitFromToken.originChain, transitFromToken.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM), this.balanceService.getTransferableBalance(transitSender, transitFromToken.originChain, transitSelectedFeeToken.slug, _KoniTypes.ExtrinsicType.TRANSFER_XCM)]);
631
+
632
+ // Native token balance has already accounted for ED aka strict mode
633
+ const bnTransitFromTokenBalance = new _bignumber.default(transitFromTokenBalance.value).plus(bnSwapReceivingAmount);
634
+ const bnTransitFeeTokenBalance = new _bignumber.default(transitFeeTokenBalance.value);
635
+ const transitStepValidation = await this.validateBridgeStep(transitReceiver, transitFromToken, transitToToken, transitSelectedFeeToken, transitToChainNativeToken, bnTransitAmount, bnTransitFromTokenBalance, bnTransitFeeAmount, bnTransitFeeTokenBalance, bnTransitDeliveryFee);
636
+ if (transitStepValidation.length > 0) {
637
+ return transitStepValidation;
638
+ }
477
639
  return [];
478
640
  }
479
641
  get name() {