@@ -14,6 +14,13 @@ let cachedHyperliquidChainId: number | null = null;
1414let cachedLiFiChains : ExtendedChain [ ] | null = null ;
1515let cachedHyperliquidTokens : LiFiToken [ ] | null = null ;
1616
17+ // Reset cache (for testing)
18+ export function resetLiFiCache ( ) : void {
19+ cachedHyperliquidChainId = null ;
20+ cachedLiFiChains = null ;
21+ cachedHyperliquidTokens = null ;
22+ }
23+
1724export async function discoverHyperliquidChainId ( ) : Promise < number > {
1825 if ( cachedHyperliquidChainId !== null ) {
1926 return cachedHyperliquidChainId ;
@@ -23,30 +30,41 @@ export async function discoverHyperliquidChainId(): Promise<number> {
2330 const chains = await getChains ( ) ;
2431 cachedLiFiChains = chains ;
2532
26- // Search for Hyperliquid with various possible identifiers
27- const hyperliquid = chains . find ( c =>
33+ // IMPORTANT: Always use chain ID 999 for HyperEVM mainnet
34+ // The LI.FI SDK incorrectly returns 1337 for "Hyperliquid" name, but 999 is the correct mainnet ID
35+ // Priority: 1) Chain ID 999, 2) Chain ID from our config, 3) Name-based search
36+
37+ // First, explicitly look for chain ID 999 (HyperEVM mainnet)
38+ const byCorrectId = chains . find ( c => c . id === 999 ) ;
39+ if ( byCorrectId ) {
40+ cachedHyperliquidChainId = 999 ;
41+ return 999 ;
42+ }
43+
44+ // Fallback to our configured chain ID
45+ const byConfigId = chains . find ( c => c . id === HYPERLIQUID_CHAIN_ID ) ;
46+ if ( byConfigId ) {
47+ cachedHyperliquidChainId = HYPERLIQUID_CHAIN_ID ;
48+ return HYPERLIQUID_CHAIN_ID ;
49+ }
50+
51+ // Last resort: name-based search (but verify the ID is correct)
52+ const byName = chains . find ( c =>
2853 c . name . toLowerCase ( ) === 'hyperliquid' ||
2954 c . name . toLowerCase ( ) . includes ( 'hyperliquid' ) ||
3055 c . name . toLowerCase ( ) . includes ( 'hyperevm' ) ||
3156 c . key ?. toLowerCase ( ) === 'hyperliquid' ||
3257 c . key ?. toLowerCase ( ) === 'hyp' ||
33- c . key ?. toLowerCase ( ) === 'hpl' ||
34- c . id === HYPERLIQUID_CHAIN_ID ||
35- c . id === 999
58+ c . key ?. toLowerCase ( ) === 'hpl'
3659 ) ;
3760
38- if ( hyperliquid ) {
39- cachedHyperliquidChainId = hyperliquid . id ;
40- return hyperliquid . id ;
41- }
42-
43- // If not found by name, try searching by chain ID 999 (mainnet)
44- const byId = chains . find ( c => c . id === 999 ) ;
45- if ( byId ) {
46- cachedHyperliquidChainId = byId . id ;
47- return byId . id ;
61+ // If found by name, use it only if the ID matches expected values
62+ if ( byName && ( byName . id === 999 || byName . id === HYPERLIQUID_CHAIN_ID ) ) {
63+ cachedHyperliquidChainId = byName . id ;
64+ return byName . id ;
4865 }
4966
67+ // Default to our known correct chain ID
5068 cachedHyperliquidChainId = HYPERLIQUID_CHAIN_ID ;
5169 return HYPERLIQUID_CHAIN_ID ;
5270 } catch {
0 commit comments