Skip to content

Commit

Permalink
add LastMatchHeight in match engine
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyyangz authored and forcodedancing committed May 19, 2022
1 parent 032116b commit a07b85b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
7 changes: 5 additions & 2 deletions plugins/dex/matcheng/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

type MatchEng struct {
Book OrderBookInterface
LastMatchHeight int64
Book OrderBookInterface
// LotSize may be based on price level, which can be set
// before any match() call
LotSize int64
Expand All @@ -30,6 +31,7 @@ type MatchEng struct {
// NewMatchEng constructs a new MatchEng.
func NewMatchEng(pairSymbol string, basePrice, lotSize int64, priceLimit float64) *MatchEng {
return &MatchEng{
LastMatchHeight: 0,
Book: NewOrderBookOnULList(10000, 16),
LotSize: lotSize,
PriceLimitPct: priceLimit,
Expand Down Expand Up @@ -164,7 +166,7 @@ func (me *MatchEng) reserveQty(residual int64, orders []OrderPart) bool {
// in such case, there should be alerts and all the new orders in this round should be rejected and dropped from order books
// cancel order should be handled 1st before calling Match().
// IOC orders should be handled after Match()
func (me *MatchEng) MatchBeforeGalileo() bool {
func (me *MatchEng) MatchBeforeGalileo(height int64) bool {
me.Trades = me.Trades[:0]
r := me.Book.GetOverlappedRange(&me.overLappedLevel, &me.buyBuf, &me.sellBuf)
if r <= 0 {
Expand All @@ -178,6 +180,7 @@ func (me *MatchEng) MatchBeforeGalileo() bool {
totalExec := me.overLappedLevel[index].AccumulatedExecutions
me.Trades = me.Trades[:0]
me.LastTradePrice = lastPx
me.LastMatchHeight = height
i, j := 0, len(me.overLappedLevel)-1
//sell below the price at index or buy above the price would not get filled
for i <= index && j >= index && compareBuy(totalExec, 0) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion plugins/dex/matcheng/engine_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func (me *MatchEng) Match(height int64) bool {
if !sdk.IsUpgrade(upgrade.BEP19) {
return me.MatchBeforeGalileo()
return me.MatchBeforeGalileo(height)
}
me.logger.Debug("match starts...", "height", height)
me.Trades = me.Trades[:0]
Expand All @@ -41,6 +41,7 @@ func (me *MatchEng) Match(height int64) bool {
surplus := me.overLappedLevel[index].BuySellSurplus
me.fillOrdersNew(takerSide, takerSideOrders, index, tradePrice, surplus)
me.LastTradePrice = tradePrice
me.LastMatchHeight = height
return true
}

Expand Down
12 changes: 6 additions & 6 deletions plugins/dex/matcheng/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("91", BUYSIDE, 107, 100.0, 50)
me.Book.InsertOrder("92", SELLSIDE, 108, 97.0, 50)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
assert.Equal(3, len(me.overLappedLevel))
assert.Equal(int64(98), me.LastTradePrice)
assert.Equal("[{92 98 50 50 50 1 0} {3 98 80 80 80 2 0} {3 98 20 20 100 4 0} {5 98 50 50 50 6 0} {5 98 50 50 100 91 0} {9 98 50 50 50 8 0}]", fmt.Sprint(me.Trades))
Expand All @@ -710,7 +710,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("9", SELLSIDE, 106, 101, 50)
me.Book.InsertOrder("91", BUYSIDE, 107, 100, 50)
me.Book.InsertOrder("92", SELLSIDE, 108, 102, 50)
assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
assert.Equal(0, len(me.overLappedLevel))
assert.Equal(0, len(me.Trades))

Expand All @@ -720,7 +720,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("1", BUYSIDE, 102, 100.0, 100)
me.Book.InsertOrder("8", BUYSIDE, 103, 99.0, 100)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
assert.Equal(3, len(me.overLappedLevel))
assert.Equal("[{3 99 100 100 100 1 0} {5 99 100 100 100 8 0}]", fmt.Sprint(me.Trades))

Expand All @@ -736,7 +736,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("91", BUYSIDE, 107, 100.0, 50)
me.Book.InsertOrder("92", SELLSIDE, 108, 97.0, 50)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
assert.Equal(3, len(me.overLappedLevel))
assert.Equal("[{92 98 50 50 50 1 0} {3 98 80 80 80 2 0} {3 98 20 20 100 4 0} {5 98 50 50 50 6 0} {5 98 50 50 100 91 0}]", fmt.Sprint(me.Trades))

Expand All @@ -754,7 +754,7 @@ func TestMatchEng_MatchDeprecated(t *testing.T) {
me.Book.InsertOrder("92", SELLSIDE, 105, 100, 100)
me.Book.InsertOrder("93", BUYSIDE, 105, 100, 300)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
t.Log(me.overLappedLevel)
assert.Equal(6, len(me.overLappedLevel))
assert.Equal(int64(100), me.LastTradePrice)
Expand Down Expand Up @@ -790,7 +790,7 @@ func TestMatchEng_DropFilledOrder(t *testing.T) {
me.Book.InsertOrder("92", SELLSIDE, 105, 100, 100)
me.Book.InsertOrder("93", BUYSIDE, 105, 100, 300)

assert.True(me.MatchBeforeGalileo())
assert.True(me.MatchBeforeGalileo(1))
t.Log(me.overLappedLevel)
assert.Equal(6, len(me.overLappedLevel))
assert.Equal(int64(100), me.LastTradePrice)
Expand Down
10 changes: 10 additions & 0 deletions plugins/dex/order/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,16 @@ func (kp *Keeper) GetPriceLevel(pair string, side int8, price int64) *me.PriceLe
}
}

func (kp *Keeper) GetLastTrades(height int64, pair string) ([]me.Trade, int64) {
if eng, ok := kp.engines[pair]; ok {
if eng.LastMatchHeight == height {
return eng.Trades, eng.LastTradePrice
}
}
return nil, 0
}

// !!! FOR TEST USE ONLY
func (kp *Keeper) GetLastTradesForPair(pair string) ([]me.Trade, int64) {
if eng, ok := kp.engines[pair]; ok {
return eng.Trades, eng.LastTradePrice
Expand Down

0 comments on commit a07b85b

Please sign in to comment.