-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathnativeChainIds.ts
180 lines (174 loc) · 6.42 KB
/
nativeChainIds.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
import type { MapLevels, ToMapping, Widen } from "./../utils/index.js";
import { constMap } from "./../utils/index.js";
import type { Chain } from "./chains.js";
import type { Network } from "./networks.js";
import type { Platform, PlatformToChains } from "./platforms.js";
import { chainToPlatform } from "./platforms.js";
// prettier-ignore
const chainNetworkNativeChainIdEntries = [
[
"Mainnet",
[
["Aptos", 1n],
["Algorand", "mainnet-v1.0"],
["Near", "mainnet"],
["Cosmoshub", "cosmoshub-4"],
["Evmos", "evmos_9001-2"],
["Injective", "injective-1"],
["Osmosis", "osmosis-1"],
["Sei", "pacific-1"],
["Terra", "columbus-5"],
["Terra2", "phoenix-1"],
["Wormchain", "wormchain"],
["Xpla", "dimension_37-1"],
["Kujira", "kaiyo-1"],
["Solana", "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d"],
["Sui", "35834a8a"],
["Acala", 787n],
["Arbitrum", 42161n],
["Aurora", 1313161554n],
["Avalanche", 43114n],
["Base", 8453n],
["Bsc", 56n],
["Celo", 42220n],
["Ethereum", 1n],
["Fantom", 250n],
["Gnosis", 100n],
["Karura", 686n],
["Klaytn", 8217n],
["Moonbeam", 1284n],
["Neon", 245022934n],
["Oasis", 42262n],
["Optimism", 10n],
["Polygon", 137n],
["Neutron", "neutron-1"],
["Stargaze", "stargaze-1"],
["Celestia", "celestia"],
["Dymension", "dymension_1100-1"],
["Provenance","pio-mainnet-1"],
["Noble", "noble-1"],
["Xlayer", 196n],
["Mantle", 5000n],
["Scroll", 534352n],
["Blast", 81457n],
["Linea", 59144n],
["Snaxchain", 2192n],
["Berachain", 80094n],
["Unichain", 130n],
["Worldchain",480n],
["Ink", 57073n],
],
],
[
"Testnet",
[
["Aptos", 2n],
["Algorand", "testnet-v1.0"],
["Near", "testnet"],
["Cosmoshub", "theta-testnet-001"],
["Evmos", "evmos_9000-4"],
["Injective", "injective-888"],
["Osmosis", "osmo-test-5"],
["Sei", "atlantic-2"],
["Terra", "bombay-12"],
["Terra2", "pisco-1"],
["Wormchain", "wormchain-testnet-0"],
["Xpla", "cube_47-5"],
["Kujira", "harpoon-4"],
["Solana", "EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG"],
["Sui", "4c78adac"],
["Acala", 597n],
["Arbitrum", 421613n],
["Aurora", 1313161555n],
["Avalanche", 43113n], //fuji
["Base", 84531n],
["Bsc", 97n],
["Celo", 44787n], //alfajores
["Ethereum", 5n], //goerli
["Fantom", 4002n],
["Gnosis", 10200n],
["Karura", 596n],
["Klaytn", 1001n], //baobab
["Moonbeam", 1287n], //moonbase alpha
["Neon", 245022940n],
["Oasis", 42261n],
["Optimism", 420n],
["Polygon", 80001n], //mumbai
["Sepolia", 11155111n],
["ArbitrumSepolia", 421614n],
["BaseSepolia", 84532n],
["OptimismSepolia", 11155420n],
["PolygonSepolia", 80002n],
["Holesky", 17000n],
["Neutron", "pion-1"],
["Celestia", "mocha-4"],
["Seda", "seda-1-testnet"],
["Noble", "grand-1"],
["Blast", 168587773n], // Sepolia testnet
["Mantle", 5003n], // Sepolia testnet
["Scroll", 534351n],
["Berachain", 80069n], // Testnet v3
["Seievm", 1328n],
["Snaxchain", 13001n],
["Unichain", 1301n],
["Worldchain", 4801n],
["Ink", 763373n],
["HyperEVM", 998n],
["Xlayer", 195n],
["Linea", 59141n], // Sepolia
["Monad", 10143n],
["Movement", 250n], // Bardock
["Mezo", 31611n],
],
],
[
"Devnet",
[
["Aptos", 0n],
["Algorand", "sandnet-v1.0"],
["Bsc", 1397n],
["Ethereum", 1337n],
["Injective","injective_devnet_fake"],
["Solana", "8wF6jKV3cKwyaVkWcoV9KpDqmkjvEYno9hKZrKx8TbZn"],
],
],
] as const satisfies MapLevels<[Network, Chain, bigint | string]>;
export const networkChainToNativeChainId = constMap(chainNetworkNativeChainIdEntries);
type NetworkChainToNativeChainId = ToMapping<typeof chainNetworkNativeChainIdEntries>;
export type PlatformToNativeChainIds<
P extends Platform,
N extends Network = Network,
> = PlatformToChains<P> extends infer C
? C extends keyof NetworkChainToNativeChainId[N]
? NetworkChainToNativeChainId[N][C]
: // If the platform doesn't support the network, return the widened type for
// mainnet
C extends keyof NetworkChainToNativeChainId["Mainnet"]
? Widen<NetworkChainToNativeChainId["Mainnet"][C]>
: never
: never;
//When mapping a Platform and native chain id to a network and chain pair, we assume that the
// mapping is injective, i.e. that for any given platform there is only a single chain that
// has the given chain id.
//Currently this is the case, in fact only Evm and Aptos share a chain id (1n).
const nativeChainIdToNetworkChain = constMap(chainNetworkNativeChainIdEntries, [2, [0, 1]]);
export type PlatformNativeChainIdToNetworkChainPair<P extends Platform> =
PlatformToChains<P> extends infer C
? ReturnType<typeof nativeChainIdToNetworkChain>[number] extends infer NCP
? NCP extends readonly [Network, C]
? NCP
: never
: never
: never;
export function platformNativeChainIdToNetworkChain<
P extends Platform,
CI extends PlatformToNativeChainIds<P>,
>(platform: P, chainId: Widen<CI>): PlatformNativeChainIdToNetworkChainPair<P> {
//typescript really struggles to comprehend the types here so we have to help it out
//@ts-ignore
const candidates = nativeChainIdToNetworkChain(chainId) as readonly (readonly [Network, Chain])[];
const mustBeSingleton = candidates.filter(([_, chain]) => chainToPlatform(chain) === platform);
if (mustBeSingleton.length !== 1)
throw new Error(`Platform ${platform} has multiple chains with native chain id ${chainId}`);
return mustBeSingleton[0] as any;
}