Skip to content

Commit c865723

Browse files
committed
fix: explorers url
1 parent 580dd51 commit c865723

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ yarn-error.log*
3434
.vercel
3535

3636
TODO
37-
.eslintcache
37+
.eslintcache
38+
.env

chainlist.d.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
21
interface NativeCurrency {
32
name: string
43
symbol: string
54
decimals: number
65
}
76

7+
interface Explorer {
8+
name: string
9+
url: string
10+
icon: string
11+
standard: string
12+
}
13+
814
interface Chain {
915
name: string
1016
chainId: number
@@ -16,6 +22,7 @@ interface Chain {
1622
rpc: string[]
1723
faucets: string[]
1824
infoURL: string
25+
explorers?: Explorer[]
1926
selectCounts?: number
2027
}
2128

@@ -24,7 +31,7 @@ interface AddEthereumChainParameter {
2431
* the integer ID of the chain as a hexadecimal string
2532
*/
2633
chainId: string
27-
blockExplorerUrls?: string[]
34+
blockExplorerUrls?: string[] | Explorer[]
2835
chainName?: string
2936
iconUrls?: string[]
3037
nativeCurrency?: {

common/hooks/useChain.tsx

+21-14
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ export const useChain = (): [number | undefined, (chain: Chain) => void] => {
1818

1919
const addEthChain = (chain: Chain) => {
2020
if (!enable) return
21+
2122
const params: AddEthereumChainParameter = {
2223
chainId: hexValue(chain.chainId),
23-
blockExplorerUrls: [chain.infoURL],
24+
blockExplorerUrls: chain.explorers?.length
25+
? [chain.explorers[0].url]
26+
: [chain.infoURL],
2427
chainName: chain.name,
2528
nativeCurrency: {
2629
name: chain.nativeCurrency.name,
@@ -29,26 +32,30 @@ export const useChain = (): [number | undefined, (chain: Chain) => void] => {
2932
},
3033
rpcUrls: chain.rpc,
3134
}
35+
3236
try {
3337
updateNetworkRecord(chain.chainId)
3438
} catch (error) {
3539
//
3640
}
37-
window.ethereum.request({
38-
method: 'wallet_addEthereumChain',
39-
params: [params],
40-
}).then(() => {
41-
setToast({
42-
text: 'add network successfully!',
41+
window.ethereum
42+
.request({
43+
method: 'wallet_addEthereumChain',
44+
params: [params],
45+
})
46+
.then(() => {
47+
setToast({
48+
text: 'add network successfully!',
49+
})
50+
const prev = window.localStorage.getItem(EVM_BOX_PERSIST)
51+
const persist = [chain.chainId, prev].filter(Boolean).join(',')
52+
window.localStorage.setItem(EVM_BOX_PERSIST, persist)
4353
})
44-
const prev = window.localStorage.getItem(EVM_BOX_PERSIST)
45-
const persist = [chain.chainId, prev].filter(Boolean).join(',')
46-
window.localStorage.setItem(EVM_BOX_PERSIST, persist)
47-
}).catch((e: Error) => {
48-
setToast({
49-
text: 'add network failed: ' + e.message,
54+
.catch((e: Error) => {
55+
setToast({
56+
text: 'add network failed: ' + e.message,
57+
})
5058
})
51-
})
5259
}
5360

5461
return [currentChainId, addEthChain]

0 commit comments

Comments
 (0)