Skip to content

Commit

Permalink
common: add convenience functions to detect L1 and L2 networks
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Jul 24, 2023
1 parent 939e5ee commit 51e2074
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
IndexingRuleAttributes,
} from '../models'
import { specification as spec } from '../../index'
import { networkIsL1, networkIsL2 } from '../types'
import { fetchIndexingRules, upsertIndexingRule } from '../rules'
import { SubgraphIdentifierType } from '../../subgraphs'
import { ActionManager } from '../actions'
Expand Down Expand Up @@ -329,3 +330,52 @@ describe.skip('Monitor', () => {
).rejects.toEqual(indexerError(IndexerErrorCode.IE018, `Could not resolve POI`))
})
})

describe('Network layer detection', () => {
interface NetworkLayer {
name: string
l1: boolean
l2: boolean
}

// Should be true for L1 and false for L2
const l1Networks: NetworkLayer[] = ['mainnet', 'eip155:1', 'goerli', 'eip155:5'].map(
(name: string) => ({ name, l1: true, l2: false }),
)

// Should be false for L1 and true for L2
const l2Networks: NetworkLayer[] = [
'arbitrum-one',
'eip155:42161',
'arbitrum-goerli',
'eip155:421613',
].map((name: string) => ({ name, l1: false, l2: true }))

// Those will be false for L1 and L2
const nonProtocolNetworks: NetworkLayer[] = [
'fantom',
'eip155:250',
'hardhat',
'eip155:1337',
'matic',
'eip155:137',
'gnosis',
'eip155:100',
].map((name: string) => ({ name, l1: false, l2: false }))

const testCases = [...l1Networks, ...l2Networks, ...nonProtocolNetworks]

test.each(testCases)('Can detect network layer [$name]', (network) => {
expect(networkIsL1(network.name)).toStrictEqual(network.l1)
expect(networkIsL2(network.name)).toStrictEqual(network.l2)
})

const invalidTProtocolNetworkNames = ['invalid-name', 'eip155:9999']

test.each(invalidTProtocolNetworkNames)(
'Throws error when protocol network is unknown [%s]',
(invalidProtocolNetworkName) => {
expect(() => networkIsL1(invalidProtocolNetworkName)).toThrow()
},
)
})
14 changes: 14 additions & 0 deletions packages/indexer-common/src/indexer-management/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,17 @@ export async function validateProviderNetworkIdentifier(
throw new Error(errorMsg)
}
}

// Convenience function to check if a given network identifier is a supported Layer-1 protocol network
export function networkIsL1(networkIdentifier: string): boolean {
// Normalize network identifier
networkIdentifier = resolveChainId(networkIdentifier)
return networkIdentifier === 'eip155:1' || networkIdentifier === 'eip155:5'
}

// Convenience function to check if a given network identifier is a supported Layer-2 protocol network
export function networkIsL2(networkIdentifier: string): boolean {
// Normalize network identifier
networkIdentifier = resolveChainId(networkIdentifier)
return networkIdentifier === 'eip155:42161' || networkIdentifier === 'eip155:421613'
}

0 comments on commit 51e2074

Please sign in to comment.