Skip to content

CAIP-2 chain IDs should be defined as constants #12481

@gibson042

Description

@gibson042

(originally posted by @gibson042 in #12396 (comment))

This pattern [an object keyed by CAIP-2 chain IDs] keeps coming up, and I like the ability to associate specific links with the data but not the repetition of magic strings. I think a future PR should refactor, e.g.

import {
  CaipChainIds as CHAIN_IDS_BY_CLUSTER,
} from '@agoric/portfolio-api/src/constants.js';

const { entries, fromEntries, values } = Object;

/** XXX this probably belongs in @agoric/orchestration */
const CHAIN_IDS: Record<string, CaipChainId> = {
  // mainnet
  ARBITRUM: 'eip155:42161',
  AVALANCHE: 'eip155:43114',
  BASE: 'eip155:8453',
  ETHEREUM: 'eip155:1',
  OPTIMISM: 'eip155:10',
  AGORIC: 'cosmos:agoric-3',
  NOBLE: 'cosmos:noble-1',
  // testnet
  ARBITRUM_SEPOLIA: 'eip155:421614',
  AVALANCHE_FUJI: 'eip155:43113',
  BASE_SEPOLIA: 'eip155:84532',
  ETHEREUM_SEPOLIA: 'eip155:11155111',
  OPTIMISM_SEPOLIA: 'eip155:11155420',
  AGORIC_DEVNET: 'cosmos:agoricdev-25',
  NOBLE_TESTNET: 'cosmos:grand-1',
};
{
  // Cross-check
  const allChainIds = new Set(
    values(CHAIN_IDS_BY_CLUSTER).flatMap(cluster => values(cluster)),
  );
  const unknownChains = entries(CHAIN_IDS).filter(
    ([_name, chainId]) => !allChainIds.has(chainId),
  );
  if (unknownChains.length) {
    throw Error(
      `unknown CAIP-2 chain ids: ${JSON.stringify(fromEntries(unknownChains))}`,
    );
  }
}

const traceFilterSupportedChains: ReadonlySet<CaipChainId> = new Set([
  // mainnet
  CHAIN_IDS.BASE,
  CHAIN_IDS.ETHEREUM,
  CHAIN_IDS.OPTIMISM,
  // testnet
  CHAIN_IDS.BASE_SEPOLIA,
  CHAIN_IDS.ETHEREUM_SEPOLIA,
  CHAIN_IDS.OPTIMISM_SEPOLIA
]);

(and likewise for similar constants)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions