From 2f2a32f4975b0cb9c615bafe7efc0e4930ea3a79 Mon Sep 17 00:00:00 2001 From: billettc Date: Tue, 17 Nov 2020 15:18:39 -0500 Subject: [PATCH] bump binary to fix instruction decoding --- go.mod | 2 +- go.sum | 2 ++ serum/instruction.go | 6 +++--- serum/instruction_test.go | 20 ++++++++++++++++++++ serum/types.go | 14 +++++++------- serum/types_test.go | 29 ++++++++++++++++++++--------- 6 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 serum/instruction_test.go diff --git a/go.mod b/go.mod index e87a761..94ef061 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.14 require ( github.com/GeertJohan/go.rice v1.0.0 - github.com/dfuse-io/binary v0.0.0-20201117130902-7df96997b0a8 + github.com/dfuse-io/binary v0.0.0-20201117201711-8656308cf309 github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79 github.com/google/go-cmp v0.4.1 // indirect github.com/gorilla/rpc v1.2.0 diff --git a/go.sum b/go.sum index d36c463..1c5e02d 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dfuse-io/binary v0.0.0-20201117130902-7df96997b0a8 h1:/bHa8o+/kXoQU4VBLPm+Pe8aR+UPotIqzh3UVNi11cA= github.com/dfuse-io/binary v0.0.0-20201117130902-7df96997b0a8/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0= +github.com/dfuse-io/binary v0.0.0-20201117201711-8656308cf309 h1:GO2JfJeCROHoV48h+1ioDCTW2sqeu7exi3cZrzIak+g= +github.com/dfuse-io/binary v0.0.0-20201117201711-8656308cf309/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0= github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79 h1:+HRtcJejUYA/2rnyTMbOaZ4g7f4aVuFduTV/03dbpLY= github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79/go.mod h1:V+ED4kT/t/lKtH99JQmKIb0v9WL3VaYkJ36CfHlVECI= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= diff --git a/serum/instruction.go b/serum/instruction.go index 9f4cceb..505ebeb 100644 --- a/serum/instruction.go +++ b/serum/instruction.go @@ -21,9 +21,9 @@ func registryDecodeInstruction(accounts []solana.PublicKey, rawInstruction *sola return inst, nil } -func DecodeInstruction(accounts []solana.PublicKey, rawInstruction *solana.CompiledInstruction) (*Instruction, error) { +func DecodeInstruction(accounts []solana.PublicKey, compiledInstruction *solana.CompiledInstruction) (*Instruction, error) { var inst *Instruction - if err := bin.NewDecoder(rawInstruction.Data).Decode(&inst); err != nil { + if err := bin.NewDecoder(compiledInstruction.Data).Decode(&inst); err != nil { return nil, fmt.Errorf("unable to decode instruction for serum program: %w", err) } @@ -165,7 +165,7 @@ type MatchOrderAccounts struct { type InstructionMatchOrder struct { Limit uint16 - Accounts *MatchOrderAccounts `bin="-"` + Accounts *MatchOrderAccounts `bin:"-"` } func (i *InstructionMatchOrder) setAccounts(accounts []solana.PublicKey) error { diff --git a/serum/instruction_test.go b/serum/instruction_test.go new file mode 100644 index 0000000..17f95a1 --- /dev/null +++ b/serum/instruction_test.go @@ -0,0 +1,20 @@ +package serum + +import ( + "encoding/hex" + "fmt" + "testing" + + bin "github.com/dfuse-io/binary" + "github.com/stretchr/testify/require" +) + +func TestDecodeInstruction(t *testing.T) { + x := `00020000000500` + data, err := hex.DecodeString(x) + require.NoError(t, err) + var instruction *Instruction + err = bin.NewDecoder(data).Decode(&instruction) + require.NoError(t, err) + fmt.Println(instruction) +} diff --git a/serum/types.go b/serum/types.go index f3aaf9c..dbb35b6 100644 --- a/serum/types.go +++ b/serum/types.go @@ -239,14 +239,14 @@ func (q *EventQueue) Decode(data []byte) error { return fmt.Errorf("event queue: decode header: %w", err) } - remainingData := decoder.Remaining() - if remainingData%EventDataLength != 0 { - return fmt.Errorf("event queue: wrong event data length %d", remainingData) - } + //remainingData := decoder.Remaining() + //if remainingData%EventDataLength != 0 { + // return fmt.Errorf("event queue: wrong event data length %d", remainingData) + //} + // + //eventCount := remainingData / EventDataLength - eventCount := remainingData / EventDataLength - - for i := 0; i < eventCount; i++ { + for decoder.Remaining() >= 88 { var e *Event err = decoder.Decode(&e) if err != nil { diff --git a/serum/types_test.go b/serum/types_test.go index 062585c..4c2cbc6 100644 --- a/serum/types_test.go +++ b/serum/types_test.go @@ -1,6 +1,7 @@ package serum import ( + "context" "encoding/base64" "encoding/hex" "encoding/json" @@ -8,6 +9,8 @@ import ( "io/ioutil" "testing" + "github.com/dfuse-io/solana-go/rpc" + bin "github.com/dfuse-io/binary" "github.com/dfuse-io/solana-go" "github.com/stretchr/testify/assert" @@ -29,19 +32,27 @@ func TestDecoder_Market(t *testing.T) { } func TestDecoder_Event(t *testing.T) { - b64 := `c2VydW0RAAAAAAAAAKMoAAAAAAAAAAAAAAAAAAAD+gUAAAAAAAYJAAAAAAAAP9MRAAAAAACwUAY6AAAAAAAAAAAAAAAA/Af4//////8aBQAAAAAAAJGeNN64UdRK+szEsGLeTBiPnrTkfJaOEzsacSpRiAYs6zimHOimKK8CAQAAAAAAAEC6CycAAAAAAAAAAAAAAAAAAAAAAAAAAP/3BwAAAAAAIAUAAAAAAACRnjTeuFHUSvrMxLBi3kwYj5605HyWjhM7GnEqUYgGLH2wFQ01ceby` + //b64 := `c2VydW0RAAAAAAAAAKMoAAAAAAAAAAAAAAAAAAAD+gUAAAAAAAYJAAAAAAAAP9MRAAAAAACwUAY6AAAAAAAAAAAAAAAA/Af4//////8aBQAAAAAAAJGeNN64UdRK+szEsGLeTBiPnrTkfJaOEzsacSpRiAYs6zimHOimKK8CAQAAAAAAAEC6CycAAAAAAAAAAAAAAAAAAAAAAAAAAP/3BwAAAAAAIAUAAAAAAACRnjTeuFHUSvrMxLBi3kwYj5605HyWjhM7GnEqUYgGLH2wFQ01ceby` - q := &EventQueue{} - err := q.DecodeFromBase64(b64) + client := rpc.NewClient("http://api.mainnet-beta.solana.com:80/rpc") + info, err := client.GetAccountInfo(context.Background(), solana.MustPublicKeyFromBase58("13iGJcA4w5hcJZDjJbJQor1zUiDLE4jv2rMW9HkD5Eo1")) require.NoError(t, err) + q := &EventQueue{} + err = q.Decode(info.Value.Data) + require.NoError(t, err) + + fmt.Println("data length:", len(info.Value.Data)) fmt.Println("serum?:", string(q.Header.Serum[:])) - for _, e := range q.Events { - fmt.Println("Type:", e.Flag) - fmt.Println("Side:", e.Side()) - fmt.Println("Filled:", e.Filled()) - fmt.Println("event owner:", e.Owner) - } + fmt.Println("count:", q.Header.Count) + fmt.Println("seq", q.Header.SeqNum) + fmt.Println("events", len(q.Events)) + //for _, e := range q.Events { + // fmt.Println("Type:", e.Flag) + // fmt.Println("Side:", e.Side()) + // fmt.Println("Filled:", e.Filled()) + // fmt.Println("event owner:", e.Owner) + //} }