Skip to content

Commit

Permalink
add helper function and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhen1997 committed Nov 12, 2024
1 parent d27ceaf commit 5a30203
Show file tree
Hide file tree
Showing 4 changed files with 57 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))
}
}
31 changes: 31 additions & 0 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ 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)
}

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 5a30203

Please sign in to comment.