From 51e2074390a8b52bbff2955f9ab22b0c03ee02a6 Mon Sep 17 00:00:00 2001 From: tilacog Date: Mon, 24 Jul 2023 15:18:18 -0300 Subject: [PATCH] common: add convenience functions to detect L1 and L2 networks --- .../__tests__/helpers.test.ts | 50 +++++++++++++++++++ .../src/indexer-management/types.ts | 14 ++++++ 2 files changed, 64 insertions(+) diff --git a/packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts b/packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts index a687ccab8..a80cc9a7c 100644 --- a/packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts +++ b/packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts @@ -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' @@ -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() + }, + ) +}) diff --git a/packages/indexer-common/src/indexer-management/types.ts b/packages/indexer-common/src/indexer-management/types.ts index 527767d9b..5630b06f2 100644 --- a/packages/indexer-common/src/indexer-management/types.ts +++ b/packages/indexer-common/src/indexer-management/types.ts @@ -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' +}