diff --git a/go.mod b/go.mod index 3eb0d38..ba5fb88 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-20201113172957-3eda1bb6412c + github.com/dfuse-io/binary v0.0.0-20201113225852-eb73efbfa745 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 110f565..a0598ab 100644 --- a/go.sum +++ b/go.sum @@ -79,6 +79,8 @@ github.com/dfuse-io/binary v0.0.0-20201113172717-8fc2fd76c8d2 h1:qeVi/lwj1tYUxdE github.com/dfuse-io/binary v0.0.0-20201113172717-8fc2fd76c8d2/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0= github.com/dfuse-io/binary v0.0.0-20201113172957-3eda1bb6412c h1:ufKbFyLlpy/Rg6iErIBcsTqnql66Fq7iZynLfhYpNKg= github.com/dfuse-io/binary v0.0.0-20201113172957-3eda1bb6412c/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0= +github.com/dfuse-io/binary v0.0.0-20201113225852-eb73efbfa745 h1:41kLpvyYOkN/VenBzd5fwH069Oc11xJTvGwqK3kf6IU= +github.com/dfuse-io/binary v0.0.0-20201113225852-eb73efbfa745/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0= github.com/dfuse-io/dauth v0.0.0-20200529171443-21c0e2d262c2/go.mod h1:tab5biQg59hbrHjaXTjaLcsP3zblx6B0I0Keu03xB44= github.com/dfuse-io/derr v0.0.0-20200406214256-c690655246a1/go.mod h1:JW/hUKChGd6ytDtvwx4JFo57m1pnFvMxaq9WbDAb2fQ= github.com/dfuse-io/derr v0.0.0-20200417132224-d333cfd0e9a0/go.mod h1:/DjjRCyTi/KIiI1E1hnhvfWwg+K06jo+qM4VcjHvHq4= diff --git a/init_test.go b/init_test.go index 9f16cba..3887bb3 100644 --- a/init_test.go +++ b/init_test.go @@ -1,11 +1,9 @@ package solana import ( - "github.com/dfuse-io/logging" "go.uber.org/zap" ) func init() { zlog, _ = zap.NewDevelopment() - logging.TestingOverride() } diff --git a/serum/init_test.go b/serum/init_test.go new file mode 100644 index 0000000..627f33a --- /dev/null +++ b/serum/init_test.go @@ -0,0 +1,7 @@ +package serum + +import "github.com/dfuse-io/logging" + +func init() { + logging.TestingOverride() +} diff --git a/serum/types.go b/serum/types.go index 9144398..ab8f7ac 100644 --- a/serum/types.go +++ b/serum/types.go @@ -18,10 +18,9 @@ import ( "fmt" "math/big" - "go.uber.org/zap" - bin "github.com/dfuse-io/binary" "github.com/dfuse-io/solana-go" + "go.uber.org/zap" ) type MarketV2 struct { @@ -166,3 +165,39 @@ func (s *SlabLeafNode) GetPrice() *big.Int { v := new(big.Int).SetBytes(raw[0 : len(raw)-8]) return v } + +type EventQueueHeader struct { + Serum [5]byte + AccountFlags uint64 + Head uint64 + Count uint64 + SeqNum uint64 +} + +type EventFlag uint8 + +const ( + FillEventFlag = iota + OutEventFlag + BidEventFlag + MakerEventFlag +) + +var EventFlagStrings = []string{"FILL", "OUT", "BID", "MAKER"} + +type Event struct { + Flag uint8 + OwnerSlot uint8 + FeeTier uint8 + Padding [5]uint8 + NativeQtyReleased uint64 + NativeQtyPaid uint64 + NativeFeeOrRebate uint64 + OrderID bin.Uint128 + Owner solana.PublicKey + ClientOrderID uint64 +} + +func (e *Event) Type() string { + return EventFlagStrings[e.Flag] +} diff --git a/serum/types_test.go b/serum/types_test.go index fde51b5..0a6f90e 100644 --- a/serum/types_test.go +++ b/serum/types_test.go @@ -1,6 +1,7 @@ package serum import ( + "encoding/base64" "encoding/hex" "encoding/json" "fmt" @@ -13,6 +14,48 @@ import ( "github.com/stretchr/testify/require" ) +func TestDecoder_Market(t *testing.T) { + b64 := `c2VydW0DAAAAAAAAAF4kKlwSa8cc6xshYrDN0SrwrDDLBBUwemtddQHhfjgKAQAAAAAAAACL34duLBe2W5K3QFyI1rhNSESYe+cR/nc2UqvgE9x1VMb6evO+2606PWXzaqvJdDGxu+TC0vbg5HymAgNFL11habDAgiZH59TQw5/Y/52i1DhnPZFYOUB4C3G0hhSSXiRAZw8oAwAAAAAAAAAAAAAANvvq/rQwheCOf85MPshRgZEhXzDFAUh3IjalXs/zJ3I5cTmQBAAAABoqGA0AAAAAZAAAAAAAAACuBhNqk2KYdlbj/V5jbAGnnybh+XBss48/P00r053wbACx0Z1WrY+X9jL+huHdyUdpKzL/JScDimaQlNfzjpWANi1Nu6kEazO0bu0NkhnKFyQt2psF0SRCimAVpNimaOjou1Esrd0dKTtLbedHvt62Vi1bRJYveY74GEP6vkH/qBAnAAAAAAAACgAAAAAAAAAAAAAAAAAAAMsrAAAAAAAAcGFkZGluZw==` + + data, err := base64.StdEncoding.DecodeString(b64) + require.NoError(t, err) + + var m *MarketV2 + err = bin.NewDecoder(data).Decode(&m) + require.NoError(t, err) + + fmt.Println("market event queue:", m.EventQueue.String()) + +} +func TestDecoder_Event(t *testing.T) { + + b64 := `c2VydW0RAAAAAAAAAKMoAAAAAAAAAAAAAAAAAAAD+gUAAAAAAAYJAAAAAAAAP9MRAAAAAACwUAY6AAAAAAAAAAAAAAAA/Af4//////8aBQAAAAAAAJGeNN64UdRK+szEsGLeTBiPnrTkfJaOEzsacSpRiAYs6zimHOimKK8CAQAAAAAAAEC6CycAAAAAAAAAAAAAAAAAAAAAAAAAAP/3BwAAAAAAIAUAAAAAAACRnjTeuFHUSvrMxLBi3kwYj5605HyWjhM7GnEqUYgGLH2wFQ01ceby` + + data, err := base64.StdEncoding.DecodeString(b64) + headerData := data[0:38] + eventData := data[37:] + require.NoError(t, err) + + fmt.Println("Size of data:", len(data), hex.EncodeToString(data)) + + //read the header + //read the rest ... + var h *EventQueueHeader + err = bin.NewDecoder(headerData).Decode(&h) + + var events [2]*Event + err = bin.NewDecoder(eventData).Decode(&events) + + require.NoError(t, err) + + for _, e := range events { + fmt.Println("serum?:", string(h.Serum[:])) + fmt.Println("Type:", e.Flag) + fmt.Println("event owner:", e.Owner) + } + +} + func TestDecoder_Orderbook(t *testing.T) { cnt, err := ioutil.ReadFile("./testdata/orderbook.hex") require.NoError(t, err)