@subwallet/extension-base 1.1.54-0 → 1.1.55-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 +21 -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/background/types.d.ts +2 -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/staking/bonding/utils.js +35 -6
- package/cjs/koni/background/handlers/Extension.js +214 -102
- package/cjs/koni/background/handlers/State.js +5 -2
- package/cjs/packageInfo.js +1 -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 +39 -18
- package/cjs/services/chain-service/utils/index.js +15 -4
- package/cjs/services/chain-service/utils/patch.js +1 -1
- 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/MigrateTransactionHistoryBySymbol.js +4 -17
- package/cjs/services/migration-service/scripts/databases/MigrateAssetSetting.js +4 -17
- package/cjs/services/migration-service/scripts/index.js +3 -3
- package/cjs/services/swap-service/handler/base-handler.js +189 -0
- package/cjs/services/swap-service/handler/chainflip-handler.js +407 -0
- package/cjs/services/swap-service/handler/hydradx-handler.js +531 -0
- package/cjs/services/swap-service/index.js +250 -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/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/utils/index.js +12 -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 +6 -0
- package/koni/background/handlers/Extension.js +111 -0
- package/koni/background/handlers/State.d.ts +2 -0
- package/koni/background/handlers/State.js +5 -2
- package/package.json +65 -8
- package/packageInfo.js +1 -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 +21 -1
- package/services/chain-service/utils/index.d.ts +7 -5
- package/services/chain-service/utils/index.js +9 -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/migration-service/scripts/MigrateTransactionHistoryBySymbol.js +4 -17
- package/services/migration-service/scripts/databases/MigrateAssetSetting.js +4 -17
- package/services/migration-service/scripts/index.js +3 -3
- package/services/swap-service/handler/base-handler.d.ts +38 -0
- package/services/swap-service/handler/base-handler.js +180 -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 +522 -0
- package/services/swap-service/index.d.ts +32 -0
- package/services/swap-service/index.js +241 -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/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 +168 -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/index.d.ts +1 -0
- package/utils/index.js +2 -1
- package/utils/swap.d.ts +3 -0
- package/utils/swap.js +70 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension-base
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { SwapSDK } from '@chainflip/sdk/swap';
|
|
5
|
+
import { COMMON_ASSETS } from '@subwallet/chain-list';
|
|
6
|
+
import { SwapError } from '@subwallet/extension-base/background/errors/SwapError';
|
|
7
|
+
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
|
|
8
|
+
import { BasicTxErrorType, ChainType, ExtrinsicType } from '@subwallet/extension-base/background/KoniTypes';
|
|
9
|
+
import { createTransferExtrinsic } from '@subwallet/extension-base/koni/api/dotsama/transfer';
|
|
10
|
+
import { getERC20TransactionObject, getEVMTransactionObject } from '@subwallet/extension-base/koni/api/tokens/evm/transfer';
|
|
11
|
+
import { _getAssetDecimals, _getChainNativeTokenSlug, _getContractAddressOfToken, _isNativeToken, _isSubstrateChain } from '@subwallet/extension-base/services/chain-service/utils';
|
|
12
|
+
import { SwapBaseHandler } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
|
|
13
|
+
import { calculateSwapRate, CHAIN_FLIP_SUPPORTED_MAINNET_ASSET_MAPPING, CHAIN_FLIP_SUPPORTED_MAINNET_MAPPING, CHAIN_FLIP_SUPPORTED_TESTNET_ASSET_MAPPING, CHAIN_FLIP_SUPPORTED_TESTNET_MAPPING, getChainflipEarlyValidationError, 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
|
+
var ChainflipFeeType;
|
|
17
|
+
(function (ChainflipFeeType) {
|
|
18
|
+
ChainflipFeeType["INGRESS"] = "INGRESS";
|
|
19
|
+
ChainflipFeeType["NETWORK"] = "NETWORK";
|
|
20
|
+
ChainflipFeeType["EGRESS"] = "EGRESS";
|
|
21
|
+
ChainflipFeeType["LIQUIDITY"] = "LIQUIDITY";
|
|
22
|
+
})(ChainflipFeeType || (ChainflipFeeType = {}));
|
|
23
|
+
const INTERMEDIARY_MAINNET_ASSET_SLUG = COMMON_ASSETS.USDC_ETHEREUM;
|
|
24
|
+
const INTERMEDIARY_TESTNET_ASSET_SLUG = COMMON_ASSETS.USDC_SEPOLIA;
|
|
25
|
+
var CHAINFLIP_QUOTE_ERROR;
|
|
26
|
+
(function (CHAINFLIP_QUOTE_ERROR) {
|
|
27
|
+
CHAINFLIP_QUOTE_ERROR["InsufficientLiquidity"] = "InsufficientLiquidity";
|
|
28
|
+
CHAINFLIP_QUOTE_ERROR["InsufficientEgress"] = "is lower than minimum egress amount";
|
|
29
|
+
CHAINFLIP_QUOTE_ERROR["InsufficientIngress"] = "amount is lower than estimated ingress fee";
|
|
30
|
+
})(CHAINFLIP_QUOTE_ERROR || (CHAINFLIP_QUOTE_ERROR = {}));
|
|
31
|
+
export class ChainflipSwapHandler {
|
|
32
|
+
constructor(chainService, balanceService, isTestnet = true) {
|
|
33
|
+
this.swapBaseHandler = new SwapBaseHandler({
|
|
34
|
+
chainService,
|
|
35
|
+
balanceService,
|
|
36
|
+
providerName: isTestnet ? 'Chainflip Testnet' : 'Chainflip',
|
|
37
|
+
providerSlug: isTestnet ? SwapProviderId.CHAIN_FLIP_TESTNET : SwapProviderId.CHAIN_FLIP_MAINNET
|
|
38
|
+
});
|
|
39
|
+
this.isTestnet = isTestnet;
|
|
40
|
+
this.swapSdk = new SwapSDK({
|
|
41
|
+
network: isTestnet ? 'perseverance' : 'mainnet'
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
get chainService() {
|
|
45
|
+
return this.swapBaseHandler.chainService;
|
|
46
|
+
}
|
|
47
|
+
get balanceService() {
|
|
48
|
+
return this.swapBaseHandler.balanceService;
|
|
49
|
+
}
|
|
50
|
+
get providerInfo() {
|
|
51
|
+
return this.swapBaseHandler.providerInfo;
|
|
52
|
+
}
|
|
53
|
+
get name() {
|
|
54
|
+
return this.swapBaseHandler.name;
|
|
55
|
+
}
|
|
56
|
+
get slug() {
|
|
57
|
+
return this.swapBaseHandler.slug;
|
|
58
|
+
}
|
|
59
|
+
get assetMapping() {
|
|
60
|
+
if (this.isTestnet) {
|
|
61
|
+
return CHAIN_FLIP_SUPPORTED_TESTNET_ASSET_MAPPING;
|
|
62
|
+
} else {
|
|
63
|
+
return CHAIN_FLIP_SUPPORTED_MAINNET_ASSET_MAPPING;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
get chainMapping() {
|
|
67
|
+
if (this.isTestnet) {
|
|
68
|
+
return CHAIN_FLIP_SUPPORTED_TESTNET_MAPPING;
|
|
69
|
+
} else {
|
|
70
|
+
return CHAIN_FLIP_SUPPORTED_MAINNET_MAPPING;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
get intermediaryAssetSlug() {
|
|
74
|
+
if (this.isTestnet) {
|
|
75
|
+
return INTERMEDIARY_TESTNET_ASSET_SLUG;
|
|
76
|
+
} else {
|
|
77
|
+
return INTERMEDIARY_MAINNET_ASSET_SLUG;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async validateSwapRequest(request) {
|
|
81
|
+
try {
|
|
82
|
+
// todo: risk of matching wrong chain, asset can lead to loss of funds
|
|
83
|
+
|
|
84
|
+
const fromAsset = this.chainService.getAssetBySlug(request.pair.from);
|
|
85
|
+
const toAsset = this.chainService.getAssetBySlug(request.pair.to);
|
|
86
|
+
const srcChain = fromAsset.originChain;
|
|
87
|
+
const destChain = toAsset.originChain;
|
|
88
|
+
const srcChainInfo = this.chainService.getChainInfoByKey(srcChain);
|
|
89
|
+
const srcChainId = this.chainMapping[srcChain];
|
|
90
|
+
const destChainId = this.chainMapping[destChain];
|
|
91
|
+
const fromAssetId = this.assetMapping[fromAsset.slug];
|
|
92
|
+
const toAssetId = this.assetMapping[toAsset.slug];
|
|
93
|
+
if (!srcChainId || !destChainId || !fromAssetId || !toAssetId) {
|
|
94
|
+
return {
|
|
95
|
+
error: SwapErrorType.ASSET_NOT_SUPPORTED
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const [supportedDestChains, srcAssets, destAssets] = await Promise.all([this.swapSdk.getChains(srcChainId), this.swapSdk.getAssets(srcChainId), this.swapSdk.getAssets(destChainId)]);
|
|
99
|
+
const supportedDestChainId = supportedDestChains.find(c => c.chain === destChainId);
|
|
100
|
+
const srcAssetData = srcAssets.find(a => a.asset === fromAssetId);
|
|
101
|
+
const destAssetData = destAssets.find(a => a.asset === toAssetId);
|
|
102
|
+
if (!destAssetData || !srcAssetData || !supportedDestChainId) {
|
|
103
|
+
return {
|
|
104
|
+
error: SwapErrorType.UNKNOWN
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const bnAmount = new BigNumber(request.fromAmount);
|
|
108
|
+
const bnMinSwap = new BigNumber(srcAssetData.minimumSwapAmount);
|
|
109
|
+
if (srcAssetData.maximumSwapAmount) {
|
|
110
|
+
const bnMaxProtocolSwap = new BigNumber(srcAssetData.maximumSwapAmount);
|
|
111
|
+
if (bnMinSwap.gte(bnMaxProtocolSwap)) {
|
|
112
|
+
return {
|
|
113
|
+
error: SwapErrorType.UNKNOWN
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (bnAmount.gte(bnMaxProtocolSwap)) {
|
|
117
|
+
return {
|
|
118
|
+
error: SwapErrorType.SWAP_EXCEED_ALLOWANCE,
|
|
119
|
+
metadata: {
|
|
120
|
+
minSwap: {
|
|
121
|
+
value: srcAssetData.minimumSwapAmount,
|
|
122
|
+
decimals: _getAssetDecimals(fromAsset),
|
|
123
|
+
symbol: fromAsset.symbol
|
|
124
|
+
},
|
|
125
|
+
maxSwap: {
|
|
126
|
+
value: bnMaxProtocolSwap.toString(),
|
|
127
|
+
decimals: _getAssetDecimals(fromAsset),
|
|
128
|
+
symbol: fromAsset.symbol
|
|
129
|
+
},
|
|
130
|
+
chain: srcChainInfo
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (bnAmount.lt(bnMinSwap)) {
|
|
136
|
+
// might miss case when minSwap is 0
|
|
137
|
+
return {
|
|
138
|
+
error: SwapErrorType.NOT_MEET_MIN_SWAP,
|
|
139
|
+
metadata: {
|
|
140
|
+
minSwap: {
|
|
141
|
+
value: srcAssetData.minimumSwapAmount,
|
|
142
|
+
decimals: _getAssetDecimals(fromAsset),
|
|
143
|
+
symbol: fromAsset.symbol
|
|
144
|
+
},
|
|
145
|
+
maxSwap: {
|
|
146
|
+
value: srcAssetData.maximumSwapAmount,
|
|
147
|
+
decimals: _getAssetDecimals(fromAsset),
|
|
148
|
+
symbol: fromAsset.symbol
|
|
149
|
+
},
|
|
150
|
+
chain: srcChainInfo
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
metadata: {
|
|
156
|
+
minSwap: {
|
|
157
|
+
value: srcAssetData.minimumSwapAmount,
|
|
158
|
+
decimals: _getAssetDecimals(fromAsset),
|
|
159
|
+
symbol: fromAsset.symbol
|
|
160
|
+
},
|
|
161
|
+
maxSwap: {
|
|
162
|
+
value: srcAssetData.maximumSwapAmount,
|
|
163
|
+
decimals: _getAssetDecimals(fromAsset),
|
|
164
|
+
symbol: fromAsset.symbol
|
|
165
|
+
},
|
|
166
|
+
chain: srcChainInfo
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
} catch (e) {
|
|
170
|
+
return {
|
|
171
|
+
error: SwapErrorType.UNKNOWN
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
parseSwapPath(fromAsset, toAsset) {
|
|
176
|
+
if (toAsset.slug !== this.intermediaryAssetSlug && fromAsset.slug !== this.intermediaryAssetSlug) {
|
|
177
|
+
// Chainflip always use USDC as intermediary
|
|
178
|
+
return [fromAsset.slug, this.intermediaryAssetSlug, toAsset.slug]; // todo: generalize this
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return [fromAsset.slug, toAsset.slug];
|
|
182
|
+
}
|
|
183
|
+
async getSwapQuote(request) {
|
|
184
|
+
const fromAsset = this.chainService.getAssetBySlug(request.pair.from);
|
|
185
|
+
const toAsset = this.chainService.getAssetBySlug(request.pair.to);
|
|
186
|
+
const fromChain = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
187
|
+
const fromChainNativeTokenSlug = _getChainNativeTokenSlug(fromChain);
|
|
188
|
+
if (!fromAsset || !toAsset) {
|
|
189
|
+
return new SwapError(SwapErrorType.UNKNOWN);
|
|
190
|
+
}
|
|
191
|
+
const earlyValidation = await this.validateSwapRequest(request);
|
|
192
|
+
const metadata = earlyValidation.metadata;
|
|
193
|
+
if (earlyValidation.error) {
|
|
194
|
+
return getChainflipEarlyValidationError(earlyValidation.error, metadata);
|
|
195
|
+
}
|
|
196
|
+
const srcChainId = this.chainMapping[fromAsset.originChain];
|
|
197
|
+
const destChainId = this.chainMapping[toAsset.originChain];
|
|
198
|
+
const fromAssetId = this.assetMapping[fromAsset.slug];
|
|
199
|
+
const toAssetId = this.assetMapping[toAsset.slug];
|
|
200
|
+
try {
|
|
201
|
+
var _metadata$maxSwap;
|
|
202
|
+
const quoteResponse = await this.swapSdk.getQuote({
|
|
203
|
+
srcChain: srcChainId,
|
|
204
|
+
destChain: destChainId,
|
|
205
|
+
srcAsset: fromAssetId,
|
|
206
|
+
destAsset: toAssetId,
|
|
207
|
+
amount: request.fromAmount
|
|
208
|
+
});
|
|
209
|
+
const feeComponent = [];
|
|
210
|
+
quoteResponse.quote.includedFees.forEach(fee => {
|
|
211
|
+
switch (fee.type) {
|
|
212
|
+
case ChainflipFeeType.INGRESS:
|
|
213
|
+
|
|
214
|
+
// eslint-disable-next-line no-fallthrough
|
|
215
|
+
case ChainflipFeeType.EGRESS:
|
|
216
|
+
{
|
|
217
|
+
const tokenSlug = Object.keys(this.assetMapping).find(assetSlug => this.assetMapping[assetSlug] === fee.asset);
|
|
218
|
+
feeComponent.push({
|
|
219
|
+
tokenSlug,
|
|
220
|
+
amount: fee.amount,
|
|
221
|
+
feeType: SwapFeeType.NETWORK_FEE
|
|
222
|
+
});
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
case ChainflipFeeType.NETWORK:
|
|
226
|
+
|
|
227
|
+
// eslint-disable-next-line no-fallthrough
|
|
228
|
+
case ChainflipFeeType.LIQUIDITY:
|
|
229
|
+
{
|
|
230
|
+
const tokenSlug = Object.keys(this.assetMapping).find(assetSlug => this.assetMapping[assetSlug] === fee.asset);
|
|
231
|
+
feeComponent.push({
|
|
232
|
+
tokenSlug,
|
|
233
|
+
amount: fee.amount,
|
|
234
|
+
feeType: SwapFeeType.PLATFORM_FEE
|
|
235
|
+
});
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
const defaultFeeToken = _isNativeToken(fromAsset) ? fromAsset.slug : fromChainNativeTokenSlug;
|
|
241
|
+
return {
|
|
242
|
+
pair: request.pair,
|
|
243
|
+
fromAmount: request.fromAmount,
|
|
244
|
+
toAmount: quoteResponse.quote.egressAmount.toString(),
|
|
245
|
+
rate: calculateSwapRate(request.fromAmount, quoteResponse.quote.egressAmount.toString(), fromAsset, toAsset),
|
|
246
|
+
provider: this.providerInfo,
|
|
247
|
+
aliveUntil: +Date.now() + (SWAP_QUOTE_TIMEOUT_MAP[this.slug] || SWAP_QUOTE_TIMEOUT_MAP.default),
|
|
248
|
+
minSwap: metadata.minSwap.value,
|
|
249
|
+
maxSwap: (_metadata$maxSwap = metadata.maxSwap) === null || _metadata$maxSwap === void 0 ? void 0 : _metadata$maxSwap.value,
|
|
250
|
+
estimatedArrivalTime: quoteResponse.quote.estimatedDurationSeconds,
|
|
251
|
+
// in seconds
|
|
252
|
+
isLowLiquidity: quoteResponse.quote.lowLiquidityWarning,
|
|
253
|
+
feeInfo: {
|
|
254
|
+
feeComponent: feeComponent,
|
|
255
|
+
defaultFeeToken,
|
|
256
|
+
feeOptions: [defaultFeeToken]
|
|
257
|
+
},
|
|
258
|
+
route: {
|
|
259
|
+
path: this.parseSwapPath(fromAsset, toAsset)
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
} catch (e) {
|
|
263
|
+
var _error$response;
|
|
264
|
+
const error = e;
|
|
265
|
+
const errorObj = error === null || error === void 0 ? void 0 : (_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.data;
|
|
266
|
+
if (errorObj && errorObj.error && errorObj.error.includes(CHAINFLIP_QUOTE_ERROR.InsufficientLiquidity)) {
|
|
267
|
+
// todo: Chainflip will improve this
|
|
268
|
+
return new SwapError(SwapErrorType.NOT_ENOUGH_LIQUIDITY);
|
|
269
|
+
}
|
|
270
|
+
if (errorObj && errorObj.message && errorObj.message.includes(CHAINFLIP_QUOTE_ERROR.InsufficientLiquidity)) {
|
|
271
|
+
return new SwapError(SwapErrorType.NOT_ENOUGH_LIQUIDITY);
|
|
272
|
+
}
|
|
273
|
+
return new SwapError(SwapErrorType.ERROR_FETCHING_QUOTE);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async validateSwapProcess(params) {
|
|
277
|
+
const amount = params.selectedQuote.fromAmount;
|
|
278
|
+
const bnAmount = new BigNumber(amount);
|
|
279
|
+
if (bnAmount.lte(0)) {
|
|
280
|
+
return [new TransactionError(BasicTxErrorType.INVALID_PARAMS, 'Amount must be greater than 0')];
|
|
281
|
+
}
|
|
282
|
+
let isXcmOk = false;
|
|
283
|
+
for (const [index, step] of params.process.steps.entries()) {
|
|
284
|
+
const getErrors = async () => {
|
|
285
|
+
switch (step.type) {
|
|
286
|
+
case SwapStepType.DEFAULT:
|
|
287
|
+
return Promise.resolve([]);
|
|
288
|
+
case SwapStepType.TOKEN_APPROVAL:
|
|
289
|
+
return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
|
|
290
|
+
default:
|
|
291
|
+
return this.swapBaseHandler.validateSwapStep(params, isXcmOk, index);
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
const errors = await getErrors();
|
|
295
|
+
if (errors.length) {
|
|
296
|
+
return errors;
|
|
297
|
+
} else if (step.type === SwapStepType.XCM) {
|
|
298
|
+
isXcmOk = true;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return [];
|
|
302
|
+
}
|
|
303
|
+
async handleSubmitStep(params) {
|
|
304
|
+
const {
|
|
305
|
+
address,
|
|
306
|
+
quote,
|
|
307
|
+
recipient
|
|
308
|
+
} = params;
|
|
309
|
+
const pair = quote.pair;
|
|
310
|
+
const fromAsset = this.chainService.getAssetBySlug(pair.from);
|
|
311
|
+
const toAsset = this.chainService.getAssetBySlug(pair.to);
|
|
312
|
+
const chainInfo = this.chainService.getChainInfoByKey(fromAsset.originChain);
|
|
313
|
+
const chainType = _isSubstrateChain(chainInfo) ? ChainType.SUBSTRATE : ChainType.EVM;
|
|
314
|
+
const receiver = recipient !== null && recipient !== void 0 ? recipient : address;
|
|
315
|
+
const srcChainId = this.chainMapping[fromAsset.originChain];
|
|
316
|
+
const destChainId = this.chainMapping[toAsset.originChain];
|
|
317
|
+
const fromAssetId = this.assetMapping[fromAsset.slug];
|
|
318
|
+
const toAssetId = this.assetMapping[toAsset.slug];
|
|
319
|
+
const depositAddressResponse = await this.swapSdk.requestDepositAddress({
|
|
320
|
+
srcChain: srcChainId,
|
|
321
|
+
destChain: destChainId,
|
|
322
|
+
srcAsset: fromAssetId,
|
|
323
|
+
destAsset: toAssetId,
|
|
324
|
+
destAddress: receiver,
|
|
325
|
+
amount: quote.fromAmount
|
|
326
|
+
});
|
|
327
|
+
const txData = {
|
|
328
|
+
address,
|
|
329
|
+
provider: this.providerInfo,
|
|
330
|
+
quote: params.quote,
|
|
331
|
+
slippage: params.slippage,
|
|
332
|
+
recipient,
|
|
333
|
+
depositChannelId: depositAddressResponse.depositChannelId,
|
|
334
|
+
depositAddress: depositAddressResponse.depositAddress,
|
|
335
|
+
process: params.process
|
|
336
|
+
};
|
|
337
|
+
let extrinsic;
|
|
338
|
+
if (chainType === ChainType.SUBSTRATE) {
|
|
339
|
+
const chainApi = this.chainService.getSubstrateApi(chainInfo.slug);
|
|
340
|
+
const substrateApi = await chainApi.isReady;
|
|
341
|
+
const [submittableExtrinsic] = await createTransferExtrinsic({
|
|
342
|
+
from: address,
|
|
343
|
+
networkKey: chainInfo.slug,
|
|
344
|
+
substrateApi,
|
|
345
|
+
to: depositAddressResponse.depositAddress,
|
|
346
|
+
tokenInfo: fromAsset,
|
|
347
|
+
transferAll: false,
|
|
348
|
+
// always false, because we do not allow swapping all the balance
|
|
349
|
+
value: quote.fromAmount
|
|
350
|
+
});
|
|
351
|
+
extrinsic = submittableExtrinsic;
|
|
352
|
+
} else {
|
|
353
|
+
if (_isNativeToken(fromAsset)) {
|
|
354
|
+
const [transactionConfig] = await getEVMTransactionObject(chainInfo, address, depositAddressResponse.depositAddress, quote.fromAmount, false, this.chainService.getEvmApi(chainInfo.slug));
|
|
355
|
+
extrinsic = transactionConfig;
|
|
356
|
+
} else {
|
|
357
|
+
const [transactionConfig] = await getERC20TransactionObject(_getContractAddressOfToken(fromAsset), chainInfo, address, depositAddressResponse.depositAddress, quote.fromAmount, false, this.chainService.getEvmApi(chainInfo.slug));
|
|
358
|
+
extrinsic = transactionConfig;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
txChain: fromAsset.originChain,
|
|
363
|
+
txData,
|
|
364
|
+
extrinsic,
|
|
365
|
+
transferNativeAmount: _isNativeToken(fromAsset) ? quote.fromAmount : '0',
|
|
366
|
+
// todo
|
|
367
|
+
extrinsicType: ExtrinsicType.SWAP,
|
|
368
|
+
chainType
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
async handleSwapProcess(params) {
|
|
372
|
+
const {
|
|
373
|
+
currentStep,
|
|
374
|
+
process
|
|
375
|
+
} = params;
|
|
376
|
+
const type = process.steps[currentStep].type;
|
|
377
|
+
switch (type) {
|
|
378
|
+
case SwapStepType.DEFAULT:
|
|
379
|
+
return Promise.reject(new TransactionError(BasicTxErrorType.UNSUPPORTED));
|
|
380
|
+
case SwapStepType.SWAP:
|
|
381
|
+
return this.handleSubmitStep(params);
|
|
382
|
+
default:
|
|
383
|
+
return this.handleSubmitStep(params);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
async getSubmitStep(params) {
|
|
387
|
+
if (params.selectedQuote) {
|
|
388
|
+
const submitStep = {
|
|
389
|
+
name: 'Swap',
|
|
390
|
+
type: SwapStepType.SWAP
|
|
391
|
+
};
|
|
392
|
+
return Promise.resolve([submitStep, params.selectedQuote.feeInfo]);
|
|
393
|
+
}
|
|
394
|
+
return Promise.resolve(undefined);
|
|
395
|
+
}
|
|
396
|
+
generateOptimalProcess(params) {
|
|
397
|
+
return this.swapBaseHandler.generateOptimalProcess(params, [this.getSubmitStep]);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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 { SwapBaseInterface } from '@subwallet/extension-base/services/swap-service/handler/base-handler';
|
|
6
|
+
import { BaseStepDetail } from '@subwallet/extension-base/types/service-base';
|
|
7
|
+
import { OptimalSwapPath, OptimalSwapPathParams, SwapEarlyValidation, SwapFeeInfo, SwapQuote, SwapRequest, SwapSubmitParams, SwapSubmitStepData, ValidateSwapProcessParams } from '@subwallet/extension-base/types/swap';
|
|
8
|
+
export declare class HydradxHandler implements SwapBaseInterface {
|
|
9
|
+
private swapBaseHandler;
|
|
10
|
+
private tradeRouter;
|
|
11
|
+
private readonly isTestnet;
|
|
12
|
+
isReady: boolean;
|
|
13
|
+
constructor(chainService: ChainService, balanceService: BalanceService, isTestnet?: boolean);
|
|
14
|
+
init(): Promise<void>;
|
|
15
|
+
chain: () => string;
|
|
16
|
+
get chainService(): ChainService;
|
|
17
|
+
get balanceService(): BalanceService;
|
|
18
|
+
get providerInfo(): import("@subwallet/extension-base/types/swap").SwapProvider;
|
|
19
|
+
get name(): string;
|
|
20
|
+
get slug(): string;
|
|
21
|
+
getXcmStep(params: OptimalSwapPathParams): Promise<[BaseStepDetail, SwapFeeInfo] | undefined>;
|
|
22
|
+
getFeeOptionStep(params: OptimalSwapPathParams): Promise<[BaseStepDetail, SwapFeeInfo] | undefined>;
|
|
23
|
+
getSubmitStep(params: OptimalSwapPathParams): Promise<[BaseStepDetail, SwapFeeInfo] | undefined>;
|
|
24
|
+
generateOptimalProcess(params: OptimalSwapPathParams): Promise<OptimalSwapPath>;
|
|
25
|
+
private getSwapPathErrors;
|
|
26
|
+
private parseSwapPath;
|
|
27
|
+
getSwapQuote(request: SwapRequest): Promise<SwapQuote | SwapError>;
|
|
28
|
+
handleXcmStep(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
|
|
29
|
+
handleSetFeeStep(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
|
|
30
|
+
handleSubmitStep(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
|
|
31
|
+
handleSwapProcess(params: SwapSubmitParams): Promise<SwapSubmitStepData>;
|
|
32
|
+
validateSwapProcess(params: ValidateSwapProcessParams): Promise<TransactionError[]>;
|
|
33
|
+
validateSwapRequest(request: SwapRequest): Promise<SwapEarlyValidation>;
|
|
34
|
+
get referralCode(): "WALLET" | "ASSETHUB";
|
|
35
|
+
get referralAccount(): "7PCsCpkgsHdNaZhv79wCCQ5z97uxVbSeSCtDMUa1eZHKXy4a" | "7LCt6dFqtxzdKVB2648jWW9d85doiFfLSbZJDNAMVJNxh5rJ";
|
|
36
|
+
}
|