Skip to content

Commit 860aafb

Browse files
committed
common, agent: use NetworksRegistry instead of hard-coded networks list
1 parent 4b1726f commit 860aafb

File tree

4 files changed

+82
-29
lines changed

4 files changed

+82
-29
lines changed

packages/indexer-agent/src/commands/start.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
SubgraphDeploymentID,
1212
} from '@graphprotocol/common-ts'
1313
import {
14+
common_init,
1415
createIndexerManagementClient,
1516
createIndexerManagementServer,
1617
defineIndexerManagementModels,
@@ -467,6 +468,7 @@ export async function run(
467468
networkSpecification: spec.NetworkSpecification,
468469
logger: Logger,
469470
): Promise<void> {
471+
await common_init(logger)
470472
// --------------------------------------------------------------------------------
471473
// * Configure event listeners for unhandled promise rejections and uncaught
472474
// exceptions.

packages/indexer-common/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"clean": "rm -rf ./node_modules ./dist ./tsconfig.tsbuildinfo"
2323
},
2424
"dependencies": {
25+
"@pinax/graph-networks-registry": "0.6.7",
2526
"@graphprotocol/common-ts": "2.0.11",
2627
"@graphprotocol/cost-model": "0.1.18",
2728
"@semiotic-labs/tap-contracts-bindings": "^1.2.1",

packages/indexer-common/src/indexer-management/types.ts

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import { Allocation } from '../allocations'
99
import { GraphNode } from '../graph-node'
1010
import { SubgraphDeployment } from '../types'
1111

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+
1220
export interface CreateAllocationResult {
1321
actionID: number
1422
type: 'allocate'
@@ -166,7 +174,34 @@ export function epochElapsedBlocks(networkEpoch: NetworkEpoch): number {
166174
return networkEpoch.startBlockNumber - networkEpoch.latestBlock
167175
}
168176

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 } = {
170205
mainnet: 'eip155:1',
171206
goerli: 'eip155:5',
172207
gnosis: 'eip155:100',
@@ -191,47 +226,57 @@ export const Caip2ByChainAlias: { [key: string]: string } = {
191226
'boba-bnb': 'eip155:56288',
192227
}
193228

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+
})
217262
}
218263

219264
/// Unified entrypoint to resolve CAIP ID based either on chain aliases (strings)
220265
/// or chain ids (numbers).
221266
export function resolveChainId(key: number | string): string {
222267
if (typeof key === 'number' || !isNaN(+key)) {
223268
// If key is a number, then it must be a `chainId`
224-
const chainId = Caip2ByChainId[+key]
269+
const chainId = caip2ByChainId[+key]
225270
if (chainId !== undefined) {
226271
return chainId
227272
}
228273
} else if (typeof key === 'string') {
229274
const splitKey = key.split(':')
230275
let chainId
231276
if (splitKey.length === 2) {
232-
chainId = Caip2ByChainId[+splitKey[1]]
277+
chainId = caip2ByChainId[+splitKey[1]]
233278
} else {
234-
chainId = Caip2ByChainAlias[key]
279+
chainId = caip2ByChainAlias[key]
235280
}
236281
if (chainId !== undefined) {
237282
return chainId
@@ -241,8 +286,8 @@ export function resolveChainId(key: number | string): string {
241286
}
242287

243288
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,
246291
)
247292
if (aliasMatches.length === 1) {
248293
return aliasMatches[0]

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,11 @@
22212221
node-addon-api "^3.2.1"
22222222
node-gyp-build "^4.3.0"
22232223

2224+
2225+
version "0.6.7"
2226+
resolved "https://registry.yarnpkg.com/@pinax/graph-networks-registry/-/graph-networks-registry-0.6.7.tgz#ceb994f3b31e2943b9c9d9b09dd86eb00d067c0e"
2227+
integrity sha512-xogeCEZ50XRMxpBwE3TZjJ8RCO8Guv39gDRrrKtlpDEDEMLm0MzD3A0SQObgj7aF7qTZNRTWzsuvQdxgzw25wQ==
2228+
22242229
"@pkgjs/parseargs@^0.11.0":
22252230
version "0.11.0"
22262231
resolved "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz"

0 commit comments

Comments
 (0)