-
Notifications
You must be signed in to change notification settings - Fork 4
/
solana.go
80 lines (66 loc) · 1.82 KB
/
solana.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
package chain_selectors
import (
_ "embed"
"fmt"
"github.com/mr-tron/base58"
"gopkg.in/yaml.v3"
)
//go:generate go run genchains_solana.go
//go:embed selectors_solana.yml
var solanaSelectorsYml []byte
var (
solanaSelectorsMap = parseSolanaYml(solanaSelectorsYml)
solanaChainIdBySelector = make(map[uint64]string)
)
func init() {
for k, v := range solanaSelectorsMap {
solanaChainIdBySelector[v.ChainSelector] = k
}
}
func parseSolanaYml(ymlFile []byte) map[string]ChainDetails {
type ymlData struct {
SelectorsBySolanaChainId map[string]ChainDetails `yaml:"selectors"`
}
var data ymlData
err := yaml.Unmarshal(ymlFile, &data)
if err != nil {
panic(err)
}
validateSolanaChainID(data.SelectorsBySolanaChainId)
return data.SelectorsBySolanaChainId
}
func validateSolanaChainID(data map[string]ChainDetails) {
for genesisHash := range data {
b, err := base58.Decode(genesisHash)
if err != nil {
panic(fmt.Errorf("failed to decode base58 genesis hash %s: %w", genesisHash, err))
}
if len(b) != 32 {
panic(fmt.Errorf("decoded genesis hash %s is not 32 bytes long", genesisHash))
}
}
}
func SolanaChainIdToChainSelector() map[string]uint64 {
copyMap := make(map[string]uint64, len(solanaSelectorsMap))
for k, v := range solanaSelectorsMap {
copyMap[k] = v.ChainSelector
}
return copyMap
}
func SolanaNameFromChainId(chainId string) (string, error) {
details, exist := solanaSelectorsMap[chainId]
if !exist {
return "", fmt.Errorf("chain name not found for chain %v", chainId)
}
if details.ChainName == "" {
return chainId, nil
}
return details.ChainName, nil
}
func SolanaChainIdFromSelector(selector uint64) (string, error) {
chainId, exist := solanaChainIdBySelector[selector]
if !exist {
return "", fmt.Errorf("chain id not found for selector %d", selector)
}
return chainId, nil
}