Skip to content

Commit

Permalink
common, agent: use NetworksRegistry instead of hard-coded networks list
Browse files Browse the repository at this point in the history
  • Loading branch information
dwerner committed Feb 13, 2025
1 parent 283ac9b commit 1ab7a5e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 32 deletions.
2 changes: 2 additions & 0 deletions packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SubgraphDeploymentID,
} from '@graphprotocol/common-ts'
import {
common_init,
createIndexerManagementClient,
createIndexerManagementServer,
defineIndexerManagementModels,
Expand Down Expand Up @@ -462,6 +463,7 @@ export async function run(
networkSpecifications: spec.NetworkSpecification[],
logger: Logger,
): Promise<void> {
await common_init(logger)
// --------------------------------------------------------------------------------
// * Configure event listeners for unhandled promise rejections and uncaught
// exceptions.
Expand Down
1 change: 1 addition & 0 deletions packages/indexer-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
104 changes: 72 additions & 32 deletions packages/indexer-common/src/indexer-management/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -166,36 +174,13 @@ export function epochElapsedBlocks(networkEpoch: NetworkEpoch): number {
return networkEpoch.startBlockNumber - networkEpoch.latestBlock
}

export const Caip2ByChainAlias: { [key: string]: string } = {
mainnet: 'eip155:1',
goerli: 'eip155:5',
gnosis: 'eip155:100',
hardhat: 'eip155:1337',
'arbitrum-one': 'eip155:42161',
'arbitrum-goerli': 'eip155:421613',
'arbitrum-sepolia': 'eip155:421614',
avalanche: 'eip155:43114',
matic: 'eip155:137',
celo: 'eip155:42220',
optimism: 'eip155:10',
fantom: 'eip155:250',
sepolia: 'eip155:11155111',
bsc: 'eip155:56',
linea: 'eip155:59144',
scroll: 'eip155:534352',
base: 'eip155:8453',
moonbeam: 'eip155:1284',
fuse: 'eip155:122',
'blast-mainnet': 'eip155:81457',
boba: 'eip155:288',
'boba-bnb': 'eip155:56288',
}

export const Caip2ByChainId: { [key: number]: 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',
1337: 'eip155:1337',
42161: 'eip155:42161',
421613: 'eip155:421613',
43114: 'eip155:43114',
Expand All @@ -215,23 +200,78 @@ export const Caip2ByChainId: { [key: number]: string } = {
288: 'eip155:288',
56288: 'eip155:56288',
}
const caip2ByChainAlias: { [key: string]: string } = {
hardhat: 'eip155:1337',
'arbitrum-one': 'eip155:42161',
'arbitrum-goerli': 'eip155:421613',
'arbitrum-sepolia': 'eip155:421614',
avalanche: 'eip155:43114',
matic: 'eip155:137',
celo: 'eip155:42220',
optimism: 'eip155:10',
fantom: 'eip155:250',
sepolia: 'eip155:11155111',
bsc: 'eip155:56',
linea: 'eip155:59144',
scroll: 'eip155:534352',
base: 'eip155:8453',
moonbeam: 'eip155:1284',
fuse: 'eip155:122',
'blast-mainnet': 'eip155:81457',
boba: 'eip155:288',
'boba-bnb': '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)
/// or chain ids (numbers).
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
}
} else if (typeof key === '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
Expand All @@ -241,8 +281,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]
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,11 @@
node-addon-api "^3.2.1"
node-gyp-build "^4.3.0"

"@pinax/[email protected]":
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"
Expand Down

0 comments on commit 1ab7a5e

Please sign in to comment.