Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7ed8fb8
added rust codegen (wip)
amirylm Oct 9, 2024
87894ea
go generate ./rs
amirylm Nov 1, 2024
4bbb182
fix warnings
amirylm Nov 1, 2024
0dffa94
rust workflow
amirylm Nov 1, 2024
814ef2e
update rs workflow
amirylm Nov 1, 2024
320414e
update wf
amirylm Nov 1, 2024
1af1f4e
update wd
amirylm Nov 1, 2024
262c8e5
update wrk dir
amirylm Nov 1, 2024
f3eb87f
update main README.md
amirylm Nov 1, 2024
a72fd5c
fix wd
amirylm Nov 1, 2024
5fd0ce7
refactor code
amirylm Nov 1, 2024
29c90da
update generated
amirylm Nov 1, 2024
8819204
update wf
amirylm Nov 1, 2024
a2a446a
fallback to docker if rustfmt is not installed
amirylm Nov 1, 2024
08ffcaa
fix rustfmt docker cmd
amirylm Nov 3, 2024
d0e2e48
change comment
amirylm Nov 3, 2024
faf5e8f
update rs/README.md
amirylm Nov 3, 2024
df0be05
update rs/README.md
amirylm Nov 3, 2024
a77c837
update example
amirylm Nov 3, 2024
9118e15
Update rs/chainselectors/src/lib.rs
amirylm Nov 4, 2024
3898819
Update rs/chainselectors/src/lib.rs
amirylm Nov 4, 2024
fcf4bef
print meaningful errors
amirylm Nov 4, 2024
6c69bfa
fix imports
amirylm Nov 4, 2024
78d823d
print fmt error
amirylm Nov 4, 2024
0333591
cargo fmt
amirylm Nov 4, 2024
2a1eb11
format template file
amirylm Nov 4, 2024
4a7dfd0
unwrap err
amirylm Nov 4, 2024
2d7c5d8
review fixes:
amirylm Nov 4, 2024
e96b443
update ChainSelector and ChainId structs
amirylm Nov 4, 2024
821c592
updates
amirylm Nov 4, 2024
1321d00
cargo fmt
amirylm Nov 4, 2024
05b92c8
rm Cargo.lock (should not be present in libraries)
amirylm Jan 7, 2025
c1e7d40
update gitignore
amirylm Jan 7, 2025
6a9aec4
Merge branch 'main' of github.com:smartcontractkit/chain-selectors in…
amirylm Jan 7, 2025
5b97045
lint
amirylm Jan 9, 2025
92edba3
pkg gotmpl: support multiple chain types
amirylm Jan 9, 2025
3511223
update wf
amirylm Jan 9, 2025
97f96fb
Merge branch 'main' of github.com:smartcontractkit/chain-selectors in…
amirylm Jan 9, 2025
3a8f124
update name encode to support solana testnet names
amirylm Jan 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/rs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Rust - Build and test

on: [ push ]

jobs:
build-test:
name: Build and test
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
- name: Install rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version-file: "go.mod"
cache: false
- name: Make sure generated files are updated
run: |
if go generate ./rs | grep -q 'evm (rust): no changes detected'; then
exit 0;
fi
exit 1;
- name: cargo fmt
working-directory: rs/chainselectors
run: cargo fmt --all -- --check
- name: cargo clippy
working-directory: rs/chainselectors
run: cargo clippy --all-targets --all-features -- -D warnings
- name: cargo test
working-directory: rs/chainselectors
run: cargo test
timeout-minutes: 1
- name: cargo build
working-directory: rs/chainselectors
run: cargo build
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Any new chains and selectors should be always added to [selectors.yml](selectors
details from this file. This ensures that all client libraries are in sync and use the same mapping.
To add a new chain, please add new entry to the `selectors.yml` file and use the following format:

Make sure to run `go generate` after making any changes.
Make sure to run `go generate ./...` after making any changes.

```yaml
$chain_id:
Expand Down
2 changes: 1 addition & 1 deletion evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNoSameChainSelectorsAreGenerated(t *testing.T) {
}

func TestNoOverlapBetweenRealAndTestChains(t *testing.T) {
for k, _ := range evmSelectorsMap {
for k := range evmSelectorsMap {
_, exist := evmTestSelectorsMap[k]
assert.False(t, exist, "Chain %d is duplicated between real and test chains", k)
}
Expand Down
48 changes: 3 additions & 45 deletions genchains_aptos.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
package main

import (
"bytes"
"fmt"
"go/format"
"html/template"
"os"
"sort"
"strconv"
"strings"
"unicode"
"text/template"

chain_selectors "github.com/smartcontractkit/chain-selectors"
"github.com/smartcontractkit/chain-selectors/internal/gotmpl"
)

const filename = "generated_chains_aptos.go"
Expand Down Expand Up @@ -48,7 +44,7 @@ var AptosALL = []AptosChain{
`)

func main() {
src, err := genChainsSourceCode()
src, err := gotmpl.Run(chainTemplate, chain_selectors.AptosChainIdToChainSelector, chain_selectors.AptosNameFromChainId)
if err != nil {
panic(err)
}
Expand All @@ -74,41 +70,3 @@ func main() {
panic(err)
}
}

func genChainsSourceCode() (string, error) {
var wr = new(bytes.Buffer)
chains := make([]chain, 0)

for ChainID, chainSel := range chain_selectors.AptosChainIdToChainSelector() {
name, err := chain_selectors.AptosNameFromChainId(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
}
57 changes: 4 additions & 53 deletions genchains_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,17 @@
package main

import (
"bytes"
"fmt"
"go/format"
"html/template"
"os"
"sort"
"strconv"
"strings"
"unicode"
"text/template"

chain_selectors "github.com/smartcontractkit/chain-selectors"
"github.com/smartcontractkit/chain-selectors/internal/gotmpl"
)

const filename = "generated_chains_evm.go"

type chain struct {
EvmChainID uint64
Selector uint64
Name string
VarName string
}

var chainTemplate, _ = template.New("").Parse(`// Code generated by go generate please DO NOT EDIT
package chain_selectors

Expand All @@ -37,7 +26,7 @@ type Chain struct {

var (
{{ range . }}
{{.VarName}} = Chain{EvmChainID: {{ .EvmChainID }}, Selector: {{ .Selector }}, Name: "{{ .Name }}"}{{ end }}
{{.VarName}} = Chain{EvmChainID: {{ .ChainID }}, Selector: {{ .Selector }}, Name: "{{ .Name }}"}{{ end }}
)

var ALL = []Chain{
Expand All @@ -48,7 +37,7 @@ var ALL = []Chain{
`)

func main() {
src, err := genChainsSourceCode()
src, err := gotmpl.Run(chainTemplate, chain_selectors.EvmChainIdToChainSelector, chain_selectors.NameFromChainId)
if err != nil {
panic(err)
}
Expand All @@ -74,41 +63,3 @@ func main() {
panic(err)
}
}

func genChainsSourceCode() (string, error) {
var wr = new(bytes.Buffer)
chains := make([]chain, 0)

for evmChainID, chainSel := range chain_selectors.EvmChainIdToChainSelector() {
name, err := chain_selectors.NameFromChainId(evmChainID)
if err != nil {
return "", err
}

chains = append(chains, chain{
EvmChainID: evmChainID,
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
}
55 changes: 3 additions & 52 deletions genchains_solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
package main

import (
"bytes"
"fmt"
"go/format"
"html/template"
"os"
"sort"
"strconv"
"strings"
"text/template"

"github.com/mr-tron/base58"
chain_selectors "github.com/smartcontractkit/chain-selectors"
"github.com/smartcontractkit/chain-selectors/internal/gotmpl"
)

const filename = "generated_chains_solana.go"
Expand Down Expand Up @@ -48,7 +44,7 @@ var SolanaALL = []SolanaChain{
`)

func main() {
src, err := genChainsSourceCode()
src, err := gotmpl.Run(chainTemplate, chain_selectors.SolanaChainIdToChainSelector, chain_selectors.SolanaNameFromChainId)
if err != nil {
panic(err)
}
Expand All @@ -74,48 +70,3 @@ func main() {
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]))
// for evm, the above condition is used to detect if name == chainId == (some number) -> which means its a test chain
// for solana, as chainId is not a number but a base58 encoded hash, we cannot use the above condition
// we need to check if the name == chainId == a valid base58 encoded hash

_, err := base58.Decode(name)
if err == nil {
x = unnamed + "_" + x
}
if len(x) == 0 {
x = unnamed + "_" + strconv.FormatUint(chainSel, 10)
}
return x
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/text v0.19.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
51 changes: 51 additions & 0 deletions internal/gotmpl/encoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gotmpl

import (
"regexp"
"strconv"
"strings"
"unicode"

"github.com/mr-tron/base58"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

var (
reSeperators = regexp.MustCompile("[-_]+")
caser = cases.Title(language.English)
)

func encodeVarName(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])) || isSolTestChain(name)) {
x = unnamed + "_" + x
}
if len(x) == 0 {
x = unnamed + "_" + strconv.FormatUint(chainSel, 10)
}
return x
}

func encodeEnumName(name string, chainSel uint64) string {
const unnamed = "Test"
x := reSeperators.ReplaceAllString(name, " ")
varName := strings.ReplaceAll(caser.String(x), " ", "")
if len(varName) > 0 && (unicode.IsDigit(rune(varName[0])) || isSolTestChain(name)) {
varName = unnamed + varName
}
if len(varName) == 0 {
varName = unnamed + strconv.FormatUint(chainSel, 10)
}
return varName
}

// for evm, the above condition is used to detect if name == chainId == (some number) -> which means its a test chain
// for solana, as chainId is not a number but a base58 encoded hash, we cannot use the above condition
// we need to check if the name == chainId == a valid base58 encoded hash
func isSolTestChain(name string) bool {
_, err := base58.Decode(name)
return err == nil
}
Loading
Loading