Skip to content

Commit

Permalink
test: fix checkbook logic
Browse files Browse the repository at this point in the history
Signed-off-by: Elias Van Ootegem <[email protected]>
  • Loading branch information
EVODelavega committed Aug 5, 2024
1 parent b946fd1 commit 7119e43
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion core/matching/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,9 @@ func (b *OrderBook) CancelAllOrders(party string) ([]*types.OrderCancellationCon
}

func (b *OrderBook) CheckBook() bool {
checkOBB, checkOBS := true, true
checkOBB, checkOBS := false, false
if len(b.buy.levels) > 0 {
checkOBB = true
for _, o := range b.buy.levels[len(b.buy.levels)-1].orders {
if o.PeggedOrder == nil || o.PeggedOrder.Reference != types.PeggedReferenceBestBid {
checkOBB = false
Expand All @@ -735,6 +736,7 @@ func (b *OrderBook) CheckBook() bool {
}
}
if len(b.sell.levels) > 0 {
checkOBS = true
for _, o := range b.sell.levels[len(b.sell.levels)-1].orders {
if o.PeggedOrder == nil || o.PeggedOrder.Reference != types.PeggedReferenceBestAsk {
checkOBS = false
Expand Down
38 changes: 33 additions & 5 deletions core/matching/orderbook_amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,45 @@ func TestOrderbookAMM(t *testing.T) {
func testOnlyAMMOrders(t *testing.T) {
tst := getTestOrderBookWithAMM(t)
defer tst.ctrl.Finish()
sellPrice := num.NewUint(111)
buyPrice := num.NewUint(95)
// create some regular orders
sell := createOrder(t, tst, 10, sellPrice)
buy := createOrder(t, tst, 10, buyPrice)
buy.Side = types.SideBuy
tst.obs.EXPECT().SubmitOrder(gomock.Any(), gomock.Any(), gomock.Any()).Times(4).Return(nil)
tst.obs.EXPECT().NotifyFinished().Times(3)
_, err := tst.book.SubmitOrder(sell)
require.NoError(t, err)
_, err = tst.book.SubmitOrder(buy)
require.NoError(t, err)
// create some pegged orders
pob := createOrder(t, tst, 100, nil)
pob.Party = "B"
pob.Side = types.SideBuy
pob.PeggedOrder = &types.PeggedOrder{
Reference: types.PeggedReferenceBestBid,
Offset: num.NewUint(10),
}
pos := createOrder(t, tst, 100, nil)
pos.Party = "S"
pos.PeggedOrder = &types.PeggedOrder{
Reference: types.PeggedReferenceBestAsk,
Offset: num.NewUint(10),
}
require.NoError(t, err)
_, err = tst.book.SubmitOrder(pos)
require.NoError(t, err)
require.Equal(t, uint64(1), tst.book.GetPeggedOrdersCount())
// now cancel the non-pegged orders
require.NoError(t, err)
one, zero := uint64(1), uint64(0)

// only buy orders
tst.obs.EXPECT().BestPricesAndVolumes().Times(1).Return(num.UintOne(), one, nil, zero)
check := tst.book.CheckBook()
require.False(t, check)

// only sell orders
tst.obs.EXPECT().BestPricesAndVolumes().Times(1).Return(nil, zero, num.UintOne(), one)
check = tst.book.CheckBook()
require.False(t, check)

// buy and sell orders
tst.obs.EXPECT().BestPricesAndVolumes().Times(1).Return(num.UintOne(), one, num.UintOne(), one)
check = tst.book.CheckBook()
Expand Down

0 comments on commit 7119e43

Please sign in to comment.