@subwallet/extension-base 1.1.53-beta.0 → 1.1.54-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 +1 -9
- package/background/KoniTypes.js +0 -1
- package/background/errors/TransactionError.js +0 -9
- package/cjs/background/KoniTypes.js +0 -1
- package/cjs/background/errors/TransactionError.js +0 -9
- package/cjs/koni/api/staking/bonding/utils.js +6 -35
- package/cjs/koni/background/handlers/Extension.js +0 -85
- package/cjs/koni/background/handlers/State.js +5 -25
- package/cjs/packageInfo.js +1 -1
- package/cjs/services/balance-service/index.js +3 -6
- package/cjs/services/chain-service/constants.js +1 -1
- package/cjs/services/chain-service/index.js +18 -43
- package/cjs/services/chain-service/utils/index.js +2 -7
- package/cjs/services/chain-service/utils/patch.js +1 -1
- package/cjs/services/earning-service/constants/chains.js +2 -4
- package/cjs/services/earning-service/handlers/native-staking/astar.js +3 -4
- package/cjs/services/earning-service/handlers/native-staking/relay-chain.js +3 -22
- package/cjs/services/transaction-service/index.js +0 -20
- package/cjs/services/transaction-service/utils.js +0 -6
- package/cjs/types/fee/index.js +1 -27
- package/cjs/utils/index.js +0 -12
- package/koni/api/staking/bonding/utils.d.ts +1 -3
- package/koni/api/staking/bonding/utils.js +6 -32
- package/koni/background/handlers/Extension.d.ts +0 -5
- package/koni/background/handlers/Extension.js +0 -85
- package/koni/background/handlers/State.d.ts +0 -2
- package/koni/background/handlers/State.js +6 -26
- package/package.json +7 -64
- package/packageInfo.js +1 -1
- package/services/balance-service/index.js +3 -6
- package/services/base/types.d.ts +0 -4
- package/services/chain-service/constants.js +1 -1
- package/services/chain-service/index.d.ts +0 -4
- package/services/chain-service/index.js +0 -24
- package/services/chain-service/utils/index.d.ts +5 -6
- package/services/chain-service/utils/index.js +2 -5
- package/services/chain-service/utils/patch.js +1 -1
- package/services/earning-service/constants/chains.d.ts +0 -1
- package/services/earning-service/constants/chains.js +0 -1
- package/services/earning-service/handlers/native-staking/astar.js +3 -4
- package/services/earning-service/handlers/native-staking/relay-chain.js +5 -24
- package/services/event-service/types.d.ts +0 -1
- package/services/transaction-service/index.js +0 -20
- package/services/transaction-service/utils.d.ts +0 -2
- package/services/transaction-service/utils.js +2 -6
- package/types/fee/index.d.ts +49 -2
- package/types/fee/index.js +1 -5
- package/types/yield/info/chain/target.d.ts +0 -2
- package/types/yield/info/pallet.d.ts +0 -4
- package/utils/index.d.ts +0 -1
- package/utils/index.js +1 -2
- package/background/errors/SwapError.d.ts +0 -6
- package/background/errors/SwapError.js +0 -57
- package/cjs/background/errors/SwapError.js +0 -64
- package/cjs/koni/api/dotsama/balance.js +0 -464
- package/cjs/koni/api/nft/ordinal_nft/utils.js +0 -41
- package/cjs/services/balance-service/helpers/subscribe/balance.js +0 -140
- package/cjs/services/chain-service/utils.js +0 -506
- package/cjs/services/migration-service/scripts/MigrateEthProvider.js +0 -17
- package/cjs/services/migration-service/scripts/MigratePioneerProvider.js +0 -17
- package/cjs/services/migration-service/scripts/MigrateProvider.js +0 -29
- package/cjs/services/storage-service/index.js +0 -241
- package/cjs/services/swap-service/handler/base-handler.js +0 -171
- package/cjs/services/swap-service/handler/chainflip-handler.js +0 -407
- package/cjs/services/swap-service/handler/hydradx-handler.js +0 -444
- package/cjs/services/swap-service/index.js +0 -249
- package/cjs/services/swap-service/utils.js +0 -126
- package/cjs/types/balance.js +0 -1
- package/cjs/types/fee/evm.js +0 -1
- package/cjs/types/fee/fee.js +0 -70
- package/cjs/types/service-base.js +0 -1
- package/cjs/types/swap/index.js +0 -50
- package/cjs/types.js +0 -1
- package/cjs/utils/address.js +0 -34
- package/cjs/utils/keyring.js +0 -57
- package/cjs/utils/swap.js +0 -78
- package/services/swap-service/handler/base-handler.d.ts +0 -38
- package/services/swap-service/handler/base-handler.js +0 -162
- package/services/swap-service/handler/chainflip-handler.d.ts +0 -30
- package/services/swap-service/handler/chainflip-handler.js +0 -399
- package/services/swap-service/handler/hydradx-handler.d.ts +0 -36
- package/services/swap-service/handler/hydradx-handler.js +0 -435
- package/services/swap-service/index.d.ts +0 -32
- package/services/swap-service/index.js +0 -240
- package/services/swap-service/utils.d.ts +0 -18
- package/services/swap-service/utils.js +0 -105
- package/types/fee/evm.d.ts +0 -49
- package/types/fee/evm.js +0 -1
- package/types/fee/fee.d.ts +0 -32
- package/types/fee/fee.js +0 -63
- package/types/service-base.d.ts +0 -10
- package/types/service-base.js +0 -1
- package/types/swap/index.d.ts +0 -166
- package/types/swap/index.js +0 -41
- package/utils/swap.d.ts +0 -3
- package/utils/swap.js +0 -70
|
@@ -1,435 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension-base
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import { PoolError, PoolService, TradeRouter } from '@galacticcouncil/sdk';
|
|
5
|
-
import { COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
|
|
6
|
-
import { _AssetType } from '@subwallet/chain-list/types';
|
|
7
|
-
import { SwapError } from '@subwallet/extension-base/background/errors/SwapError';
|
|
8
|
-
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
|
|
9
|
-
import { BasicTxErrorType, ChainType, ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
|
|
10
|
-
import { createXcmExtrinsic } from '@subwallet/extension-base/koni/api/xcm';
|
|
11
|
-
import { _getAssetDecimals, _getChainNativeTokenSlug, _getTokenOnChainAssetId, _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
|
|
12
|
-
import { SwapBaseHandler } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
|
|
13
|
-
import { calculateSwapRate, getEarlyHydradxValidationError, getSwapAlternativeAsset, SWAP_QUOTE_TIMEOUT_MAP } from '@subwallet/extension-base/services/swap-service/utils';
|
|
14
|
-
import { SwapErrorType, SwapFeeType, SwapProviderId, SwapStepType } from '@subwallet/extension-base/types/swap';
|
|
15
|
-
import BigNumber from 'bignumber.js';
|
|
16
|
-
const HYDRADX_LOW_LIQUIDITY_THRESHOLD = 0.15;
|
|
17
|
-
const HYDRADX_SUBWALLET_REFERRAL_CODE = 'WALLET';
|
|
18
|
-
const HYDRADX_SUBWALLET_REFERRAL_ACCOUNT = '7PCsCpkgsHdNaZhv79wCCQ5z97uxVbSeSCtDMUa1eZHKXy4a';
|
|
19
|
-
const HYDRADX_TESTNET_SUBWALLET_REFERRAL_CODE = 'ASSETHUB';
|
|
20
|
-
const HYDRADX_TESTNET_SUBWALLET_REFERRAL_ACCOUNT = '7LCt6dFqtxzdKVB2648jWW9d85doiFfLSbZJDNAMVJNxh5rJ';
|
|
21
|
-
export class HydradxHandler {
|
|
22
|
-
isReady = false;
|
|
23
|
-
constructor(chainService, balanceService, isTestnet = true) {
|
|
24
|
-
// todo: pass in baseHandler from service
|
|
25
|
-
this.swapBaseHandler = new SwapBaseHandler({
|
|
26
|
-
balanceService,
|
|
27
|
-
chainService,
|
|
28
|
-
providerName: isTestnet ? 'HydraDX Testnet' : 'HydraDX',
|
|
29
|
-
providerSlug: isTestnet ? SwapProviderId.HYDRADX_TESTNET : SwapProviderId.HYDRADX_MAINNET
|
|
30
|
-
});
|
|
31
|
-
this.isTestnet = isTestnet;
|
|
32
|
-
}
|
|
33
|
-
async init() {
|
|
34
|
-
const chainState = this.chainService.getChainStateByKey(this.chain);
|
|
35
|
-
if (!chainState.active) {
|
|
36
|
-
await this.chainService.enableChain(this.chain);
|
|
37
|
-
}
|
|
38
|
-
const substrateApi = this.chainService.getSubstrateApi(this.chain);
|
|
39
|
-
await substrateApi.api.isReady;
|
|
40
|
-
const poolService = new PoolService(substrateApi.api);
|
|
41
|
-
this.tradeRouter = new TradeRouter(poolService);
|
|
42
|
-
this.isReady = true;
|
|
43
|
-
}
|
|
44
|
-
get chain() {
|
|
45
|
-
// TODO: check origin chain of tokens in swap pair to determine support
|
|
46
|
-
if (!this.isTestnet) {
|
|
47
|
-
return COMMON_CHAIN_SLUGS.HYDRADX;
|
|
48
|
-
} else {
|
|
49
|
-
return COMMON_CHAIN_SLUGS.HYDRADX_TESTNET;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
get chainService() {
|
|
53
|
-
return this.swapBaseHandler.chainService;
|
|
54
|
-
}
|
|
55
|
-
get balanceService() {
|
|
56
|
-
return this.swapBaseHandler.balanceService;
|
|
57
|
-
}
|
|
58
|
-
get providerInfo() {
|
|
59
|
-
return this.swapBaseHandler.providerInfo;
|
|
60
|
-
}
|
|
61
|
-
get name() {
|
|
62
|
-
return this.swapBaseHandler.name;
|
|
63
|
-
}
|
|
64
|
-
get slug() {
|
|
65
|
-
return this.swapBaseHandler.slug;
|
|
66
|
-
}
|
|
67
|
-
async getXcmStep(params) {
|
|
68
|
-
const bnAmount = new BigNumber(params.request.fromAmount);
|
|
69
|
-
const fromAsset = this.chainService.getAssetBySlug(params.request.pair.from);
|
|
70
|
-
const fromAssetBalance = await this.balanceService.getTokenFreeBalance(params.request.address, fromAsset.originChain, fromAsset.slug);
|
|
71
|
-
const bnFromAssetBalance = new BigNumber(fromAssetBalance.value);
|
|
72
|
-
if (bnFromAssetBalance.gte(bnAmount)) {
|
|
73
|
-
return undefined; // enough balance, no need to xcm
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const alternativeAssetSlug = getSwapAlternativeAsset(params.request.pair);
|
|
77
|
-
if (!alternativeAssetSlug) {
|
|
78
|
-
return undefined;
|
|
79
|
-
}
|
|
80
|
-
const alternativeAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
81
|
-
const alternativeAssetBalance = await this.balanceService.getTokenFreeBalance(params.request.address, alternativeAsset.originChain, alternativeAsset.slug);
|
|
82
|
-
const bnAlternativeAssetBalance = new BigNumber(alternativeAssetBalance.value);
|
|
83
|
-
if (bnAlternativeAssetBalance.lte(0)) {
|
|
84
|
-
return undefined;
|
|
85
|
-
}
|
|
86
|
-
try {
|
|
87
|
-
const alternativeChainInfo = this.chainService.getChainInfoByKey(alternativeAsset.originChain);
|
|
88
|
-
const step = {
|
|
89
|
-
metadata: {
|
|
90
|
-
sendingValue: bnAmount.toString(),
|
|
91
|
-
originTokenInfo: alternativeAsset,
|
|
92
|
-
destinationTokenInfo: fromAsset
|
|
93
|
-
},
|
|
94
|
-
name: `Transfer ${alternativeAsset.symbol} from ${alternativeChainInfo.name}`,
|
|
95
|
-
type: SwapStepType.XCM
|
|
96
|
-
};
|
|
97
|
-
const xcmOriginSubstrateApi = await this.chainService.getSubstrateApi(alternativeAsset.originChain).isReady;
|
|
98
|
-
const xcmTransfer = await createXcmExtrinsic({
|
|
99
|
-
originTokenInfo: alternativeAsset,
|
|
100
|
-
destinationTokenInfo: fromAsset,
|
|
101
|
-
sendingValue: bnAmount.toString(),
|
|
102
|
-
recipient: params.request.address,
|
|
103
|
-
chainInfoMap: this.chainService.getChainInfoMap(),
|
|
104
|
-
substrateApi: xcmOriginSubstrateApi
|
|
105
|
-
});
|
|
106
|
-
const _xcmFeeInfo = await xcmTransfer.paymentInfo(params.request.address);
|
|
107
|
-
const xcmFeeInfo = _xcmFeeInfo.toPrimitive();
|
|
108
|
-
const fee = {
|
|
109
|
-
feeComponent: [{
|
|
110
|
-
feeType: SwapFeeType.NETWORK_FEE,
|
|
111
|
-
amount: Math.round(xcmFeeInfo.partialFee * 1.2).toString(),
|
|
112
|
-
tokenSlug: _getChainNativeTokenSlug(alternativeChainInfo)
|
|
113
|
-
}],
|
|
114
|
-
defaultFeeToken: _getChainNativeTokenSlug(alternativeChainInfo),
|
|
115
|
-
feeOptions: [_getChainNativeTokenSlug(alternativeChainInfo)]
|
|
116
|
-
};
|
|
117
|
-
return [step, fee];
|
|
118
|
-
} catch (e) {
|
|
119
|
-
return undefined;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
async getFeeOptionStep(params) {
|
|
123
|
-
return Promise.resolve(undefined);
|
|
124
|
-
}
|
|
125
|
-
async getSubmitStep(params) {
|
|
126
|
-
if (params.selectedQuote) {
|
|
127
|
-
const submitStep = {
|
|
128
|
-
name: 'Swap',
|
|
129
|
-
type: SwapStepType.SWAP
|
|
130
|
-
};
|
|
131
|
-
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
|
|
132
|
-
}
|
|
133
|
-
return Promise.resolve(undefined);
|
|
134
|
-
}
|
|
135
|
-
generateOptimalProcess(params) {
|
|
136
|
-
return this.swapBaseHandler.generateOptimalProcess(params, [this.getXcmStep, this.getFeeOptionStep, this.getSubmitStep]);
|
|
137
|
-
}
|
|
138
|
-
getSwapPathErrors(swapList) {
|
|
139
|
-
return swapList.reduce((prev, current) => {
|
|
140
|
-
return [...prev, ...current.errors];
|
|
141
|
-
}, []);
|
|
142
|
-
}
|
|
143
|
-
parseSwapPath(swapList) {
|
|
144
|
-
const swapAssets = this.chainService.getAssetByChainAndType(this.chain, [_AssetType.NATIVE, _AssetType.LOCAL]);
|
|
145
|
-
const swapAssetIdMap = Object.values(swapAssets).reduce((accumulator, asset) => {
|
|
146
|
-
return {
|
|
147
|
-
...accumulator,
|
|
148
|
-
[_getTokenOnChainAssetId(asset)]: asset
|
|
149
|
-
};
|
|
150
|
-
}, {});
|
|
151
|
-
const path = [];
|
|
152
|
-
swapList.forEach(swap => {
|
|
153
|
-
var _swapAssetIdMap$swap$, _swapAssetIdMap$swap$2;
|
|
154
|
-
const swapAssetIn = (_swapAssetIdMap$swap$ = swapAssetIdMap[swap.assetIn]) === null || _swapAssetIdMap$swap$ === void 0 ? void 0 : _swapAssetIdMap$swap$.slug;
|
|
155
|
-
const swapAssetOut = (_swapAssetIdMap$swap$2 = swapAssetIdMap[swap.assetOut]) === null || _swapAssetIdMap$swap$2 === void 0 ? void 0 : _swapAssetIdMap$swap$2.slug;
|
|
156
|
-
if (swapAssetIn && !path.includes(swapAssetIn)) {
|
|
157
|
-
path.push(swapAssetIn);
|
|
158
|
-
}
|
|
159
|
-
if (swapAssetOut && !path.includes(swapAssetOut)) {
|
|
160
|
-
path.push(swapAssetOut);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
return {
|
|
164
|
-
path
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
async getSwapQuote(request) {
|
|
168
|
-
const fromAsset = this.chainService.getAssetBySlug(request.pair.from);
|
|
169
|
-
const toAsset = this.chainService.getAssetBySlug(request.pair.to);
|
|
170
|
-
const fromChain = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
171
|
-
const fromChainNativeTokenSlug = _getChainNativeTokenSlug(fromChain);
|
|
172
|
-
if (!this.isReady || !this.tradeRouter) {
|
|
173
|
-
return new SwapError(SwapErrorType.UNKNOWN);
|
|
174
|
-
}
|
|
175
|
-
const earlyValidation = await this.validateSwapRequest(request);
|
|
176
|
-
if (earlyValidation.error) {
|
|
177
|
-
const metadata = earlyValidation.metadata;
|
|
178
|
-
return getEarlyHydradxValidationError(earlyValidation.error, metadata);
|
|
179
|
-
}
|
|
180
|
-
try {
|
|
181
|
-
const fromAssetId = _getTokenOnChainAssetId(fromAsset);
|
|
182
|
-
const toAssetId = _getTokenOnChainAssetId(toAsset);
|
|
183
|
-
const parsedFromAmount = new BigNumber(request.fromAmount).shiftedBy(-1 * _getAssetDecimals(fromAsset)).toString();
|
|
184
|
-
const quoteResponse = await this.tradeRouter.getBestSell(fromAssetId, toAssetId, parsedFromAmount);
|
|
185
|
-
const toAmount = quoteResponse.amountOut;
|
|
186
|
-
const minReceive = toAmount.times(1 - request.slippage).integerValue();
|
|
187
|
-
const txHex = quoteResponse.toTx(minReceive).hex;
|
|
188
|
-
const substrateApi = this.chainService.getSubstrateApi(this.chain);
|
|
189
|
-
const extrinsic = substrateApi.api.tx(txHex);
|
|
190
|
-
const paymentInfo = await extrinsic.paymentInfo(request.address);
|
|
191
|
-
const networkFee = {
|
|
192
|
-
tokenSlug: fromChainNativeTokenSlug,
|
|
193
|
-
amount: paymentInfo.partialFee.toString(),
|
|
194
|
-
feeType: SwapFeeType.NETWORK_FEE
|
|
195
|
-
};
|
|
196
|
-
const tradeFee = {
|
|
197
|
-
tokenSlug: toAsset.slug,
|
|
198
|
-
// fee is subtracted from receiving amount
|
|
199
|
-
amount: quoteResponse.tradeFee.toString(),
|
|
200
|
-
feeType: SwapFeeType.PLATFORM_FEE
|
|
201
|
-
};
|
|
202
|
-
const swapRoute = this.parseSwapPath(quoteResponse.swaps);
|
|
203
|
-
const swapPathErrors = this.getSwapPathErrors(quoteResponse.swaps);
|
|
204
|
-
if (swapPathErrors.length > 0) {
|
|
205
|
-
const defaultError = swapPathErrors[0]; // might parse more errors
|
|
206
|
-
|
|
207
|
-
switch (defaultError) {
|
|
208
|
-
case PoolError.InsufficientTradingAmount:
|
|
209
|
-
return new SwapError(SwapErrorType.SWAP_NOT_ENOUGH_BALANCE);
|
|
210
|
-
case PoolError.TradeNotAllowed:
|
|
211
|
-
return new SwapError(SwapErrorType.ERROR_FETCHING_QUOTE);
|
|
212
|
-
case PoolError.MaxInRatioExceeded:
|
|
213
|
-
return new SwapError(SwapErrorType.NOT_ENOUGH_LIQUIDITY);
|
|
214
|
-
case PoolError.UnknownError:
|
|
215
|
-
return new SwapError(SwapErrorType.ERROR_FETCHING_QUOTE);
|
|
216
|
-
case PoolError.MaxOutRatioExceeded:
|
|
217
|
-
return new SwapError(SwapErrorType.NOT_ENOUGH_LIQUIDITY);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return {
|
|
221
|
-
pair: request.pair,
|
|
222
|
-
fromAmount: request.fromAmount,
|
|
223
|
-
toAmount: toAmount.toString(),
|
|
224
|
-
rate: calculateSwapRate(request.fromAmount, toAmount.toString(), fromAsset, toAsset),
|
|
225
|
-
provider: this.providerInfo,
|
|
226
|
-
aliveUntil: +Date.now() + (SWAP_QUOTE_TIMEOUT_MAP[this.slug] || SWAP_QUOTE_TIMEOUT_MAP.default),
|
|
227
|
-
feeInfo: {
|
|
228
|
-
feeComponent: [networkFee, tradeFee],
|
|
229
|
-
defaultFeeToken: fromChainNativeTokenSlug,
|
|
230
|
-
feeOptions: [fromChainNativeTokenSlug] // todo: parse fee options
|
|
231
|
-
},
|
|
232
|
-
|
|
233
|
-
isLowLiquidity: Math.abs(quoteResponse.priceImpactPct) >= HYDRADX_LOW_LIQUIDITY_THRESHOLD,
|
|
234
|
-
route: swapRoute,
|
|
235
|
-
metadata: txHex
|
|
236
|
-
};
|
|
237
|
-
} catch (e) {
|
|
238
|
-
return new SwapError(SwapErrorType.ERROR_FETCHING_QUOTE);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
async handleXcmStep(params) {
|
|
242
|
-
const pair = params.quote.pair;
|
|
243
|
-
const alternativeAssetSlug = getSwapAlternativeAsset(pair);
|
|
244
|
-
const originAsset = this.chainService.getAssetBySlug(alternativeAssetSlug);
|
|
245
|
-
const destinationAsset = this.chainService.getAssetBySlug(pair.from);
|
|
246
|
-
const substrateApi = this.chainService.getSubstrateApi(originAsset.originChain);
|
|
247
|
-
const chainApi = await substrateApi.isReady;
|
|
248
|
-
const destinationAssetBalance = await this.balanceService.getTokenFreeBalance(params.address, destinationAsset.originChain, destinationAsset.slug);
|
|
249
|
-
const xcmFee = params.process.totalFee[params.currentStep];
|
|
250
|
-
const bnAmount = new BigNumber(params.quote.fromAmount);
|
|
251
|
-
const bnDestinationAssetBalance = new BigNumber(destinationAssetBalance.value);
|
|
252
|
-
let bnTotalAmount = bnAmount.minus(bnDestinationAssetBalance);
|
|
253
|
-
if (_isNativeToken(originAsset)) {
|
|
254
|
-
const bnXcmFee = new BigNumber(xcmFee.feeComponent[0].amount); // xcm fee is paid in native token but swap token is not always native token
|
|
255
|
-
|
|
256
|
-
bnTotalAmount = bnTotalAmount.plus(bnXcmFee);
|
|
257
|
-
}
|
|
258
|
-
const xcmTransfer = await createXcmExtrinsic({
|
|
259
|
-
originTokenInfo: originAsset,
|
|
260
|
-
destinationTokenInfo: destinationAsset,
|
|
261
|
-
sendingValue: bnTotalAmount.toString(),
|
|
262
|
-
recipient: params.address,
|
|
263
|
-
chainInfoMap: this.chainService.getChainInfoMap(),
|
|
264
|
-
substrateApi: chainApi
|
|
265
|
-
});
|
|
266
|
-
const xcmData = {
|
|
267
|
-
originNetworkKey: originAsset.originChain,
|
|
268
|
-
destinationNetworkKey: destinationAsset.originChain,
|
|
269
|
-
from: params.address,
|
|
270
|
-
to: params.address,
|
|
271
|
-
value: bnTotalAmount.toString(),
|
|
272
|
-
tokenSlug: originAsset.slug,
|
|
273
|
-
showExtraWarning: true
|
|
274
|
-
};
|
|
275
|
-
return {
|
|
276
|
-
txChain: originAsset.originChain,
|
|
277
|
-
extrinsic: xcmTransfer,
|
|
278
|
-
transferNativeAmount: _isNativeToken(originAsset) ? bnTotalAmount.toString() : '0',
|
|
279
|
-
extrinsicType: ExtrinsicType.TRANSFER_XCM,
|
|
280
|
-
chainType: ChainType.SUBSTRATE,
|
|
281
|
-
txData: xcmData
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
async handleSubmitStep(params) {
|
|
285
|
-
const txHex = params.quote.metadata;
|
|
286
|
-
const fromAsset = this.chainService.getAssetBySlug(params.quote.pair.from);
|
|
287
|
-
const substrateApi = this.chainService.getSubstrateApi(this.chain);
|
|
288
|
-
const chainApi = await substrateApi.isReady;
|
|
289
|
-
const txData = {
|
|
290
|
-
provider: this.providerInfo,
|
|
291
|
-
quote: params.quote,
|
|
292
|
-
address: params.address,
|
|
293
|
-
slippage: params.slippage,
|
|
294
|
-
txHex,
|
|
295
|
-
process: params.process
|
|
296
|
-
};
|
|
297
|
-
let extrinsic;
|
|
298
|
-
const _referral = await chainApi.api.query.referrals.linkedAccounts(params.address);
|
|
299
|
-
const referral = _referral === null || _referral === void 0 ? void 0 : _referral.toString();
|
|
300
|
-
if (!referral || referral === '') {
|
|
301
|
-
extrinsic = chainApi.api.tx.utility.batchAll([chainApi.api.tx.referrals.linkCode(this.referralCode), chainApi.api.tx(txHex)]);
|
|
302
|
-
} else {
|
|
303
|
-
extrinsic = chainApi.api.tx(txHex);
|
|
304
|
-
}
|
|
305
|
-
return {
|
|
306
|
-
txChain: fromAsset.originChain,
|
|
307
|
-
txData,
|
|
308
|
-
extrinsic,
|
|
309
|
-
transferNativeAmount: _isNativeToken(fromAsset) ? params.quote.fromAmount : '0',
|
|
310
|
-
// todo
|
|
311
|
-
extrinsicType: ExtrinsicType.SWAP,
|
|
312
|
-
chainType: ChainType.SUBSTRATE
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
handleSwapProcess(params) {
|
|
316
|
-
const {
|
|
317
|
-
currentStep,
|
|
318
|
-
process
|
|
319
|
-
} = params;
|
|
320
|
-
const type = process.steps[currentStep].type;
|
|
321
|
-
switch (type) {
|
|
322
|
-
case SwapStepType.DEFAULT:
|
|
323
|
-
return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
|
|
324
|
-
case SwapStepType.XCM:
|
|
325
|
-
return this.handleXcmStep(params);
|
|
326
|
-
case SwapStepType.SET_FEE_TOKEN:
|
|
327
|
-
return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
|
|
328
|
-
case SwapStepType.SWAP:
|
|
329
|
-
return this.handleSubmitStep(params);
|
|
330
|
-
default:
|
|
331
|
-
return this.handleSubmitStep(params);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
async validateSwapProcess(params) {
|
|
335
|
-
const amount = params.selectedQuote.fromAmount;
|
|
336
|
-
const bnAmount = new BigNumber(amount);
|
|
337
|
-
if (bnAmount.lte(0)) {
|
|
338
|
-
return [new TransactionError(BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
339
|
-
}
|
|
340
|
-
let isXcmOk = false;
|
|
341
|
-
for (const [index, step] of params.process.steps.entries()) {
|
|
342
|
-
const getErrors = async () => {
|
|
343
|
-
switch (step.type) {
|
|
344
|
-
case SwapStepType.DEFAULT:
|
|
345
|
-
return Promise.resolve([]);
|
|
346
|
-
case SwapStepType.XCM:
|
|
347
|
-
return this.swapBaseHandler.validateXcmStep(params, index);
|
|
348
|
-
case SwapStepType.SET_FEE_TOKEN:
|
|
349
|
-
return this.swapBaseHandler.validateSetFeeTokenStep(params, index);
|
|
350
|
-
default:
|
|
351
|
-
return this.swapBaseHandler.validateSwapStep(params, isXcmOk, index);
|
|
352
|
-
}
|
|
353
|
-
};
|
|
354
|
-
const errors = await getErrors();
|
|
355
|
-
if (errors.length) {
|
|
356
|
-
return errors;
|
|
357
|
-
} else if (step.type === SwapStepType.XCM) {
|
|
358
|
-
isXcmOk = true;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
return [];
|
|
362
|
-
}
|
|
363
|
-
async validateSwapRequest(request) {
|
|
364
|
-
const fromAsset = this.chainService.getAssetBySlug(request.pair.from);
|
|
365
|
-
const toAsset = this.chainService.getAssetBySlug(request.pair.to);
|
|
366
|
-
const fromAssetId = _getTokenOnChainAssetId(fromAsset);
|
|
367
|
-
const toAssetId = _getTokenOnChainAssetId(toAsset);
|
|
368
|
-
try {
|
|
369
|
-
var _this$tradeRouter, _this$tradeRouter2;
|
|
370
|
-
// todo: might need to optimize for performance, but prioritize safety for now
|
|
371
|
-
const allAssets = await ((_this$tradeRouter = this.tradeRouter) === null || _this$tradeRouter === void 0 ? void 0 : _this$tradeRouter.getAllAssets());
|
|
372
|
-
if (!allAssets) {
|
|
373
|
-
return {
|
|
374
|
-
error: SwapErrorType.UNKNOWN
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
const supportedFromAsset = allAssets.find(asset => asset.id === fromAssetId && asset.symbol === fromAsset.symbol);
|
|
378
|
-
const supportedToAsset = allAssets.find(asset => asset.id === toAssetId && asset.symbol === toAsset.symbol);
|
|
379
|
-
if (!supportedFromAsset || !supportedToAsset) {
|
|
380
|
-
return {
|
|
381
|
-
error: SwapErrorType.ASSET_NOT_SUPPORTED
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
const assetPairs = await ((_this$tradeRouter2 = this.tradeRouter) === null || _this$tradeRouter2 === void 0 ? void 0 : _this$tradeRouter2.getAssetPairs(fromAssetId));
|
|
385
|
-
if (!assetPairs) {
|
|
386
|
-
return {
|
|
387
|
-
error: SwapErrorType.UNKNOWN
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
const pairedToAsset = assetPairs.find(supportedToAsset => supportedToAsset.id === toAssetId && supportedToAsset.symbol === toAsset.symbol);
|
|
391
|
-
if (!pairedToAsset) {
|
|
392
|
-
return {
|
|
393
|
-
error: SwapErrorType.ASSET_NOT_SUPPORTED
|
|
394
|
-
};
|
|
395
|
-
}
|
|
396
|
-
if (!(fromAsset.originChain === this.chain && toAsset.originChain === this.chain)) {
|
|
397
|
-
return {
|
|
398
|
-
error: SwapErrorType.ASSET_NOT_SUPPORTED
|
|
399
|
-
};
|
|
400
|
-
}
|
|
401
|
-
if (!fromAssetId || !toAssetId) {
|
|
402
|
-
return {
|
|
403
|
-
error: SwapErrorType.UNKNOWN
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
const bnAmount = new BigNumber(request.fromAmount);
|
|
407
|
-
if (bnAmount.lte(0)) {
|
|
408
|
-
return {
|
|
409
|
-
error: SwapErrorType.AMOUNT_CANNOT_BE_ZERO
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
return {
|
|
413
|
-
metadata: {
|
|
414
|
-
chain: this.chainService.getChainInfoByKey(this.chain)
|
|
415
|
-
}
|
|
416
|
-
};
|
|
417
|
-
} catch (e) {
|
|
418
|
-
return {
|
|
419
|
-
error: SwapErrorType.UNKNOWN
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
get referralCode() {
|
|
424
|
-
if (this.isTestnet) {
|
|
425
|
-
return HYDRADX_TESTNET_SUBWALLET_REFERRAL_CODE;
|
|
426
|
-
}
|
|
427
|
-
return HYDRADX_SUBWALLET_REFERRAL_CODE;
|
|
428
|
-
}
|
|
429
|
-
get referralAccount() {
|
|
430
|
-
if (this.isTestnet) {
|
|
431
|
-
return HYDRADX_TESTNET_SUBWALLET_REFERRAL_ACCOUNT;
|
|
432
|
-
}
|
|
433
|
-
return HYDRADX_SUBWALLET_REFERRAL_ACCOUNT;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
|
|
2
|
-
import KoniState from '@subwallet/extension-base/koni/background/handlers/State';
|
|
3
|
-
import { ServiceStatus, ServiceWithProcessInterface, StoppableServiceInterface } from '@subwallet/extension-base/services/base/types';
|
|
4
|
-
import { OptimalSwapPath, OptimalSwapPathParams, SwapPair, SwapQuoteResponse, SwapRequest, SwapRequestResult, SwapSubmitParams, SwapSubmitStepData, ValidateSwapProcessParams } from '@subwallet/extension-base/types/swap';
|
|
5
|
-
import { PromiseHandler } from '@subwallet/extension-base/utils';
|
|
6
|
-
export declare class SwapService implements ServiceWithProcessInterface, StoppableServiceInterface {
|
|
7
|
-
protected readonly state: KoniState;
|
|
8
|
-
private eventService;
|
|
9
|
-
private readonly chainService;
|
|
10
|
-
private swapPairSubject;
|
|
11
|
-
private handlers;
|
|
12
|
-
startPromiseHandler: PromiseHandler<void>;
|
|
13
|
-
stopPromiseHandler: PromiseHandler<void>;
|
|
14
|
-
status: ServiceStatus;
|
|
15
|
-
constructor(state: KoniState);
|
|
16
|
-
private askProvidersForQuote;
|
|
17
|
-
private getDefaultProcess;
|
|
18
|
-
generateOptimalProcess(params: OptimalSwapPathParams): Promise<OptimalSwapPath>;
|
|
19
|
-
handleSwapRequest(request: SwapRequest): Promise<SwapRequestResult>;
|
|
20
|
-
getLatestQuotes(request: SwapRequest): Promise<SwapQuoteResponse>;
|
|
21
|
-
private initHandlers;
|
|
22
|
-
init(): Promise<void>;
|
|
23
|
-
start(): Promise<void>;
|
|
24
|
-
stop(): Promise<void>;
|
|
25
|
-
waitForStarted(): Promise<void>;
|
|
26
|
-
waitForStopped(): Promise<void>;
|
|
27
|
-
getSwapPairs(): SwapPair[];
|
|
28
|
-
private getSwapPairMetadata;
|
|
29
|
-
validateSwapProcess(params: ValidateSwapProcessParams): Promise<TransactionError[]>;
|
|
30
|
-
handleSwapProcess(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
|
|
31
|
-
subscribeSwapPairs(callback: (pairs: SwapPair[]) => void): import("rxjs").Subscription;
|
|
32
|
-
}
|