Skip to content

Commit

Permalink
initial work for adding chain family specific id and remove old selec…
Browse files Browse the repository at this point in the history
…tors
  • Loading branch information
huangzhen1997 committed Oct 30, 2024
1 parent 19b1f6e commit 1def2f9
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 132 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import (
)

func main() {
// Getting selector based on ChainId
selector, err := chainselectors.SelectorFromChainId(420)
// Getting selector based on ChainId and family
selector, err := chainselectors.SelectorFromChainIdAndFamily(420, "evm")

// Getting ChainId based on ChainSelector
chainId, err := chainselectors.ChainIdFromSelector(2664363617261496610)

// Getting ChainName based on ChainId
chainName, err := chainselectors.NameFromChainId(420)
chainName, err := chainselectors.NameFromChainIdAndFamily(420, "evm")

// Getting ChainId based on the ChainName
chainId, err := chainselectors.ChainIdFromName("binance_smart_chain-testnet")
// Getting ChainId based on the ChainName and family
chainId, err := chainselectors.ChainIdFromNameAndFamily("binance_smart_chain-testnet", "evm")

// Accessing mapping directly
lookupChainId := uint64(1337)
Expand Down
37 changes: 36 additions & 1 deletion selector_families.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,37 @@ selector_families:
222782988166878823:
family: evm
name: hedera-testnet
chain_id: "296"
241851231317828981:
family: evm
name: bitcoin-merlin-mainnet
chain_id: "4200"
328334718812072308:
family: evm
374210358663784372:
family: evm
name: velas-mainnet
chain_id: "106"
465200170687744372:
family: evm
name: gnosis_chain-mainnet
chain_id: "100"
572210378683744374:
family: evm
name: velas-testnet
chain_id: "111"
665284410079532457:
family: evm
729797994450396300:
family: evm
name: telos-evm-testnet
chain_id: "41"
781901677223027175:
family: evm
789068866484373046:
family: evm
829525985033418733:
chain_id: "919"
family: evm
name: ethereum-testnet-sepolia-mode-1
909606746561742123:
Expand All @@ -41,32 +48,41 @@ selector_families:
1113014352258747600:
family: evm
name: neonlink-testnet
chain_id: "9559"
1237925231416731909:
family: evm
name: ethereum-mainnet-immutable-zkevm-1
chain_id: "13371"
1252863800116739621:
family: evm
name: polkadot-mainnet-moonbeam
chain_id : "1284"
1273605685587320666:
family: evm
1346049177634351622:
family: evm
name: celo-mainnet
chain_id : "42220"
1355020143337428062:
family: evm
name: kusama-mainnet-moonriver
chain_id: "1285"
1355246678561316402:
family: evm
name: ethereum-testnet-goerli-linea-1
chain_id: "59140"
1456215246176062136:
family: evm
name: cronos-mainnet
chain_id: "25"
1458281248224512906:
family: evm
name: avalanche-subnet-dexalot-testnet
chain_id: "432201"
1462016016387883143:
family: evm
name: fraxtal-mainnet
chain_id: "252"
1467223411771711614:
family: evm
name: bitcoin-testnet-botanix
Expand Down Expand Up @@ -225,18 +241,22 @@ selector_families:
4874388048629246000:
family: evm
name: bitcichain-mainnet
chain_id: "1907"
4888058894222120000:
family: evm
name: bitcichain-testnet
4905564228793744293:
family: evm
name: fantom-testnet
chain_id: "4002"
4949039107694359620:
family: evm
name: ethereum-mainnet-arbitrum-1
chain_id: "42161"
5009297550715157269:
family: evm
name: ethereum-mainnet
chain_id: "1"
5061593697262339000:
family: evm
name: near-testnet
Expand Down Expand Up @@ -584,9 +604,11 @@ selector_families:
3849287863852499584:
family: evm
name: "bitcoin-mainnet-bob-1"
chain_id: "60808"
9027416829622342829:
family: evm
name: "sei-mainnet"
chain_id: "1329"
13274425992935471758:
family: evm
7717148896336251131:
Expand All @@ -606,7 +628,20 @@ selector_families:
6916147374840168594:
family: evm
name: "ronin-mainnet"
chain_id: "2020"
5299555114858065850:
family: evm
2049429975587534727:
family: evm
family: evm
124615329519749607:
family: solana
name: "solana-mainnet"
chain_id: "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d"
6302590918974934319:
family: solana
name: "solana-testnet"
chain_id: "4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY"
16423721717087811551:
family: solana
name: "solana-devnet"
chain_id: "EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG"
170 changes: 87 additions & 83 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package chain_selectors
import (
_ "embed"
"fmt"
"strconv"

"gopkg.in/yaml.v3"
)
Expand All @@ -19,9 +18,10 @@ var testSelectorsYml []byte
//go:embed selector_families.yml
var selectorFamiliesYml []byte

type chainDetails struct {
ChainSelector uint64 `yaml:"selector"`
ChainName string `yaml:"name"`
type newChainDetails struct {
Family string `yaml:"family"`
Name string `yaml:"name"`
ChainID string `yaml:"chain_id"`
}

const (
Expand All @@ -32,31 +32,12 @@ const (
FamilyAptos = "aptos"
)

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

var evmChainIdToChainSelector = loadAllSelectors()
var selectorToChainFamily = loadSelectorToFamilyMap()

func loadAllSelectors() map[uint64]chainDetails {
output := make(map[uint64]chainDetails, len(selectorsMap)+len(testSelectorsMap))
for k, v := range selectorsMap {
output[k] = v
}
for k, v := range testSelectorsMap {
output[k] = v
}
return output
}

func loadSelectorToFamilyMap() map[uint64]string {
type familyDetails struct {
Family string `yaml:"family"`
Name string `yaml:"name"`
}
var chainIDToSelectorMapForFamily = make(map[string]map[string]uint64)
var selectorToChainDetails = loadChainDetailsBySelector()

func loadChainDetailsBySelector() map[uint64]newChainDetails {
type yamlData struct {
SelectorFamilies map[uint64]familyDetails `yaml:"selector_families"`
SelectorFamilies map[uint64]newChainDetails `yaml:"selector_families"`
}

var data yamlData
Expand All @@ -65,95 +46,118 @@ func loadSelectorToFamilyMap() map[uint64]string {
panic(err)
}

var selectorFamilies = make(map[uint64]string, len(data.SelectorFamilies))
for k, v := range data.SelectorFamilies {
selectorFamilies[k] = v.Family
}

return selectorFamilies
}

func parseYml(ymlFile []byte) map[uint64]chainDetails {
type ymlData struct {
Selectors map[uint64]chainDetails `yaml:"selectors"`
}
if v.Family == "" {
continue
}

var data ymlData
err := yaml.Unmarshal(ymlFile, &data)
if err != nil {
panic(err)
// update chainIDToSelectorMapForFamily
_, exist := chainIDToSelectorMapForFamily[v.Family]
if exist {
chainIDToSelectorMapForFamily[v.Family][v.ChainID] = k
} else {
chainIDToSelectorMapForFamily[v.Family] = make(map[string]uint64)
}
}

return data.Selectors
return data.SelectorFamilies
}

func GetSelectorFamily(selector uint64) (string, error) {
family, exist := selectorToChainFamily[selector]
details, exist := selectorToChainDetails[selector]
if !exist {
return "", fmt.Errorf("family not found for selector %d", selector)
return "", fmt.Errorf("chain detail not found for selector %d", selector)
}

return family, nil
return details.Family, nil
}

func EvmChainIdToChainSelector() map[uint64]uint64 {
copyMap := make(map[uint64]uint64, len(evmChainIdToChainSelector))
for k, v := range evmChainIdToChainSelector {
copyMap[k] = v.ChainSelector
func ChainSelectorToChainDetails() map[uint64]newChainDetails {
copyMap := make(map[uint64]newChainDetails, len(selectorToChainDetails))
for k, v := range selectorToChainDetails {
copyMap[k] = v
}

return copyMap
}

func ChainIdFromSelector(chainSelectorId uint64) (uint64, error) {
for k, v := range evmChainIdToChainSelector {
if v.ChainSelector == chainSelectorId {
return k, nil
}
func ChainIdFromSelector(chainSelectorId uint64) (string, error) {
chainDetail, ok := selectorToChainDetails[chainSelectorId]
if !ok {
return "0", fmt.Errorf("chain not found for chain selector %d", chainSelectorId)
}
return 0, fmt.Errorf("chain not found for chain selector %d", chainSelectorId)
return chainDetail.ChainID, nil
}

func SelectorFromChainId(chainId uint64) (uint64, error) {
if chainSelectorId, exist := evmChainIdToChainSelector[chainId]; exist {
return chainSelectorId.ChainSelector, nil
func SelectorFromChainIdAndFamily(chainId string, family string) (uint64, error) {
// if family is missing use EVM as default
if family == "" {
family = FamilyEVM
}
return 0, fmt.Errorf("chain selector not found for chain %d", chainId)
}

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

selector, exist := selectorMap[chainId]
if !exist {
return 0, fmt.Errorf("chain selector not found for chain %v", chainId)
}
return details.ChainName, nil
return selector, nil
}

func ChainIdFromName(name string) (uint64, error) {
for k, v := range evmChainIdToChainSelector {
if v.ChainName == name {
return k, nil
}
func NameFromChainIdAndFamily(chainId string, family string) (string, error) {
// if family is missing use EVM as default
if family == "" {
family = FamilyEVM
}
chainId, err := strconv.ParseUint(name, 10, 64)
if err == nil {
if details, exist := evmChainIdToChainSelector[chainId]; exist && details.ChainName == "" {
return chainId, nil
}

selectorMap, exist := chainIDToSelectorMapForFamily[family]
if !exist {
return "", fmt.Errorf("chain family not found for chain %v, %v", chainId, family)
}
return 0, fmt.Errorf("chain not found for name %s", name)

selector, exist := selectorMap[chainId]
if !exist {
return "", fmt.Errorf("chain selector not found for chain %v, %v", chainId, family)
}

details, exist := selectorToChainDetails[selector]
if !exist {
return "", fmt.Errorf("chain details not found for chain %v, %v", chainId, family)
}

if details.Name == "" {
return chainId, nil
}
return details.Name, nil
}

func TestChainIds() []uint64 {
chainIds := make([]uint64, 0, len(testSelectorsMap))
for k := range testSelectorsMap {
chainIds = append(chainIds, k)
func ChainIdFromNameAndFamily(name string, family string) (string, error) {
// if family is missing use EVM as default
if family == "" {
family = FamilyEVM
}
return chainIds

for _, v := range selectorToChainDetails {
if v.Name == name && family == v.Family {
return v.ChainID, nil
}
}

return "0", fmt.Errorf("chain not found for name %s", name)
}

//TODO fix after code gen
//func TestChainIds() []uint64 {
// chainIds := make([]uint64, 0, len(testSelectorsMap))
// for k := range testSelectorsMap {
// chainIds = append(chainIds, k)
// }
// return chainIds
//}

var chainsBySelector = make(map[uint64]Chain)
var chainsByEvmChainID = make(map[uint64]Chain)

Expand Down
Loading

0 comments on commit 1def2f9

Please sign in to comment.