Skip to content

Commit 739c343

Browse files
committed
feat: support both chains.json keys and selector names in API
Add bidirectional mapping to accept both naming conventions: - chains.json keys: mainnet, bsc-mainnet - selector names: ethereum-mainnet, binance_smart_chain-mainnet This enables gradual migration to consistent naming while maintaining backward compatibility with existing URLs. Updated: - LaneDataService.resolveToInternalId: accepts both formats - RateLimitsDataService.getFilteredRateLimits: resolves identifiers before lookup
1 parent 05bcbd0 commit 739c343

2 files changed

Lines changed: 81 additions & 5 deletions

File tree

src/lib/ccip/services/lane-data.ts

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,21 +524,69 @@ export class LaneDataService {
524524
}
525525

526526
/**
527-
* Resolves a chain identifier to its internal ID
527+
* Builds a mapping from selector names (e.g., "ethereum-mainnet") to chains.json keys (e.g., "mainnet")
528+
* This enables the API to accept both naming conventions.
529+
*
530+
* @param chainsReferenceData - Chain configuration data
531+
* @returns Map of selector name → chains.json key
532+
*/
533+
private buildSelectorNameToChainKeyMap(chainsReferenceData: Record<string, ChainConfig>): Map<string, string> {
534+
const map = new Map<string, string>()
535+
536+
for (const [chainKey, chainConfig] of Object.entries(chainsReferenceData)) {
537+
try {
538+
// Get the chain ID and type to look up the selector entry
539+
const supportedChain = directoryToSupportedChain(chainKey)
540+
const chainId = getChainId(supportedChain)
541+
const { chainType } = getChainTypeAndFamily(supportedChain)
542+
543+
if (chainId) {
544+
const selectorEntry = getSelectorEntry(chainId, chainType)
545+
if (selectorEntry?.name && selectorEntry.name !== chainKey) {
546+
// Map selector name to chains.json key
547+
map.set(selectorEntry.name, chainKey)
548+
}
549+
}
550+
} catch {
551+
// Skip chains that can't be resolved
552+
}
553+
}
554+
555+
return map
556+
}
557+
558+
/**
559+
* Resolves a chain identifier to its internal ID (chains.json key)
560+
*
561+
* Accepts both:
562+
* - chains.json keys (e.g., "mainnet", "bsc-mainnet")
563+
* - selector names (e.g., "ethereum-mainnet", "binance_smart_chain-mainnet")
528564
*
529565
* @param identifier - Chain identifier (chainId, selector, or internalId)
530566
* @param inputKeyType - Type of identifier
531567
* @param chainsReferenceData - Chain configuration data
532-
* @returns Internal ID or null if not found
568+
* @returns Internal ID (chains.json key) or null if not found
533569
*/
534570
resolveToInternalId(
535571
identifier: string,
536572
inputKeyType: LaneInputKeyType,
537573
chainsReferenceData: Record<string, ChainConfig>
538574
): string | null {
539-
// If already an internal_id, return it directly
575+
// If already an internal_id, check both chains.json key and selector name
540576
if (inputKeyType === "internalId") {
541-
return chainsReferenceData[identifier] ? identifier : null
577+
// First, try direct lookup in chains.json keys
578+
if (chainsReferenceData[identifier]) {
579+
return identifier
580+
}
581+
582+
// If not found, check if identifier is a selector name and map to chains.json key
583+
const selectorNameMap = this.buildSelectorNameToChainKeyMap(chainsReferenceData)
584+
const chainKey = selectorNameMap.get(identifier)
585+
if (chainKey && chainsReferenceData[chainKey]) {
586+
return chainKey
587+
}
588+
589+
return null
542590
}
543591

544592
// Search through chains to find matching chain_id or selector

src/lib/ccip/services/rate-limits-data.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,39 @@ export class RateLimitsDataService {
6060
})
6161

6262
try {
63+
// Load reference data to resolve chain identifiers
64+
const { chainsReferenceData } = loadReferenceData({
65+
environment,
66+
version: Version.V1_2_0,
67+
})
68+
69+
// Resolve chain identifiers to chains.json keys (handles both short names and selector names)
70+
const laneDataService = new LaneDataService()
71+
const resolvedSourceId =
72+
laneDataService.resolveToInternalId(
73+
filters.sourceInternalId,
74+
"internalId",
75+
chainsReferenceData as Record<string, ChainConfig>
76+
) || filters.sourceInternalId
77+
const resolvedDestId =
78+
laneDataService.resolveToInternalId(
79+
filters.destinationInternalId,
80+
"internalId",
81+
chainsReferenceData as Record<string, ChainConfig>
82+
) || filters.destinationInternalId
83+
84+
// Create resolved filters
85+
const resolvedFilters: RateLimitsFilterType = {
86+
...filters,
87+
sourceInternalId: resolvedSourceId,
88+
destinationInternalId: resolvedDestId,
89+
}
90+
6391
// Load the appropriate rate limits data based on environment
6492
const rateLimitsData = this.loadRateLimitsData(environment)
6593

6694
// Extract rate limits for the specified lane
67-
const result = this.extractLaneRateLimits(rateLimitsData, filters)
95+
const result = this.extractLaneRateLimits(rateLimitsData, resolvedFilters)
6896

6997
const tokenCount = Object.keys(result).length
7098

0 commit comments

Comments
 (0)