Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not update matching height when there's no matching #179

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions x/exchange/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,29 @@ func (k Keeper) RunBatchMatching(ctx sdk.Context, market types.Market) (err erro
PriceLimit: &bestBuyPrice,
}, escrow)

var lastPrice sdk.Dec
marketState := k.MustGetMarketState(ctx, market.Id)
defer func() {
// If there was an error, exit early.
if err != nil {
return
}
// If there was no matching, exit early, too.
if lastPrice.IsNil() {
return
}

// Apply the match results.
memOrders := append(append(([]*types.MemOrder)(nil), buyObs.Orders()...), sellObs.Orders()...)
if err = k.finalizeMatching(ctx, market, memOrders, escrow); err != nil {
return
}
marketState.LastPrice = &lastPrice
marketState.LastMatchingHeight = ctx.BlockHeight()
k.SetMarketState(ctx, market.Id, marketState)
}()

mCtx := types.NewMatchingContext(market, false)
var (
lastPrice sdk.Dec
matched bool
)
if marketState.LastPrice == nil {
lastPrice = mCtx.RunSinglePriceAuction(buyObs, sellObs)
lastPrice, matched = mCtx.RunSinglePriceAuction(buyObs, sellObs)
} else {
lastPrice = mCtx.BatchMatchOrderBookSides(buyObs, sellObs, *marketState.LastPrice)
lastPrice, matched = mCtx.BatchMatchOrderBookSides(buyObs, sellObs, *marketState.LastPrice)
}
// If there was no matching, exit early.
if !matched {
return
}

// Apply the match results.
memOrders := append(append(([]*types.MemOrder)(nil), buyObs.Orders()...), sellObs.Orders()...)
if err = k.finalizeMatching(ctx, market, memOrders, escrow); err != nil {
return
}
marketState.LastPrice = &lastPrice
marketState.LastMatchingHeight = ctx.BlockHeight()
k.SetMarketState(ctx, market.Id, marketState)
return nil
}
11 changes: 7 additions & 4 deletions x/exchange/types/matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (ctx *MatchingContext) ExecuteOrder(
return
}

func (ctx *MatchingContext) RunSinglePriceAuction(buyObs, sellObs *MemOrderBookSide) (matchPrice sdk.Dec) {
func (ctx *MatchingContext) RunSinglePriceAuction(buyObs, sellObs *MemOrderBookSide) (matchPrice sdk.Dec, matched bool) {
buyLevelIdx, sellLevelIdx := 0, 0
var buyLastPrice, sellLastPrice sdk.Dec
for buyLevelIdx < len(buyObs.levels) && sellLevelIdx < len(sellObs.levels) {
Expand Down Expand Up @@ -241,11 +241,12 @@ func (ctx *MatchingContext) RunSinglePriceAuction(buyObs, sellObs *MemOrderBookS
sellLevelIdx++
}
}
matched = true
}
return
}

func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBookSide, lastPrice sdk.Dec) (newLastPrice sdk.Dec) {
func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBookSide, lastPrice sdk.Dec) (newLastPrice sdk.Dec, matched bool) {
// 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 @@ -264,10 +265,11 @@ func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBo
if sellFull {
sellLevelIdx++
}
matched = true
}
// If there's no more levels to match, return earlier.
if buyLevelIdx >= len(buyObs.levels) || sellLevelIdx >= len(sellObs.levels) {
return lastPrice
return lastPrice, matched
}

// Phase 2: Match orders in traditional exchange's manner.
Expand Down Expand Up @@ -296,8 +298,9 @@ func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBo
if sellFull {
sellLevelIdx++
}
matched = true
}
return newLastPrice
return newLastPrice, matched
}

func PayReceiveDenoms(baseDenom, quoteDenom string, isBuy bool) (payDenom, receiveDenom string) {
Expand Down