@subwallet/extension-base 1.3.26-0 → 1.3.27-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.
- 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/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/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/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/types/service-base.d.ts +6 -2
- package/types/swap/index.d.ts +34 -6
- package/types/transaction/process.d.ts +0 -6
|
@@ -14,12 +14,14 @@ var _constants = require("@subwallet/extension-base/constants");
|
|
|
14
14
|
var _xcm = require("@subwallet/extension-base/services/balance-service/transfer/xcm");
|
|
15
15
|
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
16
16
|
var _baseHandler = require("@subwallet/extension-base/services/swap-service/handler/base-handler");
|
|
17
|
+
var _interface = require("@subwallet/extension-base/services/swap-service/interface");
|
|
17
18
|
var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
|
|
18
19
|
var _types = require("@subwallet/extension-base/types");
|
|
19
20
|
var _serviceBase = require("@subwallet/extension-base/types/service-base");
|
|
20
21
|
var _swap = require("@subwallet/extension-base/types/swap");
|
|
21
22
|
var _getId = require("@subwallet/extension-base/utils/getId");
|
|
22
23
|
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
24
|
+
var _util = require("@polkadot/util");
|
|
23
25
|
// Copyright 2019-2022 @subwallet/extension-base
|
|
24
26
|
// SPDX-License-Identifier: Apache-2.0
|
|
25
27
|
|
|
@@ -195,35 +197,138 @@ class HydradxHandler {
|
|
|
195
197
|
}
|
|
196
198
|
return Promise.resolve(undefined);
|
|
197
199
|
}
|
|
200
|
+
async getXcmStepV2(params) {
|
|
201
|
+
var _params$path$find;
|
|
202
|
+
// todo: improve this function for Round 2
|
|
203
|
+
|
|
204
|
+
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;
|
|
205
|
+
if (!xcmPairInfo) {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
const fromTokenInfo = this.chainService.getAssetBySlug(xcmPairInfo.from);
|
|
209
|
+
const toTokenInfo = this.chainService.getAssetBySlug(xcmPairInfo.to);
|
|
210
|
+
const fromChainInfo = this.chainService.getChainInfoByKey(fromTokenInfo.originChain);
|
|
211
|
+
const toChainInfo = this.chainService.getChainInfoByKey(toTokenInfo.originChain);
|
|
212
|
+
if (!fromChainInfo || !toChainInfo || !fromChainInfo || !toChainInfo) {
|
|
213
|
+
throw Error('Token and chain not found');
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const substrateApi = await this.chainService.getSubstrateApi(fromTokenInfo.originChain).isReady;
|
|
217
|
+
const id = (0, _getId.getId)();
|
|
218
|
+
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)]);
|
|
219
|
+
const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
|
|
220
|
+
originTokenInfo: fromTokenInfo,
|
|
221
|
+
destinationTokenInfo: toTokenInfo,
|
|
222
|
+
// Mock sending value to get payment info
|
|
223
|
+
sendingValue: params.request.fromAmount,
|
|
224
|
+
recipient: params.request.address,
|
|
225
|
+
substrateApi: substrateApi,
|
|
226
|
+
sender: params.request.address,
|
|
227
|
+
originChain: fromChainInfo,
|
|
228
|
+
destinationChain: toChainInfo,
|
|
229
|
+
feeInfo
|
|
230
|
+
});
|
|
231
|
+
const _xcmFeeInfo = await xcmTransfer.paymentInfo(params.request.address);
|
|
232
|
+
const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
|
|
233
|
+
const fee = {
|
|
234
|
+
feeComponent: [{
|
|
235
|
+
feeType: _swap.SwapFeeType.NETWORK_FEE,
|
|
236
|
+
amount: Math.ceil(xcmFeeInfo.partialFee * _utils2.FEE_RATE_MULTIPLIER.high).toString(),
|
|
237
|
+
tokenSlug: (0, _utils._getChainNativeTokenSlug)(fromChainInfo)
|
|
238
|
+
}],
|
|
239
|
+
defaultFeeToken: (0, _utils._getChainNativeTokenSlug)(fromChainInfo),
|
|
240
|
+
feeOptions: [(0, _utils._getChainNativeTokenSlug)(fromChainInfo)]
|
|
241
|
+
};
|
|
242
|
+
let bnTransferAmount = new _bignumber.default(params.request.fromAmount);
|
|
243
|
+
|
|
244
|
+
// todo: increase transfer amount when XCM local token
|
|
245
|
+
if ((0, _utils._isNativeToken)(fromTokenInfo)) {
|
|
246
|
+
// xcm fee is paid in native token but swap token is not always native token
|
|
247
|
+
// add amount of fee into sending value to ensure has enough token to swap
|
|
248
|
+
const bnXcmFee = new _bignumber.default(fee.feeComponent[0].amount);
|
|
249
|
+
bnTransferAmount = bnTransferAmount.plus(bnXcmFee);
|
|
250
|
+
} else {
|
|
251
|
+
bnTransferAmount = bnTransferAmount.plus((0, _bignumber.default)((0, _utils._getTokenMinAmount)(toTokenInfo)).multipliedBy(_utils2.FEE_RATE_MULTIPLIER.medium));
|
|
252
|
+
}
|
|
253
|
+
if ((0, _bignumber.default)(toTokenBalance.value).lte(0)) {
|
|
254
|
+
bnTransferAmount = bnTransferAmount.plus((0, _utils._getTokenMinAmount)(toTokenInfo));
|
|
255
|
+
}
|
|
256
|
+
const step = {
|
|
257
|
+
metadata: {
|
|
258
|
+
sendingValue: bnTransferAmount.toString(),
|
|
259
|
+
originTokenInfo: fromTokenInfo,
|
|
260
|
+
destinationTokenInfo: toTokenInfo
|
|
261
|
+
},
|
|
262
|
+
name: `Transfer ${fromTokenInfo.symbol} from ${fromChainInfo.name}`,
|
|
263
|
+
type: _serviceBase.CommonStepType.XCM
|
|
264
|
+
};
|
|
265
|
+
return [step, fee];
|
|
266
|
+
} catch (e) {
|
|
267
|
+
console.error('Error creating xcm step', e);
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async getSwapStepV2(params) {
|
|
272
|
+
var _params$path$find2;
|
|
273
|
+
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;
|
|
274
|
+
if (!swapPairInfo) {
|
|
275
|
+
return Promise.resolve(undefined);
|
|
276
|
+
}
|
|
277
|
+
if (!params.selectedQuote) {
|
|
278
|
+
return Promise.resolve(undefined);
|
|
279
|
+
}
|
|
280
|
+
const txHex = params.selectedQuote.metadata;
|
|
281
|
+
if (!txHex || !(0, _util.isHex)(txHex)) {
|
|
282
|
+
return Promise.resolve(undefined);
|
|
283
|
+
}
|
|
284
|
+
const metadata = {
|
|
285
|
+
sendingValue: params.request.fromAmount.toString(),
|
|
286
|
+
originTokenInfo: this.chainService.getAssetBySlug(swapPairInfo.from),
|
|
287
|
+
destinationTokenInfo: this.chainService.getAssetBySlug(swapPairInfo.to),
|
|
288
|
+
txHex
|
|
289
|
+
};
|
|
290
|
+
const submitStep = {
|
|
291
|
+
name: 'Swap',
|
|
292
|
+
type: _swap.SwapStepType.SWAP,
|
|
293
|
+
// @ts-ignore
|
|
294
|
+
metadata
|
|
295
|
+
};
|
|
296
|
+
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]); // review fee
|
|
297
|
+
}
|
|
298
|
+
|
|
198
299
|
generateOptimalProcess(params) {
|
|
199
300
|
return this.swapBaseHandler.generateOptimalProcess(params, [this.getXcmStep.bind(this),
|
|
200
301
|
// this.getFeeOptionStep.bind(this),
|
|
201
302
|
this.getSubmitStep.bind(this)]);
|
|
202
303
|
}
|
|
304
|
+
generateOptimalProcessV2(params) {
|
|
305
|
+
const stepFuncList = params.path.map(step => {
|
|
306
|
+
if (step.action === _interface.DynamicSwapType.BRIDGE) {
|
|
307
|
+
return this.getXcmStepV2.bind(this);
|
|
308
|
+
}
|
|
309
|
+
if (step.action === _interface.DynamicSwapType.SWAP) {
|
|
310
|
+
return this.getSwapStepV2.bind(this);
|
|
311
|
+
}
|
|
312
|
+
throw new Error(`Error generating optimal process: Action ${step.action} is not supported`);
|
|
313
|
+
});
|
|
314
|
+
return this.swapBaseHandler.generateOptimalProcessV2(params, stepFuncList);
|
|
315
|
+
}
|
|
203
316
|
async handleXcmStep(params) {
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
317
|
+
const briefXcmStep = params.process.steps[params.currentStep].metadata;
|
|
318
|
+
if (!briefXcmStep || !briefXcmStep.originTokenInfo || !briefXcmStep.destinationTokenInfo || !briefXcmStep.sendingValue) {
|
|
319
|
+
throw new Error('XCM metadata error');
|
|
320
|
+
}
|
|
321
|
+
const originAsset = briefXcmStep.originTokenInfo;
|
|
322
|
+
const destinationAsset = briefXcmStep.destinationTokenInfo;
|
|
208
323
|
const originChain = this.chainService.getChainInfoByKey(originAsset.originChain);
|
|
209
324
|
const destinationChain = this.chainService.getChainInfoByKey(destinationAsset.originChain);
|
|
210
325
|
const substrateApi = this.chainService.getSubstrateApi(originAsset.originChain);
|
|
211
326
|
const chainApi = await substrateApi.isReady;
|
|
212
|
-
const destinationAssetBalance = await this.balanceService.getTransferableBalance(params.address, destinationAsset.originChain, destinationAsset.slug);
|
|
213
|
-
const xcmFee = params.process.totalFee[params.currentStep];
|
|
214
|
-
const bnAmount = new _bignumber.default(params.quote.fromAmount);
|
|
215
|
-
const bnDestinationAssetBalance = new _bignumber.default(destinationAssetBalance.value);
|
|
216
|
-
let bnTotalAmount = bnAmount.minus(bnDestinationAssetBalance);
|
|
217
|
-
if ((0, _utils._isNativeToken)(originAsset)) {
|
|
218
|
-
const bnXcmFee = new _bignumber.default(xcmFee.feeComponent[0].amount); // xcm fee is paid in native token but swap token is not always native token
|
|
219
|
-
|
|
220
|
-
bnTotalAmount = bnTotalAmount.plus(bnXcmFee);
|
|
221
|
-
}
|
|
222
327
|
const feeInfo = await this.swapBaseHandler.feeService.subscribeChainFee((0, _getId.getId)(), originAsset.originChain, 'substrate');
|
|
223
328
|
const xcmTransfer = await (0, _xcm.createXcmExtrinsic)({
|
|
224
329
|
originTokenInfo: originAsset,
|
|
225
330
|
destinationTokenInfo: destinationAsset,
|
|
226
|
-
sendingValue:
|
|
331
|
+
sendingValue: briefXcmStep.sendingValue,
|
|
227
332
|
recipient: params.address,
|
|
228
333
|
substrateApi: chainApi,
|
|
229
334
|
sender: params.address,
|
|
@@ -236,14 +341,14 @@ class HydradxHandler {
|
|
|
236
341
|
destinationNetworkKey: destinationAsset.originChain,
|
|
237
342
|
from: params.address,
|
|
238
343
|
to: params.address,
|
|
239
|
-
value:
|
|
344
|
+
value: briefXcmStep.sendingValue,
|
|
240
345
|
tokenSlug: originAsset.slug,
|
|
241
346
|
showExtraWarning: true
|
|
242
347
|
};
|
|
243
348
|
return {
|
|
244
349
|
txChain: originAsset.originChain,
|
|
245
350
|
extrinsic: xcmTransfer,
|
|
246
|
-
transferNativeAmount: (0, _utils._isNativeToken)(originAsset) ?
|
|
351
|
+
transferNativeAmount: (0, _utils._isNativeToken)(originAsset) ? briefXcmStep.sendingValue : '0',
|
|
247
352
|
extrinsicType: _KoniTypes.ExtrinsicType.TRANSFER_XCM,
|
|
248
353
|
chainType: _KoniTypes.ChainType.SUBSTRATE,
|
|
249
354
|
txData: xcmData
|
|
@@ -274,18 +379,18 @@ class HydradxHandler {
|
|
|
274
379
|
};
|
|
275
380
|
}
|
|
276
381
|
async handleSubmitStep(params) {
|
|
277
|
-
const
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
382
|
+
const metadata = params.process.steps[params.currentStep].metadata;
|
|
383
|
+
const txHex = params.quote.metadata;
|
|
384
|
+
if (!txHex || !(0, _util.isHex)(txHex)) {
|
|
385
|
+
return new _SwapError.SwapError(_swap.SwapErrorType.UNKNOWN);
|
|
386
|
+
}
|
|
387
|
+
if (!metadata || !metadata.sendingValue || !metadata.txHex || !metadata.destinationTokenInfo || !metadata.originTokenInfo) {
|
|
388
|
+
throw new Error('Swap metadata error');
|
|
389
|
+
}
|
|
390
|
+
const fromAsset = metadata.originTokenInfo;
|
|
281
391
|
if (!this.isReady || !this.tradeRouter) {
|
|
282
392
|
return new _SwapError.SwapError(_swap.SwapErrorType.UNKNOWN);
|
|
283
393
|
}
|
|
284
|
-
const parsedFromAmount = new _bignumber.default(params.quote.fromAmount).shiftedBy(-1 * (0, _utils._getAssetDecimals)(fromAsset)).toString();
|
|
285
|
-
const quoteResponse = await this.tradeRouter.getBestSell(fromAssetId, toAssetId, parsedFromAmount);
|
|
286
|
-
const toAmount = quoteResponse.amountOut;
|
|
287
|
-
const minReceive = toAmount.times(1 - params.slippage).integerValue();
|
|
288
|
-
const txHex = quoteResponse.toTx(minReceive).hex;
|
|
289
394
|
const substrateApi = this.chainService.getSubstrateApi(this.chain());
|
|
290
395
|
const chainApi = await substrateApi.isReady;
|
|
291
396
|
const txData = {
|
|
@@ -293,7 +398,7 @@ class HydradxHandler {
|
|
|
293
398
|
quote: params.quote,
|
|
294
399
|
address: params.address,
|
|
295
400
|
slippage: params.slippage,
|
|
296
|
-
txHex,
|
|
401
|
+
txHex: txHex,
|
|
297
402
|
process: params.process
|
|
298
403
|
};
|
|
299
404
|
let extrinsic;
|
|
@@ -321,8 +426,7 @@ class HydradxHandler {
|
|
|
321
426
|
txChain: fromAsset.originChain,
|
|
322
427
|
txData,
|
|
323
428
|
extrinsic,
|
|
324
|
-
transferNativeAmount: (0, _utils._isNativeToken)(fromAsset) ?
|
|
325
|
-
// todo
|
|
429
|
+
transferNativeAmount: (0, _utils._isNativeToken)(fromAsset) ? metadata.sendingValue : '0',
|
|
326
430
|
extrinsicType: _KoniTypes.ExtrinsicType.SWAP,
|
|
327
431
|
chainType: _KoniTypes.ChainType.SUBSTRATE
|
|
328
432
|
};
|
|
@@ -352,14 +456,22 @@ class HydradxHandler {
|
|
|
352
456
|
if (bnAmount.lte(0)) {
|
|
353
457
|
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
354
458
|
}
|
|
459
|
+
const swapStep = params.process.steps.find(item => item.type === _swap.SwapStepType.SWAP);
|
|
460
|
+
if (!swapStep) {
|
|
461
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR, 'Swap step not found')];
|
|
462
|
+
}
|
|
355
463
|
let isXcmOk = false;
|
|
464
|
+
const currentStep = params.currentStep;
|
|
356
465
|
for (const [index, step] of params.process.steps.entries()) {
|
|
466
|
+
if (currentStep > index) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
357
469
|
const getErrors = async () => {
|
|
358
470
|
switch (step.type) {
|
|
359
471
|
case _serviceBase.CommonStepType.DEFAULT:
|
|
360
472
|
return Promise.resolve([]);
|
|
361
473
|
case _serviceBase.CommonStepType.XCM:
|
|
362
|
-
return this.swapBaseHandler.
|
|
474
|
+
return this.swapBaseHandler.validateXcmStepV2(params, index);
|
|
363
475
|
case _serviceBase.CommonStepType.SET_FEE_TOKEN:
|
|
364
476
|
return this.swapBaseHandler.validateSetFeeTokenStep(params, index);
|
|
365
477
|
default:
|
|
@@ -375,6 +487,38 @@ class HydradxHandler {
|
|
|
375
487
|
}
|
|
376
488
|
return [];
|
|
377
489
|
}
|
|
490
|
+
async validateSwapProcessV2(params) {
|
|
491
|
+
// todo: recheck address and recipient format in params
|
|
492
|
+
const {
|
|
493
|
+
process,
|
|
494
|
+
selectedQuote
|
|
495
|
+
} = params; // todo: review flow, currentStep param.
|
|
496
|
+
|
|
497
|
+
// todo: validate path with optimalProcess
|
|
498
|
+
// todo: review error message in case many step swap
|
|
499
|
+
if ((0, _bignumber.default)(selectedQuote.fromAmount).lte(0)) {
|
|
500
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
501
|
+
}
|
|
502
|
+
const actionList = JSON.stringify(process.path.map(step => step.action));
|
|
503
|
+
const swap = actionList === JSON.stringify([_interface.DynamicSwapType.SWAP]);
|
|
504
|
+
const swapXcm = actionList === JSON.stringify([_interface.DynamicSwapType.SWAP, _interface.DynamicSwapType.BRIDGE]);
|
|
505
|
+
const xcmSwap = actionList === JSON.stringify([_interface.DynamicSwapType.BRIDGE, _interface.DynamicSwapType.SWAP]);
|
|
506
|
+
const xcmSwapXcm = actionList === JSON.stringify([_interface.DynamicSwapType.BRIDGE, _interface.DynamicSwapType.SWAP, _interface.DynamicSwapType.BRIDGE]);
|
|
507
|
+
if (swap) {
|
|
508
|
+
return this.swapBaseHandler.validateSwapOnlyProcess(params, 1); // todo: create interface for input request
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if (swapXcm) {
|
|
512
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
513
|
+
}
|
|
514
|
+
if (xcmSwap) {
|
|
515
|
+
return this.swapBaseHandler.validateXcmSwapProcess(params, 2, 1);
|
|
516
|
+
}
|
|
517
|
+
if (xcmSwapXcm) {
|
|
518
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
519
|
+
}
|
|
520
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
521
|
+
}
|
|
378
522
|
get referralCode() {
|
|
379
523
|
if (this.isTestnet) {
|
|
380
524
|
return HYDRADX_TESTNET_SUBWALLET_REFERRAL_CODE;
|
|
@@ -8,6 +8,7 @@ var _SwapError = require("@subwallet/extension-base/background/errors/SwapError"
|
|
|
8
8
|
var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
|
|
9
9
|
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
10
10
|
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
11
|
+
var _interface = require("@subwallet/extension-base/services/swap-service/interface");
|
|
11
12
|
var _types = require("@subwallet/extension-base/types");
|
|
12
13
|
var _utils2 = require("@subwallet/extension-base/utils");
|
|
13
14
|
var _getId = require("@subwallet/extension-base/utils/getId");
|
|
@@ -114,11 +115,19 @@ class SimpleSwapHandler {
|
|
|
114
115
|
generateOptimalProcess(params) {
|
|
115
116
|
return this.swapBaseHandler.generateOptimalProcess(params, [this.getSubmitStep.bind(this)]);
|
|
116
117
|
}
|
|
118
|
+
generateOptimalProcessV2(params) {
|
|
119
|
+
return this.swapBaseHandler.generateOptimalProcessV2(params, [this.getSubmitStep.bind(this)]);
|
|
120
|
+
}
|
|
117
121
|
async getSubmitStep(params) {
|
|
118
122
|
if (params.selectedQuote) {
|
|
119
123
|
const submitStep = {
|
|
120
124
|
name: 'Swap',
|
|
121
|
-
type: _types.SwapStepType.SWAP
|
|
125
|
+
type: _types.SwapStepType.SWAP,
|
|
126
|
+
metadata: {
|
|
127
|
+
sendingValue: params.request.fromAmount.toString(),
|
|
128
|
+
originTokenInfo: this.chainService.getAssetBySlug(params.selectedQuote.pair.from),
|
|
129
|
+
destinationTokenInfo: this.chainService.getAssetBySlug(params.selectedQuote.pair.to)
|
|
130
|
+
}
|
|
122
131
|
};
|
|
123
132
|
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
|
|
124
133
|
}
|
|
@@ -171,6 +180,9 @@ class SimpleSwapHandler {
|
|
|
171
180
|
sender,
|
|
172
181
|
toAsset
|
|
173
182
|
});
|
|
183
|
+
if (!id || id.length === 0 || !addressFrom || addressFrom.length === 0) {
|
|
184
|
+
throw new _SwapError.SwapError(_types.SwapErrorType.UNKNOWN);
|
|
185
|
+
}
|
|
174
186
|
|
|
175
187
|
// Validate the amount to be swapped
|
|
176
188
|
const rate = (0, _bignumber.default)(amountTo).div((0, _bignumber.default)(quote.toAmount)).multipliedBy(100);
|
|
@@ -239,5 +251,42 @@ class SimpleSwapHandler {
|
|
|
239
251
|
chainType
|
|
240
252
|
};
|
|
241
253
|
}
|
|
254
|
+
async validateSwapProcessV2(params) {
|
|
255
|
+
// todo: recheck address and recipient format in params
|
|
256
|
+
const {
|
|
257
|
+
process,
|
|
258
|
+
selectedQuote
|
|
259
|
+
} = params; // todo: review flow, currentStep param.
|
|
260
|
+
|
|
261
|
+
// todo: validate path with optimalProcess
|
|
262
|
+
// todo: review error message in case many step swap
|
|
263
|
+
if ((0, _bignumber.BigNumber)(selectedQuote.fromAmount).lte(0)) {
|
|
264
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
265
|
+
}
|
|
266
|
+
const actionList = JSON.stringify(process.path.map(step => step.action));
|
|
267
|
+
const swap = actionList === JSON.stringify([_interface.DynamicSwapType.SWAP]);
|
|
268
|
+
const swapXcm = actionList === JSON.stringify([_interface.DynamicSwapType.SWAP, _interface.DynamicSwapType.BRIDGE]);
|
|
269
|
+
const xcmSwap = actionList === JSON.stringify([_interface.DynamicSwapType.BRIDGE, _interface.DynamicSwapType.SWAP]);
|
|
270
|
+
const xcmSwapXcm = actionList === JSON.stringify([_interface.DynamicSwapType.BRIDGE, _interface.DynamicSwapType.SWAP, _interface.DynamicSwapType.BRIDGE]);
|
|
271
|
+
const swapIndex = params.process.steps.findIndex(step => step.type === _types.SwapStepType.SWAP); // todo
|
|
272
|
+
|
|
273
|
+
if (swapIndex <= -1) {
|
|
274
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
275
|
+
}
|
|
276
|
+
if (swap) {
|
|
277
|
+
return this.swapBaseHandler.validateSwapOnlyProcess(params, swapIndex); // todo: create interface for input request
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (swapXcm) {
|
|
281
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
282
|
+
}
|
|
283
|
+
if (xcmSwap) {
|
|
284
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
285
|
+
}
|
|
286
|
+
if (xcmSwapXcm) {
|
|
287
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
288
|
+
}
|
|
289
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
290
|
+
}
|
|
242
291
|
}
|
|
243
292
|
exports.SimpleSwapHandler = SimpleSwapHandler;
|
|
@@ -8,6 +8,7 @@ exports.UniswapHandler = 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 _interface = require("@subwallet/extension-base/services/swap-service/interface");
|
|
11
12
|
var _types = require("@subwallet/extension-base/types");
|
|
12
13
|
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
13
14
|
var _utils = require("../../chain-service/utils");
|
|
@@ -67,6 +68,9 @@ class UniswapHandler {
|
|
|
67
68
|
generateOptimalProcess(params) {
|
|
68
69
|
return this.swapBaseHandler.generateOptimalProcess(params, [this.getApprovalStep.bind(this), this.getPermitStep.bind(this), this.getSubmitStep.bind(this)]);
|
|
69
70
|
}
|
|
71
|
+
generateOptimalProcessV2(params) {
|
|
72
|
+
return this.swapBaseHandler.generateOptimalProcessV2(params, [this.getApprovalStep.bind(this), this.getPermitStep.bind(this), this.getSubmitStep.bind(this)]);
|
|
73
|
+
}
|
|
70
74
|
async getApprovalStep(params) {
|
|
71
75
|
if (params.selectedQuote) {
|
|
72
76
|
const walletAddress = params.request.address;
|
|
@@ -125,7 +129,12 @@ class UniswapHandler {
|
|
|
125
129
|
if (params.selectedQuote) {
|
|
126
130
|
const submitStep = {
|
|
127
131
|
name: 'Swap',
|
|
128
|
-
type: _types.SwapStepType.SWAP
|
|
132
|
+
type: _types.SwapStepType.SWAP,
|
|
133
|
+
metadata: {
|
|
134
|
+
sendingValue: params.request.fromAmount.toString(),
|
|
135
|
+
originTokenInfo: this.chainService.getAssetBySlug(params.selectedQuote.pair.from),
|
|
136
|
+
destinationTokenInfo: this.chainService.getAssetBySlug(params.selectedQuote.pair.to)
|
|
137
|
+
}
|
|
129
138
|
};
|
|
130
139
|
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
|
|
131
140
|
}
|
|
@@ -327,5 +336,42 @@ class UniswapHandler {
|
|
|
327
336
|
isPermit: true
|
|
328
337
|
};
|
|
329
338
|
}
|
|
339
|
+
async validateSwapProcessV2(params) {
|
|
340
|
+
// todo: recheck address and recipient format in params
|
|
341
|
+
const {
|
|
342
|
+
process,
|
|
343
|
+
selectedQuote
|
|
344
|
+
} = params; // todo: review flow, currentStep param.
|
|
345
|
+
|
|
346
|
+
// todo: validate path with optimalProcess
|
|
347
|
+
// todo: review error message in case many step swap
|
|
348
|
+
if ((0, _bignumber.default)(selectedQuote.fromAmount).lte(0)) {
|
|
349
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
350
|
+
}
|
|
351
|
+
const actionList = JSON.stringify(process.path.map(step => step.action));
|
|
352
|
+
const swap = actionList === JSON.stringify([_interface.DynamicSwapType.SWAP]);
|
|
353
|
+
const swapXcm = actionList === JSON.stringify([_interface.DynamicSwapType.SWAP, _interface.DynamicSwapType.BRIDGE]);
|
|
354
|
+
const xcmSwap = actionList === JSON.stringify([_interface.DynamicSwapType.BRIDGE, _interface.DynamicSwapType.SWAP]);
|
|
355
|
+
const xcmSwapXcm = actionList === JSON.stringify([_interface.DynamicSwapType.BRIDGE, _interface.DynamicSwapType.SWAP, _interface.DynamicSwapType.BRIDGE]);
|
|
356
|
+
const swapIndex = params.process.steps.findIndex(step => step.type === _types.SwapStepType.SWAP); // todo
|
|
357
|
+
|
|
358
|
+
if (swapIndex <= -1) {
|
|
359
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
360
|
+
}
|
|
361
|
+
if (swap) {
|
|
362
|
+
return this.swapBaseHandler.validateSwapOnlyProcess(params, swapIndex); // todo: create interface for input request
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (swapXcm) {
|
|
366
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
367
|
+
}
|
|
368
|
+
if (xcmSwap) {
|
|
369
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
370
|
+
}
|
|
371
|
+
if (xcmSwapXcm) {
|
|
372
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
373
|
+
}
|
|
374
|
+
return [new _TransactionError.TransactionError(_types.BasicTxErrorType.INTERNAL_ERROR)];
|
|
375
|
+
}
|
|
330
376
|
}
|
|
331
377
|
exports.UniswapHandler = UniswapHandler;
|