@@ -9,6 +9,14 @@ import { Allocation } from '../allocations'
9
9
import { GraphNode } from '../graph-node'
10
10
import { SubgraphDeployment } from '../types'
11
11
12
+ /* eslint-disable @typescript-eslint/no-explicit-any */
13
+ let registry : any
14
+ async function initializeNetworksRegistry ( ) {
15
+ // Dynamically import NetworksRegistry
16
+ const { NetworksRegistry } = await import ( '@pinax/graph-networks-registry' )
17
+ registry = await NetworksRegistry . fromLatestVersion ( )
18
+ }
19
+
12
20
export interface CreateAllocationResult {
13
21
actionID : number
14
22
type : 'allocate'
@@ -166,7 +174,34 @@ export function epochElapsedBlocks(networkEpoch: NetworkEpoch): number {
166
174
return networkEpoch . startBlockNumber - networkEpoch . latestBlock
167
175
}
168
176
169
- export const Caip2ByChainAlias : { [ key : string ] : string } = {
177
+ // Construct Caip2ByChainId from the registry data, keeping the manual
178
+ // overrides for backward compatibility
179
+ const caip2ByChainId : { [ key : number ] : string } = {
180
+ 1337 : 'eip155:1337' ,
181
+ 1 : 'eip155:1' ,
182
+ 5 : 'eip155:5' ,
183
+ 100 : 'eip155:100' ,
184
+ 42161 : 'eip155:42161' ,
185
+ 421613 : 'eip155:421613' ,
186
+ 43114 : 'eip155:43114' ,
187
+ 137 : 'eip155:137' ,
188
+ 42220 : 'eip155:42220' ,
189
+ 10 : 'eip155:10' ,
190
+ 250 : 'eip155:250' ,
191
+ 11155111 : 'eip155:11155111' ,
192
+ 421614 : 'eip155:421614' ,
193
+ 56 : 'eip155:56' ,
194
+ 59144 : 'eip155:59144' ,
195
+ 534352 : 'eip155:534352' ,
196
+ 8453 : 'eip155:8453' ,
197
+ 1284 : 'eip155:1284' ,
198
+ 122 : 'eip155:122' ,
199
+ 81457 : 'eip155:81457' ,
200
+ 288 : 'eip155:288' ,
201
+ 56288 : 'eip155:56288' ,
202
+ }
203
+
204
+ const caip2ByChainAlias : { [ key : string ] : string } = {
170
205
mainnet : 'eip155:1' ,
171
206
goerli : 'eip155:5' ,
172
207
gnosis : 'eip155:100' ,
@@ -191,47 +226,57 @@ export const Caip2ByChainAlias: { [key: string]: string } = {
191
226
'boba-bnb' : 'eip155:56288' ,
192
227
}
193
228
194
- export const Caip2ByChainId : { [ key : number ] : string } = {
195
- 1 : 'eip155:1' ,
196
- 5 : 'eip155:5' ,
197
- 100 : 'eip155:100' ,
198
- 1337 : 'eip155:1337' ,
199
- 42161 : 'eip155:42161' ,
200
- 421613 : 'eip155:421613' ,
201
- 43114 : 'eip155:43114' ,
202
- 137 : 'eip155:137' ,
203
- 42220 : 'eip155:42220' ,
204
- 10 : 'eip155:10' ,
205
- 250 : 'eip155:250' ,
206
- 11155111 : 'eip155:11155111' ,
207
- 421614 : 'eip155:421614' ,
208
- 56 : 'eip155:56' ,
209
- 59144 : 'eip155:59144' ,
210
- 534352 : 'eip155:534352' ,
211
- 8453 : 'eip155:8453' ,
212
- 1284 : 'eip155:1284' ,
213
- 122 : 'eip155:122' ,
214
- 81457 : 'eip155:81457' ,
215
- 288 : 'eip155:288' ,
216
- 56288 : 'eip155:56288' ,
229
+ async function buildCaip2MappingsFromRegistry ( ) {
230
+ const networks = registry . networks
231
+
232
+ for ( const network of networks ) {
233
+ if ( ! network . aliases ) {
234
+ continue
235
+ }
236
+ for ( const alias of network . aliases ) {
237
+ caip2ByChainAlias [ alias ] = network . caip2Id
238
+ }
239
+ const chainId = parseInt ( network . caip2Id . split ( ':' ) [ 1 ] )
240
+ if (
241
+ typeof chainId === 'number' &&
242
+ ! isNaN ( chainId ) &&
243
+ ! caip2ByChainId [ chainId ] // if we manually set an alias don't overwrite it
244
+ ) {
245
+ caip2ByChainId [ + chainId ] = network . caip2Id
246
+ }
247
+ }
248
+ }
249
+
250
+ /**
251
+ * Unified async initialization needed for the common module.
252
+ * This function should be called once when an application starts.
253
+ * Needed to fetch & construct lookups for the networks registry.
254
+ */
255
+ export async function common_init ( logger : Logger ) {
256
+ await initializeNetworksRegistry ( )
257
+ await buildCaip2MappingsFromRegistry ( )
258
+ logger . debug ( 'Networks Registry loaded' , {
259
+ caip2ByChainAlias,
260
+ caip2ByChainId,
261
+ } )
217
262
}
218
263
219
264
/// Unified entrypoint to resolve CAIP ID based either on chain aliases (strings)
220
265
/// or chain ids (numbers).
221
266
export function resolveChainId ( key : number | string ) : string {
222
267
if ( typeof key === 'number' || ! isNaN ( + key ) ) {
223
268
// If key is a number, then it must be a `chainId`
224
- const chainId = Caip2ByChainId [ + key ]
269
+ const chainId = caip2ByChainId [ + key ]
225
270
if ( chainId !== undefined ) {
226
271
return chainId
227
272
}
228
273
} else if ( typeof key === 'string' ) {
229
274
const splitKey = key . split ( ':' )
230
275
let chainId
231
276
if ( splitKey . length === 2 ) {
232
- chainId = Caip2ByChainId [ + splitKey [ 1 ] ]
277
+ chainId = caip2ByChainId [ + splitKey [ 1 ] ]
233
278
} else {
234
- chainId = Caip2ByChainAlias [ key ]
279
+ chainId = caip2ByChainAlias [ key ]
235
280
}
236
281
if ( chainId !== undefined ) {
237
282
return chainId
@@ -241,8 +286,8 @@ export function resolveChainId(key: number | string): string {
241
286
}
242
287
243
288
export function resolveChainAlias ( id : string ) : string {
244
- const aliasMatches = Object . keys ( Caip2ByChainAlias ) . filter (
245
- ( name ) => Caip2ByChainAlias [ name ] == id ,
289
+ const aliasMatches = Object . keys ( caip2ByChainAlias ) . filter (
290
+ ( name ) => caip2ByChainAlias [ name ] == id ,
246
291
)
247
292
if ( aliasMatches . length === 1 ) {
248
293
return aliasMatches [ 0 ]
0 commit comments