/
batch.go
60 lines (54 loc) 路 1.78 KB
/
batch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crescent-network/crescent/v5/x/exchange/types"
)
func (k Keeper) RunBatchMatching(ctx sdk.Context, market types.Market) (err error) {
// Find the best buy(bid) and sell(ask) prices to limit the price to load
// on the other side.
bestBuyPrice, found := k.getBestPrice(ctx, market, true)
if !found { // Nothing to match, exit early
return nil
}
bestSellPrice, found := k.getBestPrice(ctx, market, false)
if !found { // Nothing to match, exit early
return nil
}
if bestBuyPrice.LT(bestSellPrice) {
return nil
}
// Construct order book sides with the price limits we obtained previously.
escrow := types.NewEscrow(market.MustGetEscrowAddress())
buyObs := k.ConstructMemOrderBookSide(ctx, market, types.MemOrderBookSideOptions{
IsBuy: true,
PriceLimit: &bestSellPrice,
}, escrow)
sellObs := k.ConstructMemOrderBookSide(ctx, market, types.MemOrderBookSideOptions{
IsBuy: false,
PriceLimit: &bestBuyPrice,
}, escrow)
marketState := k.MustGetMarketState(ctx, market.Id)
mCtx := types.NewMatchingContext(market, false)
var (
lastPrice sdk.Dec
matched bool
)
if marketState.LastPrice == nil {
lastPrice, matched = mCtx.RunSinglePriceAuction(buyObs, sellObs)
} else {
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
}