Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize and deduplicate selectors code #76

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading