-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CaipNetworkUtil.ts
212 lines (189 loc) · 5.86 KB
/
CaipNetworkUtil.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
ConstantsUtil,
type AppKitNetwork,
type CaipNetwork,
type CaipNetworkId
} from '@reown/appkit-common'
import { fallback, http } from 'viem'
import { PresetsUtil } from './PresetsUtil.js'
const RPC_URL_HOST = 'rpc.walletconnect.org'
export function getBlockchainApiRpcUrl(caipNetworkId: CaipNetworkId, projectId: string) {
const url = new URL('https://rpc.walletconnect.org/v1/')
url.searchParams.set('chainId', caipNetworkId)
url.searchParams.set('projectId', projectId)
return url.toString()
}
const WC_HTTP_RPC_SUPPORTED_CHAINS = [
'near:mainnet',
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
'eip155:1101',
'eip155:56',
'eip155:42161',
'eip155:7777777',
'eip155:59144',
'eip155:324',
'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1',
'eip155:5000',
'solana:4sgjmw1sunhzsxgspuhpqldx6wiyjntz',
'eip155:80084',
'eip155:5003',
'eip155:100',
'eip155:8453',
'eip155:42220',
'eip155:1313161555',
'eip155:17000',
'eip155:1',
'eip155:300',
'eip155:1313161554',
'eip155:1329',
'eip155:84532',
'eip155:421614',
'eip155:11155111',
'eip155:8217',
'eip155:43114',
'solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z',
'eip155:999999999',
'eip155:11155420',
'eip155:80002',
'eip155:97',
'eip155:43113',
'eip155:137',
'eip155:10',
'eip155:1301'
]
type ExtendCaipNetworkParams = {
customNetworkImageUrls: Record<number | string, string> | undefined
projectId: string
enableCustomRpc: boolean | undefined
}
export const CaipNetworksUtil = {
/**
* Extends the RPC URL with the project ID if the RPC URL is a Reown URL
* @param rpcUrl - The RPC URL to extend
* @param projectId - The project ID to extend the RPC URL with
* @returns The extended RPC URL
*/
extendRpcUrlWithProjectId(rpcUrl: string, projectId: string) {
let isReownUrl = false
try {
const url = new URL(rpcUrl)
isReownUrl = url.host === RPC_URL_HOST
} catch (e) {
isReownUrl = false
}
if (isReownUrl) {
const url = new URL(rpcUrl)
if (!url.searchParams.has('projectId')) {
url.searchParams.set('projectId', projectId)
}
return url.toString()
}
return rpcUrl
},
isCaipNetwork(network: AppKitNetwork): network is CaipNetwork {
return 'chainNamespace' in network && 'caipNetworkId' in network
},
getChainNamespace(network: AppKitNetwork) {
if (this.isCaipNetwork(network)) {
return network.chainNamespace
}
return ConstantsUtil.CHAIN.EVM
},
getCaipNetworkId(network: AppKitNetwork) {
if (this.isCaipNetwork(network)) {
return network.caipNetworkId
}
return `${ConstantsUtil.CHAIN.EVM}:${network.id}` as CaipNetworkId
},
getRpcUrl(
caipNetwork: AppKitNetwork,
caipNetworkId: CaipNetworkId,
extraParam: { projectId: string; enableCustomRpc?: boolean }
) {
const defaultRpcUrl = caipNetwork.rpcUrls?.default?.http?.[0]
if (WC_HTTP_RPC_SUPPORTED_CHAINS.includes(caipNetworkId)) {
if (extraParam.enableCustomRpc === false) {
return getBlockchainApiRpcUrl(caipNetworkId, extraParam.projectId)
}
return defaultRpcUrl || ''
}
return defaultRpcUrl || ''
},
/**
* Extends the CaipNetwork object with the image ID and image URL if the image ID is not provided
* @param params - The parameters object
* @param params.caipNetwork - The CaipNetwork object to extend
* @param params.networkImageIds - The network image IDs
* @param params.customNetworkImageUrls - The custom network image URLs
* @param params.projectId - The project ID
* @returns The extended array of CaipNetwork objects
*/
extendCaipNetwork(
caipNetwork: AppKitNetwork,
{ customNetworkImageUrls, projectId, enableCustomRpc = false }: ExtendCaipNetworkParams
): CaipNetwork {
const caipNetworkId = this.getCaipNetworkId(caipNetwork)
const chainNamespace = this.getChainNamespace(caipNetwork)
const rpcUrl = this.getRpcUrl(caipNetwork, caipNetworkId, { projectId, enableCustomRpc })
return {
...caipNetwork,
chainNamespace,
caipNetworkId,
assets: {
imageId: PresetsUtil.NetworkImageIds[caipNetwork.id],
imageUrl: customNetworkImageUrls?.[caipNetwork.id]
},
rpcUrls: {
...caipNetwork.rpcUrls,
default: {
http: [rpcUrl]
},
// Save the networks original RPC URL default
chainDefault: {
http: [caipNetwork.rpcUrls.default.http[0] || '']
}
}
}
},
/**
* Extends the array of CaipNetwork objects with the image ID and image URL if the image ID is not provided
* @param caipNetworks - The array of CaipNetwork objects to extend
* @param params - The parameters object
* @param params.networkImageIds - The network image IDs
* @param params.customNetworkImageUrls - The custom network image URLs
* @param params.projectId - The project ID
* @returns The extended array of CaipNetwork objects
*/
extendCaipNetworks(
caipNetworks: AppKitNetwork[],
{ customNetworkImageUrls, projectId, enableCustomRpc }: ExtendCaipNetworkParams
) {
return caipNetworks.map(caipNetwork =>
CaipNetworksUtil.extendCaipNetwork(caipNetwork, {
customNetworkImageUrls,
enableCustomRpc,
projectId
})
) as [CaipNetwork, ...CaipNetwork[]]
},
getViemTransport(caipNetwork: CaipNetwork) {
const chainDefaultUrl = caipNetwork.rpcUrls.default.http?.[0]
if (!WC_HTTP_RPC_SUPPORTED_CHAINS.includes(caipNetwork.caipNetworkId)) {
return http(chainDefaultUrl)
}
return fallback([
http(chainDefaultUrl, {
/*
* The Blockchain API uses "Content-Type: text/plain" to avoid OPTIONS preflight requests
* It will only work for viem >= 2.17.7
*/
fetchOptions: {
headers: {
'Content-Type': 'text/plain'
}
}
}),
http(chainDefaultUrl)
])
}
}