Skip to content

Commit

Permalink
Merge branch 'develop' into idl_add_accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
enoldev committed Dec 5, 2024
2 parents 30bd4ca + 048425f commit 939cdcf
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 4 deletions.
7 changes: 7 additions & 0 deletions sol-anchor/convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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
15 changes: 11 additions & 4 deletions sol-anchor/templates/proto/program.proto.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ message {{$event.Name}} {
{{- $counter := 1 }}
{{- 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 @@ -30,7 +30,7 @@ message {{$instruction.PascalCaseName}} {
{{- $counter := 1 }}
{{- 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}}
{{- range $i, $acc := $instruction.Accounts }}
{{- if not $acc.Address }}
Expand All @@ -43,6 +43,13 @@ message {{$instruction.PascalCaseName}} {

{{ range $i, $type := .Idl.Types }}
{{- if $type.Type.IsStruct }}
message {{$type.Name}} {
{{- $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}}{{ if $.Idl.IsTypeEnum $field.Type.ResolveProtobufType }}Enum{{end}} {{$field.SnakeCaseName}} = {{ $counter }};
{{- end}}
}
message {{$type.Name}} {
{{- $counter := 0 }}
{{- range $i, $field := $type.Type.Struct.Fields }}
Expand All @@ -51,9 +58,9 @@ message {{$type.Name}} {
{{- 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 @@ -15,7 +15,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
}

0 comments on commit 939cdcf

Please sign in to comment.