-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathNetworkUtils.ts
168 lines (151 loc) · 5.7 KB
/
NetworkUtils.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
import {
CCTP_NO_DOMAIN,
ChainFamily,
CHAIN_IDs,
MAINNET_CHAIN_IDs,
PRODUCTION_NETWORKS,
PUBLIC_NETWORKS,
TESTNET_CHAIN_IDs,
} from "../constants";
export const hreNetworks: Record<number, string> = {
666: "Hardhat1",
1337: "Hardhat2",
31337: "HardhatNetwork",
};
/**
* Resolves a network name from a network id.
* @param networkId The network id to resolve the name for
* @returns The network name for the network id. If the network id is not found, returns "unknown"
*/
export function getNetworkName(networkId: number | string): string {
networkId = Number(networkId);
return PUBLIC_NETWORKS[networkId]?.name ?? hreNetworks[networkId] ?? "unknown";
}
/**
* Resolves a native token symbol from a chain id.
* @param chainId The chain id to resolve the native token symbol for
* @returns The native token symbol for the chain id. If the chain id is not found, returns "ETH"
*/
export function getNativeTokenSymbol(chainId: number | string): string {
return PUBLIC_NETWORKS[Number(chainId)]?.nativeToken ?? "ETH";
}
/**
* Determines whether a chain ID is part of the production network.
* @param chainId Chain ID to query.
* @returns true if the chain ID is part of the production network, otherwise false.
*/
export function chainIsProd(chainId: number): boolean {
return Object.values(MAINNET_CHAIN_IDs).includes(chainId);
}
/**
* Determines whether a chain ID is part of the production network.
* @param chainId Chain ID to query.
* @returns true if the chain ID is part of the production network, otherwise false.
*/
export function chainIsTestnet(chainId: number): boolean {
return Object.values(TESTNET_CHAIN_IDs).includes(chainId);
}
/**
* Determines whether a chain ID is a Polygon implementation.
* @param chainId Chain ID to evaluate.
* @returns True if chainId is a Polygon chain (mainnet or testnet), otherwise false.
*/
export function chainIsMatic(chainId: number): boolean {
return [CHAIN_IDs.POLYGON, CHAIN_IDs.POLYGON_AMOY].includes(chainId);
}
/**
* Determines whether a chain ID is an Optimism OP Stack implementation.
* @param chainId Chain ID to evaluate.
* @returns True if chainId is an OP stack, otherwise false.
*/
export function chainIsOPStack(chainId: number): boolean {
return PUBLIC_NETWORKS[chainId]?.family === ChainFamily.OP_STACK;
}
/**
* Determines whether a chain ID is a ZkStack implementation.
* @param chainId Chain ID to evaluate.
* @returns True if chainId is a ZkStack chain, otherwise false.
*/
export function chainIsZkStack(chainId: number): boolean {
return PUBLIC_NETWORKS[chainId]?.family === ChainFamily.ZK_STACK;
}
/**
* Determines whether a chain ID is an Arbitrum Orbit implementation.
* @param chainId Chain ID to evaluate.
* @returns True if chainId is an Orbit chain, otherwise false.
*/
export function chainIsOrbit(chainId: number): boolean {
return PUBLIC_NETWORKS[chainId]?.family === ChainFamily.ORBIT;
}
/**
* Determines whether a chain ID is an Arbitrum implementation.
* @param chainId Chain ID to evaluate.
* @returns True if chainId is an Arbitrum chain, otherwise false.
*/
export function chainIsArbitrum(chainId: number): boolean {
return [CHAIN_IDs.ARBITRUM, CHAIN_IDs.ARBITRUM_SEPOLIA].includes(chainId);
}
/**
* Determines whether a chain ID is a Linea implementation.
* @param chainId Chain ID to evaluate.
* @returns True if chainId is a Linea chain, otherwise false.
*/
export function chainIsLinea(chainId: number): boolean {
return [CHAIN_IDs.LINEA].includes(chainId);
}
/**
* Determines whether a chain ID has a corresponding hub pool contract.
* @param chainId Chain ID to evaluate.
* @returns True if chain corresponding to chainId has a hub pool implementation.
*/
export function chainIsL1(chainId: number): boolean {
return [CHAIN_IDs.MAINNET, CHAIN_IDs.SEPOLIA].includes(chainId);
}
/**
* Determines whether a chain ID runs on an EVM-like execution layer.
* @param chainId Chain ID to evaluate.
* @returns True if chain corresponding to chainId has an EVM-like execution layer.
*/
export function chainIsEvm(chainId: number): boolean {
// TODO: Update when additional execution layers beyond EVM and SVM are supported.
return PUBLIC_NETWORKS[chainId]?.family !== ChainFamily.SVM;
}
/**
* Determines whether a chain ID runs on an SVM-like execution layer.
* @param chainId Chain ID to evaluate.
* @returns True if chain corresponding to chainId has an SVM-like execution layer.
*/
export function chainIsSvm(chainId: number): boolean {
return PUBLIC_NETWORKS[chainId]?.family === ChainFamily.SVM;
}
/**
* Determines whether a chain ID has the capacity for having its USDC bridged via CCTP.
* @param chainId Chain ID to evaluate.
* @returns True if chainId is a CCTP-bridging enabled chain, otherwise false.
*/
export function chainIsCCTPEnabled(chainId: number): boolean {
// Add chainIds to cctpExceptions to administratively disable CCTP on a chain.
// This is useful if constants has been updated to specify a CCTP domain in advance of it being activated.
const cctpExceptions: number[] = [];
return PRODUCTION_NETWORKS[chainId]?.cctpDomain !== CCTP_NO_DOMAIN && !cctpExceptions.includes(chainId);
}
/**
* Determines if a chain ID requires a manual L1 -> L2 finalization step.
* @param chainId Chain ID to evaluate.
* @returns True if chainId requires manual L1 -> L2 finalization, otherwise false.
*/
export function chainRequiresL1ToL2Finalization(chainId: number): boolean {
return chainIsCCTPEnabled(chainId) || chainIsLinea(chainId);
}
/**
* Returns the origin of a URL.
* @param url A URL.
* @returns The origin of the URL, or "UNKNOWN" if the URL is invalid.
*/
export function getOriginFromURL(url: string): string {
try {
return new URL(url).origin;
} catch (e) {
return "UNKNOWN";
}
}