Skip to content

Commit

Permalink
add helper function for fetching chainID from chain selector and test…
Browse files Browse the repository at this point in the history
… coverage (#74)

* add helper function and test coverage

* default handler
  • Loading branch information
huangzhen1997 authored Nov 12, 2024
1 parent d27ceaf commit 5218193
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions aptos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ func Test_AptosGetChainDetailsByChainIDAndFamily(t *testing.T) {
assert.Equal(t, v, details)
}
}

func Test_AptosGetChainIDByChainSelector(t *testing.T) {
for k, v := range aptosSelectorsMap {
chainID, err := GetChainIDFromSelector(v.ChainSelector)
assert.NoError(t, err)
assert.Equal(t, chainID, fmt.Sprintf("%v", k))
}
}
9 changes: 9 additions & 0 deletions evm_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chain_selectors

import (
"fmt"
"math/rand"
"strconv"
"testing"
Expand Down Expand Up @@ -235,3 +236,11 @@ func Test_EVMGetChainDetailsByChainIDAndFamily(t *testing.T) {
assert.Equal(t, v, details)
}
}

func Test_EVMGetChainIDByChainSelector(t *testing.T) {
for k, v := range evmSelectorsMap {
chainID, err := GetChainIDFromSelector(v.ChainSelector)
assert.NoError(t, err)
assert.Equal(t, chainID, fmt.Sprintf("%v", k))
}
}
33 changes: 33 additions & 0 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,39 @@ func GetSelectorFamily(selector uint64) (string, error) {
return "", fmt.Errorf("unknown chain selector %d", selector)
}

func GetChainIDFromSelector(selector uint64) (string, error) {
destChainFamily, err := GetSelectorFamily(selector)
if err != nil {
return "", err
}

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 destChainID, nil
}

func GetChainDetailsByChainIDAndFamily(chainID string, family string) (ChainDetails, error) {
switch family {
case FamilyEVM:
Expand Down
9 changes: 9 additions & 0 deletions solana_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chain_selectors

import (
"fmt"
"math/rand"
"testing"

Expand Down Expand Up @@ -75,3 +76,11 @@ func Test_SolanaGetChainDetailsByChainIDAndFamily(t *testing.T) {
assert.Equal(t, v, details)
}
}

func Test_SolanaGetChainIDByChainSelector(t *testing.T) {
for k, v := range solanaSelectorsMap {
chainID, err := GetChainIDFromSelector(v.ChainSelector)
assert.NoError(t, err)
assert.Equal(t, chainID, fmt.Sprintf("%v", k))
}
}

0 comments on commit 5218193

Please sign in to comment.