From 091447277e249a7521f117b3dfec59f547df1941 Mon Sep 17 00:00:00 2001 From: Julien Cassis Date: Thu, 18 Feb 2021 09:32:37 -0500 Subject: [PATCH] serum: added order helper function --- diff/diff.go | 21 ++++++++++++++++++++ programs/serum/types.go | 8 ++++++++ programs/serum/types_test.go | 38 ++++++++++++++++++++---------------- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index c5140ff..7c075ab 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -60,6 +60,27 @@ func (k Kind) String() string { type Path cmp.Path +func (pa Path) SliceIndex() (int, bool) { + last := pa[len(pa)-1] + if slcIdx, ok := last.(cmp.SliceIndex); ok { + xkey, ykey := slcIdx.SplitKeys() + switch { + case xkey == ykey: + return xkey, true + case ykey == -1: + // [5->?] means "I don't know where X[5] went" + return xkey, true + case xkey == -1: + // [?->3] means "I don't know where Y[3] came from" + return ykey, true + default: + // [5->3] means "X[5] moved to Y[3]" + return ykey, true + } + } + return 0, false +} + func (pa Path) String() string { if len(pa) == 1 { return "" diff --git a/programs/serum/types.go b/programs/serum/types.go index 0ea5caf..65aa0c6 100644 --- a/programs/serum/types.go +++ b/programs/serum/types.go @@ -281,6 +281,14 @@ type Order struct { side Side } +func (o *Order) SeqNum() uint64 { + return o.ID.SeqNum(o.side) +} + +func (o *Order) Price() uint64 { + return o.ID.Price() +} + func (o *OpenOrders) GetOrder(index uint32) *Order { order := &Order{ ID: o.Orders[index], diff --git a/programs/serum/types_test.go b/programs/serum/types_test.go index 0c23cae..efb6494 100644 --- a/programs/serum/types_test.go +++ b/programs/serum/types_test.go @@ -271,29 +271,33 @@ func Test_OpenOrderDiff(t *testing.T) { require.NoError(t, err) writeFile(t, newDataJSONFile, newCnt) - fmt.Println("==>> All diff(s)") - + hasNewOrder := false + newOrderIndex := uint32(0) diff.Diff(oldOpenOrders, newOpenOrders, diff.OnEvent(func(event diff.Event) { - fmt.Printf("Event: %s, path: %s\n", event.String(), event.Path.String()) - if match, _ := event.RawMatch("^IsBidBits.*"); match { - isBid := event.Element().Interface().(uint64) - fmt.Println("is bid bits") - fmt.Println(isBid) - fmt.Println(newOpenOrders.IsBidBits.Hi) - fmt.Println(newOpenOrders.IsBidBits.Lo) - fmt.Println(oldOpenOrders.IsBidBits.Lo) - } if match, _ := event.Match("Orders[#]"); match { - fmt.Println("order diff") - - orderId := event.Element().Interface().(OrderID) switch event.Kind { case diff.KindAdded: - fmt.Println(orderId.Price()) + if index, found := event.Path.SliceIndex(); found { + hasNewOrder = true + newOrderIndex = uint32(index) + } } } })) + assert.Equal(t, hasNewOrder, true) + assert.Equal(t, newOrderIndex, uint32(20)) + newOrder := newOpenOrders.GetOrder(newOrderIndex) + assert.Equal(t, &Order{ + ID: OrderID{ + Hi: 0x0000000000000840, + Lo: 0xffffffffffacdefd, + }, + side: SideBid, + }, newOrder) + assert.Equal(t, newOrder.SeqNum(), uint64(5447938)) + assert.Equal(t, newOrder.Price(), uint64(2112)) + } func Test_OpenOrder_GetOrder(t *testing.T) { @@ -309,8 +313,8 @@ func Test_OpenOrder_GetOrder(t *testing.T) { }, side: SideBid, }, o) - assert.Equal(t, o.ID.SeqNum(o.side), uint64(5447938)) - assert.Equal(t, o.ID.Price(), uint64(2112)) + assert.Equal(t, o.SeqNum(), uint64(5447938)) + assert.Equal(t, o.Price(), uint64(2112)) } func TestIsBitZero(t *testing.T) {