|
| 1 | +import type { ChainFamily, NetworkInfo } from './types' |
| 2 | + |
| 3 | +export interface GetNetworkOptions { |
| 4 | + chainSelector?: bigint |
| 5 | + chainSelectorName?: string |
| 6 | + isTestnet?: boolean | undefined |
| 7 | + chainFamily?: ChainFamily |
| 8 | +} |
| 9 | + |
| 10 | +export type NetworkMapBySelector = Map<bigint, NetworkInfo> |
| 11 | +export type NetworkMapByName = Map<string, NetworkInfo> |
| 12 | +export type NetworkFamilyMapBySelector = Record<ChainFamily, NetworkMapBySelector> |
| 13 | +export type NetworkFamilyMapByName = Record<ChainFamily, NetworkMapByName> |
| 14 | + |
| 15 | +export interface NetworkMaps { |
| 16 | + mainnetByName: NetworkMapByName |
| 17 | + mainnetByNameByFamily: NetworkFamilyMapByName |
| 18 | + mainnetBySelector: NetworkMapBySelector |
| 19 | + mainnetBySelectorByFamily: NetworkFamilyMapBySelector |
| 20 | + testnetByName: NetworkMapByName |
| 21 | + testnetByNameByFamily: NetworkFamilyMapByName |
| 22 | + testnetBySelector: NetworkMapBySelector |
| 23 | + testnetBySelectorByFamily: NetworkFamilyMapBySelector |
| 24 | +} |
| 25 | + |
| 26 | +export class NetworkLookup { |
| 27 | + constructor(private maps: NetworkMaps) {} |
| 28 | + |
| 29 | + /** |
| 30 | + * High-performance network lookup using Maps for O(1) performance |
| 31 | + * @param options - Search criteria |
| 32 | + * @returns NetworkInfo if found, undefined otherwise |
| 33 | + */ |
| 34 | + find(options: GetNetworkOptions): NetworkInfo | undefined { |
| 35 | + const { chainSelector, chainSelectorName, isTestnet, chainFamily } = options |
| 36 | + |
| 37 | + const getBySelector = (map: Map<bigint, NetworkInfo>) => { |
| 38 | + if (chainSelector === undefined) return undefined |
| 39 | + return map.get(chainSelector) |
| 40 | + } |
| 41 | + |
| 42 | + // Validate input - need either chainSelector or chainSelectorName |
| 43 | + if (!chainSelector && !chainSelectorName) { |
| 44 | + return undefined |
| 45 | + } |
| 46 | + |
| 47 | + // If both chainFamily and network type are specified, use the most specific maps |
| 48 | + if (chainFamily && chainSelector !== undefined) { |
| 49 | + if (isTestnet === false) { |
| 50 | + return getBySelector(this.maps.mainnetBySelectorByFamily[chainFamily]) |
| 51 | + } |
| 52 | + |
| 53 | + if (isTestnet === true) { |
| 54 | + return getBySelector(this.maps.testnetBySelectorByFamily[chainFamily]) |
| 55 | + } |
| 56 | + |
| 57 | + // If user haven't defined if it's testnet or not we can try all networks, starting from testnet |
| 58 | + let network = getBySelector(this.maps.testnetBySelectorByFamily[chainFamily]) |
| 59 | + if (!network) { |
| 60 | + network = getBySelector(this.maps.mainnetBySelectorByFamily[chainFamily]) |
| 61 | + } |
| 62 | + return network |
| 63 | + } |
| 64 | + |
| 65 | + if (chainFamily && chainSelectorName) { |
| 66 | + if (isTestnet === false) { |
| 67 | + return this.maps.mainnetByNameByFamily[chainFamily].get(chainSelectorName) |
| 68 | + } |
| 69 | + |
| 70 | + if (isTestnet === true) { |
| 71 | + return this.maps.testnetByNameByFamily[chainFamily].get(chainSelectorName) |
| 72 | + } |
| 73 | + |
| 74 | + // If user haven't defined if it's testnet or not we can try all networks, starting from testnet |
| 75 | + let network = this.maps.testnetByNameByFamily[chainFamily].get(chainSelectorName) |
| 76 | + if (!network) { |
| 77 | + network = this.maps.mainnetByNameByFamily[chainFamily].get(chainSelectorName) |
| 78 | + } |
| 79 | + return network |
| 80 | + } |
| 81 | + |
| 82 | + // If only network type is specified, use the general maps |
| 83 | + if (chainSelector !== undefined) { |
| 84 | + if (isTestnet === false) { |
| 85 | + return getBySelector(this.maps.mainnetBySelector) |
| 86 | + } |
| 87 | + |
| 88 | + if (isTestnet === true) { |
| 89 | + return getBySelector(this.maps.testnetBySelector) |
| 90 | + } |
| 91 | + |
| 92 | + // If user haven't defined if it's testnet or not we can try all networks, starting from testnet |
| 93 | + let network = getBySelector(this.maps.testnetBySelector) |
| 94 | + if (!network) { |
| 95 | + network = getBySelector(this.maps.mainnetBySelector) |
| 96 | + } |
| 97 | + return network |
| 98 | + } |
| 99 | + |
| 100 | + if (chainSelectorName) { |
| 101 | + if (isTestnet === false) { |
| 102 | + return this.maps.mainnetByName.get(chainSelectorName) |
| 103 | + } |
| 104 | + |
| 105 | + if (isTestnet === true) { |
| 106 | + return this.maps.testnetByName.get(chainSelectorName) |
| 107 | + } |
| 108 | + |
| 109 | + // If user haven't defined if it's testnet or not we can try all networks, starting from testnet |
| 110 | + let network = this.maps.testnetByName.get(chainSelectorName) |
| 111 | + if (!network) { |
| 112 | + network = this.maps.mainnetByName.get(chainSelectorName) |
| 113 | + } |
| 114 | + return network |
| 115 | + } |
| 116 | + |
| 117 | + return undefined |
| 118 | + } |
| 119 | +} |
0 commit comments