From f943844c51127334085d49f458e366a740d91ffe Mon Sep 17 00:00:00 2001 From: jlaveracll Date: Thu, 21 Nov 2024 14:47:43 -0300 Subject: [PATCH] Generalize and deduplicate selectors code --- selectors.go | 102 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 31 deletions(-) diff --git a/selectors.go b/selectors.go index 2c01945..6f37e2e 100644 --- a/selectors.go +++ b/selectors.go @@ -13,59 +13,99 @@ const ( FamilyAptos = "aptos" ) -func GetSelectorFamily(selector uint64) (string, error) { +type chainInfo struct { + Family string + ChainID string + ChainDetails ChainDetails +} + +func getChainInfo(selector uint64) (chainInfo, error) { // check EVM _, exist := evmChainsBySelector[selector] if exist { - return FamilyEVM, nil + family := FamilyEVM + + evmChainId, err := ChainIdFromSelector(selector) + if err != nil { + return chainInfo{}, fmt.Errorf("failed to get %v chain ID from selector %d: %w", family, selector, err) + } + + details, exist := evmChainIdToChainSelector[evmChainId] + if !exist { + return chainInfo{}, fmt.Errorf("invalid chain id %d for %s", evmChainId, family) + } + + return chainInfo{ + Family: family, + ChainID: fmt.Sprintf("%d", evmChainId), + ChainDetails: details, + }, nil } // check solana _, exist = solanaChainIdBySelector[selector] if exist { - return FamilySolana, nil + family := FamilySolana + + chainID, err := SolanaChainIdFromSelector(selector) + if err != nil { + return chainInfo{}, fmt.Errorf("failed to get %s chain ID from selector %d: %w", chainID, selector, err) + } + + details, exist := solanaSelectorsMap[chainID] + if !exist { + return chainInfo{}, fmt.Errorf("invalid chain id %s for %s", chainID, family) + } + + return chainInfo{ + Family: family, + ChainID: chainID, + ChainDetails: details, + }, nil + } // check aptos _, exist = aptosChainIdBySelector[selector] if exist { - return FamilyAptos, nil + family := FamilyAptos + + chainID, err := AptosChainIdFromSelector(selector) + if err != nil { + return chainInfo{}, fmt.Errorf("failed to get %v chain ID from selector %d: %w", chainID, selector, err) + } + + details, exist := aptosSelectorsMap[chainID] + if !exist { + return chainInfo{}, fmt.Errorf("invalid chain id %d for %s", chainID, family) + } + + return chainInfo{ + Family: family, + ChainID: fmt.Sprintf("%d", chainID), + ChainDetails: details, + }, nil } - return "", fmt.Errorf("unknown chain selector %d", selector) + return chainInfo{}, fmt.Errorf("unknown chain selector %d", selector) } -func GetChainIDFromSelector(selector uint64) (string, error) { - destChainFamily, err := GetSelectorFamily(selector) +func GetSelectorFamily(selector uint64) (string, error) { + chainInfo, err := getChainInfo(selector) if err != nil { - return "", err + return "", fmt.Errorf("unknown chain selector %d", selector) } - var id uint64 - var destChainID string - switch destChainFamily { - case FamilyEVM: - id, err = ChainIdFromSelector(selector) - if err != nil { - return "", fmt.Errorf("failed to get %v chain ID from selector %d: %w", destChainFamily, selector, err) - } - destChainID = fmt.Sprintf("%d", id) - case FamilySolana: - destChainID, err = SolanaChainIdFromSelector(selector) - if err != nil { - return "", fmt.Errorf("failed to get %v chain ID from selector %d: %w", destChainFamily, selector, err) - } - case FamilyAptos: - id, err = AptosChainIdFromSelector(selector) - if err != nil { - return "", fmt.Errorf("failed to get %v chain ID from selector %d: %w", destChainFamily, selector, err) - } - destChainID = fmt.Sprintf("%d", id) - default: - return "", fmt.Errorf("selector %d is not supported", selector) + return chainInfo.Family, nil +} + +func GetChainIDFromSelector(selector uint64) (string, error) { + chainInfo, err := getChainInfo(selector) + if err != nil { + return "", fmt.Errorf("unknown chain selector %d", selector) } - return destChainID, nil + return chainInfo.ChainID, nil } func GetChainDetailsByChainIDAndFamily(chainID string, family string) (ChainDetails, error) {