Skip to content

Commit 4ef6f53

Browse files
Merge pull request #64 from StabilityNexus/sarthak
Added chains module
2 parents 38db762 + aa24bca commit 4ef6f53

1 file changed

Lines changed: 261 additions & 0 deletions

File tree

utils/chains.ts

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
export interface ChainConfig {
2+
id: number
3+
name: string
4+
shortName: string
5+
explorerUrl: string
6+
nativeCurrency: {
7+
name: string
8+
symbol: string
9+
decimals: number
10+
}
11+
rpcUrls: string[]
12+
blockExplorers: {
13+
default: {
14+
name: string
15+
url: string
16+
}
17+
}
18+
color: string
19+
}
20+
21+
export const SUPPORTED_CHAINS: { [key: number]: ChainConfig } = {
22+
1: {
23+
id: 1,
24+
name: 'Ethereum Mainnet',
25+
shortName: 'Ethereum',
26+
explorerUrl: 'https://etherscan.io',
27+
nativeCurrency: {
28+
name: 'Ether',
29+
symbol: 'ETH',
30+
decimals: 18,
31+
},
32+
rpcUrls: ['https://mainnet.infura.io/v3/'],
33+
blockExplorers: {
34+
default: {
35+
name: 'Etherscan',
36+
url: 'https://etherscan.io',
37+
},
38+
},
39+
color: 'bg-blue-400/10 text-blue-500 border-blue-400/20',
40+
},
41+
61: {
42+
id: 61,
43+
name: 'Ethereum Classic',
44+
shortName: 'Ethereum Classic',
45+
explorerUrl: 'https://blockscout.com/etc/mainnet',
46+
nativeCurrency: {
47+
name: 'Ethereum Classic',
48+
symbol: 'ETC',
49+
decimals: 18,
50+
},
51+
rpcUrls: ['https://etc.rivet.link'],
52+
blockExplorers: {
53+
default: {
54+
name: 'Blockscout',
55+
url: 'https://blockscout.com/etc/mainnet',
56+
},
57+
},
58+
color: 'bg-green-400/10 text-green-500 border-green-400/20',
59+
},
60+
137: {
61+
id: 137,
62+
name: 'Polygon',
63+
shortName: 'Polygon',
64+
explorerUrl: 'https://polygonscan.com',
65+
nativeCurrency: {
66+
name: 'MATIC',
67+
symbol: 'MATIC',
68+
decimals: 18,
69+
},
70+
rpcUrls: ['https://polygon-rpc.com'],
71+
blockExplorers: {
72+
default: {
73+
name: 'PolygonScan',
74+
url: 'https://polygonscan.com',
75+
},
76+
},
77+
color: 'bg-violet-400/10 text-violet-500 border-violet-400/20',
78+
},
79+
8453: {
80+
id: 8453,
81+
name: 'Base',
82+
shortName: 'Base',
83+
explorerUrl: 'https://basescan.org',
84+
nativeCurrency: {
85+
name: 'Ether',
86+
symbol: 'ETH',
87+
decimals: 18,
88+
},
89+
rpcUrls: ['https://mainnet.base.org'],
90+
blockExplorers: {
91+
default: {
92+
name: 'BaseScan',
93+
url: 'https://basescan.org',
94+
},
95+
},
96+
color: 'bg-blue-400/10 text-blue-500 border-blue-400/20',
97+
},
98+
56: {
99+
id: 56,
100+
name: 'Binance Smart Chain',
101+
shortName: 'Binance Smart Chain',
102+
explorerUrl: 'https://bscscan.com',
103+
nativeCurrency: {
104+
name: 'BNB',
105+
symbol: 'BNB',
106+
decimals: 18,
107+
},
108+
rpcUrls: ['https://bsc-dataseed1.binance.org'],
109+
blockExplorers: {
110+
default: {
111+
name: 'BscScan',
112+
url: 'https://bscscan.com',
113+
},
114+
},
115+
color: 'bg-yellow-400/10 text-yellow-500 border-yellow-400/20',
116+
},
117+
534351: {
118+
id: 534351,
119+
name: 'Scroll Sepolia',
120+
shortName: 'Scroll Sepolia',
121+
explorerUrl: 'https://sepolia.scrollscan.com',
122+
nativeCurrency: {
123+
name: 'Ether',
124+
symbol: 'ETH',
125+
decimals: 18,
126+
},
127+
rpcUrls: ['https://sepolia-rpc.scroll.io'],
128+
blockExplorers: {
129+
default: {
130+
name: 'Scroll Sepolia Explorer',
131+
url: 'https://sepolia.scrollscan.com',
132+
},
133+
},
134+
color: 'bg-orange-400/10 text-orange-500 border-orange-400/20',
135+
},
136+
5115: {
137+
id: 5115,
138+
name: 'Citrea Testnet',
139+
shortName: 'Citrea Testnet',
140+
explorerUrl: 'https://explorer.testnet.citrea.xyz',
141+
nativeCurrency: {
142+
name: 'Citrea Bitcoin',
143+
symbol: 'cBTC',
144+
decimals: 18,
145+
},
146+
rpcUrls: ['https://rpc.devnet.citrea.xyz'],
147+
blockExplorers: {
148+
default: {
149+
name: 'Citrea Explorer',
150+
url: 'https://explorer.testnet.citrea.xyz',
151+
},
152+
},
153+
color: 'bg-yellow-300/10 text-yellow-300 border-yellow-300/20',
154+
},
155+
2001: {
156+
id: 2001,
157+
name: 'Milkomeda',
158+
shortName: 'Milkomeda',
159+
explorerUrl: 'https://explorer-mainnet-cardano-evm.c1.milkomeda.com',
160+
nativeCurrency: {
161+
name: 'milkADA',
162+
symbol: 'mADA',
163+
decimals: 18,
164+
},
165+
rpcUrls: ['https://rpc-mainnet-cardano-evm.c1.milkomeda.com'],
166+
blockExplorers: {
167+
default: {
168+
name: 'Milkomeda Explorer',
169+
url: 'https://explorer-mainnet-cardano-evm.c1.milkomeda.com',
170+
},
171+
},
172+
color: 'bg-purple-400/10 text-purple-500 border-purple-400/20',
173+
},
174+
}
175+
176+
/**
177+
* Get the display name for a chain
178+
*/
179+
export const getChainName = (chainId: number): string => {
180+
return SUPPORTED_CHAINS[chainId]?.shortName || `Chain ${chainId}`
181+
}
182+
183+
/**
184+
* Get the full name for a chain
185+
*/
186+
export const getChainFullName = (chainId: number): string => {
187+
return SUPPORTED_CHAINS[chainId]?.name || `Chain ${chainId}`
188+
}
189+
190+
/**
191+
* Get the block explorer URL for a chain
192+
*/
193+
export const getChainExplorer = (chainId: number): string => {
194+
return SUPPORTED_CHAINS[chainId]?.explorerUrl || ''
195+
}
196+
197+
/**
198+
* Get the block explorer URL for a specific transaction
199+
*/
200+
export const getTransactionUrl = (chainId: number, txHash: string): string => {
201+
const explorerUrl = getChainExplorer(chainId)
202+
return explorerUrl ? `${explorerUrl}/tx/${txHash}` : ''
203+
}
204+
205+
/**
206+
* Get the block explorer URL for a specific address
207+
*/
208+
export const getAddressUrl = (chainId: number, address: string): string => {
209+
const explorerUrl = getChainExplorer(chainId)
210+
return explorerUrl ? `${explorerUrl}/address/${address}` : ''
211+
}
212+
213+
/**
214+
* Get the block explorer URL for a specific token
215+
*/
216+
export const getTokenUrl = (chainId: number, tokenAddress: string): string => {
217+
const explorerUrl = getChainExplorer(chainId)
218+
return explorerUrl ? `${explorerUrl}/token/${tokenAddress}` : ''
219+
}
220+
221+
/**
222+
* Get the chain color classes for UI styling
223+
*/
224+
export const getChainColor = (chainId: number): string => {
225+
return SUPPORTED_CHAINS[chainId]?.color || 'bg-gray-400/10 text-gray-500 border-gray-400/20'
226+
}
227+
228+
/**
229+
* Get the block explorer URL for an address (alias for getAddressUrl for backward compatibility)
230+
*/
231+
export const getBlockExplorerUrl = (chainId: number, address: string): string => {
232+
return getAddressUrl(chainId, address)
233+
}
234+
235+
/**
236+
* Get the native currency symbol for a chain
237+
*/
238+
export const getNativeCurrencySymbol = (chainId: number): string => {
239+
return SUPPORTED_CHAINS[chainId]?.nativeCurrency.symbol || 'ETH'
240+
}
241+
242+
/**
243+
* Check if a chain is supported
244+
*/
245+
export const isSupportedChain = (chainId: number): boolean => {
246+
return chainId in SUPPORTED_CHAINS
247+
}
248+
249+
/**
250+
* Get all supported chain IDs
251+
*/
252+
export const getSupportedChainIds = (): number[] => {
253+
return Object.keys(SUPPORTED_CHAINS).map(Number)
254+
}
255+
256+
/**
257+
* Get chain configuration
258+
*/
259+
export const getChainConfig = (chainId: number): ChainConfig | null => {
260+
return SUPPORTED_CHAINS[chainId] || null
261+
}

0 commit comments

Comments
 (0)