-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaptos.go
More file actions
125 lines (107 loc) · 3.42 KB
/
aptos.go
File metadata and controls
125 lines (107 loc) · 3.42 KB
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
package chain_selectors
import (
_ "embed"
"fmt"
"log"
"gopkg.in/yaml.v3"
)
//go:generate go run genchains_aptos.go
//go:generate go run generate_all_selectors.go
//go:embed selectors_aptos.yml
var aptosSelectorsYml []byte
var (
aptosSelectorsMap = parseAptosYml(aptosSelectorsYml)
aptosChainsBySelector = make(map[uint64]AptosChain)
)
func init() {
// Load extra selectors
for chainID, chainDetails := range getExtraSelectors().Aptos {
if _, exists := aptosSelectorsMap[chainID]; exists {
log.Printf("WARN: Skipping extra selector for chain %d because it already exists", chainID)
continue
}
aptosSelectorsMap[chainID] = chainDetails
aptosChainsBySelector[chainDetails.ChainSelector] = AptosChain{
ChainID: chainID,
Selector: chainDetails.ChainSelector,
Name: chainDetails.ChainName,
NetworkType: chainDetails.NetworkType,
}
}
for _, v := range AptosALL {
aptosChainsBySelector[v.Selector] = v
}
}
func parseAptosYml(ymlFile []byte) map[uint64]ChainDetails {
type ymlData struct {
SelectorsByAptosChainId map[uint64]ChainDetails `yaml:"selectors"`
}
var data ymlData
err := yaml.Unmarshal(ymlFile, &data)
if err != nil {
panic(err)
}
err = validateAptosChainID(data.SelectorsByAptosChainId)
if err != nil {
panic(err)
}
return data.SelectorsByAptosChainId
}
func validateAptosChainID(data map[uint64]ChainDetails) error {
seenSelectors := make(map[uint64]uint64) // selector -> chainID
for chainID, details := range data {
if chainID == 0 {
return fmt.Errorf("invalid aptos chain ID: must be > 0")
}
if details.ChainSelector == 0 {
return fmt.Errorf("invalid chain selector for aptos chain %d: must be > 0", chainID)
}
if details.ChainName == "" {
return fmt.Errorf("chain name is empty for aptos chain %d", chainID)
}
if details.NetworkType != NetworkTypeTestnet && details.NetworkType != NetworkTypeMainnet {
return fmt.Errorf("invalid network type %q for aptos chain %d: must be %q or %q",
details.NetworkType, chainID, NetworkTypeTestnet, NetworkTypeMainnet)
}
if existingChainID, exists := seenSelectors[details.ChainSelector]; exists {
return fmt.Errorf("duplicate chain selector %d: used by both aptos chain %d and %d",
details.ChainSelector, existingChainID, chainID)
}
seenSelectors[details.ChainSelector] = chainID
}
return nil
}
func AptosChainIdToChainSelector() map[uint64]uint64 {
copyMap := make(map[uint64]uint64, len(aptosSelectorsMap))
for k, v := range aptosSelectorsMap {
copyMap[k] = v.ChainSelector
}
return copyMap
}
func AptosNameFromChainId(chainId uint64) (string, error) {
details, exist := aptosSelectorsMap[chainId]
if !exist {
return "", fmt.Errorf("chain name not found for chain %v", chainId)
}
if details.ChainName == "" {
return fmt.Sprint(chainId), nil
}
return details.ChainName, nil
}
func AptosChainIdFromSelector(selector uint64) (uint64, error) {
chain, exist := aptosChainsBySelector[selector]
if !exist {
return 0, fmt.Errorf("chain id not found for selector %d", selector)
}
return chain.ChainID, nil
}
func AptosChainBySelector(selector uint64) (AptosChain, bool) {
chain, exist := aptosChainsBySelector[selector]
return chain, exist
}
func AptosNetworkTypeFromChainId(chainId uint64) (NetworkType, error) {
if chainDetails, exist := aptosSelectorsMap[chainId]; exist {
return chainDetails.NetworkType, nil
}
return "", fmt.Errorf("chain network type not found for chain %v", chainId)
}