-
Notifications
You must be signed in to change notification settings - Fork 4
/
genchains_solana.go
114 lines (94 loc) · 2.16 KB
/
genchains_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
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
//go:build ignore
package main
import (
"bytes"
"fmt"
"go/format"
"html/template"
"os"
"sort"
"strconv"
"strings"
"unicode"
chain_selectors "github.com/smartcontractkit/chain-selectors"
)
const filename = "generated_chains_solana.go"
type chain struct {
ChainID string
Selector uint64
Name string
VarName string
}
var chainTemplate, _ = template.New("").Parse(`// Code generated by go generate please DO NOT EDIT
package chain_selectors
type SolanaChain struct {
ChainID string
Selector uint64
Name string
VarName string
}
var (
{{ range . }}
{{.VarName}} = SolanaChain{ChainID: "{{ .ChainID }}", Selector: {{ .Selector }}, Name: "{{ .Name }}"}{{ end }}
)
var SolanaALL = []SolanaChain{
{{ range . }}{{ .VarName }},
{{ end }}
}
`)
func main() {
src, err := genChainsSourceCode()
if err != nil {
panic(err)
}
formatted, err := format.Source([]byte(src))
if err != nil {
panic(err)
}
existingContent, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
if string(existingContent) == string(formatted) {
fmt.Println("solana: no changes detected")
return
}
fmt.Println("solana: updating generations")
err = os.WriteFile(filename, formatted, 0644)
if err != nil {
panic(err)
}
}
func genChainsSourceCode() (string, error) {
var wr = new(bytes.Buffer)
chains := make([]chain, 0)
for ChainID, chainSel := range chain_selectors.SolanaChainIdToChainSelector() {
name, err := chain_selectors.SolanaNameFromChainId(ChainID)
if err != nil {
return "", err
}
chains = append(chains, chain{
ChainID: ChainID,
Selector: chainSel,
Name: name,
VarName: toVarName(name, chainSel),
})
}
sort.Slice(chains, func(i, j int) bool { return chains[i].VarName < chains[j].VarName })
if err := chainTemplate.ExecuteTemplate(wr, "", chains); err != nil {
return "", err
}
return wr.String(), nil
}
func toVarName(name string, chainSel uint64) string {
const unnamed = "TEST"
x := strings.ReplaceAll(name, "-", "_")
x = strings.ToUpper(x)
if len(x) > 0 && unicode.IsDigit(rune(x[0])) {
x = unnamed + "_" + x
}
if len(x) == 0 {
x = unnamed + "_" + strconv.FormatUint(chainSel, 10)
}
return x
}