@subwallet/extension-base 1.3.29-0 → 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 (82) 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 +9 -3
  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/chain-service/constants.js +1 -1
  12. package/cjs/services/earning-service/handlers/base.js +6 -3
  13. package/cjs/services/earning-service/handlers/native-staking/base.js +4 -1
  14. package/cjs/services/earning-service/handlers/native-staking/dtao.js +68 -50
  15. package/cjs/services/earning-service/handlers/native-staking/tao.js +12 -2
  16. package/cjs/services/earning-service/service.js +2 -1
  17. package/cjs/services/fee-service/utils/index.js +16 -4
  18. package/cjs/services/inapp-notification-service/index.js +19 -13
  19. package/cjs/services/swap-service/handler/asset-hub/handler.js +61 -314
  20. package/cjs/services/swap-service/handler/base-handler.js +393 -231
  21. package/cjs/services/swap-service/handler/chainflip-handler.js +18 -40
  22. package/cjs/services/swap-service/handler/hydradx-handler.js +77 -269
  23. package/cjs/services/swap-service/handler/simpleswap-handler.js +27 -48
  24. package/cjs/services/swap-service/handler/uniswap-handler.js +33 -54
  25. package/cjs/services/swap-service/index.js +154 -143
  26. package/cjs/services/swap-service/utils.js +107 -17
  27. package/cjs/services/transaction-service/index.js +1 -1
  28. package/cjs/types/swap/index.js +13 -1
  29. package/cjs/utils/swap.js +5 -1
  30. package/constants/blocked-actions.d.ts +1 -1
  31. package/constants/blocked-actions.js +1 -1
  32. package/constants/remind-notification-time.d.ts +1 -1
  33. package/constants/remind-notification-time.js +1 -1
  34. package/core/logic-validation/swap.d.ts +15 -0
  35. package/core/logic-validation/swap.js +60 -4
  36. package/core/utils.js +9 -3
  37. package/koni/background/handlers/Extension.d.ts +0 -1
  38. package/koni/background/handlers/Extension.js +6 -83
  39. package/package.json +6 -12
  40. package/packageInfo.js +1 -1
  41. package/services/balance-service/transfer/xcm/availBridge.js +6 -6
  42. package/services/base/types.d.ts +0 -4
  43. package/services/chain-service/constants.js +1 -1
  44. package/services/earning-service/handlers/base.d.ts +4 -3
  45. package/services/earning-service/handlers/base.js +6 -4
  46. package/services/earning-service/handlers/native-staking/base.js +4 -1
  47. package/services/earning-service/handlers/native-staking/dtao.d.ts +9 -6
  48. package/services/earning-service/handlers/native-staking/dtao.js +69 -48
  49. package/services/earning-service/handlers/native-staking/tao.js +12 -2
  50. package/services/earning-service/service.d.ts +2 -1
  51. package/services/earning-service/service.js +2 -1
  52. package/services/fee-service/utils/index.d.ts +1 -0
  53. package/services/fee-service/utils/index.js +14 -4
  54. package/services/inapp-notification-service/index.js +13 -7
  55. package/services/swap-service/handler/asset-hub/handler.d.ts +2 -9
  56. package/services/swap-service/handler/asset-hub/handler.js +64 -317
  57. package/services/swap-service/handler/base-handler.d.ts +6 -9
  58. package/services/swap-service/handler/base-handler.js +391 -229
  59. package/services/swap-service/handler/chainflip-handler.d.ts +2 -4
  60. package/services/swap-service/handler/chainflip-handler.js +15 -37
  61. package/services/swap-service/handler/hydradx-handler.d.ts +3 -10
  62. package/services/swap-service/handler/hydradx-handler.js +78 -270
  63. package/services/swap-service/handler/simpleswap-handler.d.ts +2 -4
  64. package/services/swap-service/handler/simpleswap-handler.js +24 -45
  65. package/services/swap-service/handler/uniswap-handler.d.ts +4 -6
  66. package/services/swap-service/handler/uniswap-handler.js +25 -46
  67. package/services/swap-service/index.d.ts +8 -14
  68. package/services/swap-service/index.js +141 -129
  69. package/services/swap-service/utils.d.ts +11 -3
  70. package/services/swap-service/utils.js +96 -15
  71. package/services/transaction-service/index.js +2 -2
  72. package/types/service-base.d.ts +2 -3
  73. package/types/swap/index.d.ts +25 -9
  74. package/types/swap/index.js +10 -0
  75. package/types/transaction/process.d.ts +19 -0
  76. package/types/yield/actions/join/submit.d.ts +4 -1
  77. package/types/yield/actions/others.d.ts +2 -0
  78. package/utils/swap.d.ts +3 -0
  79. package/utils/swap.js +3 -0
  80. package/cjs/services/swap-service/interface.js +0 -14
  81. package/services/swap-service/interface.d.ts +0 -9
  82. package/services/swap-service/interface.js +0 -8
@@ -8,14 +8,10 @@ exports.AssetHubSwapHandler = void 0;
8
8
  var _SwapError = require("@subwallet/extension-base/background/errors/SwapError");
9
9
  var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
10
10
  var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
11
- var _constants = require("@subwallet/extension-base/constants");
12
- var _swap = require("@subwallet/extension-base/core/logic-validation/swap");
13
- var _xcm = require("@subwallet/extension-base/services/balance-service/transfer/xcm");
14
11
  var _utils = require("@subwallet/extension-base/services/chain-service/utils");
15
- var _interface = require("@subwallet/extension-base/services/swap-service/interface");
16
12
  var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
17
13
  var _types = require("@subwallet/extension-base/types");
18
- var _getId = require("@subwallet/extension-base/utils/getId");
14
+ var _utils3 = require("@subwallet/extension-base/utils");
19
15
  var _bignumber = _interopRequireDefault(require("bignumber.js"));
20
16
  var _baseHandler = require("../base-handler");
21
17
  var _router = require("./router");
@@ -68,235 +64,70 @@ class AssetHubSwapHandler {
68
64
  if (!chainState.active) {
69
65
  await this.chainService.enableChain(this.chain);
70
66
  }
71
- const substrateApi = this.chainService.getSubstrateApi(this.chain);
72
- await substrateApi.api.isReady;
73
67
  this.router = new _router.AssetHubRouter(this.chain, this.chainService);
74
68
  this.isReady = true;
75
69
  }
76
- async getXcmStep(params) {
77
- const bnAmount = new _bignumber.default(params.request.fromAmount);
78
- const fromAsset = this.chainService.getAssetBySlug(params.request.pair.from);
79
- const fromAssetBalance = await this.balanceService.getTransferableBalance(params.request.address, fromAsset.originChain, fromAsset.slug);
80
- const bnFromAssetBalance = new _bignumber.default(fromAssetBalance.value);
81
- if (bnFromAssetBalance.gte(bnAmount)) {
82
- return undefined; // enough balance, no need to xcm
83
- }
84
-
85
- const alternativeAssetSlug = (0, _utils2.getSwapAlternativeAsset)(params.request.pair);
86
- if (!alternativeAssetSlug) {
87
- return undefined;
88
- }
89
- const alternativeAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
90
- const alternativeAssetBalance = await this.balanceService.getTransferableBalance(params.request.address, alternativeAsset.originChain, alternativeAsset.slug);
91
- const bnAlternativeAssetBalance = new _bignumber.default(alternativeAssetBalance.value);
92
- if (bnAlternativeAssetBalance.lte(0)) {
93
- return undefined;
94
- }
95
- try {
96
- const alternativeChainInfo = this.chainService.getChainInfoByKey(alternativeAsset.originChain);
97
- const originalChainInfo = this.chainService.getChainInfoByKey(this.chain);
98
- const xcmOriginSubstrateApi = await this.chainService.getSubstrateApi(alternativeAsset.originChain).isReady;
99
- const id = (0, _getId.getId)();
100
- const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee(id, alternativeChainInfo.slug, 'substrate');
101
- const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
102
- originTokenInfo: alternativeAsset,
103
- destinationTokenInfo: fromAsset,
104
- // Mock sending value to get payment info
105
- sendingValue: bnAmount.toString(),
106
- recipient: params.request.address,
107
- sender: params.request.address,
108
- feeInfo: feeInfo,
109
- substrateApi: xcmOriginSubstrateApi,
110
- destinationChain: originalChainInfo,
111
- originChain: alternativeChainInfo
112
- });
113
- const _xcmFeeInfo = await xcmTransfer.paymentInfo(params.request.address);
114
- const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
115
- const fee = {
116
- feeComponent: [{
117
- feeType: _types.SwapFeeType.NETWORK_FEE,
118
- amount: Math.round(xcmFeeInfo.partialFee * _constants.XCM_MIN_AMOUNT_RATIO).toString(),
119
- tokenSlug: (0, _utils._getChainNativeTokenSlug)(alternativeChainInfo)
120
- }],
121
- defaultFeeToken: (0, _utils._getChainNativeTokenSlug)(alternativeChainInfo),
122
- feeOptions: [(0, _utils._getChainNativeTokenSlug)(alternativeChainInfo)]
123
- };
124
- let bnTransferAmount = bnAmount.minus(bnFromAssetBalance);
125
- if ((0, _utils._isNativeToken)(alternativeAsset)) {
126
- const bnXcmFee = new _bignumber.default(fee.feeComponent[0].amount); // xcm fee is paid in native token but swap token is not always native token
127
-
128
- bnTransferAmount = bnTransferAmount.plus(bnXcmFee);
129
- }
130
- const step = {
131
- metadata: {
132
- sendingValue: bnTransferAmount.toString(),
133
- originTokenInfo: alternativeAsset,
134
- destinationTokenInfo: fromAsset
135
- },
136
- name: `Transfer ${alternativeAsset.symbol} from ${alternativeChainInfo.name}`,
137
- type: _types.CommonStepType.XCM
138
- };
139
- return [step, fee];
140
- } catch (e) {
141
- console.error('Error creating xcm step', e);
142
- return undefined;
143
- }
144
- }
145
- async getSubmitStep(params) {
146
- if (params.selectedQuote) {
147
- const submitStep = {
148
- name: 'Swap',
149
- type: _types.SwapStepType.SWAP
150
- };
151
- return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
152
- }
153
- return Promise.resolve(undefined);
154
- }
155
- async getXcmStepV2(params) {
156
- var _params$path$find;
157
- // todo: improve this function for Round 2
158
-
159
- const xcmPairInfo = (_params$path$find = params.path.find((step, i) => i === 0 && step.action === _interface.DynamicSwapType.BRIDGE)) === null || _params$path$find === void 0 ? void 0 : _params$path$find.pair;
160
- if (!xcmPairInfo) {
161
- return undefined;
162
- }
163
- const fromTokenInfo = this.chainService.getAssetBySlug(xcmPairInfo.from);
164
- const toTokenInfo = this.chainService.getAssetBySlug(xcmPairInfo.to);
165
- const fromChainInfo = this.chainService.getChainInfoByKey(fromTokenInfo.originChain);
166
- const toChainInfo = this.chainService.getChainInfoByKey(toTokenInfo.originChain);
167
- const substrateApi = await this.chainService.getSubstrateApi(fromTokenInfo.originChain).isReady;
168
- if (!fromChainInfo || !toChainInfo || !fromChainInfo || !toChainInfo) {
169
- throw Error('Token and chain not found');
170
- }
171
- try {
172
- const id = (0, _getId.getId)();
173
- const [feeInfo, toTokenBalance] = await Promise.all([this.swapBaseHandler.feeService.subscribeChainFee(id, fromTokenInfo.originChain, 'substrate'), this.balanceService.getTotalBalance(params.request.address, toTokenInfo.originChain, toTokenInfo.slug, _KoniTypes.ExtrinsicType.TRANSFER_BALANCE)]);
174
- const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
175
- originTokenInfo: fromTokenInfo,
176
- destinationTokenInfo: toTokenInfo,
177
- // Mock sending value to get payment info
178
- sendingValue: params.request.fromAmount,
179
- recipient: params.request.address,
180
- substrateApi: substrateApi,
181
- sender: params.request.address,
182
- originChain: fromChainInfo,
183
- destinationChain: toChainInfo,
184
- feeInfo
185
- });
186
- const _xcmFeeInfo = await xcmTransfer.paymentInfo(params.request.address);
187
- const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
188
- const fee = {
189
- feeComponent: [{
190
- feeType: _types.SwapFeeType.NETWORK_FEE,
191
- amount: Math.ceil(xcmFeeInfo.partialFee * _utils2.FEE_RATE_MULTIPLIER.high).toString(),
192
- tokenSlug: (0, _utils._getChainNativeTokenSlug)(fromChainInfo)
193
- }],
194
- defaultFeeToken: (0, _utils._getChainNativeTokenSlug)(fromChainInfo),
195
- feeOptions: [(0, _utils._getChainNativeTokenSlug)(fromChainInfo)]
196
- };
197
- let bnTransferAmount = new _bignumber.default(params.request.fromAmount);
198
- if ((0, _utils._isNativeToken)(fromTokenInfo)) {
199
- // xcm fee is paid in native token but swap token is not always native token
200
- // add amount of fee into sending value to ensure has enough token to swap
201
- const bnXcmFee = new _bignumber.default(fee.feeComponent[0].amount);
202
- bnTransferAmount = bnTransferAmount.plus(bnXcmFee);
203
- } else {
204
- bnTransferAmount = bnTransferAmount.plus((0, _bignumber.default)((0, _utils._getTokenMinAmount)(toTokenInfo)).multipliedBy(_utils2.FEE_RATE_MULTIPLIER.medium));
205
- }
206
- if ((0, _bignumber.default)(toTokenBalance.value).lte(0)) {
207
- bnTransferAmount = bnTransferAmount.plus((0, _utils._getTokenMinAmount)(toTokenInfo));
208
- }
209
- const step = {
210
- metadata: {
211
- sendingValue: bnTransferAmount.toString(),
212
- originTokenInfo: fromTokenInfo,
213
- destinationTokenInfo: toTokenInfo
214
- },
215
- name: `Transfer ${fromTokenInfo.symbol} from ${fromChainInfo.name}`,
216
- type: _types.CommonStepType.XCM
217
- };
218
- return [step, fee];
219
- } catch (e) {
220
- console.error('Error creating xcm step', e);
221
- return undefined;
222
- }
223
- }
224
- async getSwapStepV2(params) {
225
- var _params$path$find2;
226
- const swapPairInfo = (_params$path$find2 = params.path.find(step => step.action === _interface.DynamicSwapType.SWAP)) === null || _params$path$find2 === void 0 ? void 0 : _params$path$find2.pair;
227
- if (!swapPairInfo) {
70
+ async getSubmitStep(params, stepIndex) {
71
+ const {
72
+ path,
73
+ request: {
74
+ fromAmount
75
+ },
76
+ selectedQuote
77
+ } = params;
78
+ const stepData = path[stepIndex];
79
+ if (stepData.action !== _types.DynamicSwapType.SWAP) {
228
80
  return Promise.resolve(undefined);
229
81
  }
230
- if (params.selectedQuote) {
231
- const submitStep = {
232
- name: 'Swap',
233
- type: _types.SwapStepType.SWAP,
234
- metadata: {
235
- sendingValue: params.request.fromAmount.toString(),
236
- originTokenInfo: this.chainService.getAssetBySlug(swapPairInfo.from),
237
- destinationTokenInfo: this.chainService.getAssetBySlug(swapPairInfo.to)
238
- }
239
- };
240
- return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
82
+ const swapPairInfo = stepData.pair;
83
+ if (!swapPairInfo || !selectedQuote) {
84
+ return Promise.resolve(undefined);
241
85
  }
242
- return Promise.resolve(undefined);
243
- }
244
- generateOptimalProcess(params) {
245
- return this.swapBaseHandler.generateOptimalProcess(params, [this.getXcmStep.bind(this), this.getSubmitStep.bind(this)]);
86
+ const originTokenInfo = this.chainService.getAssetBySlug(swapPairInfo.from);
87
+ const destinationTokenInfo = this.chainService.getAssetBySlug(swapPairInfo.to);
88
+ const originChain = this.chainService.getChainInfoByKey(originTokenInfo.originChain);
89
+ const destinationChain = this.chainService.getChainInfoByKey(destinationTokenInfo.originChain);
90
+ const actionList = JSON.stringify(path.map(step => step.action));
91
+ const xcmSwapXcm = actionList === JSON.stringify([_types.DynamicSwapType.BRIDGE, _types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
92
+ const swapXcm = actionList === JSON.stringify([_types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
93
+ const needModifyData = swapXcm || xcmSwapXcm;
94
+ let bnSendingValue = (0, _bignumber.default)(fromAmount);
95
+ let bnExpectedReceive = (0, _bignumber.default)(selectedQuote.toAmount);
96
+ const sender = (0, _utils3._reformatAddressWithChain)(params.request.address, originChain);
97
+ let receiver = (0, _utils3._reformatAddressWithChain)(params.request.recipient || params.request.address, destinationChain);
98
+ if (needModifyData) {
99
+ bnSendingValue = bnSendingValue.multipliedBy(_utils2.DEFAULT_EXCESS_AMOUNT_WEIGHT);
100
+ bnExpectedReceive = bnExpectedReceive.multipliedBy(_utils2.DEFAULT_EXCESS_AMOUNT_WEIGHT);
101
+ receiver = (0, _utils3._reformatAddressWithChain)(params.request.address, destinationChain);
102
+ }
103
+ const submitStep = {
104
+ name: 'Swap',
105
+ type: _types.SwapStepType.SWAP,
106
+ // @ts-ignore
107
+ metadata: {
108
+ sendingValue: bnSendingValue.toFixed(0, 1),
109
+ expectedReceive: bnExpectedReceive.toFixed(0, 1),
110
+ originTokenInfo,
111
+ destinationTokenInfo,
112
+ sender,
113
+ receiver,
114
+ version: 2
115
+ }
116
+ };
117
+ return Promise.resolve([submitStep, selectedQuote.feeInfo]);
246
118
  }
247
119
  generateOptimalProcessV2(params) {
248
120
  const stepFuncList = params.path.map(step => {
249
- if (step.action === _interface.DynamicSwapType.BRIDGE) {
250
- return this.getXcmStepV2.bind(this);
121
+ if (step.action === _types.DynamicSwapType.SWAP) {
122
+ return this.getSubmitStep.bind(this);
251
123
  }
252
- if (step.action === _interface.DynamicSwapType.SWAP) {
253
- return this.getSwapStepV2.bind(this);
124
+ if (step.action === _types.DynamicSwapType.BRIDGE) {
125
+ return this.swapBaseHandler.getBridgeStep.bind(this.swapBaseHandler);
254
126
  }
255
127
  throw new Error(`Error generating optimal process: Action ${step.action} is not supported`);
256
128
  });
257
129
  return this.swapBaseHandler.generateOptimalProcessV2(params, stepFuncList);
258
130
  }
259
- async handleXcmStep(params) {
260
- const briefXcmStep = params.process.steps[params.currentStep].metadata;
261
- if (!briefXcmStep || !briefXcmStep.originTokenInfo || !briefXcmStep.destinationTokenInfo || !briefXcmStep.sendingValue) {
262
- throw new Error('XCM metadata error');
263
- }
264
- const originAsset = briefXcmStep.originTokenInfo;
265
- const destinationAsset = briefXcmStep.destinationTokenInfo;
266
- const originChain = this.chainService.getChainInfoByKey(originAsset.originChain);
267
- const destinationChain = this.chainService.getChainInfoByKey(destinationAsset.originChain);
268
- const substrateApi = this.chainService.getSubstrateApi(originAsset.originChain);
269
- const chainApi = await substrateApi.isReady;
270
- const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), originAsset.originChain, 'substrate');
271
- const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
272
- originTokenInfo: originAsset,
273
- destinationTokenInfo: destinationAsset,
274
- sendingValue: briefXcmStep.sendingValue,
275
- recipient: params.address,
276
- substrateApi: chainApi,
277
- sender: params.address,
278
- destinationChain,
279
- originChain,
280
- feeInfo
281
- });
282
- const xcmData = {
283
- originNetworkKey: originAsset.originChain,
284
- destinationNetworkKey: destinationAsset.originChain,
285
- from: params.address,
286
- to: params.address,
287
- value: briefXcmStep.sendingValue,
288
- tokenSlug: originAsset.slug,
289
- showExtraWarning: true
290
- };
291
- return {
292
- txChain: originAsset.originChain,
293
- extrinsic: xcmTransfer,
294
- transferNativeAmount: (0, _utils._isNativeToken)(originAsset) ? briefXcmStep.sendingValue : '0',
295
- extrinsicType: _KoniTypes.ExtrinsicType.TRANSFER_XCM,
296
- chainType: _KoniTypes.ChainType.SUBSTRATE,
297
- txData: xcmData
298
- };
299
- }
300
131
  async handleSubmitStep(params) {
301
132
  var _this$router;
302
133
  const metadata = params.process.steps[params.currentStep].metadata;
@@ -312,17 +143,11 @@ class AssetHubSwapHandler {
312
143
  process: params.process
313
144
  };
314
145
  const paths = params.quote.route.path.map(slug => this.chainService.getAssetBySlug(slug));
315
- const {
316
- fromAmount,
317
- toAmount
318
- } = params.quote;
319
-
320
- // todo: move to gen process
321
- const minReceive = new _bignumber.default(1 - params.slippage).times(toAmount).integerValue(_bignumber.default.ROUND_DOWN);
322
- if (!params.address || !paths || !fromAmount || !minReceive) {
146
+ const minReceive = (0, _bignumber.default)((0, _utils2.getAmountAfterSlippage)(metadata.expectedReceive, params.slippage));
147
+ if (!params.address || !paths || !minReceive) {
323
148
  throw new _SwapError.SwapError(_types.SwapErrorType.UNKNOWN);
324
149
  }
325
- const extrinsic = await ((_this$router = this.router) === null || _this$router === void 0 ? void 0 : _this$router.buildSwapExtrinsic(paths, params.address, fromAmount, minReceive.toString()));
150
+ const extrinsic = await ((_this$router = this.router) === null || _this$router === void 0 ? void 0 : _this$router.buildSwapExtrinsic(paths, params.address, metadata.sendingValue, minReceive.toString()));
326
151
  return {
327
152
  txChain: fromAsset.originChain,
328
153
  txData,
@@ -341,90 +166,13 @@ class AssetHubSwapHandler {
341
166
  const type = process.steps[currentStep].type;
342
167
  switch (type) {
343
168
  case _types.CommonStepType.XCM:
344
- return this.handleXcmStep(params);
169
+ return this.swapBaseHandler.handleBridgeStep(params);
345
170
  case _types.SwapStepType.SWAP:
346
171
  return this.handleSubmitStep(params);
347
172
  default:
348
173
  return Promise.reject(new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED));
349
174
  }
350
175
  }
351
- async validateSwapStep(params, isXcmOk, stepIndex) {
352
- // check swap quote timestamp
353
- // check balance to pay transaction fee
354
- // check balance against spending amount
355
- if (!params.selectedQuote) {
356
- return Promise.resolve([new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)]);
357
- }
358
- const selectedQuote = params.selectedQuote;
359
- const currentTimestamp = +Date.now();
360
- if (selectedQuote.aliveUntil <= currentTimestamp) {
361
- return Promise.resolve([new _TransactionError.TransactionError(_types.SwapErrorType.QUOTE_TIMEOUT)]);
362
- }
363
- const stepFee = params.process.totalFee[stepIndex].feeComponent;
364
- const networkFee = stepFee.find(fee => fee.feeType === _types.SwapFeeType.NETWORK_FEE);
365
- if (!networkFee) {
366
- return Promise.resolve([new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)]);
367
- }
368
- const fromAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.from);
369
- const feeTokenInfo = this.chainService.getAssetBySlug(networkFee.tokenSlug);
370
- const feeTokenChain = this.chainService.getChainInfoByKey(feeTokenInfo.originChain);
371
- const {
372
- fromAmount,
373
- minSwap
374
- } = params.selectedQuote;
375
- const [feeTokenBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTransferableBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug), this.balanceService.getTransferableBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
376
- const balanceError = (0, _swap._validateBalanceToSwapOnAssetHub)(fromAsset, feeTokenInfo, feeTokenChain, networkFee.amount, fromAssetBalance.value, feeTokenBalance.value, fromAmount, isXcmOk, minSwap);
377
- if (balanceError) {
378
- return Promise.resolve([balanceError]);
379
- }
380
- if (!params.recipient) {
381
- return Promise.resolve([]);
382
- }
383
- const toAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.to);
384
- const toAssetChain = this.chainService.getChainInfoByKey(toAsset.originChain);
385
- const recipientError = (0, _swap._validateSwapRecipient)(toAssetChain, params.recipient);
386
- if (recipientError) {
387
- return Promise.resolve([recipientError]);
388
- }
389
- return Promise.resolve([]);
390
- }
391
- async validateSwapProcess(params) {
392
- const amount = params.selectedQuote.fromAmount;
393
- const bnAmount = new _bignumber.default(amount);
394
- if (bnAmount.lte(0)) {
395
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
396
- }
397
- const swapStep = params.process.steps.find(item => item.type === _types.SwapStepType.SWAP);
398
- if (!swapStep) {
399
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR, 'Swap step not found')];
400
- }
401
- let isXcmOk = false;
402
- const currentStep = params.currentStep;
403
- for (const [index, step] of params.process.steps.entries()) {
404
- if (currentStep > index) {
405
- continue;
406
- }
407
- const getErrors = async () => {
408
- switch (step.type) {
409
- case _types.CommonStepType.DEFAULT:
410
- return Promise.resolve([]);
411
- case _types.CommonStepType.XCM:
412
- return this.swapBaseHandler.validateXcmStepV2(params, index);
413
- case _types.SwapStepType.SWAP:
414
- return this.validateSwapStep(params, isXcmOk, index);
415
- default:
416
- return Promise.reject(new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED));
417
- }
418
- };
419
- const errors = await getErrors();
420
- if (errors.length) {
421
- return errors;
422
- } else if (step.type === _types.CommonStepType.XCM) {
423
- isXcmOk = true;
424
- }
425
- }
426
- return [];
427
- }
428
176
  async validateSwapProcessV2(params) {
429
177
  // todo: recheck address and recipient format in params
430
178
  const {
@@ -437,24 +185,23 @@ class AssetHubSwapHandler {
437
185
  if ((0, _bignumber.default)(selectedQuote.fromAmount).lte(0)) {
438
186
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
439
187
  }
440
- const actionList = process.steps.map(step => step.type);
441
- const [firstStep, secondStep, thirdStep, fourthStep, fifthStep] = actionList;
442
- const swap = firstStep === _types.CommonStepType.DEFAULT && secondStep === _types.SwapStepType.SWAP && !thirdStep;
443
- const swapXcm = firstStep === _types.CommonStepType.DEFAULT && secondStep === _types.SwapStepType.SWAP && thirdStep === _types.CommonStepType.XCM && !fourthStep;
444
- const xcmSwap = firstStep === _types.CommonStepType.DEFAULT && secondStep === _types.CommonStepType.XCM && thirdStep === _types.SwapStepType.SWAP && !fourthStep;
445
- const xcmSwapXcm = firstStep === _types.CommonStepType.DEFAULT && secondStep === _types.CommonStepType.XCM && thirdStep === _types.SwapStepType.SWAP && fourthStep === _types.CommonStepType.XCM && !fifthStep;
188
+ const actionList = JSON.stringify(process.path.map(step => step.action));
189
+ const swap = actionList === JSON.stringify([_types.DynamicSwapType.SWAP]);
190
+ const swapXcm = actionList === JSON.stringify([_types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
191
+ const xcmSwap = actionList === JSON.stringify([_types.DynamicSwapType.BRIDGE, _types.DynamicSwapType.SWAP]);
192
+ const xcmSwapXcm = actionList === JSON.stringify([_types.DynamicSwapType.BRIDGE, _types.DynamicSwapType.SWAP, _types.DynamicSwapType.BRIDGE]);
446
193
  if (swap) {
447
194
  return this.swapBaseHandler.validateSwapOnlyProcess(params, 1); // todo: create interface for input request
448
195
  }
449
196
 
450
197
  if (swapXcm) {
451
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
198
+ return this.swapBaseHandler.validateSwapXcmProcess(params, 1, 2);
452
199
  }
453
200
  if (xcmSwap) {
454
201
  return this.swapBaseHandler.validateXcmSwapProcess(params, 2, 1);
455
202
  }
456
203
  if (xcmSwapXcm) {
457
- return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
204
+ return this.swapBaseHandler.validateXcmSwapXcmProcess(params, 2, 1, 3);
458
205
  }
459
206
  return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
460
207
  }