@subwallet/extension-base 1.2.3-0 → 1.2.4-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 -153
- package/background/KoniTypes.js +0 -21
- package/cjs/background/KoniTypes.js +1 -22
- package/cjs/core/logic-validation/earning.js +47 -0
- package/cjs/core/logic-validation/swap.js +99 -0
- package/cjs/core/logic-validation/transfer.js +212 -0
- package/cjs/core/substrate/nominationpools-pallet.js +22 -0
- package/cjs/core/substrate/system-pallet.js +77 -0
- package/cjs/core/substrate/xcm-parser.js +190 -0
- package/cjs/koni/api/dotsama/transfer.js +58 -122
- package/cjs/koni/api/staking/bonding/utils.js +1 -1
- package/cjs/koni/api/xcm/index.js +30 -2
- package/cjs/koni/api/xcm/polkadotXcm.js +12 -20
- package/cjs/koni/api/xcm/utils.js +8 -126
- package/cjs/koni/api/xcm/xTokens.js +10 -8
- package/cjs/koni/api/xcm/xcmPallet.js +6 -6
- package/cjs/koni/background/handlers/Extension.js +223 -356
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/helpers/group.js +4 -27
- package/cjs/services/balance-service/helpers/subscribe/index.js +2 -30
- package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +46 -82
- package/cjs/services/balance-service/index.js +11 -9
- package/cjs/services/chain-service/constants.js +2 -2
- package/cjs/services/chain-service/index.js +0 -1
- package/cjs/services/chain-service/utils/index.js +13 -2
- package/cjs/services/earning-service/handlers/base.js +1 -1
- package/cjs/services/earning-service/handlers/special.js +11 -12
- package/cjs/services/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +2 -1
- package/cjs/services/migration-service/scripts/databases/MigrateAssetSetting.js +2 -1
- package/cjs/services/migration-service/scripts/index.js +2 -2
- package/cjs/services/swap-service/handler/base-handler.js +30 -44
- package/cjs/services/swap-service/handler/chainflip-handler.js +23 -21
- package/cjs/services/swap-service/handler/hydradx-handler.js +41 -39
- package/cjs/services/swap-service/index.js +6 -0
- package/cjs/services/swap-service/utils.js +8 -49
- package/cjs/services/transaction-service/index.js +66 -155
- package/core/logic-validation/earning.d.ts +10 -0
- package/core/logic-validation/earning.js +37 -0
- package/core/logic-validation/swap.d.ts +8 -0
- package/core/logic-validation/swap.js +89 -0
- package/core/logic-validation/transfer.d.ts +16 -0
- package/core/logic-validation/transfer.js +199 -0
- package/core/substrate/nominationpools-pallet.d.ts +10 -0
- package/core/substrate/nominationpools-pallet.js +13 -0
- package/core/substrate/system-pallet.d.ts +27 -0
- package/core/substrate/system-pallet.js +70 -0
- package/core/substrate/xcm-parser.d.ts +49 -0
- package/core/substrate/xcm-parser.js +181 -0
- package/koni/api/dotsama/transfer.d.ts +3 -4
- package/koni/api/dotsama/transfer.js +54 -118
- package/koni/api/staking/bonding/relayChain.d.ts +2 -1
- package/koni/api/staking/bonding/utils.js +1 -1
- package/koni/api/xcm/index.d.ts +2 -0
- package/koni/api/xcm/index.js +27 -1
- package/koni/api/xcm/polkadotXcm.js +14 -22
- package/koni/api/xcm/utils.d.ts +3 -48
- package/koni/api/xcm/utils.js +5 -114
- package/koni/api/xcm/xTokens.js +12 -10
- package/koni/api/xcm/xcmPallet.js +7 -7
- package/koni/background/handlers/Extension.d.ts +6 -8
- package/koni/background/handlers/Extension.js +117 -247
- package/package.json +39 -9
- package/packageInfo.js +1 -1
- package/services/balance-service/helpers/group.js +4 -27
- package/services/balance-service/helpers/subscribe/index.d.ts +2 -1
- package/services/balance-service/helpers/subscribe/index.js +2 -30
- package/services/balance-service/helpers/subscribe/substrate/index.d.ts +2 -1
- package/services/balance-service/helpers/subscribe/substrate/index.js +31 -66
- package/services/balance-service/index.d.ts +7 -6
- package/services/balance-service/index.js +12 -10
- package/services/chain-service/constants.js +2 -2
- package/services/chain-service/index.js +0 -1
- package/services/chain-service/utils/index.d.ts +4 -2
- package/services/chain-service/utils/index.js +9 -2
- package/services/earning-service/handlers/base.js +1 -1
- package/services/earning-service/handlers/nomination-pool/index.d.ts +2 -1
- package/services/earning-service/handlers/special.js +11 -12
- package/services/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +2 -1
- package/services/migration-service/scripts/databases/MigrateAssetSetting.js +2 -1
- package/services/migration-service/scripts/index.js +2 -2
- package/services/swap-service/handler/base-handler.d.ts +3 -2
- package/services/swap-service/handler/base-handler.js +28 -42
- package/services/swap-service/handler/chainflip-handler.d.ts +2 -1
- package/services/swap-service/handler/chainflip-handler.js +4 -2
- package/services/swap-service/handler/hydradx-handler.d.ts +2 -1
- package/services/swap-service/handler/hydradx-handler.js +8 -6
- package/services/swap-service/index.js +7 -1
- package/services/swap-service/utils.d.ts +2 -4
- package/services/swap-service/utils.js +7 -47
- package/services/transaction-service/index.d.ts +1 -1
- package/services/transaction-service/index.js +30 -119
- package/services/transaction-service/types.d.ts +1 -0
- package/types/balance/index.d.ts +6 -10
- package/types/yield/info/pallet.d.ts +0 -6
|
@@ -74,11 +74,11 @@ class BaseSpecialStakingPoolHandler extends _base.default {
|
|
|
74
74
|
const feeAssetInfo = this.state.chainService.getAssetBySlug(this.feeAssets[0]);
|
|
75
75
|
const altInputAssetInfo = this.state.chainService.getAssetBySlug(this.altInputAsset);
|
|
76
76
|
const inputAssetInfo = this.state.chainService.getAssetBySlug(this.inputAsset);
|
|
77
|
-
const [inputAssetBalance, altInputAssetBalance, feeAssetBalance] = await Promise.all([this.state.balanceService.
|
|
77
|
+
const [inputAssetBalance, altInputAssetBalance, feeAssetBalance] = await Promise.all([this.state.balanceService.getTransferableBalance(request.address, inputAssetInfo.originChain, inputAssetInfo.slug), altInputAssetInfo ? this.state.balanceService.getTransferableBalance(request.address, altInputAssetInfo.originChain, altInputAssetInfo.slug) : Promise.resolve({
|
|
78
78
|
symbol: '',
|
|
79
79
|
decimals: 0,
|
|
80
80
|
value: '0'
|
|
81
|
-
}), this.state.balanceService.
|
|
81
|
+
}), this.state.balanceService.getTransferableBalance(request.address, feeAssetInfo.originChain, feeAssetInfo.slug)]);
|
|
82
82
|
const bnInputAssetBalance = new _util.BN(inputAssetBalance.value);
|
|
83
83
|
const bnAltInputAssetBalance = new _util.BN(altInputAssetBalance.value);
|
|
84
84
|
const bnMinJoinPool = new _util.BN(poolInfo.statistic.earningThreshold.join);
|
|
@@ -200,13 +200,13 @@ class BaseSpecialStakingPoolHandler extends _base.default {
|
|
|
200
200
|
const bnAmount = new _util.BN(amount);
|
|
201
201
|
const inputTokenSlug = this.inputAsset; // assume that the pool only has 1 input token, will update later
|
|
202
202
|
const inputTokenInfo = this.state.getAssetBySlug(inputTokenSlug);
|
|
203
|
-
const inputTokenBalance = await this.state.balanceService.
|
|
203
|
+
const inputTokenBalance = await this.state.balanceService.getTransferableBalance(address, inputTokenInfo.originChain, inputTokenSlug);
|
|
204
204
|
const bnInputTokenBalance = new _util.BN(inputTokenBalance.value);
|
|
205
205
|
if (!bnInputTokenBalance.gte(bnAmount)) {
|
|
206
206
|
if (this.altInputAsset) {
|
|
207
207
|
const altInputTokenSlug = this.altInputAsset;
|
|
208
208
|
const altInputTokenInfo = this.state.getAssetBySlug(altInputTokenSlug);
|
|
209
|
-
const altInputTokenBalance = await this.state.balanceService.
|
|
209
|
+
const altInputTokenBalance = await this.state.balanceService.getTransferableBalance(address, altInputTokenInfo.originChain, altInputTokenSlug);
|
|
210
210
|
const bnAltInputTokenBalance = new _util.BN(altInputTokenBalance.value || '0');
|
|
211
211
|
if (bnAltInputTokenBalance.gt(_util.BN_ZERO)) {
|
|
212
212
|
const altChainInfo = this.state.getChainInfo(altInputTokenInfo.originChain);
|
|
@@ -279,17 +279,16 @@ class BaseSpecialStakingPoolHandler extends _base.default {
|
|
|
279
279
|
const altInputTokenSlug = this.altInputAsset || '';
|
|
280
280
|
const altInputTokenInfo = this.state.getAssetBySlug(altInputTokenSlug);
|
|
281
281
|
const inputTokenInfo = this.state.getAssetBySlug(this.inputAsset);
|
|
282
|
-
const altInputTokenBalance = await this.state.balanceService.
|
|
282
|
+
const altInputTokenBalance = await this.state.balanceService.getTransferableBalance(params.address, altInputTokenInfo.originChain, altInputTokenSlug);
|
|
283
283
|
const missingAmount = bnAmount.sub(bnInputTokenBalance); // TODO: what if input token is not LOCAL ??
|
|
284
284
|
const xcmFee = new _util.BN(path.totalFee[1].amount || '0');
|
|
285
285
|
const xcmAmount = missingAmount.add(xcmFee);
|
|
286
286
|
const bnAltInputTokenBalance = new _util.BN(altInputTokenBalance.value || '0');
|
|
287
|
-
|
|
288
|
-
if (!bnAltInputTokenBalance.sub(xcmAmount).gte(altInputTokenMinAmount)) {
|
|
287
|
+
if (!bnAltInputTokenBalance.sub(xcmAmount).sub(xcmFee).gt(_util.BN_ZERO)) {
|
|
289
288
|
processValidation.failedStep = path.steps[1];
|
|
290
289
|
processValidation.ok = false;
|
|
291
290
|
processValidation.status = _types.YieldValidationStatus.NOT_ENOUGH_BALANCE;
|
|
292
|
-
const maxBn = bnInputTokenBalance.add(new _util.BN(altInputTokenBalance.value)).sub(xcmFee).sub(
|
|
291
|
+
const maxBn = bnInputTokenBalance.add(new _util.BN(altInputTokenBalance.value)).sub(xcmFee).sub(xcmFee);
|
|
293
292
|
const maxValue = (0, _utils3.formatNumber)(maxBn.toString(), inputTokenInfo.decimals || 0);
|
|
294
293
|
const altInputTokenInfo = this.state.getAssetBySlug(altInputTokenSlug);
|
|
295
294
|
const symbol = altInputTokenInfo.symbol;
|
|
@@ -297,7 +296,7 @@ class BaseSpecialStakingPoolHandler extends _base.default {
|
|
|
297
296
|
const inputNetworkName = this.chainInfo.name;
|
|
298
297
|
const altNetworkName = altNetwork.name;
|
|
299
298
|
const currentValue = (0, _utils3.formatNumber)(bnInputTokenBalance.toString(), inputTokenInfo.decimals || 0);
|
|
300
|
-
const bnMaxXCM = new _util.BN(altInputTokenBalance.value).sub(xcmFee).sub(
|
|
299
|
+
const bnMaxXCM = new _util.BN(altInputTokenBalance.value).sub(xcmFee).sub(xcmFee);
|
|
301
300
|
const maxXCMValue = (0, _utils3.formatNumber)(bnMaxXCM.toString(), inputTokenInfo.decimals || 0);
|
|
302
301
|
processValidation.message = (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.', {
|
|
303
302
|
replace: {
|
|
@@ -334,7 +333,7 @@ class BaseSpecialStakingPoolHandler extends _base.default {
|
|
|
334
333
|
if (this.feeAssets.length === 1 && feeTokenSlug === defaultFeeTokenSlug) {
|
|
335
334
|
var _path$totalFee$id;
|
|
336
335
|
const bnFeeAmount = new _util.BN(((_path$totalFee$id = path.totalFee[id]) === null || _path$totalFee$id === void 0 ? void 0 : _path$totalFee$id.amount) || '0');
|
|
337
|
-
const feeTokenBalance = await this.state.balanceService.
|
|
336
|
+
const feeTokenBalance = await this.state.balanceService.getTransferableBalance(params.address, feeTokenInfo.originChain, feeTokenSlug);
|
|
338
337
|
const bnFeeTokenBalance = new _util.BN(feeTokenBalance.value || '0');
|
|
339
338
|
const bnFeeTokenMinAmount = new _util.BN((feeTokenInfo === null || feeTokenInfo === void 0 ? void 0 : feeTokenInfo.minAmount) || '0');
|
|
340
339
|
if (!bnFeeTokenBalance.sub(bnFeeAmount).gte(bnFeeTokenMinAmount)) {
|
|
@@ -372,7 +371,7 @@ class BaseSpecialStakingPoolHandler extends _base.default {
|
|
|
372
371
|
const inputTokenSlug = this.inputAsset;
|
|
373
372
|
const inputTokenInfo = this.state.getAssetBySlug(inputTokenSlug);
|
|
374
373
|
const balanceService = this.state.balanceService;
|
|
375
|
-
const inputTokenBalance = await balanceService.
|
|
374
|
+
const inputTokenBalance = await balanceService.getTransferableBalance(params.address, inputTokenInfo.originChain, inputTokenSlug);
|
|
376
375
|
const bnInputTokenBalance = new _util.BN(inputTokenBalance.value || '0');
|
|
377
376
|
const bnAmount = new _util.BN(params.amount);
|
|
378
377
|
if (bnAmount.lte(_util.BN_ZERO)) {
|
|
@@ -422,7 +421,7 @@ class BaseSpecialStakingPoolHandler extends _base.default {
|
|
|
422
421
|
const originTokenInfo = this.state.getAssetBySlug(originTokenSlug);
|
|
423
422
|
const destinationTokenInfo = this.state.getAssetBySlug(destinationTokenSlug);
|
|
424
423
|
const substrateApi = this.state.getSubstrateApi(originChainInfo.slug);
|
|
425
|
-
const inputTokenBalance = await this.state.balanceService.
|
|
424
|
+
const inputTokenBalance = await this.state.balanceService.getTransferableBalance(address, destinationTokenInfo.originChain, destinationTokenSlug);
|
|
426
425
|
const bnInputTokenBalance = new _util.BN(inputTokenBalance.value);
|
|
427
426
|
const bnXcmFee = new _util.BN(xcmFee);
|
|
428
427
|
const bnAmount = new _util.BN(amount);
|
|
@@ -14,7 +14,8 @@ class MigrateTransactionHistoryBySymbol extends _Base.default {
|
|
|
14
14
|
const state = this.state;
|
|
15
15
|
try {
|
|
16
16
|
const changeSlugsMap = {
|
|
17
|
-
'
|
|
17
|
+
'bobMainnet-LOCAL-wBTC-0x03C7054BCB39f7b2e5B2c7AcB37583e32D70Cfa3': 'bobMainnet-LOCAL-WBTC-0x03C7054BCB39f7b2e5B2c7AcB37583e32D70Cfa3',
|
|
18
|
+
'hydradx_main-LOCAL-LRNA': 'hydradx_main-LOCAL-H2O'
|
|
18
19
|
};
|
|
19
20
|
const allTxs = [];
|
|
20
21
|
await Promise.all(Object.entries(changeSlugsMap).map(async (_ref, i) => {
|
|
@@ -13,7 +13,8 @@ class MigrateAssetSetting extends _Base.default {
|
|
|
13
13
|
async run() {
|
|
14
14
|
try {
|
|
15
15
|
const changeSlugsMap = {
|
|
16
|
-
'
|
|
16
|
+
'bobMainnet-LOCAL-wBTC-0x03C7054BCB39f7b2e5B2c7AcB37583e32D70Cfa3': 'bobMainnet-LOCAL-WBTC-0x03C7054BCB39f7b2e5B2c7AcB37583e32D70Cfa3',
|
|
17
|
+
'hydradx_main-LOCAL-LRNA': 'hydradx_main-LOCAL-H2O'
|
|
17
18
|
};
|
|
18
19
|
const assetSetting = await this.state.chainService.getAssetSettings();
|
|
19
20
|
const migratedAssetSetting = {};
|
|
@@ -57,8 +57,8 @@ var _default = {
|
|
|
57
57
|
'1.1.28-01': _MigrateEarningVersion.default,
|
|
58
58
|
'1.1.41-01': _DeleteChainStaking.default,
|
|
59
59
|
'1.1.46-01': _AutoEnableSomeTokens.default,
|
|
60
|
-
'1.1.
|
|
61
|
-
'1.1.
|
|
60
|
+
'1.1.69-03': _MigrateAssetSetting.default,
|
|
61
|
+
'1.1.69-02': _MigrateTransactionHistoryBySymbol.default,
|
|
62
62
|
'1.2.69-01': _MigrateRemoveGenesisHash.default
|
|
63
63
|
// [`${EVERYTIME}-1.1.42-02`]: MigrateTransactionHistoryBySymbol
|
|
64
64
|
// [`${EVERYTIME}-1`]: AutoEnableChainsTokens
|
|
@@ -7,13 +7,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
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
|
+
var _swap = require("@subwallet/extension-base/core/logic-validation/swap");
|
|
10
11
|
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
11
12
|
var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
|
|
12
|
-
var
|
|
13
|
+
var _swap2 = require("@subwallet/extension-base/types/swap");
|
|
13
14
|
var _utils3 = require("@subwallet/extension-base/utils");
|
|
14
15
|
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
15
16
|
var _i18next = require("i18next");
|
|
16
|
-
var _utilCrypto = require("@polkadot/util-crypto");
|
|
17
17
|
// Copyright 2019-2022 @subwallet/extension-base
|
|
18
18
|
// SPDX-License-Identifier: Apache-2.0
|
|
19
19
|
|
|
@@ -62,18 +62,19 @@ class SwapBaseHandler {
|
|
|
62
62
|
}
|
|
63
63
|
const alternativeAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
64
64
|
const fromAsset = this.chainService.getAssetBySlug(swapPair.from);
|
|
65
|
-
const [alternativeAssetBalance, fromAssetBalance] = await Promise.all([this.balanceService.
|
|
65
|
+
const [alternativeAssetBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTransferableBalance(params.address, alternativeAsset.originChain, alternativeAssetSlug), this.balanceService.getTransferableBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
|
|
66
66
|
const bnAlternativeAssetBalance = new _bignumber.default(alternativeAssetBalance.value);
|
|
67
67
|
const bnFromAssetBalance = new _bignumber.default(fromAssetBalance.value);
|
|
68
68
|
const xcmFeeComponent = params.process.totalFee[stepIndex].feeComponent[0]; // todo: can do better than indexing
|
|
69
69
|
const xcmFee = new _bignumber.default(xcmFeeComponent.amount || '0');
|
|
70
70
|
let xcmAmount = bnAmount.minus(bnFromAssetBalance);
|
|
71
|
+
let editedXcmFee = new _bignumber.default(0);
|
|
71
72
|
if ((0, _utils._isNativeToken)(alternativeAsset)) {
|
|
72
73
|
xcmAmount = xcmAmount.plus(xcmFee);
|
|
74
|
+
editedXcmFee = xcmFee.times(2);
|
|
73
75
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const maxBn = bnFromAssetBalance.plus(new _bignumber.default(alternativeAssetBalance.value)).minus(xcmFee).minus(alternativeTokenMinAmount);
|
|
76
|
+
if (!bnAlternativeAssetBalance.minus((0, _utils._isNativeToken)(alternativeAsset) ? xcmAmount.plus(xcmFee) : xcmFee).gt(0)) {
|
|
77
|
+
const maxBn = bnFromAssetBalance.plus(new _bignumber.default(alternativeAssetBalance.value)).minus((0, _utils._isNativeToken)(alternativeAsset) ? editedXcmFee : xcmFee);
|
|
77
78
|
const maxValue = (0, _utils3.formatNumber)(maxBn.toString(), fromAsset.decimals || 0);
|
|
78
79
|
const altInputTokenInfo = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
79
80
|
const symbol = altInputTokenInfo.symbol;
|
|
@@ -82,7 +83,7 @@ class SwapBaseHandler {
|
|
|
82
83
|
const inputNetworkName = chain.name;
|
|
83
84
|
const altNetworkName = alternativeChain.name;
|
|
84
85
|
const currentValue = (0, _utils3.formatNumber)(bnFromAssetBalance.toString(), fromAsset.decimals || 0);
|
|
85
|
-
const bnMaxXCM = new _bignumber.default(alternativeAssetBalance.value).minus(
|
|
86
|
+
const bnMaxXCM = new _bignumber.default(alternativeAssetBalance.value).minus((0, _utils._isNativeToken)(alternativeAsset) ? editedXcmFee : xcmFee);
|
|
86
87
|
const maxXCMValue = (0, _utils3.formatNumber)(bnMaxXCM.toString(), fromAsset.decimals || 0);
|
|
87
88
|
if (maxBn.lte(0) || bnFromAssetBalance.lte(0) || bnMaxXCM.lte(0)) {
|
|
88
89
|
return [new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.NOT_ENOUGH_BALANCE, (0, _i18next.t)(`Insufficient balance. Deposit ${fromAsset.symbol} and try again.`))];
|
|
@@ -110,7 +111,7 @@ class SwapBaseHandler {
|
|
|
110
111
|
const feeInfo = params.process.totalFee[stepIndex];
|
|
111
112
|
const feeAmount = feeInfo.feeComponent[0];
|
|
112
113
|
const feeTokenInfo = this.chainService.getAssetBySlug(feeInfo.defaultFeeToken);
|
|
113
|
-
const feeTokenBalance = await this.balanceService.
|
|
114
|
+
const feeTokenBalance = await this.balanceService.getTransferableBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug);
|
|
114
115
|
const bnFeeTokenBalance = new _bignumber.default(feeTokenBalance.value);
|
|
115
116
|
const bnFeeAmount = new _bignumber.default(feeAmount.amount);
|
|
116
117
|
if (bnFeeAmount.gte(bnFeeTokenBalance)) {
|
|
@@ -119,57 +120,42 @@ class SwapBaseHandler {
|
|
|
119
120
|
return [];
|
|
120
121
|
}
|
|
121
122
|
async validateSwapStep(params, isXcmOk, stepIndex) {
|
|
123
|
+
// check swap quote timestamp
|
|
124
|
+
// check balance to pay transaction fee
|
|
125
|
+
// check balance against spending amount
|
|
122
126
|
if (!params.selectedQuote) {
|
|
123
127
|
return Promise.resolve([new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.INTERNAL_ERROR)]);
|
|
124
128
|
}
|
|
125
129
|
const selectedQuote = params.selectedQuote;
|
|
126
130
|
const currentTimestamp = +Date.now();
|
|
127
131
|
if (selectedQuote.aliveUntil <= currentTimestamp) {
|
|
128
|
-
return Promise.resolve([new _TransactionError.TransactionError(
|
|
132
|
+
return Promise.resolve([new _TransactionError.TransactionError(_swap2.SwapErrorType.QUOTE_TIMEOUT)]);
|
|
129
133
|
}
|
|
130
|
-
const bnAmount = new _bignumber.default(params.selectedQuote.fromAmount);
|
|
131
|
-
const fromAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.from);
|
|
132
134
|
const stepFee = params.process.totalFee[stepIndex].feeComponent;
|
|
133
|
-
const networkFee = stepFee.find(fee => fee.feeType ===
|
|
135
|
+
const networkFee = stepFee.find(fee => fee.feeType === _swap2.SwapFeeType.NETWORK_FEE);
|
|
134
136
|
if (!networkFee) {
|
|
135
137
|
return Promise.resolve([new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.INTERNAL_ERROR)]);
|
|
136
138
|
}
|
|
139
|
+
const fromAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.from);
|
|
137
140
|
const feeTokenInfo = this.chainService.getAssetBySlug(networkFee.tokenSlug);
|
|
138
141
|
const feeTokenChain = this.chainService.getChainInfoByKey(feeTokenInfo.originChain);
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
const {
|
|
143
|
+
fromAmount,
|
|
144
|
+
minSwap
|
|
145
|
+
} = params.selectedQuote;
|
|
146
|
+
const [feeTokenBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTransferableBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug), this.balanceService.getTransferableBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
|
|
147
|
+
const balanceError = (0, _swap._validateBalanceToSwap)(fromAsset, feeTokenInfo, feeTokenChain, networkFee.amount, fromAssetBalance.value, feeTokenBalance.value, fromAmount, isXcmOk, minSwap);
|
|
148
|
+
if (balanceError) {
|
|
149
|
+
return Promise.resolve([balanceError]);
|
|
145
150
|
}
|
|
146
|
-
if (
|
|
147
|
-
|
|
148
|
-
return Promise.resolve([new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.NOT_ENOUGH_BALANCE, `Insufficient balance. Deposit ${fromAsset.symbol} and try again.`)]);
|
|
149
|
-
}
|
|
151
|
+
if (!params.recipient) {
|
|
152
|
+
return Promise.resolve([]);
|
|
150
153
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const bnSrcAssetMinAmount = new _bignumber.default((0, _utils._getTokenMinAmount)(fromAsset));
|
|
160
|
-
const bnMaxBalanceSwap = bnFromAssetBalance.minus(bnSrcAssetMinAmount);
|
|
161
|
-
if (!isXcmOk && bnAmount.gte(bnMaxBalanceSwap)) {
|
|
162
|
-
const parsedMaxBalanceSwap = (0, _utils3.formatNumber)(bnMaxBalanceSwap, (0, _utils._getAssetDecimals)(fromAsset));
|
|
163
|
-
return Promise.resolve([new _TransactionError.TransactionError(_swap.SwapErrorType.SWAP_EXCEED_ALLOWANCE, `Amount too high. Lower your amount ${bnMaxBalanceSwap.gt(0) ? `below ${parsedMaxBalanceSwap} ${fromAsset.symbol}` : ''} and try again`)]);
|
|
164
|
-
}
|
|
165
|
-
if (params.recipient) {
|
|
166
|
-
const toAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.to);
|
|
167
|
-
const destChainInfo = this.chainService.getChainInfoByKey(toAsset.originChain);
|
|
168
|
-
const isEvmAddress = (0, _utilCrypto.isEthereumAddress)(params.recipient);
|
|
169
|
-
const isEvmDestChain = (0, _utils._isChainEvmCompatible)(destChainInfo);
|
|
170
|
-
if (isEvmAddress && !isEvmDestChain || !isEvmAddress && isEvmDestChain) {
|
|
171
|
-
return Promise.resolve([new _TransactionError.TransactionError(_swap.SwapErrorType.INVALID_RECIPIENT)]);
|
|
172
|
-
}
|
|
154
|
+
const toAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.to);
|
|
155
|
+
const toAssetChain = this.chainService.getChainInfoByKey(toAsset.originChain);
|
|
156
|
+
const recipientError = (0, _swap._validateSwapRecipient)(toAssetChain, params.recipient);
|
|
157
|
+
if (recipientError) {
|
|
158
|
+
return Promise.resolve([recipientError]);
|
|
173
159
|
}
|
|
174
160
|
return Promise.resolve([]);
|
|
175
161
|
}
|
|
@@ -10,12 +10,13 @@ var _chainList = require("@subwallet/chain-list");
|
|
|
10
10
|
var _SwapError = require("@subwallet/extension-base/background/errors/SwapError");
|
|
11
11
|
var _TransactionError = require("@subwallet/extension-base/background/errors/TransactionError");
|
|
12
12
|
var _KoniTypes = require("@subwallet/extension-base/background/KoniTypes");
|
|
13
|
+
var _swap2 = require("@subwallet/extension-base/core/logic-validation/swap");
|
|
13
14
|
var _transfer = require("@subwallet/extension-base/koni/api/dotsama/transfer");
|
|
14
15
|
var _transfer2 = require("@subwallet/extension-base/koni/api/tokens/evm/transfer");
|
|
15
16
|
var _utils = require("@subwallet/extension-base/services/chain-service/utils");
|
|
16
17
|
var _baseHandler = require("@subwallet/extension-base/services/swap-service/handler/base-handler");
|
|
17
18
|
var _utils2 = require("@subwallet/extension-base/services/swap-service/utils");
|
|
18
|
-
var
|
|
19
|
+
var _swap3 = require("@subwallet/extension-base/types/swap");
|
|
19
20
|
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
20
21
|
// Copyright 2019-2022 @subwallet/extension-base
|
|
21
22
|
// SPDX-License-Identifier: Apache-2.0
|
|
@@ -41,9 +42,10 @@ class ChainflipSwapHandler {
|
|
|
41
42
|
chainService,
|
|
42
43
|
balanceService,
|
|
43
44
|
providerName: isTestnet ? 'Chainflip Testnet' : 'Chainflip',
|
|
44
|
-
providerSlug: isTestnet ?
|
|
45
|
+
providerSlug: isTestnet ? _swap3.SwapProviderId.CHAIN_FLIP_TESTNET : _swap3.SwapProviderId.CHAIN_FLIP_MAINNET
|
|
45
46
|
});
|
|
46
47
|
this.isTestnet = isTestnet;
|
|
48
|
+
this.providerSlug = isTestnet ? _swap3.SwapProviderId.CHAIN_FLIP_TESTNET : _swap3.SwapProviderId.CHAIN_FLIP_MAINNET;
|
|
47
49
|
this.swapSdk = new _swap.SwapSDK({
|
|
48
50
|
network: isTestnet ? 'perseverance' : 'mainnet'
|
|
49
51
|
});
|
|
@@ -99,7 +101,7 @@ class ChainflipSwapHandler {
|
|
|
99
101
|
const toAssetId = this.assetMapping[toAsset.slug];
|
|
100
102
|
if (!srcChainId || !destChainId || !fromAssetId || !toAssetId) {
|
|
101
103
|
return {
|
|
102
|
-
error:
|
|
104
|
+
error: _swap3.SwapErrorType.ASSET_NOT_SUPPORTED
|
|
103
105
|
};
|
|
104
106
|
}
|
|
105
107
|
const [supportedDestChains, srcAssets, destAssets] = await Promise.all([this.swapSdk.getChains(srcChainId), this.swapSdk.getAssets(srcChainId), this.swapSdk.getAssets(destChainId)]);
|
|
@@ -108,7 +110,7 @@ class ChainflipSwapHandler {
|
|
|
108
110
|
const destAssetData = destAssets.find(a => a.asset === toAssetId);
|
|
109
111
|
if (!destAssetData || !srcAssetData || !supportedDestChainId) {
|
|
110
112
|
return {
|
|
111
|
-
error:
|
|
113
|
+
error: _swap3.SwapErrorType.UNKNOWN
|
|
112
114
|
};
|
|
113
115
|
}
|
|
114
116
|
const bnAmount = new _bignumber.default(request.fromAmount);
|
|
@@ -117,12 +119,12 @@ class ChainflipSwapHandler {
|
|
|
117
119
|
const bnMaxProtocolSwap = new _bignumber.default(srcAssetData.maximumSwapAmount);
|
|
118
120
|
if (bnMinSwap.gte(bnMaxProtocolSwap)) {
|
|
119
121
|
return {
|
|
120
|
-
error:
|
|
122
|
+
error: _swap3.SwapErrorType.UNKNOWN
|
|
121
123
|
};
|
|
122
124
|
}
|
|
123
125
|
if (bnAmount.gte(bnMaxProtocolSwap)) {
|
|
124
126
|
return {
|
|
125
|
-
error:
|
|
127
|
+
error: _swap3.SwapErrorType.SWAP_EXCEED_ALLOWANCE,
|
|
126
128
|
metadata: {
|
|
127
129
|
minSwap: {
|
|
128
130
|
value: srcAssetData.minimumSwapAmount,
|
|
@@ -142,7 +144,7 @@ class ChainflipSwapHandler {
|
|
|
142
144
|
if (bnAmount.lt(bnMinSwap)) {
|
|
143
145
|
// might miss case when minSwap is 0
|
|
144
146
|
return {
|
|
145
|
-
error:
|
|
147
|
+
error: _swap3.SwapErrorType.NOT_MEET_MIN_SWAP,
|
|
146
148
|
metadata: {
|
|
147
149
|
minSwap: {
|
|
148
150
|
value: srcAssetData.minimumSwapAmount,
|
|
@@ -175,7 +177,7 @@ class ChainflipSwapHandler {
|
|
|
175
177
|
};
|
|
176
178
|
} catch (e) {
|
|
177
179
|
return {
|
|
178
|
-
error:
|
|
180
|
+
error: _swap3.SwapErrorType.UNKNOWN
|
|
179
181
|
};
|
|
180
182
|
}
|
|
181
183
|
}
|
|
@@ -193,12 +195,12 @@ class ChainflipSwapHandler {
|
|
|
193
195
|
const fromChain = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
194
196
|
const fromChainNativeTokenSlug = (0, _utils._getChainNativeTokenSlug)(fromChain);
|
|
195
197
|
if (!fromAsset || !toAsset) {
|
|
196
|
-
return new _SwapError.SwapError(
|
|
198
|
+
return new _SwapError.SwapError(_swap3.SwapErrorType.UNKNOWN);
|
|
197
199
|
}
|
|
198
200
|
const earlyValidation = await this.validateSwapRequest(request);
|
|
199
201
|
const metadata = earlyValidation.metadata;
|
|
200
202
|
if (earlyValidation.error) {
|
|
201
|
-
return (0,
|
|
203
|
+
return (0, _swap2._getChainflipEarlyValidationError)(earlyValidation.error, metadata);
|
|
202
204
|
}
|
|
203
205
|
const srcChainId = this.chainMapping[fromAsset.originChain];
|
|
204
206
|
const destChainId = this.chainMapping[toAsset.originChain];
|
|
@@ -225,7 +227,7 @@ class ChainflipSwapHandler {
|
|
|
225
227
|
feeComponent.push({
|
|
226
228
|
tokenSlug,
|
|
227
229
|
amount: fee.amount,
|
|
228
|
-
feeType:
|
|
230
|
+
feeType: _swap3.SwapFeeType.NETWORK_FEE
|
|
229
231
|
});
|
|
230
232
|
break;
|
|
231
233
|
}
|
|
@@ -238,7 +240,7 @@ class ChainflipSwapHandler {
|
|
|
238
240
|
feeComponent.push({
|
|
239
241
|
tokenSlug,
|
|
240
242
|
amount: fee.amount,
|
|
241
|
-
feeType:
|
|
243
|
+
feeType: _swap3.SwapFeeType.PLATFORM_FEE
|
|
242
244
|
});
|
|
243
245
|
break;
|
|
244
246
|
}
|
|
@@ -272,12 +274,12 @@ class ChainflipSwapHandler {
|
|
|
272
274
|
const errorObj = error === null || error === void 0 ? void 0 : (_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.data;
|
|
273
275
|
if (errorObj && errorObj.error && errorObj.error.includes(CHAINFLIP_QUOTE_ERROR.InsufficientLiquidity)) {
|
|
274
276
|
// todo: Chainflip will improve this
|
|
275
|
-
return new _SwapError.SwapError(
|
|
277
|
+
return new _SwapError.SwapError(_swap3.SwapErrorType.NOT_ENOUGH_LIQUIDITY);
|
|
276
278
|
}
|
|
277
279
|
if (errorObj && errorObj.message && errorObj.message.includes(CHAINFLIP_QUOTE_ERROR.InsufficientLiquidity)) {
|
|
278
|
-
return new _SwapError.SwapError(
|
|
280
|
+
return new _SwapError.SwapError(_swap3.SwapErrorType.NOT_ENOUGH_LIQUIDITY);
|
|
279
281
|
}
|
|
280
|
-
return new _SwapError.SwapError(
|
|
282
|
+
return new _SwapError.SwapError(_swap3.SwapErrorType.ERROR_FETCHING_QUOTE);
|
|
281
283
|
}
|
|
282
284
|
}
|
|
283
285
|
async validateSwapProcess(params) {
|
|
@@ -290,9 +292,9 @@ class ChainflipSwapHandler {
|
|
|
290
292
|
for (const [index, step] of params.process.steps.entries()) {
|
|
291
293
|
const getErrors = async () => {
|
|
292
294
|
switch (step.type) {
|
|
293
|
-
case
|
|
295
|
+
case _swap3.SwapStepType.DEFAULT:
|
|
294
296
|
return Promise.resolve([]);
|
|
295
|
-
case
|
|
297
|
+
case _swap3.SwapStepType.TOKEN_APPROVAL:
|
|
296
298
|
return Promise.reject(new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.UNSUPPORTED));
|
|
297
299
|
default:
|
|
298
300
|
return this.swapBaseHandler.validateSwapStep(params, isXcmOk, index);
|
|
@@ -301,7 +303,7 @@ class ChainflipSwapHandler {
|
|
|
301
303
|
const errors = await getErrors();
|
|
302
304
|
if (errors.length) {
|
|
303
305
|
return errors;
|
|
304
|
-
} else if (step.type ===
|
|
306
|
+
} else if (step.type === _swap3.SwapStepType.XCM) {
|
|
305
307
|
isXcmOk = true;
|
|
306
308
|
}
|
|
307
309
|
}
|
|
@@ -382,9 +384,9 @@ class ChainflipSwapHandler {
|
|
|
382
384
|
} = params;
|
|
383
385
|
const type = process.steps[currentStep].type;
|
|
384
386
|
switch (type) {
|
|
385
|
-
case
|
|
387
|
+
case _swap3.SwapStepType.DEFAULT:
|
|
386
388
|
return Promise.reject(new _TransactionError.TransactionError(_KoniTypes.BasicTxErrorType.UNSUPPORTED));
|
|
387
|
-
case
|
|
389
|
+
case _swap3.SwapStepType.SWAP:
|
|
388
390
|
return this.handleSubmitStep(params);
|
|
389
391
|
default:
|
|
390
392
|
return this.handleSubmitStep(params);
|
|
@@ -394,7 +396,7 @@ class ChainflipSwapHandler {
|
|
|
394
396
|
if (params.selectedQuote) {
|
|
395
397
|
const submitStep = {
|
|
396
398
|
name: 'Swap',
|
|
397
|
-
type:
|
|
399
|
+
type: _swap3.SwapStepType.SWAP
|
|
398
400
|
};
|
|
399
401
|
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
|
|
400
402
|
}
|