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

Solana Anchor Decoding #8

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/huandu/xstrings v1.4.0
github.com/iancoleman/strcase v0.3.0
github.com/lib/pq v1.10.9
github.com/mr-tron/base58 v1.2.0
github.com/rs/cors v1.10.0
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249 h1:NHrXEjTNQY7P0Zfx1aMrNhpgxHmow66XQtm0aQLY0AE=
github.com/nsf/jsondiff v0.0.0-20210926074059-1e845ec5d249/go.mod h1:mpRZBD8SJ55OIICQ3iWH0Yz3cjzA61JdqMLoWXeB2+8=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down
1 change: 1 addition & 0 deletions server/handler_convo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
_ "github.com/streamingfast/substreams-codegen/evm-minimal"
_ "github.com/streamingfast/substreams-codegen/injective-events"
_ "github.com/streamingfast/substreams-codegen/injective-minimal"
_ "github.com/streamingfast/substreams-codegen/sol-anchor"
_ "github.com/streamingfast/substreams-codegen/sol-minimal"
_ "github.com/streamingfast/substreams-codegen/sol-transactions"
_ "github.com/streamingfast/substreams-codegen/starknet-events"
Expand Down
170 changes: 170 additions & 0 deletions sol-anchor/convo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package solanchor

import (
"encoding/json"
"fmt"
"strconv"

"github.com/mr-tron/base58"
codegen "github.com/streamingfast/substreams-codegen"
"github.com/streamingfast/substreams-codegen/loop"
"github.com/tidwall/sjson"
)

type Convo struct {
*codegen.Conversation[*Project]
}

func New() codegen.Converser {
return &Convo{&codegen.Conversation[*Project]{
State: &Project{},
}}
}

func init() {
codegen.RegisterConversation(
"sol-anchor",
"Get Solana transactions filtered by one or several Program IDs",
"Allows you to specified a regex containing the Program IDs used to filter the Solana transactions",
codegen.ConversationFactory(New),
100,
)
}

var cmd = codegen.Cmd

func (c *Convo) NextStep() loop.Cmd {
p := c.State
if p.Name == "" {
return cmd(codegen.AskProjectName{})
}

if p.idl == nil {
return cmd(AskIdl{})
}

if p.ChainName == "" {
return cmd(codegen.AskChainName{})
}

if p.idl.ProgramID() == "" {
return cmd(AskProgramID{})
}

if !p.InitialBlockSet {
return cmd(codegen.AskInitialStartBlockType{})
}

return cmd(codegen.RunGenerate{})
}

func (c *Convo) Update(msg loop.Msg) loop.Cmd {
switch msg := msg.(type) {
case codegen.MsgStart:
var msgCmd loop.Cmd
if msg.Hydrate != nil {
if err := json.Unmarshal([]byte(msg.Hydrate.SavedState), &c.State); err != nil {
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))
}

msgCmd = c.Msg().Message("Ok, I reloaded your state.").Cmd()
} else {
msgCmd = c.Msg().Message("Ok, let's start a new package.").Cmd()
}
return loop.Seq(msgCmd, c.NextStep())

case codegen.AskProjectName:
return c.CmdAskProjectName()

case codegen.InputProjectName:
c.State.Name = msg.Value
return c.NextStep()

case codegen.AskChainName:
labels := []string{"Solana Mainnet", "Solana Devnet"}
values := []string{"solana-mainnet", "solana-devnet"}
return c.Action(codegen.InputChainName{}).ListSelect("Please select the chain").
Labels(labels...).
Values(values...).
Cmd()

case codegen.InputChainName:
c.State.ChainName = msg.Value
return c.NextStep()

case codegen.AskInitialStartBlockType:
return c.Action(codegen.InputAskInitialStartBlockType{}).
TextInput(codegen.InputAskInitialStartBlockTypeTextInput(), "Submit").
DefaultValue("0").
Validation(codegen.InputAskInitialStartBlockTypeRegex(), codegen.InputAskInitialStartBlockTypeValidation()).
Cmd()

case codegen.InputAskInitialStartBlockType:
initialBlock, err := strconv.ParseUint(msg.Value, 10, 64)
if err != nil {
return loop.Quit(fmt.Errorf("invalid start block input value %q, expected a number", msg.Value))
}

c.State.InitialBlock = initialBlock
c.State.InitialBlockSet = true
return c.NextStep()

case AskIdl:
return c.Action(InputIdl{}).
TextInput("Input the Anchor IDL in JSON format\n", "Submit").
Cmd()

case InputIdl:
idl := &IDL{}
err := json.Unmarshal([]byte(msg.Value), &idl)
if err != nil {
fmt.Println("Error unmarshaling JSON:", err)
return loop.Quit(fmt.Errorf("could not decode IDL"))
}
if idl.Metadata.Name == "" {
idl.Metadata.Name = c.State.Name // we need a name so anchor can compile
}
c.State.idl = idl
c.State.IdlString = msg.Value
return c.NextStep()

case AskProgramID:
return c.Action(InputProgramID{}).
TextInput("Cannot get the ProgramID from the IDL. Please input the Program ID to match.\n", "Submit").
Cmd()

case InputProgramID:
newIDLString, err := sjson.Set(c.State.IdlString, "metadata.address", msg.Value)
if err != nil {
return loop.Quit(fmt.Errorf("could not set ProgramID in IDL: %w", err))
}
c.State.IdlString = newIDLString

c.State.idl.Metadata.Address = msg.Value
b, err := base58.Decode(msg.Value)
if err != nil || len(b) != 32 {
return loop.Seq(
c.Msg().Message("This address is not a valid base58-encoded solana address").Cmd(),
c.NextStep(),
)
}
return c.NextStep()

case codegen.RunGenerate:
str := ""
for _, event := range c.State.idl.Events {
for _, field := range event.Fields {
fmt.Println("-----------------------------")
fmt.Println(field.Name)

str += field.Type.Simple
}
}
return c.CmdGenerate(c.State.Generate)

case codegen.ReturnGenerate:
return c.CmdDownloadFiles(msg)
}

return loop.Quit(fmt.Errorf("invalid loop message: %T", msg))
}
24 changes: 24 additions & 0 deletions sol-anchor/convo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package solanchor

import (
"testing"

codegen "github.com/streamingfast/substreams-codegen"
"github.com/streamingfast/substreams-codegen/loop"
"github.com/stretchr/testify/assert"
)

func TestConvoNextStep(t *testing.T) {
convo := New()
next := func() loop.Msg {
return convo.NextStep()()
}
p := convo.(*Convo).State

assert.Equal(t, codegen.AskProjectName{}, next())
p.Name = "my-proj"

res := p.Generate()
assert.NoError(t, res.Err)
assert.NotEmpty(t, res.ProjectFiles)
}
25 changes: 25 additions & 0 deletions sol-anchor/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package solanchor

import (
"embed"

codegen "github.com/streamingfast/substreams-codegen"
)

//go:embed templates/*
var templatesFS embed.FS

// use the output type form the Project to render the templates
func (p *Project) Generate() codegen.ReturnGenerate {
return codegen.GenerateTemplateTree(p, templatesFS, map[string]string{
"proto/program.proto.gotmpl": "proto/program.proto",
"idls/program.json.gotmpl": "idls/program.json",
"src/lib.rs.gotmpl": "src/lib.rs",
"src/idl/mod.rs.gotmpl": "src/idl/mod.rs",
".gitignore.gotmpl": ".gitignore",
"buf.gen.yaml.gotmpl": "buf.gen.yaml",
"Cargo.lock.gotmpl": "Cargo.lock",
"Cargo.toml.gotmpl": "Cargo.toml",
"substreams.yaml.gotmpl": "substreams.yaml",
})
}
Loading