-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.ts
59 lines (48 loc) · 2.58 KB
/
util.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
import { assets } from './assets';
export const isAssetId = (assetId?: string): assetId is string => !!assetId && assetId in assets;
export const isNativeAssetId = (assetId?: string) => {
const assetConfig = isAssetId(assetId) ? assets[assetId] : undefined;
return !!(assetConfig && 'nativeAsset' in assetConfig && assetConfig.nativeAsset === assetId);
};
export const isDerivableAssetId = (assetId?: string) => {
const assetConfig = isAssetId(assetId) ? assets[assetId] : undefined;
return !!(assetConfig && 'derive' in assetConfig && assetConfig.derive);
};
export const isTransferableAsset = (assetId?: string) => {
const assetConfig = isAssetId(assetId) ? assets[assetId] : undefined;
return !!(assetConfig && 'transfer' in assetConfig && assetConfig.transfer);
};
export const isTestnetAsset = (assetId?: string) => {
const assetConfig = isAssetId(assetId) ? assets[assetId] : undefined;
return !!(assetConfig && 'testnet' in assetConfig && assetConfig.testnet);
};
export const isExplorerUrl = (url: string) => {
const endingsToRemove: { [key: string]: string } = { SOL: '?cluster=mainnet', SOL_TEST: '?cluster=devnet' };
return Object.keys(assets).some((assetId: string) => {
const asset = assets[assetId];
if (isNativeAssetId(assetId) && asset.getExplorerUrl) {
const txUrl =
assetId in endingsToRemove
? asset.getExplorerUrl('tx')('').replace(endingsToRemove[assetId], '')
: asset.getExplorerUrl('tx')('');
const addressUrl =
assetId in endingsToRemove
? asset.getExplorerUrl('address')('').replace(endingsToRemove[assetId], '')
: asset.getExplorerUrl('address')('');
return url.startsWith(addressUrl) || url.startsWith(txUrl);
}
return false;
});
};
export const getAssetConfig = (assetId?: string) => (isAssetId(assetId) ? assets[assetId] : undefined);
export const isTransferableToken = (assetId?: string) => {
if (!isAssetId) return false;
const assetConfig = assets[assetId!];
// Is this a non-native asset, has a contract / address that represents it (i.e token) and we enabled the transfer option
return assetConfig.nativeAsset !== assetConfig.id && assetConfig.address && assetConfig.transfer;
};
export const getNetworkProtocol = (assetId: string) => getAssetConfig(assetId)?.protocol;
export const getNativeAssetConfig = <ID extends string>(assetId?: ID) =>
isNativeAssetId(assetId) ? getAssetConfig(assetId) : (undefined as never);
export const getDerivableAssetConfig = <ID extends string>(assetId?: ID) =>
isDerivableAssetId(assetId) ? getAssetConfig(assetId) : (undefined as never);