Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solanchor: Add enums & integration tests #14

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions sol-anchor/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ func (c *Convo) Update(msg loop.Msg) loop.Cmd {
return loop.Quit(fmt.Errorf(`something went wrong, here's an error message to share with our devs (%s); we've notified them already`, err))
}

c.State.idl = &IDL{}
err := json.Unmarshal([]byte(c.State.IdlString), &c.State.idl)
if err != nil {
fmt.Println("Error unmarshaling JSON:", err)
return loop.Quit(fmt.Errorf("could not decode IDL"))
}

msgCmd = c.Msg().Message("Ok, I reloaded your state.").Cmd()
} else {
msgCmd = c.Msg().Message("Ok, let's start a new package.").Cmd()
Expand Down
10 changes: 10 additions & 0 deletions sol-anchor/idl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func (i *IDL) IsTypeUsed(typeName string) bool {
return false
}

func (i *IDL) IsTypeEnum(typeName string) bool {
for _, typeObj := range i.Types {
if typeObj.Name == typeName {
return typeObj.Type.IsEnum()
}
}

return false
}

type Metadata struct {
Address string `json:"address"`
Name string `json:"name"` // seen in the 'secret' program
Expand Down
11 changes: 11 additions & 0 deletions sol-anchor/idl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ func TestOrcaIDL(t *testing.T) {
assert.Equal(t, 12, len(result.Types))
}

func TestJupiterV4Swap(t *testing.T) {
idl := readFromFile("jupiter_v4_swap")

result := &IDL{}
err := json.Unmarshal(idl, &result)

assert.Nil(t, err)
assert.Equal(t, "JUP4Fb2cqiRUcaTHdrPC8h2gNsA2ETXiPDD33WcGuJB", result.Metadata.Address)
assert.True(t, result.IsTypeEnum("SwapLeg"))
}

func readFromFile(idlName string) []byte {
data, err := os.ReadFile("tests/" + idlName + ".json")
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions sol-anchor/idl_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (t *Type) SnakeCaseName() string {
return toSnakeCase(t.Name, true)
}

func (t *Type) SnakeCaseNameUpperCase() string {
return strings.ToUpper(t.SnakeCaseName())
}

func (t *Type) PascalCaseName() string {
return textcase.PascalCase(t.Name)
}
Expand Down
10 changes: 5 additions & 5 deletions sol-anchor/templates/proto/program.proto.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ message {{$event.Name}} {
{{- $counter := 0 }}
{{- range $i, $field := $event.Fields }}
{{- $counter = add $counter 1 }}
{{ if or $field.Type.IsArray $field.Type.IsVec }} repeated {{ end }} {{ if $field.Type.IsOption }} optional {{ end }} {{$field.Type.ResolveProtobufType}} {{$field.SnakeCaseName}} = {{ $counter }};
{{ if or $field.Type.IsArray $field.Type.IsVec }} repeated {{ end }} {{ if $field.Type.IsOption }} optional {{ end }} {{$field.Type.ResolveProtobufType}}{{ if $.Idl.IsTypeEnum $field.Type.ResolveProtobufType }}Enum{{end}} {{$field.SnakeCaseName}} = {{ $counter }};
{{- end}}
}
{{- end}}
Expand All @@ -34,7 +34,7 @@ message {{$instruction.PascalCaseName}} {
{{- $counter := 0 }}
{{- range $i, $arg := $instruction.Args }}
{{- $counter = add $counter 1 }}
{{ if or $arg.Type.IsArray $arg.Type.IsVec }} repeated {{ end }} {{ if $arg.Type.IsOption }} optional {{ end }} {{$arg.Type.ResolveProtobufType}} {{$arg.SnakeCaseName}} = {{ $counter }};
{{ if or $arg.Type.IsArray $arg.Type.IsVec }} repeated {{ end }} {{ if $arg.Type.IsOption }} optional {{ end }} {{$arg.Type.ResolveProtobufType}}{{ if $.Idl.IsTypeEnum $arg.Type.ResolveProtobufType }}Enum{{end}} {{$arg.SnakeCaseName}} = {{ $counter }};
{{- end}}
}
{{- end}}
Expand All @@ -46,13 +46,13 @@ message {{$instruction.PascalCaseName}} {
{{- $counter := 0 }}
{{- range $i, $field := $type.Type.Struct.Fields }}
{{- $counter = add $counter 1 }}
{{ if or $field.Type.IsArray $field.Type.IsVec }} repeated {{ end }} {{ if $field.Type.IsOption }} optional {{ end }} {{$field.Type.ResolveProtobufType}} {{$field.SnakeCaseName}} = {{ $counter }};
{{ if or $field.Type.IsArray $field.Type.IsVec }} repeated {{ end }} {{ if $field.Type.IsOption }} optional {{ end }} {{$field.Type.ResolveProtobufType}}{{ if $.Idl.IsTypeEnum $field.Type.ResolveProtobufType }}Enum{{end}} {{$field.SnakeCaseName}} = {{ $counter }};
{{- end}}
}
{{- else }}
enum {{$type.Name}} {
enum {{$type.Name}}Enum {
{{- range $i, $variant := $type.Type.Enum.Variants }}
{{ $variant.SnakeCaseName }} = {{ $i }};
{{ $type.SnakeCaseNameUpperCase }}_{{ $variant.SnakeCaseName }} = {{ $i }};
{{- end}}
}
{{- end }}
Expand Down
4 changes: 4 additions & 0 deletions sol-anchor/templates/src/lib.rs.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ use pb::substreams::v1::program::{{$instruction.PascalCaseName}};
{{- end}}

{{- range $i, $type := .Idl.Types }}
{{ if $type.Type.IsEnum }}
use pb::substreams::v1::program::{{$type.Name}}Enum;
{{ else }}
use pb::substreams::v1::program::{{$type.Name}};
{{ end }}
{{- end}}

use sologger_log_context::programs_selector::ProgramsSelector;
Expand Down
9 changes: 9 additions & 0 deletions sol-anchor/tests/integration/generators/meteora.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions sol-anchor/tests/integration/generators/orca.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions sol-anchor/tests/integration/generators/pump-fun.json

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions sol-anchor/tests/integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package solanchor

import (
"fmt"
"os"
"os/exec"
"testing"
)

func TestIntegrationPumpFun(t *testing.T) {
err := runTest("pump-fun")
if err != nil {
t.Fail()
}
}

func TestIntegrationMeteora(t *testing.T) {
err := runTest("meteora")
if err != nil {
t.Fail()
}
}

func TestIntegrationOrca(t *testing.T) {
err := runTest("orca")
if err != nil {
t.Fail()
}
}

func runTest(generatorName string) error {
tempDir, err := os.MkdirTemp("", "temp")
if err != nil {
fmt.Printf("Error creating temporary directory: %v\n", err)
return err
}
fmt.Printf("Temporary directory created: %s\n", tempDir)

defer os.RemoveAll(tempDir)

// Move generator to temp directory
cpCmd := exec.Command("cp", fmt.Sprintf("generators/%s.json", generatorName), fmt.Sprintf("%s/%s.json", tempDir, generatorName))
output, err := cpCmd.CombinedOutput()
if err != nil {
fmt.Printf("Error moving files: %v\n", err)
return err
}
fmt.Printf("Output %s\n", output)

// Set the command to execute
cmd := exec.Command("substreams", "init", "--state-file", fmt.Sprintf("%s.json", generatorName))
cmd.Env = append(os.Environ(), "SUBSTREAMS_CODEGEN_ENDPOINT=https://localhost:9000")
cmd.Dir = tempDir

// Run the command and capture the output
output, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("Error running command: %v\n", err)
return err
}

fmt.Printf("Command output:\n%s\n", output)

// Execute build
cmd1 := exec.Command("substreams", "build")
cmd.Dir = tempDir

output1, err := cmd1.CombinedOutput()
if err != nil {
fmt.Printf("Error running command: %v\n", err)
return err
}

fmt.Printf("Command output:\n%s\n", output1)

return nil
}
Loading