Skip to content

Commit

Permalink
Exposing chain names together with selector
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara committed Dec 5, 2023
1 parent 1b419b7 commit f3b9a54
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 60 deletions.
31 changes: 24 additions & 7 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chain_selectors
import (
_ "embed"
"fmt"
"strconv"

"gopkg.in/yaml.v3"
)
Expand All @@ -13,13 +14,18 @@ var selectorsYml []byte
//go:embed test_selectors.yml
var testSelectorsYml []byte

type chainDetails struct {
ChainSelector uint64 `yaml:"selector"`
ChainName string `yaml:"name"`
}

var selectorsMap = parseYml(selectorsYml)
var testSelectorsMap = parseYml(testSelectorsYml)

var evmChainIdToChainSelector = loadAllSelectors()

func loadAllSelectors() map[uint64]uint64 {
output := make(map[uint64]uint64, len(selectorsMap)+len(testSelectorsMap))
func loadAllSelectors() map[uint64]chainDetails {
output := make(map[uint64]chainDetails, len(selectorsMap)+len(testSelectorsMap))
for k, v := range selectorsMap {
output[k] = v
}
Expand All @@ -29,9 +35,9 @@ func loadAllSelectors() map[uint64]uint64 {
return output
}

func parseYml(ymlFile []byte) map[uint64]uint64 {
func parseYml(ymlFile []byte) map[uint64]chainDetails {
type ymlData struct {
Selectors map[uint64]uint64 `yaml:"selectors"`
Selectors map[uint64]chainDetails `yaml:"selectors"`
}

var data ymlData
Expand All @@ -46,14 +52,14 @@ func parseYml(ymlFile []byte) map[uint64]uint64 {
func EvmChainIdToChainSelector() map[uint64]uint64 {
copyMap := make(map[uint64]uint64, len(evmChainIdToChainSelector))
for k, v := range evmChainIdToChainSelector {
copyMap[k] = v
copyMap[k] = v.ChainSelector
}
return copyMap
}

func ChainIdFromSelector(chainSelectorId uint64) (uint64, error) {
for k, v := range evmChainIdToChainSelector {
if v == chainSelectorId {
if v.ChainSelector == chainSelectorId {
return k, nil
}
}
Expand All @@ -62,11 +68,22 @@ func ChainIdFromSelector(chainSelectorId uint64) (uint64, error) {

func SelectorFromChainId(chainId uint64) (uint64, error) {
if chainSelectorId, exist := evmChainIdToChainSelector[chainId]; exist {
return chainSelectorId, nil
return chainSelectorId.ChainSelector, nil
}
return 0, fmt.Errorf("chain selector not found for chain %d", chainId)
}

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

func TestChainIds() []uint64 {
chainIds := make([]uint64, 0, len(testSelectorsMap))
for k := range testSelectorsMap {
Expand Down
101 changes: 73 additions & 28 deletions selectors.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
selectors:
# Testnets
97: 13264668187771770619 # BSC Testnet
280: 6802309497652714138 # ZKSync Era
420: 2664363617261496610 # Optimism Goerli
1112: 9284632837123596123 # Wemix testnet
1337: 3379446385462418246 # Dev network
1442: 11059667695644972511 # Polygon zkEVM Testnet
2358: 5990477251245693094 # Kroma Sepolia
43113: 14767482510784806043 # Avalanche Fuji
76578: 781901677223027175
84531: 5790810961207155433 # BASE Goerli
84532: 10344971235874465080 # BASE Sepolia
80001: 12532609583862916517 # Polygon Mumbai
421613: 6101244977088475029 # Arbitrum Goerli
421614: 3478487238524512106 # Arbitrum Sepolia
534351: 2279865765895943307 # Scroll Sepolia
11155111: 16015286601757825753 # Sepolia
11155420: 5224473277236331295 # Optimism Sepolia
97: # BSC Testnet
selector: 13264668187771770619
name: "bsc-testnet"
280: # ZKSync Era
selector: 6802309497652714138
420: # Optimism Goerli
selector: 2664363617261496610
name: "ethereum-testnet-goerli-optimism-1"
1112: # Wemix testnet
selector: 9284632837123596123
1337: # Dev network
selector: 3379446385462418246
1442: # Polygon zkEVM Testnet
selector: 11059667695644972511
2358: # Kroma Sepolia
selector: 5990477251245693094
43113: # Avalanche Fuji
selector: 14767482510784806043
name: "avalanche-fuji-testnet"
76578:
selector: 781901677223027175
84531: # BASE Goerli
selector: 5790810961207155433
name: "ethereum-testnet-goerli-base-1"
84532: # BASE Sepolia
selector: 10344971235874465080
name: "ethereum-testnet-sepolia-base-1"
80001: # Polygon Mumbai
selector: 12532609583862916517
name: "matic-testnet"
421613: # Arbitrum Goerli
selector: 6101244977088475029
name: "ethereum-testnet-goerli-arbitrum-1"
421614: # Arbitrum Sepolia
selector: 3478487238524512106
534351: # Scroll Sepolia
selector: 2279865765895943307
11155111: # Sepolia
selector: 16015286601757825753
name: "ethereum-testnet-sepolia"
11155420: # Optimism Sepolia
selector: 5224473277236331295


# Mainnets
1: 5009297550715157269 # Ethereum
10: 3734403246176062136 # Optimism
56: 11344663589394136015 # BSC
137: 4051577828743386545 # Polygon
255: 3719320017875267166 # Kroma
1101: 4348158687435793198 # Polygon zkEVM
1111: 5142893604156789321 # Wemix
8453: 15971525489660198786 # BASE
42161: 4949039107694359620 # Arbitrum
43114: 6433500567565415381 # Avalanche
534352: 13204309965629103672 # Scroll
1: # Ethereum
selector: 5009297550715157269
name: "mainnet"
10: # Optimism
selector: 3734403246176062136
name: "ethereum-mainnet-optimism-1"
56: # BSC
selector: 11344663589394136015
name: "bsc-mainnet"
137: # Polygon
selector: 4051577828743386545
name: "matic-mainnet"
255: # Kroma
selector: 3719320017875267166
1101: # Polygon zkEVM
selector: 4348158687435793198
1111: # Wemix
selector: 5142893604156789321
8453: # BASE
selector: 15971525489660198786
name: "ethereum-mainnet-base-1"
42161: # Arbitrum
selector: 4949039107694359620
name: "ethereum-mainnet-arbitrum-1"
43114: # Avalanche
selector: 6433500567565415381
name: "avalanche-mainnet"
534352: # Scroll
selector: 13204309965629103672
35 changes: 32 additions & 3 deletions selectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ func TestNoSameChainSelectorsAreGenerated(t *testing.T) {
chainSelectors := map[uint64]bool{}

for k, v := range evmChainIdToChainSelector {
if _, exist := chainSelectors[v]; exist {
t.Errorf("Chain Selectors should be unique. Selector %d is duplicated for chain %d", v, k)
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[v] = true
chainSelectors[selector] = true
}
}

Expand Down Expand Up @@ -108,3 +109,31 @@ func TestTestChainIds(t *testing.T) {
}
}
}

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

_, err = NameFromChainId(99999999)
if err == nil {
t.Error("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")
}

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")
}
}
66 changes: 44 additions & 22 deletions test_selectors.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
selectors:
# For testing purposes
1000: 11787463284727550157
2337: 12922642891491394802
90000001: 909606746561742123
90000002: 5548718428018410741
90000003: 789068866484373046
90000004: 5721565186521185178
90000005: 964127714438319834
90000006: 8966794841936584464
90000007: 8412806778050735057
90000008: 4066443121807923198
90000009: 6747736380229414777
90000010: 8694984074292254623
90000011: 328334718812072308
90000012: 7715160997071429212
90000013: 3574539439524578558
90000014: 4543928599863227519
90000015: 6443235356619661032
90000016: 13087962012083037329
90000017: 11985232338641871056
90000018: 7777066535355430289
90000019: 1273605685587320666
90000020: 17810359353458878177
1000:
selector: 11787463284727550157
2337:
selector: 12922642891491394802
90000001:
selector: 909606746561742123
90000002:
selector: 5548718428018410741
90000003:
selector: 789068866484373046
90000004:
selector: 5721565186521185178
90000005:
selector: 964127714438319834
90000006:
selector: 8966794841936584464
90000007:
selector: 8412806778050735057
90000008:
selector: 4066443121807923198
90000009:
selector: 6747736380229414777
90000010:
selector: 8694984074292254623
90000011:
selector: 328334718812072308
90000012:
selector: 7715160997071429212
90000013:
selector: 3574539439524578558
90000014:
selector: 4543928599863227519
90000015:
selector: 6443235356619661032
90000016:
selector: 13087962012083037329
90000017:
selector: 11985232338641871056
90000018:
selector: 7777066535355430289
90000019:
selector: 1273605685587320666
90000020:
selector: 17810359353458878177

0 comments on commit f3b9a54

Please sign in to comment.