forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathutils.ts
272 lines (217 loc) · 9.02 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2019-2022 @subwallet/extension-base
// SPDX-License-Identifier: Apache-2.0
import { COMMON_ASSETS, COMMON_CHAIN_SLUGS } from '@subwallet/chain-list';
import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types';
import { ChainService } from '@subwallet/extension-base/services/chain-service';
import { _SubstrateApi } from '@subwallet/extension-base/services/chain-service/types';
import { _getAssetDecimals, _getAssetPriceId, _getAssetSymbol, _getFungibleAssetType, _getMultiChainAsset } from '@subwallet/extension-base/services/chain-service/utils';
import { CHAINFLIP_BROKER_API } from '@subwallet/extension-base/services/swap-service/handler/chainflip-handler';
import { SwapPair, SwapProviderId } from '@subwallet/extension-base/types/swap';
import BigN from 'bignumber.js';
export const CHAIN_FLIP_TESTNET_EXPLORER = 'https://blocks-perseverance.chainflip.io';
export const CHAIN_FLIP_MAINNET_EXPLORER = 'https://scan.chainflip.io';
export const SIMPLE_SWAP_EXPLORER = 'https://simpleswap.io';
export const SIMPLE_SWAP_SUPPORTED_TESTNET_ASSET_MAPPING: Record<string, string> = {
'bittensor-NATIVE-TAO': 'tao',
[COMMON_ASSETS.ETH]: 'eth',
[COMMON_ASSETS.DOT]: 'dot',
[COMMON_ASSETS.USDC_ETHEREUM]: 'usdc',
[COMMON_ASSETS.USDT_ETHEREUM]: 'usdterc20'
};
export const SWAP_QUOTE_TIMEOUT_MAP: Record<string, number> = { // in milliseconds
default: 30000,
[SwapProviderId.CHAIN_FLIP_TESTNET]: 30000,
[SwapProviderId.CHAIN_FLIP_MAINNET]: 30000
};
export const _PROVIDER_TO_SUPPORTED_PAIR_MAP: Record<string, string[]> = {
[SwapProviderId.HYDRADX_MAINNET]: [COMMON_CHAIN_SLUGS.HYDRADX],
[SwapProviderId.HYDRADX_TESTNET]: [COMMON_CHAIN_SLUGS.HYDRADX_TESTNET],
[SwapProviderId.CHAIN_FLIP_MAINNET]: [COMMON_CHAIN_SLUGS.POLKADOT, COMMON_CHAIN_SLUGS.ETHEREUM, COMMON_CHAIN_SLUGS.ARBITRUM],
[SwapProviderId.CHAIN_FLIP_TESTNET]: [COMMON_CHAIN_SLUGS.CHAINFLIP_POLKADOT, COMMON_CHAIN_SLUGS.ETHEREUM_SEPOLIA],
[SwapProviderId.POLKADOT_ASSET_HUB]: [COMMON_CHAIN_SLUGS.POLKADOT_ASSET_HUB],
[SwapProviderId.KUSAMA_ASSET_HUB]: [COMMON_CHAIN_SLUGS.KUSAMA_ASSET_HUB],
[SwapProviderId.ROCOCO_ASSET_HUB]: [COMMON_CHAIN_SLUGS.ROCOCO_ASSET_HUB],
[SwapProviderId.WESTEND_ASSET_HUB]: ['westend_assethub'],
[SwapProviderId.SIMPLE_SWAP]: ['bittensor', COMMON_CHAIN_SLUGS.ETHEREUM, COMMON_CHAIN_SLUGS.POLKADOT]
};
export function getSwapAlternativeAsset (swapPair: SwapPair): string | undefined {
return swapPair?.metadata?.alternativeAsset as string;
}
export function getSwapAltToken (chainAsset: _ChainAsset): string | undefined {
return chainAsset.metadata?.alternativeSwapAsset as string;
}
export function calculateSwapRate (fromAmount: string, toAmount: string, fromAsset: _ChainAsset, toAsset: _ChainAsset) {
const bnFromAmount = new BigN(fromAmount);
const bnToAmount = new BigN(toAmount);
const decimalDiff = _getAssetDecimals(toAsset) - _getAssetDecimals(fromAsset);
const bnRate = bnFromAmount.div(bnToAmount);
return 1 / bnRate.times(10 ** decimalDiff).toNumber();
}
export function convertSwapRate (rate: string, fromAsset: _ChainAsset, toAsset: _ChainAsset) {
const decimalDiff = _getAssetDecimals(toAsset) - _getAssetDecimals(fromAsset);
const bnRate = new BigN(rate);
return bnRate.times(10 ** decimalDiff).pow(-1).toNumber();
}
export function getChainflipOptions (isTestnet: boolean) {
if (isTestnet) {
return {
network: getChainflipNetwork(isTestnet)
};
}
return {
network: getChainflipNetwork(isTestnet),
broker: getChainflipBroker(isTestnet)
};
}
function getChainflipNetwork (isTestnet: boolean) {
return isTestnet ? 'perseverance' : 'mainnet';
}
export function getChainflipBroker (isTestnet: boolean) { // noted: currently not use testnet broker
if (isTestnet) {
return {
url: `https://perseverance.chainflip-broker.io/rpc/${CHAINFLIP_BROKER_API}`
};
} else {
return {
url: `https://chainflip-broker.io/rpc/${CHAINFLIP_BROKER_API}`
};
}
}
export function getChainflipSwap (isTestnet: boolean) {
if (isTestnet) {
return `https://perseverance.chainflip-broker.io/swap?apikey=${CHAINFLIP_BROKER_API}`;
} else {
return `https://chainflip-broker.io/swap?apikey=${CHAINFLIP_BROKER_API}`;
}
}
export function generateAllDestinations (substrateApi: _SubstrateApi, chainService: ChainService, fromAsset: _ChainAsset, maxPathLength = 1) {
if (maxPathLength < 1) {
return [];
}
let currentTargets: _ChainAsset[] = [];
let newTargets: _ChainAsset[] = [];
let currentStep = 1;
// step 1:
newTargets = findDestinations(chainService, fromAsset);
currentTargets = mergeWithoutDuplicate<_ChainAsset>(currentTargets, newTargets);
currentStep += 1;
// step 2 - n
while (currentStep <= maxPathLength) { // todo: improve by stop when nothing new
newTargets = Array.from(newTargets.reduce((farChildTargets, currentTarget) => {
const farChildTargetsLocal = findDestinations(chainService, currentTarget);
return new Set([...farChildTargets, ...farChildTargetsLocal]);
}, new Set<_ChainAsset>()));
currentTargets = mergeWithoutDuplicate<_ChainAsset>(currentTargets, newTargets);
currentStep += 1;
}
return currentTargets;
}
function findDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
const xcmTargets = findXcmDestinations(chainService, chainAsset);
const swapTargets = findSwapDestinations(chainService, chainAsset);
return mergeWithoutDuplicate<_ChainAsset>(xcmTargets, swapTargets);
}
export function findXcmDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
const xcmTargets: _ChainAsset[] = [];
const multichainAssetSlug = _getMultiChainAsset(chainAsset);
if (!multichainAssetSlug) {
return xcmTargets;
}
const assetRegistry = chainService.getAssetRegistry();
for (const asset of Object.values(assetRegistry)) {
if (multichainAssetSlug === _getMultiChainAsset(asset)) {
xcmTargets.push(asset);
}
}
return xcmTargets.filter((candidate) => candidate.slug !== chainAsset.slug);
}
export function findSwapDestinations (chainService: ChainService, chainAsset: _ChainAsset) {
const chain = chainAsset.originChain;
const swapTargets: _ChainAsset[] = [];
const availableChains = Object.values(_PROVIDER_TO_SUPPORTED_PAIR_MAP).reduce((remainChains, currentChains) => {
if (currentChains.includes(chain)) {
currentChains.forEach((candidate) => {
remainChains.add(candidate);
});
}
return remainChains;
}, new Set<string>());
availableChains.forEach((candidate) => {
const assets = chainService.getAssetByChainAndType(candidate, _getFungibleAssetType()); // todo: recheck assetType
swapTargets.push(...Object.values(assets));
});
return swapTargets.filter((candidate) => candidate.slug !== chainAsset.slug);
}
// @ts-ignore
export function findSwapDestinationsV2 (chainService: ChainService, chainAsset: _ChainAsset) {
const chain = chainAsset.originChain;
const swapTargets: _ChainAsset[] = [];
// Convert to Set once at the start
const availableChains = new Set<string>(
Object.values(_PROVIDER_TO_SUPPORTED_PAIR_MAP)
.filter((chains) => chains.includes(chain))
.flat()
);
// Use Set for O(1) lookup instead of includes()
for (const candidate of availableChains) {
const assets = chainService.getAssetByChainAndType(
candidate,
_getFungibleAssetType()
);
swapTargets.push(...Object.values(assets));
}
return swapTargets;
}
// @ts-ignore
async function isHasXcmChannelSubstrate (substrateApi: _SubstrateApi, fromChain: _ChainInfo, toChain: _ChainInfo) {
const channel = await substrateApi.api.query.hrmp.hrmpChainnels(fromChain.substrateInfo?.paraId, toChain.substrateInfo?.paraId);
return !!channel.toPrimitive();
}
// @ts-ignore
export async function getAllXcmChannelSubstrate (substrateApi: _SubstrateApi) {
const channels = await substrateApi.api.query.hrmp?.hrmpChannels?.keys();
const allKeys = [];
for (const key of channels) {
allKeys.push(key.args[0].toPrimitive());
}
return allKeys;
}
// todo: improve
// function isHasBridgeChanel ()
function mergeWithoutDuplicate<T> (arr1: T[], arr2: T[]): T[] {
return Array.from(new Set([...arr1, ...arr2]));
}
export enum DynamicSwapType {
INIT = 'INIT',
SWAP = 'SWAP',
XCM = 'XCM' // todo: rename XCM to a better name to describe cross chain transfer action;
}
export interface DynamicSwapAction {
action: DynamicSwapType;
toToken: string;
}
export function getInitStep (tokenSlug: string): DynamicSwapAction {
return {
action: DynamicSwapType.INIT,
toToken: tokenSlug
};
}
export function getXcmStep (tokenSlug: string): DynamicSwapAction {
return {
action: DynamicSwapType.XCM,
toToken: tokenSlug
};
}
export function getSwapStep (tokenSlug: string): DynamicSwapAction {
return {
action: DynamicSwapType.SWAP,
toToken: tokenSlug
};
}
export function isEquiValentAsset (from: _ChainAsset, to: _ChainAsset) {
return (
_getMultiChainAsset(from) === _getMultiChainAsset(to) ||
_getAssetPriceId(from) === _getAssetPriceId(to) ||
_getAssetSymbol(from) === _getAssetSymbol(to)
);
}