Skip to content

Commit

Permalink
adding solana test selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
yashnevatia committed Jan 6, 2025
1 parent 21b636a commit 0bca7d6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
14 changes: 12 additions & 2 deletions genchains_solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"sort"
"strconv"
"strings"
"unicode"

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

Expand Down Expand Up @@ -81,6 +81,7 @@ func genChainsSourceCode() (string, error) {

for ChainID, chainSel := range chain_selectors.SolanaChainIdToChainSelector() {
name, err := chain_selectors.SolanaNameFromChainId(ChainID)
// fmt.Println(ChainID, name, toVarName(name, chainSel), err)
if err != nil {
return "", err
}
Expand All @@ -91,6 +92,7 @@ func genChainsSourceCode() (string, error) {
Name: name,
VarName: toVarName(name, chainSel),
})

}

sort.Slice(chains, func(i, j int) bool { return chains[i].VarName < chains[j].VarName })
Expand All @@ -104,9 +106,17 @@ 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])) {

// 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 genesis hash, this check cannot corectly detect test chains
// we need to check if the name (chainId) is a valid base58 encoded genesis hash

_, err := base58.Decode(name)
if err == nil {
x = unnamed + "_" + x
}

if len(x) == 0 {
x = unnamed + "_" + strconv.FormatUint(chainSel, 10)
}
Expand Down
12 changes: 9 additions & 3 deletions generated_chains_solana.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 24 additions & 5 deletions solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,34 @@ import (
//go:embed selectors_solana.yml
var solanaSelectorsYml []byte

//go:embed test_selectors_solana.yml
var testSelectorsSolanaYml []byte

var (
solanaSelectorsMap = parseSolanaYml(solanaSelectorsYml)
solanaChainIdBySelector = make(map[uint64]string)
solanaSelectorsMap = parseSolanaYml(solanaSelectorsYml)
solanaTestSelectorsMap = parseSolanaYml(testSelectorsSolanaYml)
solanaChainIdToChainSelector = loadAllSolanaSelectors()
solanaChainIdBySelector = make(map[uint64]string)
)

func init() {
for k, v := range solanaSelectorsMap {
solanaChainIdBySelector[v.ChainSelector] = k
}
for k, v := range solanaTestSelectorsMap {
solanaChainIdBySelector[v.ChainSelector] = k
}
}

func loadAllSolanaSelectors() map[string]ChainDetails {
output := make(map[string]ChainDetails, len(solanaSelectorsMap)+len(solanaTestSelectorsMap))
for k, v := range solanaSelectorsMap {
output[k] = v
}
for k, v := range solanaTestSelectorsMap {
output[k] = v
}
return output
}

func parseSolanaYml(ymlFile []byte) map[string]ChainDetails {
Expand Down Expand Up @@ -52,15 +71,15 @@ func validateSolanaChainID(data map[string]ChainDetails) {
}

func SolanaChainIdToChainSelector() map[string]uint64 {
copyMap := make(map[string]uint64, len(solanaSelectorsMap))
for k, v := range solanaSelectorsMap {
copyMap := make(map[string]uint64, len(solanaChainIdToChainSelector))
for k, v := range solanaChainIdToChainSelector {
copyMap[k] = v.ChainSelector
}
return copyMap
}

func SolanaNameFromChainId(chainId string) (string, error) {
details, exist := solanaSelectorsMap[chainId]
details, exist := solanaChainIdToChainSelector[chainId]
if !exist {
return "", fmt.Errorf("chain name not found for chain %v", chainId)
}
Expand Down
9 changes: 9 additions & 0 deletions test_selectors_solana.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
selectors:
# random values
"7ZWZqpZ5RQNjE9zGHwEz6KvuFbGQyxhh6t7DJHDu6iXy":
selector: 12463857294658392847
"BxE7UwXVa4zStanz8Qz1vsWyM3gUMbpoQokAhWxmKWZw":
selector: 9837465928374658293
"Gh92RUkqZC8sdP3yPGGzqkKc8nC9DRXrPZeBE2YPdPnu":
selector: 16574839267584930184

0 comments on commit 0bca7d6

Please sign in to comment.