-
Notifications
You must be signed in to change notification settings - Fork 4
/
aptos.go
71 lines (57 loc) · 1.56 KB
/
aptos.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
package chain_selectors
import (
_ "embed"
"fmt"
"gopkg.in/yaml.v3"
)
//go:generate go run genchains_aptos.go
//go:embed selectors_aptos.yml
var aptosSelectorsYml []byte
var (
aptosSelectorsMap = parseAptosYml(aptosSelectorsYml)
aptosChainIdBySelector = make(map[uint64]uint64)
)
func init() {
for k, v := range aptosSelectorsMap {
aptosChainIdBySelector[v.ChainSelector] = k
}
}
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)
}
validateAptosChainID(data.SelectorsByAptosChainId)
return data.SelectorsByAptosChainId
}
func validateAptosChainID(data map[uint64]ChainDetails) {
// TODO: https://smartcontract-it.atlassian.net/browse/NONEVM-890
}
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) {
chainId, exist := aptosChainIdBySelector[selector]
if !exist {
return 0, fmt.Errorf("chain id not found for selector %d", selector)
}
return chainId, nil
}