diff --git a/pkg/solana/chainwriter/helpers.go b/pkg/solana/chainwriter/helpers.go index 7f059535e..669477097 100644 --- a/pkg/solana/chainwriter/helpers.go +++ b/pkg/solana/chainwriter/helpers.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "reflect" + "regexp" "strings" "testing" @@ -204,12 +205,18 @@ func InitializeDataAccount( } func GetDiscriminator(instruction string) [8]byte { - fullHash := sha256.Sum256([]byte("global:" + instruction)) + fullHash := sha256.Sum256([]byte("global:" + ToSnakeCase(instruction))) var discriminator [8]byte copy(discriminator[:], fullHash[:8]) return discriminator } +func ToSnakeCase(s string) string { + s = regexp.MustCompile(`([a-z0-9])([A-Z])`).ReplaceAllString(s, "${1}_${2}") + s = regexp.MustCompile(`([A-Z]+)([A-Z][a-z])`).ReplaceAllString(s, "${1}_${2}") + return strings.ToLower(s) +} + func GetRandomPubKey(t *testing.T) solana.PublicKey { privKey, err := solana.NewRandomPrivateKey() require.NoError(t, err) diff --git a/pkg/solana/chainwriter/helpers_test.go b/pkg/solana/chainwriter/helpers_test.go new file mode 100644 index 000000000..1ee0628dc --- /dev/null +++ b/pkg/solana/chainwriter/helpers_test.go @@ -0,0 +1,29 @@ +package chainwriter_test + +import ( + "testing" + + "github.com/smartcontractkit/chainlink-solana/pkg/solana/chainwriter" +) + +func TestToSnakeCase(t *testing.T) { + testCases := []struct { + input string + expected string + }{ + {"testCamelCase", "test_camel_case"}, + {"oneword", "oneword"}, + {"", ""}, + {"testCamelCaseWithCAPS", "test_camel_case_with_caps"}, + {"testCamelCaseWithCAPSAndNumbers123", "test_camel_case_with_caps_and_numbers123"}, + } + + for _, tc := range testCases { + t.Run(tc.input, func(t *testing.T) { + actual := chainwriter.ToSnakeCase(tc.input) + if actual != tc.expected { + t.Errorf("expected %s, got %s", tc.expected, actual) + } + }) + } +}