-
Notifications
You must be signed in to change notification settings - Fork 4
/
evm.go
139 lines (118 loc) · 3.33 KB
/
evm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package chain_selectors
import (
_ "embed"
"fmt"
"strconv"
"gopkg.in/yaml.v3"
)
//go:generate go run genchains_evm.go
//go:embed selectors.yml
var selectorsYml []byte
//go:embed test_selectors.yml
var testSelectorsYml []byte
type ChainDetails struct {
ChainSelector uint64 `yaml:"selector"`
ChainName string `yaml:"name"`
}
var (
evmSelectorsMap = parseYml(selectorsYml)
evmTestSelectorsMap = parseYml(testSelectorsYml)
evmChainIdToChainSelector = loadAllEVMSelectors()
evmChainsBySelector = make(map[uint64]Chain)
evmChainsByEvmChainID = make(map[uint64]Chain)
)
func init() {
for _, ch := range ALL {
evmChainsBySelector[ch.Selector] = ch
evmChainsByEvmChainID[ch.EvmChainID] = ch
}
}
func loadAllEVMSelectors() map[uint64]ChainDetails {
output := make(map[uint64]ChainDetails, len(evmSelectorsMap)+len(evmTestSelectorsMap))
for k, v := range evmSelectorsMap {
output[k] = v
}
for k, v := range evmTestSelectorsMap {
output[k] = v
}
return output
}
func parseYml(ymlFile []byte) map[uint64]ChainDetails {
type ymlData struct {
SelectorsByEvmChainId map[uint64]ChainDetails `yaml:"selectors"`
}
var data ymlData
err := yaml.Unmarshal(ymlFile, &data)
if err != nil {
panic(err)
}
return data.SelectorsByEvmChainId
}
func EvmChainIdToChainSelector() map[uint64]uint64 {
copyMap := make(map[uint64]uint64, len(evmChainIdToChainSelector))
for k, v := range evmChainIdToChainSelector {
copyMap[k] = v.ChainSelector
}
return copyMap
}
func ChainIdFromSelector(chainSelectorId uint64) (uint64, error) {
for k, v := range evmChainIdToChainSelector {
if v.ChainSelector == chainSelectorId {
return k, nil
}
}
return 0, fmt.Errorf("chain not found for chain selector %d", chainSelectorId)
}
func SelectorFromChainId(chainId uint64) (uint64, error) {
if chainSelectorId, exist := evmChainIdToChainSelector[chainId]; exist {
return chainSelectorId.ChainSelector, nil
}
return 0, fmt.Errorf("chain selector not found for chain %d", chainId)
}
func NameFromChainId(chainId uint64) (string, error) {
details, exist := evmChainIdToChainSelector[chainId]
if !exist {
return "", fmt.Errorf("chain name not found for chain %d", chainId)
}
if details.ChainName == "" {
return strconv.FormatUint(chainId, 10), nil
}
return details.ChainName, nil
}
func ChainIdFromName(name string) (uint64, error) {
for k, v := range evmChainIdToChainSelector {
if v.ChainName == name {
return k, nil
}
}
chainId, err := strconv.ParseUint(name, 10, 64)
if err == nil {
if details, exist := evmChainIdToChainSelector[chainId]; exist && details.ChainName == "" {
return chainId, nil
}
}
return 0, fmt.Errorf("chain not found for name %s", name)
}
func TestChainIds() []uint64 {
chainIds := make([]uint64, 0, len(evmTestSelectorsMap))
for k := range evmTestSelectorsMap {
chainIds = append(chainIds, k)
}
return chainIds
}
func ChainBySelector(sel uint64) (Chain, bool) {
ch, exists := evmChainsBySelector[sel]
return ch, exists
}
func ChainByEvmChainID(evmChainID uint64) (Chain, bool) {
ch, exists := evmChainsByEvmChainID[evmChainID]
return ch, exists
}
func IsEvm(chainSel uint64) (bool, error) {
_, exists := ChainBySelector(chainSel)
if !exists {
return false, fmt.Errorf("chain %d not found", chainSel)
}
// We always return true since only evm chains are supported atm.
return true, nil
}