Skip to content

Commit

Permalink
fix: fix bug in batch matching
Browse files Browse the repository at this point in the history
last price was being set to 0 if there was no matching in phase 2
  • Loading branch information
kingcre committed Sep 14, 2023
1 parent 8284117 commit d0953ae
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
49 changes: 49 additions & 0 deletions x/exchange/keeper/batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package keeper_test

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

utils "github.com/crescent-network/crescent/v5/types"
)

func (s *KeeperTestSuite) TestBatchMatchingEdgecase() {
market := s.CreateMarket("ucre", "uusd")

mmAddr := s.FundedAccount(1, enoughCoins)
s.MakeLastPrice(market.Id, mmAddr, utils.ParseDec("1"))

ordererAddr1 := s.FundedAccount(2, enoughCoins)
ordererAddr2 := s.FundedAccount(3, enoughCoins)

s.PlaceBatchLimitOrder(
market.Id, ordererAddr1, false, utils.ParseDec("1"), sdk.NewDec(5_000000), time.Hour)
s.PlaceBatchLimitOrder(
market.Id, ordererAddr1, false, utils.ParseDec("1.001"), sdk.NewDec(10_000000), time.Hour)
s.PlaceBatchLimitOrder(
market.Id, ordererAddr1, false, utils.ParseDec("1.002"), sdk.NewDec(10_000000), time.Hour)

order := s.PlaceBatchLimitOrder(
market.Id, ordererAddr2, true, utils.ParseDec("1"), sdk.NewDec(10_000000), time.Hour)
s.PlaceBatchLimitOrder(
market.Id, ordererAddr2, true, utils.ParseDec("1.01"), sdk.NewDec(1_000000), time.Hour)

// Order book looks like:
// | 1.010 | #
// ########## | 1.001 |
// ########## | 1.002 |
// ##### | 1.000 | ##########

// After batch matching, it should look like:
// | 1.010 |
// ########## | 1.001 |
// ########## | 1.002 |
// | 1.000 | ######

s.NextBlock()

s.AssertEqual(utils.ParseDec("1"), *s.App.ExchangeKeeper.MustGetMarketState(s.Ctx, market.Id).LastPrice)
order = s.keeper.MustGetOrder(s.Ctx, order.Id)
s.AssertEqual(sdk.NewDec(6_000000), order.OpenQuantity)
}
5 changes: 4 additions & 1 deletion x/exchange/types/matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ func (ctx *MatchingContext) RunSinglePriceAuction(buyObs, sellObs *MemOrderBookS
}

func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBookSide, lastPrice sdk.Dec) (newLastPrice sdk.Dec, matched bool) {
newLastPrice = lastPrice // in case of there's no matching at all

// Phase 1: Match orders with price below(or equal to) the last price and
// price above(or equal to) the last price.
// The execution price is the last price.
Expand All @@ -288,7 +290,8 @@ func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBo
matched = true
}
// If there's no more levels to match, return earlier.
if buyLevelIdx >= len(buyObs.levels) || sellLevelIdx >= len(sellObs.levels) {
if (buyLevelIdx >= len(buyObs.levels) || sellLevelIdx >= len(sellObs.levels)) ||
buyObs.levels[buyLevelIdx].price.LT(sellObs.levels[sellLevelIdx].price) {
return lastPrice, matched
}

Expand Down

0 comments on commit d0953ae

Please sign in to comment.