@subwallet/extension-base 1.3.26-0 → 1.3.27-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/background/KoniTypes.d.ts +4 -3
- package/background/warnings/TransactionWarning.d.ts +2 -0
- package/background/warnings/TransactionWarning.js +16 -1
- package/cjs/background/warnings/TransactionWarning.js +15 -0
- package/cjs/core/logic-validation/index.js +32 -1
- package/cjs/core/utils.js +25 -3
- package/cjs/koni/background/handlers/Extension.js +86 -94
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/chain-service/utils/patch.js +1 -1
- package/cjs/services/swap-service/handler/asset-hub/handler.js +182 -40
- package/cjs/services/swap-service/handler/asset-hub/utils.js +3 -0
- package/cjs/services/swap-service/handler/base-handler.js +326 -12
- package/cjs/services/swap-service/handler/chainflip-handler.js +80 -16
- package/cjs/services/swap-service/handler/hydradx-handler.js +174 -30
- package/cjs/services/swap-service/handler/simpleswap-handler.js +50 -1
- package/cjs/services/swap-service/handler/uniswap-handler.js +47 -1
- package/cjs/services/swap-service/index.js +191 -27
- package/cjs/services/swap-service/interface.js +14 -0
- package/cjs/services/swap-service/utils.js +81 -5
- package/cjs/services/transaction-service/utils.js +4 -1
- package/core/logic-validation/index.d.ts +4 -0
- package/core/logic-validation/index.js +22 -1
- package/core/utils.d.ts +3 -0
- package/core/utils.js +22 -2
- package/koni/background/handlers/Extension.d.ts +2 -2
- package/koni/background/handlers/Extension.js +20 -28
- package/package.json +12 -7
- package/packageInfo.js +1 -1
- package/services/balance-service/helpers/process.d.ts +3 -3
- package/services/balance-service/index.d.ts +2 -3
- package/services/chain-service/utils/patch.js +1 -1
- package/services/swap-service/handler/asset-hub/handler.d.ts +6 -3
- package/services/swap-service/handler/asset-hub/handler.js +170 -28
- package/services/swap-service/handler/asset-hub/utils.js +3 -0
- package/services/swap-service/handler/base-handler.d.ts +12 -3
- package/services/swap-service/handler/base-handler.js +329 -15
- package/services/swap-service/handler/chainflip-handler.d.ts +4 -3
- package/services/swap-service/handler/chainflip-handler.js +74 -10
- package/services/swap-service/handler/hydradx-handler.d.ts +8 -3
- package/services/swap-service/handler/hydradx-handler.js +176 -32
- package/services/swap-service/handler/simpleswap-handler.d.ts +4 -2
- package/services/swap-service/handler/simpleswap-handler.js +50 -1
- package/services/swap-service/handler/uniswap-handler.d.ts +4 -2
- package/services/swap-service/handler/uniswap-handler.js +47 -1
- package/services/swap-service/index.d.ts +15 -5
- package/services/swap-service/index.js +182 -18
- package/services/swap-service/interface.d.ts +9 -0
- package/services/swap-service/interface.js +8 -0
- package/services/swap-service/utils.d.ts +9 -1
- package/services/swap-service/utils.js +74 -4
- package/services/transaction-service/utils.js +4 -1
- package/types/service-base.d.ts +6 -2
- package/types/swap/index.d.ts +34 -6
- package/types/transaction/process.d.ts +0 -6
|
@@ -5,16 +5,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.AssetHubSwapHandler = void 0;
|
|
8
|
+
var _SwapError = require("@subwallet/extension-base/background/errors/SwapError");
|
|
8
9
|
var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
|
|
9
10
|
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
10
11
|
var _constants = require("@subwallet/extension-base/constants");
|
|
11
12
|
var _swap = require("@subwallet/extension-base/core/logic-validation/swap");
|
|
12
13
|
var _xcm = require("@subwallet/extension-base/services/balance-service/transfer/xcm");
|
|
13
14
|
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
15
|
+
var _interface = require("@subwallet/extension-base/services/swap-service/interface");
|
|
14
16
|
var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
|
|
15
17
|
var _types = require("@subwallet/extension-base/types");
|
|
16
|
-
var _serviceBase = require("@subwallet/extension-base/types/service-base");
|
|
17
|
-
var _swap2 = require("@subwallet/extension-base/types/swap");
|
|
18
18
|
var _getId = require("@subwallet/extension-base/utils/getId");
|
|
19
19
|
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
20
20
|
var _baseHandler = require("../base-handler");
|
|
@@ -29,13 +29,13 @@ class AssetHubSwapHandler {
|
|
|
29
29
|
const providerSlug = function () {
|
|
30
30
|
switch (chain) {
|
|
31
31
|
case 'statemint':
|
|
32
|
-
return
|
|
32
|
+
return _types.SwapProviderId.POLKADOT_ASSET_HUB;
|
|
33
33
|
case 'statemine':
|
|
34
|
-
return
|
|
34
|
+
return _types.SwapProviderId.KUSAMA_ASSET_HUB;
|
|
35
35
|
case 'westend_assethub':
|
|
36
|
-
return
|
|
36
|
+
return _types.SwapProviderId.WESTEND_ASSET_HUB;
|
|
37
37
|
default:
|
|
38
|
-
return
|
|
38
|
+
return _types.SwapProviderId.ROCOCO_ASSET_HUB;
|
|
39
39
|
}
|
|
40
40
|
}();
|
|
41
41
|
this.swapBaseHandler = new _baseHandler.SwapBaseHandler({
|
|
@@ -114,7 +114,7 @@ class AssetHubSwapHandler {
|
|
|
114
114
|
const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
|
|
115
115
|
const fee = {
|
|
116
116
|
feeComponent: [{
|
|
117
|
-
feeType:
|
|
117
|
+
feeType: _types.SwapFeeType.NETWORK_FEE,
|
|
118
118
|
amount: Math.round(xcmFeeInfo.partialFee * _constants.XCM_MIN_AMOUNT_RATIO).toString(),
|
|
119
119
|
tokenSlug: (0, _utils._getChainNativeTokenSlug)(alternativeChainInfo)
|
|
120
120
|
}],
|
|
@@ -134,7 +134,7 @@ class AssetHubSwapHandler {
|
|
|
134
134
|
destinationTokenInfo: fromAsset
|
|
135
135
|
},
|
|
136
136
|
name: `Transfer ${alternativeAsset.symbol} from ${alternativeChainInfo.name}`,
|
|
137
|
-
type:
|
|
137
|
+
type: _types.CommonStepType.XCM
|
|
138
138
|
};
|
|
139
139
|
return [step, fee];
|
|
140
140
|
} catch (e) {
|
|
@@ -146,7 +146,96 @@ class AssetHubSwapHandler {
|
|
|
146
146
|
if (params.selectedQuote) {
|
|
147
147
|
const submitStep = {
|
|
148
148
|
name: 'Swap',
|
|
149
|
-
type:
|
|
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) {
|
|
228
|
+
return Promise.resolve(undefined);
|
|
229
|
+
}
|
|
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
|
+
}
|
|
150
239
|
};
|
|
151
240
|
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
|
|
152
241
|
}
|
|
@@ -155,36 +244,39 @@ class AssetHubSwapHandler {
|
|
|
155
244
|
generateOptimalProcess(params) {
|
|
156
245
|
return this.swapBaseHandler.generateOptimalProcess(params, [this.getXcmStep.bind(this), this.getSubmitStep.bind(this)]);
|
|
157
246
|
}
|
|
247
|
+
generateOptimalProcessV2(params) {
|
|
248
|
+
const stepFuncList = params.path.map(step => {
|
|
249
|
+
if (step.action === _interface.DynamicSwapType.BRIDGE) {
|
|
250
|
+
return this.getXcmStepV2.bind(this);
|
|
251
|
+
}
|
|
252
|
+
if (step.action === _interface.DynamicSwapType.SWAP) {
|
|
253
|
+
return this.getSwapStepV2.bind(this);
|
|
254
|
+
}
|
|
255
|
+
throw new Error(`Error generating optimal process: Action ${step.action} is not supported`);
|
|
256
|
+
});
|
|
257
|
+
return this.swapBaseHandler.generateOptimalProcessV2(params, stepFuncList);
|
|
258
|
+
}
|
|
158
259
|
async handleXcmStep(params) {
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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;
|
|
163
266
|
const originChain = this.chainService.getChainInfoByKey(originAsset.originChain);
|
|
164
267
|
const destinationChain = this.chainService.getChainInfoByKey(destinationAsset.originChain);
|
|
165
268
|
const substrateApi = this.chainService.getSubstrateApi(originAsset.originChain);
|
|
166
269
|
const chainApi = await substrateApi.isReady;
|
|
167
|
-
const
|
|
168
|
-
const xcmFee = params.process.totalFee[params.currentStep];
|
|
169
|
-
const bnAmount = new _bignumber.default(params.quote.fromAmount);
|
|
170
|
-
const bnDestinationAssetBalance = new _bignumber.default(destinationAssetBalance.value);
|
|
171
|
-
const id = (0, _getId.getId)();
|
|
172
|
-
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee(id, originChain.slug, 'substrate');
|
|
173
|
-
let bnTotalAmount = bnAmount.minus(bnDestinationAssetBalance);
|
|
174
|
-
if ((0, _utils._isNativeToken)(originAsset)) {
|
|
175
|
-
const bnXcmFee = new _bignumber.default(xcmFee.feeComponent[0].amount); // xcm fee is paid in native token but swap token is not always native token
|
|
176
|
-
|
|
177
|
-
bnTotalAmount = bnTotalAmount.plus(bnXcmFee);
|
|
178
|
-
}
|
|
270
|
+
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), originAsset.originChain, 'substrate');
|
|
179
271
|
const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
|
|
180
272
|
originTokenInfo: originAsset,
|
|
181
273
|
destinationTokenInfo: destinationAsset,
|
|
182
|
-
sendingValue:
|
|
274
|
+
sendingValue: briefXcmStep.sendingValue,
|
|
183
275
|
recipient: params.address,
|
|
184
276
|
substrateApi: chainApi,
|
|
185
277
|
sender: params.address,
|
|
186
|
-
|
|
187
|
-
|
|
278
|
+
destinationChain,
|
|
279
|
+
originChain,
|
|
188
280
|
feeInfo
|
|
189
281
|
});
|
|
190
282
|
const xcmData = {
|
|
@@ -192,14 +284,14 @@ class AssetHubSwapHandler {
|
|
|
192
284
|
destinationNetworkKey: destinationAsset.originChain,
|
|
193
285
|
from: params.address,
|
|
194
286
|
to: params.address,
|
|
195
|
-
value:
|
|
287
|
+
value: briefXcmStep.sendingValue,
|
|
196
288
|
tokenSlug: originAsset.slug,
|
|
197
289
|
showExtraWarning: true
|
|
198
290
|
};
|
|
199
291
|
return {
|
|
200
292
|
txChain: originAsset.originChain,
|
|
201
293
|
extrinsic: xcmTransfer,
|
|
202
|
-
transferNativeAmount: (0, _utils._isNativeToken)(originAsset) ?
|
|
294
|
+
transferNativeAmount: (0, _utils._isNativeToken)(originAsset) ? briefXcmStep.sendingValue : '0',
|
|
203
295
|
extrinsicType: _KoniTypes.ExtrinsicType.TRANSFER_XCM,
|
|
204
296
|
chainType: _KoniTypes.ChainType.SUBSTRATE,
|
|
205
297
|
txData: xcmData
|
|
@@ -207,7 +299,11 @@ class AssetHubSwapHandler {
|
|
|
207
299
|
}
|
|
208
300
|
async handleSubmitStep(params) {
|
|
209
301
|
var _this$router;
|
|
210
|
-
const
|
|
302
|
+
const metadata = params.process.steps[params.currentStep].metadata;
|
|
303
|
+
if (!metadata || !metadata.sendingValue || !metadata.destinationTokenInfo || !metadata.originTokenInfo) {
|
|
304
|
+
return new _SwapError.SwapError(_types.SwapErrorType.UNKNOWN);
|
|
305
|
+
}
|
|
306
|
+
const fromAsset = metadata.originTokenInfo;
|
|
211
307
|
const txData = {
|
|
212
308
|
provider: this.providerInfo,
|
|
213
309
|
quote: params.quote,
|
|
@@ -220,7 +316,12 @@ class AssetHubSwapHandler {
|
|
|
220
316
|
fromAmount,
|
|
221
317
|
toAmount
|
|
222
318
|
} = params.quote;
|
|
319
|
+
|
|
320
|
+
// todo: move to gen process
|
|
223
321
|
const minReceive = new _bignumber.default(1 - params.slippage).times(toAmount).integerValue(_bignumber.default.ROUND_DOWN);
|
|
322
|
+
if (!params.address || !paths || !fromAmount || !minReceive) {
|
|
323
|
+
throw new _SwapError.SwapError(_types.SwapErrorType.UNKNOWN);
|
|
324
|
+
}
|
|
224
325
|
const extrinsic = await ((_this$router = this.router) === null || _this$router === void 0 ? void 0 : _this$router.buildSwapExtrinsic(paths, params.address, fromAmount, minReceive.toString()));
|
|
225
326
|
return {
|
|
226
327
|
txChain: fromAsset.originChain,
|
|
@@ -239,9 +340,9 @@ class AssetHubSwapHandler {
|
|
|
239
340
|
} = params;
|
|
240
341
|
const type = process.steps[currentStep].type;
|
|
241
342
|
switch (type) {
|
|
242
|
-
case
|
|
343
|
+
case _types.CommonStepType.XCM:
|
|
243
344
|
return this.handleXcmStep(params);
|
|
244
|
-
case
|
|
345
|
+
case _types.SwapStepType.SWAP:
|
|
245
346
|
return this.handleSubmitStep(params);
|
|
246
347
|
default:
|
|
247
348
|
return Promise.reject(new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED));
|
|
@@ -257,10 +358,10 @@ class AssetHubSwapHandler {
|
|
|
257
358
|
const selectedQuote = params.selectedQuote;
|
|
258
359
|
const currentTimestamp = +Date.now();
|
|
259
360
|
if (selectedQuote.aliveUntil <= currentTimestamp) {
|
|
260
|
-
return Promise.resolve([new _TransactionError.TransactionError(
|
|
361
|
+
return Promise.resolve([new _TransactionError.TransactionError(_types.SwapErrorType.QUOTE_TIMEOUT)]);
|
|
261
362
|
}
|
|
262
363
|
const stepFee = params.process.totalFee[stepIndex].feeComponent;
|
|
263
|
-
const networkFee = stepFee.find(fee => fee.feeType ===
|
|
364
|
+
const networkFee = stepFee.find(fee => fee.feeType === _types.SwapFeeType.NETWORK_FEE);
|
|
264
365
|
if (!networkFee) {
|
|
265
366
|
return Promise.resolve([new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)]);
|
|
266
367
|
}
|
|
@@ -293,15 +394,23 @@ class AssetHubSwapHandler {
|
|
|
293
394
|
if (bnAmount.lte(0)) {
|
|
294
395
|
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
295
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
|
+
}
|
|
296
401
|
let isXcmOk = false;
|
|
402
|
+
const currentStep = params.currentStep;
|
|
297
403
|
for (const [index, step] of params.process.steps.entries()) {
|
|
404
|
+
if (currentStep > index) {
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
298
407
|
const getErrors = async () => {
|
|
299
408
|
switch (step.type) {
|
|
300
|
-
case
|
|
409
|
+
case _types.CommonStepType.DEFAULT:
|
|
301
410
|
return Promise.resolve([]);
|
|
302
|
-
case
|
|
303
|
-
return this.swapBaseHandler.
|
|
304
|
-
case
|
|
411
|
+
case _types.CommonStepType.XCM:
|
|
412
|
+
return this.swapBaseHandler.validateXcmStepV2(params, index);
|
|
413
|
+
case _types.SwapStepType.SWAP:
|
|
305
414
|
return this.validateSwapStep(params, isXcmOk, index);
|
|
306
415
|
default:
|
|
307
416
|
return Promise.reject(new _TransactionError.TransactionError(_types.BasicTxErrorType.UNSUPPORTED));
|
|
@@ -310,11 +419,44 @@ class AssetHubSwapHandler {
|
|
|
310
419
|
const errors = await getErrors();
|
|
311
420
|
if (errors.length) {
|
|
312
421
|
return errors;
|
|
313
|
-
} else if (step.type ===
|
|
422
|
+
} else if (step.type === _types.CommonStepType.XCM) {
|
|
314
423
|
isXcmOk = true;
|
|
315
424
|
}
|
|
316
425
|
}
|
|
317
426
|
return [];
|
|
318
427
|
}
|
|
428
|
+
async validateSwapProcessV2(params) {
|
|
429
|
+
// todo: recheck address and recipient format in params
|
|
430
|
+
const {
|
|
431
|
+
process,
|
|
432
|
+
selectedQuote
|
|
433
|
+
} = params; // todo: review flow, currentStep param.
|
|
434
|
+
|
|
435
|
+
// todo: validate path with optimalProcess
|
|
436
|
+
// todo: review error message in case many step swap
|
|
437
|
+
if ((0, _bignumber.default)(selectedQuote.fromAmount).lte(0)) {
|
|
438
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
439
|
+
}
|
|
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;
|
|
446
|
+
if (swap) {
|
|
447
|
+
return this.swapBaseHandler.validateSwapOnlyProcess(params, 1); // todo: create interface for input request
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (swapXcm) {
|
|
451
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
452
|
+
}
|
|
453
|
+
if (xcmSwap) {
|
|
454
|
+
return this.swapBaseHandler.validateXcmSwapProcess(params, 2, 1);
|
|
455
|
+
}
|
|
456
|
+
if (xcmSwapXcm) {
|
|
457
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
458
|
+
}
|
|
459
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
460
|
+
}
|
|
319
461
|
}
|
|
320
462
|
exports.AssetHubSwapHandler = AssetHubSwapHandler;
|
|
@@ -152,6 +152,9 @@ const buildSwapExtrinsic = function (api, paths, recipient, amountIn, amountOutM
|
|
|
152
152
|
let keepAlive = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : true;
|
|
153
153
|
const pathsInfo = paths.map(asset => {
|
|
154
154
|
const multilocation = (0, _utils._getXcmAssetMultilocation)(asset);
|
|
155
|
+
if (!multilocation) {
|
|
156
|
+
throw new Error('Error getting multilocation');
|
|
157
|
+
}
|
|
155
158
|
return api.createType('MultiLocation', multilocation).toU8a();
|
|
156
159
|
});
|
|
157
160
|
return api.tx.assetConversion.swapExactTokensForTokens(pathsInfo, amountIn, amountOutMin, recipient, keepAlive);
|