-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtron.go
More file actions
85 lines (70 loc) · 2.02 KB
/
tron.go
File metadata and controls
85 lines (70 loc) · 2.02 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
package chain_selectors
import (
_ "embed"
"fmt"
"log"
"gopkg.in/yaml.v3"
)
//go:generate go run genchains_tron.go
//go:generate go run generate_all_selectors.go
//go:embed selectors_tron.yml
var tronSelectorsYml []byte
var (
tronSelectorsMap = parseTronYml(tronSelectorsYml)
tronChainIdBySelector = make(map[uint64]uint64)
)
func init() {
// Load extra selectors
for chainID, chainDetails := range getExtraSelectors().Tron {
if _, exists := tronSelectorsMap[chainID]; exists {
log.Printf("WARN: Skipping extra selector for chain %d because it already exists", chainID)
continue
}
tronSelectorsMap[chainID] = chainDetails
tronChainIdBySelector[chainDetails.ChainSelector] = chainID
}
for k, v := range tronSelectorsMap {
tronChainIdBySelector[v.ChainSelector] = k
}
}
func parseTronYml(ymlFile []byte) map[uint64]ChainDetails {
type ymlData struct {
SelectorsByTronChainId map[uint64]ChainDetails `yaml:"selectors"`
}
var data ymlData
err := yaml.Unmarshal(ymlFile, &data)
if err != nil {
panic(err)
}
return data.SelectorsByTronChainId
}
func TronChainIdToChainSelector() map[uint64]uint64 {
copyMap := make(map[uint64]uint64, len(tronSelectorsMap))
for k, v := range tronSelectorsMap {
copyMap[k] = v.ChainSelector
}
return copyMap
}
func TronNameFromChainId(chainId uint64) (string, error) {
details, exist := tronSelectorsMap[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 TronChainIdFromSelector(selector uint64) (uint64, error) {
chainId, exist := tronChainIdBySelector[selector]
if !exist {
return 0, fmt.Errorf("chain id not found for selector %d", selector)
}
return chainId, nil
}
func TronNetworkTypeFromChainId(chainId uint64) (NetworkType, error) {
if chainDetails, exist := tronSelectorsMap[chainId]; exist {
return chainDetails.NetworkType, nil
}
return "", fmt.Errorf("chain network type not found for chain %v", chainId)
}