Skip to content

Commit

Permalink
Adding chain names
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara committed Dec 5, 2023
1 parent f3b9a54 commit 4261336
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 113 deletions.
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module github.com/smartcontractkit/chain-selectors

go 1.20

require gopkg.in/yaml.v3 v3.0.1
require (
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
6 changes: 3 additions & 3 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func SelectorFromChainId(chainId uint64) (uint64, error) {
}

func NameFromChainId(chainId uint64) (string, error) {
chainDetails, exist := evmChainIdToChainSelector[chainId]
details, exist := evmChainIdToChainSelector[chainId]
if !exist {
return "", fmt.Errorf("chain name not found for chain %d", chainId)
}
if chainDetails.ChainName == "" {
if details.ChainName == "" {
return strconv.FormatUint(chainId, 10), nil
}
return chainDetails.ChainName, nil
return details.ChainName, nil
}

func TestChainIds() []uint64 {
Expand Down
78 changes: 44 additions & 34 deletions selectors.yml
Original file line number Diff line number Diff line change
@@ -1,76 +1,86 @@
selectors:
# Testnets
97: # BSC Testnet
97:
selector: 13264668187771770619
name: "bsc-testnet"
280: # ZKSync Era
name: "binance_smart_chain-testnet"
280:
selector: 6802309497652714138
420: # Optimism Goerli
name: "ethereum-testnet-goerli-zksync-1"
420:
selector: 2664363617261496610
name: "ethereum-testnet-goerli-optimism-1"
1112: # Wemix testnet
1112:
selector: 9284632837123596123
name: "wemix-testnet"
1337: # Dev network
selector: 3379446385462418246
1442: # Polygon zkEVM Testnet
1442:
selector: 11059667695644972511
2358: # Kroma Sepolia
name: "polygon-testnet-mumbai-zkevm-1"
2358:
selector: 5990477251245693094
43113: # Avalanche Fuji
name: "ethereum-testnet-sepolia-kroma-1"
43113:
selector: 14767482510784806043
name: "avalanche-fuji-testnet"
name: "avalanche-testnet-fuji"
76578:
selector: 781901677223027175
84531: # BASE Goerli
84531:
selector: 5790810961207155433
name: "ethereum-testnet-goerli-base-1"
84532: # BASE Sepolia
84532:
selector: 10344971235874465080
name: "ethereum-testnet-sepolia-base-1"
80001: # Polygon Mumbai
80001:
selector: 12532609583862916517
name: "matic-testnet"
421613: # Arbitrum Goerli
name: "polygon-testnet-mumbai"
421613:
selector: 6101244977088475029
name: "ethereum-testnet-goerli-arbitrum-1"
421614: # Arbitrum Sepolia
421614:
selector: 3478487238524512106
534351: # Scroll Sepolia
name: "ethereum-testnet-sepolia-arbitrum-1"
534351:
selector: 2279865765895943307
11155111: # Sepolia
name: "ethereum-testnet-sepolia-scroll-1"
11155111:
selector: 16015286601757825753
name: "ethereum-testnet-sepolia"
11155420: # Optimism Sepolia
11155420:
selector: 5224473277236331295

name: "ethereum-testnet-sepolia-optimism-1"

# Mainnets
1: # Ethereum
1:
selector: 5009297550715157269
name: "mainnet"
10: # Optimism
name: "ethereum-mainnet"
10:
selector: 3734403246176062136
name: "ethereum-mainnet-optimism-1"
56: # BSC
56:
selector: 11344663589394136015
name: "bsc-mainnet"
137: # Polygon
name: "binance_smart_chain-mainnet"
137:
selector: 4051577828743386545
name: "matic-mainnet"
255: # Kroma
name: "polygon-mainnet"
255:
selector: 3719320017875267166
1101: # Polygon zkEVM
name: "ethereum-mainnet-kroma-1"
1101:
selector: 4348158687435793198
1111: # Wemix
name: "polygon-mainnet-zkevm-1"
1111:
selector: 5142893604156789321
8453: # BASE
name: "wemix-mainnet"
8453:
selector: 15971525489660198786
name: "ethereum-mainnet-base-1"
42161: # Arbitrum
42161:
selector: 4949039107694359620
name: "ethereum-mainnet-arbitrum-1"
43114: # Avalanche
43114:
selector: 6433500567565415381
name: "avalanche-mainnet"
534352: # Scroll
selector: 13204309965629103672
534352:
selector: 13204309965629103672
name: "ethereum-mainnet-scroll-1"
108 changes: 33 additions & 75 deletions selectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,138 +2,96 @@ package chain_selectors

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNoSameChainSelectorsAreGenerated(t *testing.T) {
chainSelectors := map[uint64]bool{}
chainSelectors := map[uint64]struct{}{}

for k, v := range evmChainIdToChainSelector {
selector := v.ChainSelector
if _, exist := chainSelectors[selector]; exist {
t.Errorf("Chain Selectors should be unique. Selector %d is duplicated for chain %d", selector, k)
}
chainSelectors[selector] = true
_, exist := chainSelectors[selector]
assert.False(t, exist, "Chain Selectors should be unique. Selector %d is duplicated for chain %d", selector, k)
chainSelectors[selector] = struct{}{}
}
}

func TestNoOverlapBetweenRealAndTestChains(t *testing.T) {
for k, _ := range selectorsMap {
if _, exist := testSelectorsMap[k]; exist {
t.Errorf("Chain %d is duplicated between real and test chains", k)
}
_, exist := testSelectorsMap[k]
assert.False(t, exist, "Chain %d is duplicated between real and test chains", k)
}
}

func TestBothSelectorsYmlAndTestSelectorsYmlAreValid(t *testing.T) {
optimismGoerliSelector, err := SelectorFromChainId(420)
if err != nil {
t.Error("Chain Selectors not found for chain 420")
}
if optimismGoerliSelector != 2664363617261496610 {
t.Error("Invalid Chain Selector for chain 420")
}
require.NoError(t, err)
assert.Equal(t, uint64(2664363617261496610), optimismGoerliSelector)

testChainSelector, err := SelectorFromChainId(90000020)
if err != nil {
t.Error("Chain Selectors not found for test chain 90000020")
}
if testChainSelector != 17810359353458878177 {
t.Error("Invalid Chain Selector for chain 90000020")
}
require.NoError(t, err)
assert.Equal(t, uint64(17810359353458878177), testChainSelector)
}

func TestEvmChainIdToChainSelectorReturningCopiedMap(t *testing.T) {
selectors := EvmChainIdToChainSelector()
selectors[1] = 2

_, err := ChainIdFromSelector(2)
if err == nil {
t.Error("Changes to map should not affect the original map")
}
assert.Error(t, err)

_, err = ChainIdFromSelector(1)
if err == nil {
t.Error("Changes to map should not affect the original map")
}
assert.Error(t, err)
}

func TestChainIdFromSelector(t *testing.T) {
_, err := ChainIdFromSelector(0)
if err == nil {
t.Error("Should return error if chain selector not found")
}
assert.Error(t, err, "Should return error if chain selector not found")

_, err = ChainIdFromSelector(99999999)
if err == nil {
t.Error("Should return error if chain selector not found")
}
assert.Error(t, err, "Should return error if chain selector not found")

chainId, err := ChainIdFromSelector(13264668187771770619)
if err != nil {
t.Error("Should return chain id if chain selector found")
}
if chainId != 97 {
t.Error("Should return correct chain id")
}
require.NoError(t, err)
assert.Equal(t, uint64(97), chainId)
}

func TestSelectorFromChainId(t *testing.T) {
_, err := SelectorFromChainId(0)
if err == nil {
t.Error("Should return error if chain not found")
}
require.Error(t, err)

_, err = SelectorFromChainId(99999999)
if err == nil {
t.Error("Should return error if chain not found")
}
require.Error(t, err)

chainSelectorId, err := SelectorFromChainId(97)
if err != nil {
t.Error("Should return chain selector id if chain found")
}
if chainSelectorId != 13264668187771770619 {
t.Error("Should return correct chain selector id")
}
require.NoError(t, err)
assert.Equal(t, uint64(13264668187771770619), chainSelectorId)
}

func TestTestChainIds(t *testing.T) {
chainIds := TestChainIds()
if len(chainIds) != len(testSelectorsMap) {
t.Error("Should return correct number of test chain ids")
}
assert.Equal(t, len(chainIds), len(testSelectorsMap), "Should return correct number of test chain ids")

for _, chainId := range chainIds {
if _, exist := testSelectorsMap[chainId]; !exist {
t.Error("Should return correct test chain ids")
}
_, exist := testSelectorsMap[chainId]
assert.True(t, exist)
}
}

func TestNameFromChainId(t *testing.T) {
_, err := NameFromChainId(0)
if err == nil {
t.Error("Should return error if chain not found")
}
_, err := NameFromChainId(2)
require.Error(t, err, "Should return error if chain not found")

_, err = NameFromChainId(99999999)
if err == nil {
t.Error("Should return error if chain not found")
}
require.Error(t, err, "Should return error if chain not found")

chainName, err := NameFromChainId(97)
if err != nil {
t.Error("Should return chain name if chain found")
}
if chainName != "bsc-testnet" {
t.Error("Should return correct chain name")
}
require.NoError(t, err)
assert.Equal(t, "binance_smart_chain-testnet", chainName)

chainName, err = NameFromChainId(1337)
if err != nil {
t.Error("Should return chain name if chain found")
}
if chainName != "1337" {
t.Error("Should return chain id if name is not defined")
}
require.NoError(t, err)
assert.Equal(t, "1337", chainName)
}

0 comments on commit 4261336

Please sign in to comment.