Skip to content

Commit

Permalink
update name encode to support solana testnet names
Browse files Browse the repository at this point in the history
  • Loading branch information
amirylm committed Jan 9, 2025
1 parent 97f96fb commit 3a8f124
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
41 changes: 22 additions & 19 deletions internal/gotmpl/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@ import (
"strings"
"unicode"

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

func newNameEncoder() *nameEncoder {
return &nameEncoder{
re: regexp.MustCompile("[-_]+"),
caser: cases.Title(language.English),
}
}

type nameEncoder struct {
re *regexp.Regexp
caser cases.Caser
}
var (
reSeperators = regexp.MustCompile("[-_]+")
caser = cases.Title(language.English)
)

func (*nameEncoder) varName(name string, chainSel uint64) string {
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])) {
if len(x) > 0 && (unicode.IsDigit(rune(x[0])) || isSolTestChain(name)) {
x = unnamed + "_" + x
}
if len(x) == 0 {
Expand All @@ -35,14 +29,23 @@ func (*nameEncoder) varName(name string, chainSel uint64) string {
return x
}

func (enc *nameEncoder) enumName(name string, chainSel uint64) string {
x := enc.re.ReplaceAllString(name, " ")
varName := strings.ReplaceAll(enc.caser.String(x), " ", "")
if len(varName) > 0 && unicode.IsDigit(rune(varName[0])) {
varName = "Test" + varName
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 = "Test" + strconv.FormatUint(chainSel, 10)
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
}
5 changes: 2 additions & 3 deletions internal/gotmpl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type chain[C uint64 | string] struct {
// C is the type of the chain ID.
func Run[C uint64 | string](tmpl *template.Template, chainSelFunc func() map[C]uint64, nameFunc func(C) (string, error)) (string, error) {
chains := make([]chain[C], 0)
enc := newNameEncoder()

for chainID, chainSel := range chainSelFunc() {
name, err := nameFunc(chainID)
Expand All @@ -37,8 +36,8 @@ func Run[C uint64 | string](tmpl *template.Template, chainSelFunc func() map[C]u
ChainID: chainID,
Selector: chainSel,
Name: name,
VarName: enc.varName(name, chainSel),
EnumName: enc.enumName(name, chainSel),
VarName: encodeVarName(name, chainSel),
EnumName: encodeEnumName(name, chainSel),
})
}

Expand Down

0 comments on commit 3a8f124

Please sign in to comment.