@subwallet/extension-base 1.1.52-0 → 1.1.53-beta.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 +9 -1
- package/background/KoniTypes.js +1 -0
- package/background/errors/SwapError.d.ts +6 -0
- package/background/errors/SwapError.js +57 -0
- package/background/errors/TransactionError.js +9 -0
- package/cjs/background/KoniTypes.js +1 -0
- package/cjs/background/errors/SwapError.js +64 -0
- package/cjs/background/errors/TransactionError.js +9 -0
- package/cjs/koni/api/dotsama/balance.js +464 -0
- package/cjs/koni/api/nft/ordinal_nft/utils.js +41 -0
- package/cjs/koni/api/staking/bonding/utils.js +35 -6
- package/cjs/koni/background/handlers/Extension.js +85 -0
- package/cjs/koni/background/handlers/State.js +25 -5
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/helpers/subscribe/balance.js +140 -0
- package/cjs/services/balance-service/helpers/subscribe/substrate/index.js +2 -1
- package/cjs/services/balance-service/index.js +6 -3
- package/cjs/services/chain-service/constants.js +1 -1
- package/cjs/services/chain-service/index.js +43 -18
- package/cjs/services/chain-service/utils/index.js +7 -2
- package/cjs/services/chain-service/utils/patch.js +1 -1
- package/cjs/services/chain-service/utils.js +506 -0
- package/cjs/services/earning-service/constants/chains.js +4 -2
- package/cjs/services/earning-service/handlers/native-staking/astar.js +4 -3
- package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +22 -3
- package/cjs/services/migration-service/scripts/MigrateEthProvider.js +17 -0
- package/cjs/services/migration-service/scripts/MigratePioneerProvider.js +17 -0
- package/cjs/services/migration-service/scripts/MigrateProvider.js +29 -0
- package/cjs/services/request-service/handler/ConnectWCRequestHandler.js +0 -1
- package/cjs/services/storage-service/index.js +241 -0
- package/cjs/services/swap-service/handler/base-handler.js +171 -0
- package/cjs/services/swap-service/handler/chainflip-handler.js +407 -0
- package/cjs/services/swap-service/handler/hydradx-handler.js +444 -0
- package/cjs/services/swap-service/index.js +249 -0
- package/cjs/services/swap-service/utils.js +126 -0
- package/cjs/services/transaction-service/index.js +20 -0
- package/cjs/services/transaction-service/utils.js +6 -0
- package/cjs/services/wallet-connect-service/helpers.js +1 -1
- package/cjs/services/wallet-connect-service/index.js +19 -0
- package/cjs/types/balance.js +1 -0
- package/cjs/types/fee/evm.js +1 -0
- package/cjs/types/fee/fee.js +70 -0
- package/cjs/types/fee/index.js +27 -1
- package/cjs/types/service-base.js +1 -0
- package/cjs/types/swap/index.js +50 -0
- package/cjs/types.js +1 -0
- package/cjs/utils/address.js +34 -0
- package/cjs/utils/eth/parseTransaction/index.js +1 -1
- package/cjs/utils/index.js +12 -0
- package/cjs/utils/keyring.js +57 -0
- package/cjs/utils/swap.js +78 -0
- package/koni/api/staking/bonding/utils.d.ts +3 -1
- package/koni/api/staking/bonding/utils.js +32 -6
- package/koni/background/handlers/Extension.d.ts +5 -0
- package/koni/background/handlers/Extension.js +85 -0
- package/koni/background/handlers/State.d.ts +2 -0
- package/koni/background/handlers/State.js +26 -6
- package/package.json +64 -7
- package/packageInfo.js +1 -1
- package/services/balance-service/helpers/subscribe/substrate/index.js +2 -1
- package/services/balance-service/index.js +6 -3
- package/services/base/types.d.ts +4 -0
- package/services/chain-service/constants.js +1 -1
- package/services/chain-service/index.d.ts +4 -0
- package/services/chain-service/index.js +24 -0
- package/services/chain-service/utils/index.d.ts +6 -5
- package/services/chain-service/utils/index.js +5 -2
- package/services/chain-service/utils/patch.js +1 -1
- package/services/earning-service/constants/chains.d.ts +1 -0
- package/services/earning-service/constants/chains.js +1 -0
- package/services/earning-service/handlers/native-staking/astar.js +4 -3
- package/services/earning-service/handlers/native-staking/relay-chain.js +24 -5
- package/services/event-service/types.d.ts +1 -0
- package/services/request-service/handler/ConnectWCRequestHandler.js +0 -1
- package/services/swap-service/handler/base-handler.d.ts +38 -0
- package/services/swap-service/handler/base-handler.js +162 -0
- package/services/swap-service/handler/chainflip-handler.d.ts +30 -0
- package/services/swap-service/handler/chainflip-handler.js +399 -0
- package/services/swap-service/handler/hydradx-handler.d.ts +36 -0
- package/services/swap-service/handler/hydradx-handler.js +435 -0
- package/services/swap-service/index.d.ts +32 -0
- package/services/swap-service/index.js +240 -0
- package/services/swap-service/utils.d.ts +18 -0
- package/services/swap-service/utils.js +105 -0
- package/services/transaction-service/index.js +20 -0
- package/services/transaction-service/utils.d.ts +2 -0
- package/services/transaction-service/utils.js +6 -2
- package/services/wallet-connect-service/helpers.js +1 -1
- package/services/wallet-connect-service/index.d.ts +1 -0
- package/services/wallet-connect-service/index.js +19 -1
- package/types/fee/evm.d.ts +49 -0
- package/types/fee/evm.js +1 -0
- package/types/fee/fee.d.ts +32 -0
- package/types/fee/fee.js +63 -0
- package/types/fee/index.d.ts +2 -49
- package/types/fee/index.js +5 -1
- package/types/service-base.d.ts +10 -0
- package/types/service-base.js +1 -0
- package/types/swap/index.d.ts +166 -0
- package/types/swap/index.js +41 -0
- package/types/yield/info/chain/target.d.ts +2 -0
- package/types/yield/info/pallet.d.ts +4 -0
- package/utils/eth/parseTransaction/index.js +1 -1
- package/utils/index.d.ts +1 -0
- package/utils/index.js +2 -1
- package/utils/swap.d.ts +3 -0
- package/utils/swap.js +70 -0
|
@@ -242,11 +242,12 @@ export default class AstarNativeStakingPoolHandler extends BaseParaNativeStaking
|
|
|
242
242
|
};
|
|
243
243
|
}
|
|
244
244
|
async subscribePoolPosition(useAddresses, resultCallback) {
|
|
245
|
+
var _substrateApi$api$que;
|
|
245
246
|
let cancel = false;
|
|
246
247
|
const substrateApi = await this.substrateApi.isReady;
|
|
247
248
|
const defaultInfo = this.baseInfo;
|
|
248
249
|
const chainInfo = this.chainInfo;
|
|
249
|
-
const unsub = await substrateApi.api.query.dappsStaking.ledger.multi(useAddresses, async ledgers => {
|
|
250
|
+
const unsub = await ((_substrateApi$api$que = substrateApi.api.query.dappsStaking) === null || _substrateApi$api$que === void 0 ? void 0 : _substrateApi$api$que.ledger.multi(useAddresses, async ledgers => {
|
|
250
251
|
if (cancel) {
|
|
251
252
|
unsub();
|
|
252
253
|
return;
|
|
@@ -280,10 +281,10 @@ export default class AstarNativeStakingPoolHandler extends BaseParaNativeStaking
|
|
|
280
281
|
}
|
|
281
282
|
}));
|
|
282
283
|
}
|
|
283
|
-
});
|
|
284
|
+
}));
|
|
284
285
|
return () => {
|
|
285
286
|
cancel = true;
|
|
286
|
-
unsub();
|
|
287
|
+
unsub && unsub();
|
|
287
288
|
};
|
|
288
289
|
}
|
|
289
290
|
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
|
|
4
4
|
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
|
|
5
5
|
import { BasicTxErrorType, ExtrinsicType, StakingTxErrorType } from '@subwallet/extension-base/background/KoniTypes';
|
|
6
|
-
import { calculateAlephZeroValidatorReturn, calculateChainStakedReturnV2, calculateInflation, calculateTernoaValidatorReturn, calculateValidatorStakedReturn, getAvgValidatorEraReward, getCommission, getMaxValidatorErrorMessage, getMinStakeErrorMessage, getSupportedDaysByHistoryDepth } from '@subwallet/extension-base/koni/api/staking/bonding/utils';
|
|
6
|
+
import { calculateAlephZeroValidatorReturn, calculateChainStakedReturnV2, calculateInflation, calculateTernoaValidatorReturn, calculateValidatorStakedReturn, getAvgValidatorEraReward, getCommission, getMaxValidatorErrorMessage, getMinStakeErrorMessage, getSupportedDaysByHistoryDepth, getTopValidatorByPoints, getValidatorPointsMap } from '@subwallet/extension-base/koni/api/staking/bonding/utils';
|
|
7
7
|
import { _STAKING_ERA_LENGTH_MAP } from '@subwallet/extension-base/services/chain-service/constants';
|
|
8
8
|
import { _getChainSubstrateAddressPrefix } from '@subwallet/extension-base/services/chain-service/utils';
|
|
9
|
-
import { _STAKING_CHAIN_GROUP } from '@subwallet/extension-base/services/earning-service/constants';
|
|
9
|
+
import { _STAKING_CHAIN_GROUP, MaxEraRewardPointsEras } from '@subwallet/extension-base/services/earning-service/constants';
|
|
10
10
|
import { parseIdentity } from '@subwallet/extension-base/services/earning-service/utils';
|
|
11
11
|
import { EarningStatus, UnstakingStatus } from '@subwallet/extension-base/types';
|
|
12
12
|
import { balanceFormatter, formatNumber, reformatAddress } from '@subwallet/extension-base/utils';
|
|
@@ -274,11 +274,23 @@ export default class RelayNativeStakingPoolHandler extends BaseNativeStakingPool
|
|
|
274
274
|
if (!poolInfo || !poolInfo.statistic) {
|
|
275
275
|
return Promise.reject(new TransactionError(BasicTxErrorType.INTERNAL_ERROR));
|
|
276
276
|
}
|
|
277
|
-
const _era = await chainApi.api.query.staking.currentEra();
|
|
277
|
+
const [_era, _activeEraInfo] = await Promise.all([chainApi.api.query.staking.currentEra(), chainApi.api.query.staking.activeEra()]);
|
|
278
278
|
const currentEra = _era.toString();
|
|
279
|
+
const activeEraInfo = _activeEraInfo.toPrimitive();
|
|
280
|
+
const activeEra = activeEraInfo.index;
|
|
279
281
|
const allValidators = [];
|
|
280
282
|
const validatorInfoList = [];
|
|
281
|
-
const
|
|
283
|
+
const maxEraRewardPointsEras = MaxEraRewardPointsEras;
|
|
284
|
+
const endEraForPoints = parseInt(activeEra) - 1;
|
|
285
|
+
let startEraForPoints = endEraForPoints - maxEraRewardPointsEras + 1;
|
|
286
|
+
const [_totalEraStake, _eraStakers, _minBond, _stakingRewards, _validators, ..._eraRewardPoints] = await Promise.all([chainApi.api.query.staking.erasTotalStake(parseInt(currentEra)), chainApi.api.query.staking.erasStakers.entries(parseInt(currentEra)), chainApi.api.query.staking.minNominatorBond(), ((_chainApi$api$query$s = chainApi.api.query.stakingRewards) === null || _chainApi$api$query$s === void 0 ? void 0 : _chainApi$api$query$s.data) && chainApi.api.query.stakingRewards.data(), chainApi.api.query.staking.validators.entries(), chainApi.api.query.staking.erasRewardPoints.multi([...Array(maxEraRewardPointsEras).keys()].map(i => i + startEraForPoints))]);
|
|
287
|
+
const eraRewardMap = {};
|
|
288
|
+
for (const item of _eraRewardPoints[0]) {
|
|
289
|
+
eraRewardMap[startEraForPoints] = item.toHuman();
|
|
290
|
+
startEraForPoints++;
|
|
291
|
+
}
|
|
292
|
+
const validatorPointsMap = getValidatorPointsMap(eraRewardMap);
|
|
293
|
+
const topValidatorList = getTopValidatorByPoints(validatorPointsMap);
|
|
282
294
|
|
|
283
295
|
// filter blocked validators
|
|
284
296
|
const validators = _validators;
|
|
@@ -309,6 +321,11 @@ export default class RelayNativeStakingPoolHandler extends BaseNativeStakingPool
|
|
|
309
321
|
const rawValidatorStat = item[1].toHuman();
|
|
310
322
|
const validatorAddress = rawValidatorInfo[1];
|
|
311
323
|
if (!blockValidatorList.includes(validatorAddress)) {
|
|
324
|
+
var _validatorPointsMap$v;
|
|
325
|
+
let isTopQuartile = false;
|
|
326
|
+
if (topValidatorList.includes(validatorAddress)) {
|
|
327
|
+
isTopQuartile = true;
|
|
328
|
+
}
|
|
312
329
|
const rawTotalStake = rawValidatorStat.total;
|
|
313
330
|
const rawOwnStake = rawValidatorStat.own;
|
|
314
331
|
const bnTotalStake = new BN(rawTotalStake.replaceAll(',', ''));
|
|
@@ -333,7 +350,9 @@ export default class RelayNativeStakingPoolHandler extends BaseNativeStakingPool
|
|
|
333
350
|
blocked: false,
|
|
334
351
|
isVerified: false,
|
|
335
352
|
minBond,
|
|
336
|
-
isCrowded: unlimitedNominatorRewarded ? false : nominatorCount > parseInt(maxNominatorRewarded)
|
|
353
|
+
isCrowded: unlimitedNominatorRewarded ? false : nominatorCount > parseInt(maxNominatorRewarded),
|
|
354
|
+
eraRewardPoint: ((_validatorPointsMap$v = validatorPointsMap[validatorAddress]) !== null && _validatorPointsMap$v !== void 0 ? _validatorPointsMap$v : BN_ZERO).toString(),
|
|
355
|
+
topQuartile: isTopQuartile
|
|
337
356
|
});
|
|
338
357
|
}
|
|
339
358
|
}
|
|
@@ -38,6 +38,7 @@ export interface EventRegistry {
|
|
|
38
38
|
'buy.tokens.ready': [boolean];
|
|
39
39
|
'buy.services.ready': [boolean];
|
|
40
40
|
'earning.ready': [boolean];
|
|
41
|
+
'swap.ready': [boolean];
|
|
41
42
|
}
|
|
42
43
|
export declare type EventType = keyof EventRegistry;
|
|
43
44
|
export declare const COMMON_RELOAD_EVENTS: EventType[];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { SwapError } from '@subwallet/extension-base/background/errors/SwapError';
|
|
2
|
+
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
|
|
3
|
+
import { BalanceService } from '@subwallet/extension-base/services/balance-service';
|
|
4
|
+
import { ChainService } from '@subwallet/extension-base/services/chain-service';
|
|
5
|
+
import { BaseStepDetail } from '@subwallet/extension-base/types/service-base';
|
|
6
|
+
import { GenSwapStepFunc, OptimalSwapPath, OptimalSwapPathParams, SwapEarlyValidation, SwapFeeInfo, SwapProvider, SwapQuote, SwapRequest, SwapSubmitParams, SwapSubmitStepData, ValidateSwapProcessParams } from '@subwallet/extension-base/types/swap';
|
|
7
|
+
export interface SwapBaseInterface {
|
|
8
|
+
getSwapQuote: (request: SwapRequest) => Promise<SwapQuote | SwapError>;
|
|
9
|
+
generateOptimalProcess: (params: OptimalSwapPathParams) => Promise<OptimalSwapPath>;
|
|
10
|
+
getSubmitStep: (params: OptimalSwapPathParams) => Promise<[BaseStepDetail, SwapFeeInfo] | undefined>;
|
|
11
|
+
validateSwapRequest: (request: SwapRequest) => Promise<SwapEarlyValidation>;
|
|
12
|
+
validateSwapProcess: (params: ValidateSwapProcessParams) => Promise<TransactionError[]>;
|
|
13
|
+
handleSwapProcess: (params: SwapSubmitParams) => Promise<SwapSubmitStepData>;
|
|
14
|
+
handleSubmitStep: (params: SwapSubmitParams) => Promise<SwapSubmitStepData>;
|
|
15
|
+
isReady?: boolean;
|
|
16
|
+
init?: () => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export interface SwapBaseHandlerInitParams {
|
|
19
|
+
providerSlug: string;
|
|
20
|
+
providerName: string;
|
|
21
|
+
chainService: ChainService;
|
|
22
|
+
balanceService: BalanceService;
|
|
23
|
+
}
|
|
24
|
+
export declare class SwapBaseHandler {
|
|
25
|
+
private readonly providerSlug;
|
|
26
|
+
private readonly providerName;
|
|
27
|
+
chainService: ChainService;
|
|
28
|
+
balanceService: BalanceService;
|
|
29
|
+
constructor({ balanceService, chainService, providerName, providerSlug }: SwapBaseHandlerInitParams);
|
|
30
|
+
generateOptimalProcess(params: OptimalSwapPathParams, genStepFuncList: GenSwapStepFunc[]): Promise<OptimalSwapPath>;
|
|
31
|
+
validateXcmStep(params: ValidateSwapProcessParams, stepIndex: number): Promise<TransactionError[]>;
|
|
32
|
+
validateTokenApproveStep(params: ValidateSwapProcessParams, stepIndex: number): Promise<TransactionError[]>;
|
|
33
|
+
validateSetFeeTokenStep(params: ValidateSwapProcessParams, stepIndex: number): Promise<TransactionError[]>;
|
|
34
|
+
validateSwapStep(params: ValidateSwapProcessParams, isXcmOk: boolean, stepIndex: number): Promise<TransactionError[]>;
|
|
35
|
+
get name(): string;
|
|
36
|
+
get slug(): string;
|
|
37
|
+
get providerInfo(): SwapProvider;
|
|
38
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension-base
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
|
|
5
|
+
import { BasicTxErrorType } from '@subwallet/extension-base/background/KoniTypes';
|
|
6
|
+
import { _getAssetDecimals, _getTokenMinAmount, _isChainEvmCompatible, _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
|
|
7
|
+
import { DEFAULT_SWAP_FIRST_STEP, getSwapAlternativeAsset, MOCK_SWAP_FEE } from '@subwallet/extension-base/services/swap-service/utils';
|
|
8
|
+
import { SwapErrorType, SwapFeeType } from '@subwallet/extension-base/types/swap';
|
|
9
|
+
import { formatNumber } from '@subwallet/extension-base/utils';
|
|
10
|
+
import BigNumber from 'bignumber.js';
|
|
11
|
+
import { t } from 'i18next';
|
|
12
|
+
import { isEthereumAddress } from '@polkadot/util-crypto';
|
|
13
|
+
export class SwapBaseHandler {
|
|
14
|
+
constructor({
|
|
15
|
+
balanceService,
|
|
16
|
+
chainService,
|
|
17
|
+
providerName,
|
|
18
|
+
providerSlug
|
|
19
|
+
}) {
|
|
20
|
+
this.providerName = providerName;
|
|
21
|
+
this.providerSlug = providerSlug;
|
|
22
|
+
this.chainService = chainService;
|
|
23
|
+
this.balanceService = balanceService;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// public abstract getSwapQuote(request: SwapRequest): Promise<SwapQuote | SwapError>;
|
|
27
|
+
async generateOptimalProcess(params, genStepFuncList) {
|
|
28
|
+
const result = {
|
|
29
|
+
totalFee: [MOCK_SWAP_FEE],
|
|
30
|
+
steps: [DEFAULT_SWAP_FIRST_STEP]
|
|
31
|
+
};
|
|
32
|
+
try {
|
|
33
|
+
for (const genStepFunc of genStepFuncList) {
|
|
34
|
+
const step = await genStepFunc.bind(this, params)();
|
|
35
|
+
if (step) {
|
|
36
|
+
result.steps.push({
|
|
37
|
+
id: result.steps.length,
|
|
38
|
+
...step[0]
|
|
39
|
+
});
|
|
40
|
+
result.totalFee.push(step[1]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
} catch (e) {
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async validateXcmStep(params, stepIndex) {
|
|
49
|
+
const bnAmount = new BigNumber(params.selectedQuote.fromAmount);
|
|
50
|
+
const swapPair = params.selectedQuote.pair;
|
|
51
|
+
const alternativeAssetSlug = getSwapAlternativeAsset(swapPair);
|
|
52
|
+
if (!alternativeAssetSlug) {
|
|
53
|
+
return [new TransactionError(BasicTxErrorType.INTERNAL_ERROR)];
|
|
54
|
+
}
|
|
55
|
+
const alternativeAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
56
|
+
const fromAsset = this.chainService.getAssetBySlug(swapPair.from);
|
|
57
|
+
const [alternativeAssetBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTokenFreeBalance(params.address, alternativeAsset.originChain, alternativeAssetSlug), this.balanceService.getTokenFreeBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
|
|
58
|
+
const bnAlternativeAssetBalance = new BigNumber(alternativeAssetBalance.value);
|
|
59
|
+
const bnFromAssetBalance = new BigNumber(fromAssetBalance.value);
|
|
60
|
+
const xcmFeeComponent = params.process.totalFee[stepIndex].feeComponent[0]; // todo: can do better than indexing
|
|
61
|
+
const xcmFee = new BigNumber(xcmFeeComponent.amount || '0');
|
|
62
|
+
let xcmAmount = bnAmount.minus(bnFromAssetBalance);
|
|
63
|
+
if (_isNativeToken(alternativeAsset)) {
|
|
64
|
+
xcmAmount = xcmAmount.plus(xcmFee);
|
|
65
|
+
}
|
|
66
|
+
const alternativeTokenMinAmount = new BigNumber(alternativeAsset.minAmount || '0');
|
|
67
|
+
if (!bnAlternativeAssetBalance.minus(xcmAmount).gte(alternativeTokenMinAmount)) {
|
|
68
|
+
const maxBn = bnFromAssetBalance.plus(new BigNumber(alternativeAssetBalance.value)).minus(xcmFee).minus(alternativeTokenMinAmount);
|
|
69
|
+
const maxValue = formatNumber(maxBn.toString(), fromAsset.decimals || 0);
|
|
70
|
+
const altInputTokenInfo = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
71
|
+
const symbol = altInputTokenInfo.symbol;
|
|
72
|
+
const alternativeChain = this.chainService.getChainInfoByKey(altInputTokenInfo.originChain);
|
|
73
|
+
const chain = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
74
|
+
const inputNetworkName = chain.name;
|
|
75
|
+
const altNetworkName = alternativeChain.name;
|
|
76
|
+
const currentValue = formatNumber(bnFromAssetBalance.toString(), fromAsset.decimals || 0);
|
|
77
|
+
const bnMaxXCM = new BigNumber(alternativeAssetBalance.value).minus(xcmFee).minus(alternativeTokenMinAmount);
|
|
78
|
+
const maxXCMValue = formatNumber(bnMaxXCM.toString(), fromAsset.decimals || 0);
|
|
79
|
+
if (maxBn.lte(0) || bnFromAssetBalance.lte(0) || bnMaxXCM.lte(0)) {
|
|
80
|
+
return [new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE, t(`Insufficient balance. Deposit ${fromAsset.symbol} and try again.`))];
|
|
81
|
+
}
|
|
82
|
+
return [new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE, 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.', {
|
|
83
|
+
replace: {
|
|
84
|
+
symbol,
|
|
85
|
+
maxValue,
|
|
86
|
+
inputNetworkName,
|
|
87
|
+
altNetworkName,
|
|
88
|
+
currentValue,
|
|
89
|
+
maxXCMValue
|
|
90
|
+
}
|
|
91
|
+
}))];
|
|
92
|
+
}
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
async validateTokenApproveStep(params, stepIndex) {
|
|
96
|
+
return Promise.resolve([]);
|
|
97
|
+
}
|
|
98
|
+
async validateSetFeeTokenStep(params, stepIndex) {
|
|
99
|
+
return Promise.resolve([]);
|
|
100
|
+
}
|
|
101
|
+
async validateSwapStep(params, isXcmOk, stepIndex) {
|
|
102
|
+
if (!params.selectedQuote) {
|
|
103
|
+
return Promise.resolve([new TransactionError(BasicTxErrorType.INTERNAL_ERROR)]);
|
|
104
|
+
}
|
|
105
|
+
const selectedQuote = params.selectedQuote;
|
|
106
|
+
const currentTimestamp = +Date.now();
|
|
107
|
+
if (selectedQuote.aliveUntil <= currentTimestamp) {
|
|
108
|
+
return Promise.resolve([new TransactionError(SwapErrorType.QUOTE_TIMEOUT)]);
|
|
109
|
+
}
|
|
110
|
+
const bnAmount = new BigNumber(params.selectedQuote.fromAmount);
|
|
111
|
+
const fromAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.from);
|
|
112
|
+
const stepFee = params.process.totalFee[stepIndex].feeComponent;
|
|
113
|
+
const networkFee = stepFee.find(fee => fee.feeType === SwapFeeType.NETWORK_FEE);
|
|
114
|
+
if (!networkFee) {
|
|
115
|
+
return Promise.resolve([new TransactionError(BasicTxErrorType.INTERNAL_ERROR)]);
|
|
116
|
+
}
|
|
117
|
+
const feeTokenInfo = this.chainService.getAssetBySlug(networkFee.tokenSlug);
|
|
118
|
+
const feeTokenChain = this.chainService.getChainInfoByKey(feeTokenInfo.originChain);
|
|
119
|
+
const [feeTokenBalance, fromAssetBalance] = await Promise.all([this.balanceService.getTokenFreeBalance(params.address, feeTokenInfo.originChain, feeTokenInfo.slug), this.balanceService.getTokenFreeBalance(params.address, fromAsset.originChain, fromAsset.slug)]);
|
|
120
|
+
const bnFeeTokenBalance = new BigNumber(feeTokenBalance.value);
|
|
121
|
+
const bnFromAssetBalance = new BigNumber(fromAssetBalance.value);
|
|
122
|
+
if (bnFeeTokenBalance.lte(new BigNumber(networkFee.amount))) {
|
|
123
|
+
return Promise.resolve([new TransactionError(BasicTxErrorType.NOT_ENOUGH_BALANCE, `You don't have enough ${feeTokenInfo.symbol} (${feeTokenChain.name}) to pay transaction fee`)]);
|
|
124
|
+
}
|
|
125
|
+
if (params.selectedQuote.minSwap) {
|
|
126
|
+
const minProtocolSwap = new BigNumber(params.selectedQuote.minSwap);
|
|
127
|
+
if (!isXcmOk && bnFromAssetBalance.lte(minProtocolSwap)) {
|
|
128
|
+
const parsedMinSwapValue = formatNumber(minProtocolSwap, _getAssetDecimals(fromAsset));
|
|
129
|
+
return Promise.resolve([new TransactionError(SwapErrorType.SWAP_NOT_ENOUGH_BALANCE, `Insufficient balance. You need more than ${parsedMinSwapValue} ${fromAsset.symbol} to start swapping. Deposit ${fromAsset.symbol} and try again.`)]); // todo: min swap or amount?
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const bnSrcAssetMinAmount = new BigNumber(_getTokenMinAmount(fromAsset));
|
|
134
|
+
const bnMaxBalanceSwap = bnFromAssetBalance.minus(bnSrcAssetMinAmount);
|
|
135
|
+
if (!isXcmOk && bnAmount.gte(bnMaxBalanceSwap)) {
|
|
136
|
+
const parsedMaxBalanceSwap = formatNumber(bnMaxBalanceSwap, _getAssetDecimals(fromAsset));
|
|
137
|
+
return Promise.resolve([new TransactionError(SwapErrorType.SWAP_EXCEED_ALLOWANCE, `Amount too high. Lower your amount ${bnMaxBalanceSwap.gt(0) ? `below ${parsedMaxBalanceSwap} ${fromAsset.symbol}` : ''} and try again`)]);
|
|
138
|
+
}
|
|
139
|
+
if (params.recipient) {
|
|
140
|
+
const toAsset = this.chainService.getAssetBySlug(params.selectedQuote.pair.to);
|
|
141
|
+
const destChainInfo = this.chainService.getChainInfoByKey(toAsset.originChain);
|
|
142
|
+
const isEvmAddress = isEthereumAddress(params.recipient);
|
|
143
|
+
const isEvmDestChain = _isChainEvmCompatible(destChainInfo);
|
|
144
|
+
if (isEvmAddress && !isEvmDestChain || !isEvmAddress && isEvmDestChain) {
|
|
145
|
+
return Promise.resolve([new TransactionError(SwapErrorType.INVALID_RECIPIENT)]);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return Promise.resolve([]);
|
|
149
|
+
}
|
|
150
|
+
get name() {
|
|
151
|
+
return this.providerName;
|
|
152
|
+
}
|
|
153
|
+
get slug() {
|
|
154
|
+
return this.providerSlug;
|
|
155
|
+
}
|
|
156
|
+
get providerInfo() {
|
|
157
|
+
return {
|
|
158
|
+
id: this.providerSlug,
|
|
159
|
+
name: this.providerName
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { COMMON_ASSETS } from '@subwallet/chain-list';
|
|
2
|
+
import { SwapError } from '@subwallet/extension-base/background/errors/SwapError';
|
|
3
|
+
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
|
|
4
|
+
import { BalanceService } from '@subwallet/extension-base/services/balance-service';
|
|
5
|
+
import { ChainService } from '@subwallet/extension-base/services/chain-service';
|
|
6
|
+
import { SwapBaseInterface } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
|
|
7
|
+
import { BaseStepDetail } from '@subwallet/extension-base/types/service-base';
|
|
8
|
+
import { OptimalSwapPath, OptimalSwapPathParams, SwapEarlyValidation, SwapFeeInfo, SwapQuote, SwapRequest, SwapSubmitParams, SwapSubmitStepData, ValidateSwapProcessParams } from '@subwallet/extension-base/types/swap';
|
|
9
|
+
export declare class ChainflipSwapHandler implements SwapBaseInterface {
|
|
10
|
+
private swapSdk;
|
|
11
|
+
private readonly isTestnet;
|
|
12
|
+
private swapBaseHandler;
|
|
13
|
+
constructor(chainService: ChainService, balanceService: BalanceService, isTestnet?: boolean);
|
|
14
|
+
get chainService(): ChainService;
|
|
15
|
+
get balanceService(): BalanceService;
|
|
16
|
+
get providerInfo(): import("@subwallet/extension-base/types/swap").SwapProvider;
|
|
17
|
+
get name(): string;
|
|
18
|
+
get slug(): string;
|
|
19
|
+
get assetMapping(): Record<string, import("@chainflip/sdk/dist/contracts-b0BB7B5m").A>;
|
|
20
|
+
get chainMapping(): Record<string, import("@chainflip/sdk/dist/contracts-b0BB7B5m").C>;
|
|
21
|
+
get intermediaryAssetSlug(): COMMON_ASSETS.USDC_ETHEREUM | COMMON_ASSETS.USDC_SEPOLIA;
|
|
22
|
+
validateSwapRequest(request: SwapRequest): Promise<SwapEarlyValidation>;
|
|
23
|
+
private parseSwapPath;
|
|
24
|
+
getSwapQuote(request: SwapRequest): Promise<SwapQuote | SwapError>;
|
|
25
|
+
validateSwapProcess(params: ValidateSwapProcessParams): Promise<TransactionError[]>;
|
|
26
|
+
handleSubmitStep(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
|
|
27
|
+
handleSwapProcess(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
|
|
28
|
+
getSubmitStep(params: OptimalSwapPathParams): Promise<[BaseStepDetail, SwapFeeInfo] | undefined>;
|
|
29
|
+
generateOptimalProcess(params: OptimalSwapPathParams): Promise<OptimalSwapPath>;
|
|
30
|
+
}
|