-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathchains.ts
More file actions
163 lines (137 loc) · 4.44 KB
/
chains.ts
File metadata and controls
163 lines (137 loc) · 4.44 KB
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
import {
hydra,
paseo,
polkadot,
polkadot_asset_hub,
west,
west_asset_hub
} from "@polkadot-api/descriptors"
import {
paseoChain,
polkadotAssetHubChain,
polkadotChain,
westendAssetHubChain,
westendChain
} from "./supported-chains"
type DescriptorsRelayType = {
polkadot: typeof polkadot
west: typeof west
paseo: typeof paseo
}
type DescriptorsAssetHubType = {
polkadot_asset_hub: typeof polkadot_asset_hub
west_asset_hub: typeof west_asset_hub
}
type DescriptorsParaType = {
hydra: typeof hydra
}
const DESCRIPTORS_RELAY: DescriptorsRelayType = {
polkadot,
west,
paseo
}
const DESCRIPTORS_ASSET_HUB: DescriptorsAssetHubType = {
polkadot_asset_hub,
west_asset_hub
}
const DESCRIPTORS_PARA: DescriptorsParaType = {
hydra
}
export const DESCRIPTORS_ALL = {
...DESCRIPTORS_RELAY,
...DESCRIPTORS_ASSET_HUB,
...DESCRIPTORS_PARA
}
type DescriptorsAssetHub = typeof DESCRIPTORS_ASSET_HUB
type DescriptorsRelay = typeof DESCRIPTORS_RELAY
type DescriptorsPara = typeof DESCRIPTORS_PARA
export type DescriptorsAll = DescriptorsRelay & DescriptorsAssetHub & DescriptorsPara
export type ChainIdAssetHub = keyof DescriptorsAssetHub
export type ChainIdRelay = keyof DescriptorsRelay
export type ChainIdPara = keyof DescriptorsParaType
export type KnownChainId = ChainIdRelay | ChainIdAssetHub | ChainIdPara
type UnKnownChainId = string & {}
export type ChainId = KnownChainId | UnKnownChainId
export const isChainIdAssetHub = (id: unknown): id is ChainIdAssetHub =>
typeof id === "string" && !!DESCRIPTORS_ASSET_HUB[id as ChainIdAssetHub]
export const isChainIdRelay = (id: unknown): id is ChainIdRelay =>
typeof id === "string" && !!DESCRIPTORS_RELAY[id as ChainIdRelay]
export type Descriptors<Id extends KnownChainId> = DescriptorsAll[Id]
export const getDescriptors = (id: ChainId): Descriptors<KnownChainId> | undefined => {
if (DESCRIPTORS_ALL[id as KnownChainId]) {
return DESCRIPTORS_ALL[id as KnownChainId]
}
return undefined
}
export type Chain = {
id: ChainId
name: string
specName: string
wsUrls: string[]
relay: ChainIdRelay | null
chainId: number | null
type: "system" | "relay" | "para"
blockExplorerUrl: string | null
prefix: number
decimals: number
symbol: string
}
export type ChainRelay = Chain & { chainId: null }
export type ChainAssetHub = Chain & { chainId: 1000 }
export const getChainById = <T extends Chain>(id: ChainId, chains: Chain[]): T => {
const foundChain = chains.find(chain => chain.id === id) as T
if (!foundChain) throw new Error(`Could not find chain ${id}`)
return foundChain
}
export const getChainByName = <T extends Chain>(name: string, chains: Chain[]): T => {
const foundChain = chains.find(chain => chain.name === name) as T
if (!foundChain) throw new Error(`Could not find chain ${name}`)
return foundChain
}
const SUPPORTED_CHAINS: Chain[] = [
polkadotChain,
polkadotAssetHubChain,
westendChain,
westendAssetHubChain,
paseoChain
]
export const getAllSupportedChains = (): Chain[] => {
return SUPPORTED_CHAINS
}
export const isSupportedChain = (chainId: unknown): chainId is ChainId => {
return typeof chainId === "string" && SUPPORTED_CHAINS.some(chain => chain.id === chainId)
}
export function getDecimalsByChainId(chainId: string): number {
const chain = getChainById(chainId, getAllSupportedChains())
return chain.decimals
}
/**
* Filter supported chains based on allowed chain IDs
* @param allowedChains - Array of allowed chain IDs, if undefined returns all chains
* @returns Filtered array of chains
*/
export const getFilteredChains = (allowedChains?: KnownChainId[]): Chain[] => {
if (!allowedChains) {
return getAllSupportedChains()
}
return SUPPORTED_CHAINS.filter(chain => allowedChains.includes(chain.id as KnownChainId))
}
/**
* Check if a chain is allowed based on the configuration
* @param chainId - Chain ID to check
* @param allowedChains - Array of allowed chain IDs, if undefined all chains are allowed
* @returns True if chain is allowed, false otherwise
*/
export const isChainAllowed = (chainId: ChainId, allowedChains?: KnownChainId[]): boolean => {
if (!allowedChains) {
return true // All chains allowed if no restriction
}
return allowedChains.includes(chainId as KnownChainId)
}
/**
* Get default chains when no specific chains are configured
* @returns Array of all supported chain IDs
*/
export const getDefaultChains = (): KnownChainId[] => {
return SUPPORTED_CHAINS.map(chain => chain.id as KnownChainId)
}