serum: added order helper function

This commit is contained in:
Julien Cassis 2021-02-18 09:32:37 -05:00
parent 3bc141743d
commit 091447277e
3 changed files with 50 additions and 17 deletions

View File

@ -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 ""

View File

@ -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],

View File

@ -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) {