Skip to content

Commit

Permalink
Generalize and deduplicate selectors code
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaveracll authored Nov 21, 2024
1 parent 502f846 commit f943844
Showing 1 changed file with 71 additions and 31 deletions.
102 changes: 71 additions & 31 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f943844

Please sign in to comment.