-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solana and backwards compatibility
- Loading branch information
1 parent
28a3bb5
commit 392f8fb
Showing
8 changed files
with
282 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
//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("no changes detected") | ||
return | ||
} | ||
|
||
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.