From 5e72b04ab91396edcefc528b2987cd10be7d00fe Mon Sep 17 00:00:00 2001 From: Daniel Werner Date: Thu, 13 Feb 2025 11:57:36 -0800 Subject: [PATCH] common, agent: use NetworksRegistry instead of hard-coded networks list --- packages/indexer-agent/src/commands/start.ts | 2 + packages/indexer-common/package.json | 1 + .../src/indexer-management/types.ts | 103 +++++++++++++----- yarn.lock | 5 + 4 files changed, 82 insertions(+), 29 deletions(-) diff --git a/packages/indexer-agent/src/commands/start.ts b/packages/indexer-agent/src/commands/start.ts index a4453f3de..8ffd97fbf 100644 --- a/packages/indexer-agent/src/commands/start.ts +++ b/packages/indexer-agent/src/commands/start.ts @@ -11,6 +11,7 @@ import { SubgraphDeploymentID, } from '@graphprotocol/common-ts' import { + common_init, createIndexerManagementClient, createIndexerManagementServer, defineIndexerManagementModels, @@ -467,6 +468,7 @@ export async function run( networkSpecification: spec.NetworkSpecification, logger: Logger, ): Promise { + await common_init(logger) // -------------------------------------------------------------------------------- // * Configure event listeners for unhandled promise rejections and uncaught // exceptions. diff --git a/packages/indexer-common/package.json b/packages/indexer-common/package.json index 76cd5b00c..22a566d75 100644 --- a/packages/indexer-common/package.json +++ b/packages/indexer-common/package.json @@ -22,6 +22,7 @@ "clean": "rm -rf ./node_modules ./dist ./tsconfig.tsbuildinfo" }, "dependencies": { + "@pinax/graph-networks-registry": "0.6.7", "@graphprotocol/common-ts": "2.0.11", "@graphprotocol/cost-model": "0.1.18", "@semiotic-labs/tap-contracts-bindings": "^1.2.1", diff --git a/packages/indexer-common/src/indexer-management/types.ts b/packages/indexer-common/src/indexer-management/types.ts index cf801f4cb..7b4ec8c59 100644 --- a/packages/indexer-common/src/indexer-management/types.ts +++ b/packages/indexer-common/src/indexer-management/types.ts @@ -9,6 +9,14 @@ import { Allocation } from '../allocations' import { GraphNode } from '../graph-node' import { SubgraphDeployment } from '../types' +/* eslint-disable @typescript-eslint/no-explicit-any */ +let registry: any +async function initializeNetworksRegistry() { + // Dynamically import NetworksRegistry + const { NetworksRegistry } = await import('@pinax/graph-networks-registry') + registry = await NetworksRegistry.fromLatestVersion() +} + export interface CreateAllocationResult { actionID: number type: 'allocate' @@ -166,7 +174,34 @@ export function epochElapsedBlocks(networkEpoch: NetworkEpoch): number { return networkEpoch.startBlockNumber - networkEpoch.latestBlock } -export const Caip2ByChainAlias: { [key: string]: string } = { +// Construct Caip2ByChainId from the registry data, keeping the manual +// overrides for backward compatibility +const caip2ByChainId: { [key: number]: string } = { + 1337: 'eip155:1337', + 1: 'eip155:1', + 5: 'eip155:5', + 100: 'eip155:100', + 42161: 'eip155:42161', + 421613: 'eip155:421613', + 43114: 'eip155:43114', + 137: 'eip155:137', + 42220: 'eip155:42220', + 10: 'eip155:10', + 250: 'eip155:250', + 11155111: 'eip155:11155111', + 421614: 'eip155:421614', + 56: 'eip155:56', + 59144: 'eip155:59144', + 534352: 'eip155:534352', + 8453: 'eip155:8453', + 1284: 'eip155:1284', + 122: 'eip155:122', + 81457: 'eip155:81457', + 288: 'eip155:288', + 56288: 'eip155:56288', +} + +const caip2ByChainAlias: { [key: string]: string } = { mainnet: 'eip155:1', goerli: 'eip155:5', gnosis: 'eip155:100', @@ -191,29 +226,39 @@ export const Caip2ByChainAlias: { [key: string]: string } = { 'boba-bnb': 'eip155:56288', } -export const Caip2ByChainId: { [key: number]: string } = { - 1: 'eip155:1', - 5: 'eip155:5', - 100: 'eip155:100', - 1337: 'eip155:1337', - 42161: 'eip155:42161', - 421613: 'eip155:421613', - 43114: 'eip155:43114', - 137: 'eip155:137', - 42220: 'eip155:42220', - 10: 'eip155:10', - 250: 'eip155:250', - 11155111: 'eip155:11155111', - 421614: 'eip155:421614', - 56: 'eip155:56', - 59144: 'eip155:59144', - 534352: 'eip155:534352', - 8453: 'eip155:8453', - 1284: 'eip155:1284', - 122: 'eip155:122', - 81457: 'eip155:81457', - 288: 'eip155:288', - 56288: 'eip155:56288', +async function buildCaip2MappingsFromRegistry() { + const networks = registry.networks + + for (const network of networks) { + if (!network.aliases) { + continue + } + for (const alias of network.aliases) { + caip2ByChainAlias[alias] = network.caip2Id + } + const chainId = parseInt(network.caip2Id.split(':')[1]) + if ( + typeof chainId === 'number' && + !isNaN(chainId) && + !caip2ByChainId[chainId] // if we manually set an alias don't overwrite it + ) { + caip2ByChainId[+chainId] = network.caip2Id + } + } +} + +/** + * Unified async initialization needed for the common module. + * This function should be called once when an application starts. + * Needed to fetch & construct lookups for the networks registry. + */ +export async function common_init(logger: Logger) { + await initializeNetworksRegistry() + await buildCaip2MappingsFromRegistry() + logger.debug('Networks Registry loaded', { + caip2ByChainAlias, + caip2ByChainId, + }) } /// Unified entrypoint to resolve CAIP ID based either on chain aliases (strings) @@ -221,7 +266,7 @@ export const Caip2ByChainId: { [key: number]: string } = { export function resolveChainId(key: number | string): string { if (typeof key === 'number' || !isNaN(+key)) { // If key is a number, then it must be a `chainId` - const chainId = Caip2ByChainId[+key] + const chainId = caip2ByChainId[+key] if (chainId !== undefined) { return chainId } @@ -229,9 +274,9 @@ export function resolveChainId(key: number | string): string { const splitKey = key.split(':') let chainId if (splitKey.length === 2) { - chainId = Caip2ByChainId[+splitKey[1]] + chainId = caip2ByChainId[+splitKey[1]] } else { - chainId = Caip2ByChainAlias[key] + chainId = caip2ByChainAlias[key] } if (chainId !== undefined) { return chainId @@ -241,8 +286,8 @@ export function resolveChainId(key: number | string): string { } export function resolveChainAlias(id: string): string { - const aliasMatches = Object.keys(Caip2ByChainAlias).filter( - (name) => Caip2ByChainAlias[name] == id, + const aliasMatches = Object.keys(caip2ByChainAlias).filter( + (name) => caip2ByChainAlias[name] == id, ) if (aliasMatches.length === 1) { return aliasMatches[0] diff --git a/yarn.lock b/yarn.lock index 3443e3843..a54c11083 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2221,6 +2221,11 @@ node-addon-api "^3.2.1" node-gyp-build "^4.3.0" +"@pinax/graph-networks-registry@0.6.7": + version "0.6.7" + resolved "https://registry.yarnpkg.com/@pinax/graph-networks-registry/-/graph-networks-registry-0.6.7.tgz#ceb994f3b31e2943b9c9d9b09dd86eb00d067c0e" + integrity sha512-xogeCEZ50XRMxpBwE3TZjJ8RCO8Guv39gDRrrKtlpDEDEMLm0MzD3A0SQObgj7aF7qTZNRTWzsuvQdxgzw25wQ== + "@pkgjs/parseargs@^0.11.0": version "0.11.0" resolved "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz"