Skip to content

Commit

Permalink
Decode orders.
Browse files Browse the repository at this point in the history
  • Loading branch information
abourget committed Nov 6, 2020
1 parent 3aa8713 commit e01bec9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 40 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ require (
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
google.golang.org/api v0.13.0
)

replace github.com/lunixbochs/struc => /home/abourget/build/struc
84 changes: 63 additions & 21 deletions serum/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package serum

import (
"bytes"
"fmt"
"io"

"github.com/dfuse-io/solana-go"
"github.com/lunixbochs/struc"
Expand Down Expand Up @@ -63,7 +63,7 @@ type Orderbook struct {
AccountFlags solana.U64 `struc:"uint64,little"`
// SLAB_LAYOUT
// SLAB_HEADER_LAYOUT
BumpIndex uint32 `struc:"uint32,sizeof=Nodes"`
BumpIndex uint32 `struc:"uint32,little,sizeof=Nodes"`
ZeroPaddingA [4]byte `json:"-" struc:"[4]pad"`
FreeListLen uint32 `struc:"uint32,little"`
ZeroPaddingB [4]byte `json:"-" struc:"[4]pad"`
Expand Down Expand Up @@ -110,17 +110,11 @@ var slabInstructionDef = solana.NewVariantDefinition([]solana.VariantType{
})

type SlabNode struct {
Type solana.Varuint16
Variant interface{}
Type uint32 `struc:"uint32,little"`
Blob []byte `struc:"[68]byte"`
}

func (s *SlabNode) Unpack(r io.Reader, length int, opt *struc.Options) (err error) {
fmt.Println("Unpacking SlabNode")

if err = struc.Unpack(r, &s.Type); err != nil {
return
}

func (s *SlabNode) Variant() (interface{}, error) {
var el interface{}
switch s.Type {
case 0:
Expand All @@ -134,19 +128,67 @@ func (s *SlabNode) Unpack(r io.Reader, length int, opt *struc.Options) (err erro
case 4:
el = &SlabLastFreeNode{}
default:
return fmt.Errorf("unsupported SlabNode variant %d", s.Type)
return nil, fmt.Errorf("unsupported SlabNode variant %d", s.Type)
}

return struc.Unpack(r, el)
}

func (s SlabNode) Pack(p []byte, opt *struc.Options) (written int, err error) {
return 0, nil
if err := struc.Unpack(bytes.NewReader(s.Blob), el); err != nil {
return nil, err
}
return el, nil
}

func (s SlabNode) Size(opt *struc.Options) int { return 0 }

func (s SlabNode) String() string { return fmt.Sprintf("variant %d, %T", s.Type, s.Variant) }
// func (s *SlabNode) Unpack(r io.Reader, length int, opt *struc.Options) (err error) {
// fmt.Println("Unpacking SlabNode")

// if err = struc.Unpack(r, &s.Type); err != nil {
// return
// }

// if err = struc.Unpack(r, &s.Blob); err != nil {
// return
// }

// var el interface{}
// switch s.Type {
// case 0:
// el = &SlabUninitialized{}
// case 1:
// el = &SlabInnerNode{}
// case 2:
// el = &SlabLeafNode{}
// case 3:
// el = &SlabFreeNode{}
// case 4:
// el = &SlabLastFreeNode{}
// default:
// return fmt.Errorf("unsupported SlabNode variant %d", s.Type)
// }

// if err := struc.Unpack(bytes.NewReader(s.Blob), el); err != nil {
// return err
// }

// s.Variant = el

// return nil
// }

// func (s SlabNode) Pack(p []byte, opt *struc.Options) (written int, err error) {
// return 0, nil
// }

// func (s SlabNode) Size(opt *struc.Options) int {
// s1, err := struc.Sizeof(s.Type)
// if err != nil {
// panic(err)
// }
// s2, err := struc.Sizeof(s.Blob)
// if err != nil {
// panic(err)
// }
// return s1 + s2
// }

// func (s SlabNode) String() string { return fmt.Sprintf("variant %d, %T", s.Type, s.Variant) }

type SlabUninitialized struct {
}
Expand Down
16 changes: 13 additions & 3 deletions serum/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package serum
import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"testing"

"github.com/lunixbochs/struc"
Expand Down Expand Up @@ -46,13 +46,23 @@ func TestMarketDecode(t *testing.T) {
}

func TestOrderDecode(t *testing.T) {
hexData := `736572756d2100000000000000650000000000000044000000000000004c0000000000000011000000000000000100000035000000010babffffffffff4105000000000000400000003f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000007060000e92ba8ffffffffff6a050000000000005b4388a3431832af5742b863e200b8e733ce451f27006e2723d9198b363355d18813000000000000c6a1933c53bc44160200000003060000ad2ba8ffffffffff6b050000000000005b4388a3431832af5742b863e200b8e733ce451f27006e2723d9198b363355d18813000000000000f49ba2465bbc441603000000340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000140600003334a8ffffffffff66050000000000005ae01b52d00a090c6dc6fce8e37a225815cff2223a99c6dfdad5aae56d3db6705246000000000000c837c70ea5737ca70200000013060000e234a8ffffffffff67050000000000005ae01b52d00a090c6dc6fce8e37a225815cff2223a99c6dfdad5aae56d3db670e367000000000000b1223a1fe53f3899030000006200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000003200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000003d000000965ca8ffffffffff600500`
cnt, err := hex.DecodeString(hexData)
cnt, err := ioutil.ReadFile("/tmp/orders")
require.NoError(t, err)
//hexData := `736572756d2100000000000000650000000000000044000000000000004c0000000000000011000000000000000100000035000000010babffffffffff4105000000000000400000003f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000007060000e92ba8ffffffffff6a050000000000005b4388a3431832af5742b863e200b8e733ce451f27006e2723d9198b363355d18813000000000000c6a1933c53bc44160200000003060000ad2ba8ffffffffff6b050000000000005b4388a3431832af5742b863e200b8e733ce451f27006e2723d9198b363355d18813000000000000f49ba2465bbc441603000000340000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000140600003334a8ffffffffff66050000000000005ae01b52d00a090c6dc6fce8e37a225815cff2223a99c6dfdad5aae56d3db6705246000000000000c837c70ea5737ca70200000013060000e234a8ffffffffff67050000000000005ae01b52d00a090c6dc6fce8e37a225815cff2223a99c6dfdad5aae56d3db670e367000000000000b1223a1fe53f3899030000006200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000003200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000003d000000965ca8ffffffffff600500`
// cnt, err := hex.DecodeString(hexData)
// require.NoError(t, err)

var o Orderbook
require.NoError(t, struc.Unpack(bytes.NewReader(cnt), &o))

cnt, _ = json.MarshalIndent(o, "", " ")
fmt.Println(string(cnt))

for idx, n := range o.Nodes {
fmt.Println("idx", idx)
el, err := n.Variant()
require.NoError(t, err)
out, _ := json.MarshalIndent(el, "", " ")
fmt.Println(string(out))
}
}
15 changes: 0 additions & 15 deletions system/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@ type SystemInstruction struct {
Variant interface{}
}

func NewInstruction(impl interface{}) *SystemInstruction {
si := &SystemInstruction{Variant: impl}
switch impl.(type) {
case *CreateAccount:
si.Type = 0
case *Assign:
si.Type = 1
case *Transfer:
si.Type = 2
default:
return nil
}
return si
}

func (si *SystemInstruction) Unpack(r io.Reader, length int, opt *struc.Options) (err error) {
fmt.Println("CALLING OUR SI UNPACK", length)

Expand Down
2 changes: 1 addition & 1 deletion system/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestSystemInstructions(t *testing.T) {
out := SystemInstruction{}

require.NoError(t, struc.Unpack(bytes.NewReader(buf.Bytes()), &out))
assert.Equal(t, "hello", out.String())
assert.Equal(t, "hello", out.String(), out.Variant)

// tests := []struct{
// name string
Expand Down

0 comments on commit e01bec9

Please sign in to comment.