|
| 1 | +import { loadReferenceData, Version } from "@config/data/ccip/index.ts" |
| 2 | +import type { ChainConfig } from "@config/data/ccip/types.ts" |
| 3 | +import { getSelectorEntry } from "@config/data/ccip/selectors.ts" |
| 4 | +import { getChainId, getChainTypeAndFamily, directoryToSupportedChain } from "~/features/utils/index.ts" |
| 5 | +import { Environment } from "~/lib/ccip/types/index.ts" |
| 6 | +import { logger } from "@lib/logging/index.js" |
| 7 | + |
| 8 | +/** |
| 9 | + * Naming convention for chain identifiers |
| 10 | + * - 'directory': chains.json keys (e.g., "mainnet", "bsc-mainnet") |
| 11 | + * - 'selector': selectors.yml names (e.g., "ethereum-mainnet", "binance_smart_chain-mainnet") |
| 12 | + */ |
| 13 | +export type NamingConvention = "directory" | "selector" |
| 14 | + |
| 15 | +/** |
| 16 | + * Result of resolving a chain identifier |
| 17 | + */ |
| 18 | +export interface ResolvedChain { |
| 19 | + directoryKey: string // Key in chains.json (for internal data lookups) |
| 20 | + selectorName: string // Name in selectors.yml (canonical form) |
| 21 | + inputConvention: NamingConvention // Which convention was used in the input |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Service for handling chain identifier resolution and formatting. |
| 26 | + * Supports bidirectional mapping between directory keys and selector names. |
| 27 | + * |
| 28 | + * This enables the API to: |
| 29 | + * 1. Accept both naming conventions as input |
| 30 | + * 2. Mirror the user's chosen convention in responses |
| 31 | + * 3. Maintain backward compatibility (default to selector names) |
| 32 | + */ |
| 33 | +export class ChainIdentifierService { |
| 34 | + private directoryToSelector: Map<string, string> = new Map() |
| 35 | + private selectorToDirectory: Map<string, string> = new Map() |
| 36 | + private directoryKeys: Set<string> = new Set() |
| 37 | + private readonly requestId: string |
| 38 | + |
| 39 | + constructor( |
| 40 | + private readonly environment: Environment, |
| 41 | + private readonly defaultConvention: NamingConvention = "selector" |
| 42 | + ) { |
| 43 | + this.requestId = crypto.randomUUID() |
| 44 | + this.buildMappings() |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Build bidirectional mappings between directory keys and selector names |
| 49 | + */ |
| 50 | + private buildMappings(): void { |
| 51 | + const { chainsReferenceData } = loadReferenceData({ |
| 52 | + environment: this.environment, |
| 53 | + version: Version.V1_2_0, |
| 54 | + }) |
| 55 | + |
| 56 | + for (const [directoryKey, chainConfig] of Object.entries(chainsReferenceData as Record<string, ChainConfig>)) { |
| 57 | + this.directoryKeys.add(directoryKey) |
| 58 | + |
| 59 | + try { |
| 60 | + // Get chain ID and type to look up the selector entry |
| 61 | + const supportedChain = directoryToSupportedChain(directoryKey) |
| 62 | + const chainId = getChainId(supportedChain) |
| 63 | + const { chainType } = getChainTypeAndFamily(supportedChain) |
| 64 | + |
| 65 | + if (chainId) { |
| 66 | + const selectorEntry = getSelectorEntry(chainId, chainType) |
| 67 | + if (selectorEntry?.name) { |
| 68 | + const selectorName = selectorEntry.name |
| 69 | + |
| 70 | + // Only add mapping if names are different |
| 71 | + if (selectorName !== directoryKey) { |
| 72 | + this.directoryToSelector.set(directoryKey, selectorName) |
| 73 | + this.selectorToDirectory.set(selectorName, directoryKey) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } catch { |
| 78 | + // Skip chains that can't be resolved |
| 79 | + logger.debug({ |
| 80 | + message: "Could not resolve chain for mapping", |
| 81 | + requestId: this.requestId, |
| 82 | + directoryKey, |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + logger.debug({ |
| 88 | + message: "Chain identifier mappings built", |
| 89 | + requestId: this.requestId, |
| 90 | + mappingCount: this.directoryToSelector.size, |
| 91 | + directoryKeyCount: this.directoryKeys.size, |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Check if an identifier is a directory key (chains.json key) |
| 97 | + */ |
| 98 | + isDirectoryKey(identifier: string): boolean { |
| 99 | + return this.directoryKeys.has(identifier) |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Check if an identifier is a selector name (selectors.yml name) |
| 104 | + */ |
| 105 | + isSelectorName(identifier: string): boolean { |
| 106 | + // It's a selector name if: |
| 107 | + // 1. It maps to a directory key, OR |
| 108 | + // 2. It's a directory key that has no different selector name (they're the same) |
| 109 | + return this.selectorToDirectory.has(identifier) || this.directoryKeys.has(identifier) |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Resolve a chain identifier to both directory key and selector name. |
| 114 | + * Detects which convention was used in the input. |
| 115 | + * |
| 116 | + * @param identifier - Chain identifier (directory key or selector name) |
| 117 | + * @returns Resolved chain info or null if not found |
| 118 | + */ |
| 119 | + resolve(identifier: string): ResolvedChain | null { |
| 120 | + // Check if it's a directory key |
| 121 | + if (this.directoryKeys.has(identifier)) { |
| 122 | + const selectorName = this.directoryToSelector.get(identifier) ?? identifier |
| 123 | + return { |
| 124 | + directoryKey: identifier, |
| 125 | + selectorName, |
| 126 | + inputConvention: "directory", |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Check if it's a selector name that maps to a directory key |
| 131 | + if (this.selectorToDirectory.has(identifier)) { |
| 132 | + const directoryKey = this.selectorToDirectory.get(identifier)! |
| 133 | + return { |
| 134 | + directoryKey, |
| 135 | + selectorName: identifier, |
| 136 | + inputConvention: "selector", |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Not found |
| 141 | + return null |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Format a directory key using the specified naming convention. |
| 146 | + * |
| 147 | + * @param directoryKey - The chains.json key |
| 148 | + * @param convention - Which format to output |
| 149 | + * @returns Formatted identifier |
| 150 | + */ |
| 151 | + format(directoryKey: string, convention: NamingConvention): string { |
| 152 | + if (convention === "directory") { |
| 153 | + return directoryKey |
| 154 | + } |
| 155 | + |
| 156 | + // Return selector name, or directory key if no mapping exists |
| 157 | + return this.directoryToSelector.get(directoryKey) ?? directoryKey |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Detect the naming convention from a list of identifiers. |
| 162 | + * Returns the convention of the first resolvable identifier. |
| 163 | + * |
| 164 | + * @param identifiers - List of identifiers to check |
| 165 | + * @returns Detected convention or default |
| 166 | + */ |
| 167 | + detectConvention(...identifiers: (string | undefined)[]): NamingConvention { |
| 168 | + for (const identifier of identifiers) { |
| 169 | + if (!identifier) continue |
| 170 | + |
| 171 | + const resolved = this.resolve(identifier) |
| 172 | + if (resolved) { |
| 173 | + return resolved.inputConvention |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return this.defaultConvention |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Get the default naming convention |
| 182 | + */ |
| 183 | + getDefaultConvention(): NamingConvention { |
| 184 | + return this.defaultConvention |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Get the directory key for a given identifier (either format) |
| 189 | + * This is useful for internal data lookups |
| 190 | + */ |
| 191 | + getDirectoryKey(identifier: string): string | null { |
| 192 | + const resolved = this.resolve(identifier) |
| 193 | + return resolved?.directoryKey ?? null |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Get the selector name for a given identifier (either format) |
| 198 | + */ |
| 199 | + getSelectorName(identifier: string): string | null { |
| 200 | + const resolved = this.resolve(identifier) |
| 201 | + return resolved?.selectorName ?? null |
| 202 | + } |
| 203 | +} |
0 commit comments