Skip to content

Commit

Permalink
create matching request for matching run function
Browse files Browse the repository at this point in the history
  • Loading branch information
debaghtk committed Apr 10, 2024
1 parent 4eb40cc commit fe4dc2e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 13 additions & 7 deletions orderbook/matching_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type MatchingPipeline struct {
SanitaryTicker *time.Ticker
}

type MatchingRequest struct {
BlockNumber *big.Int
MarketCount int64
UnderlyingPrices []*big.Int
}

func NewMatchingPipeline(
db LimitOrderDatabase,
lotp LimitOrderTxProcessor,
Expand All @@ -46,14 +52,14 @@ func (pipeline *MatchingPipeline) RunSanitization() {
pipeline.db.RemoveExpiredSignedOrders()
}

func (pipeline *MatchingPipeline) Run(blockNumber *big.Int, marketCount int64, underlyingPrices []*big.Int) bool {
func (pipeline *MatchingPipeline) Run(req MatchingRequest) bool {
pipeline.mu.Lock()
defer pipeline.mu.Unlock()

// reset ticker
pipeline.MatchingTicker.Reset(matchingTickerDuration)
markets := pipeline.GetActiveMarkets(marketCount)
log.Info("MatchingPipeline:Run", "blockNumber", blockNumber)
markets := pipeline.GetActiveMarkets(req.MarketCount)
log.Info("MatchingPipeline:Run", "blockNumber", req.BlockNumber)

if len(markets) == 0 {
return false
Expand Down Expand Up @@ -81,16 +87,16 @@ func (pipeline *MatchingPipeline) Run(blockNumber *big.Int, marketCount int64, u

// fetch various hubble market params and run the matching engine
hState := hu.GetHubbleState()
hState.OraclePrices = hu.ArrayToMap(underlyingPrices)
hState.OraclePrices = hu.ArrayToMap(req.UnderlyingPrices)

// build trader map
liquidablePositions, ordersToCancel, marginMap := pipeline.db.GetNaughtyTraders(hState)
cancellableOrderIds := pipeline.cancelLimitOrders(ordersToCancel)
orderMap := make(map[Market]*Orders)
for _, market := range markets {
orderMap[market] = pipeline.fetchOrders(market, hState.OraclePrices[market], cancellableOrderIds, blockNumber)
orderMap[market] = pipeline.fetchOrders(market, hState.OraclePrices[market], cancellableOrderIds, req.BlockNumber)
}
pipeline.runLiquidations(liquidablePositions, orderMap, hState.OraclePrices, marginMap, marketCount)
pipeline.runLiquidations(liquidablePositions, orderMap, hState.OraclePrices, marginMap, req.MarketCount)
for _, market := range markets {
// @todo should we prioritize matching in any particular market?
upperBound, _ := pipeline.configService.GetAcceptableBounds(market)
Expand All @@ -100,7 +106,7 @@ func (pipeline *MatchingPipeline) Run(blockNumber *big.Int, marketCount int64, u
orderBookTxsCount := pipeline.lotp.GetOrderBookTxsCount()
log.Info("MatchingPipeline:Complete", "orderBookTxsCount", orderBookTxsCount)
if orderBookTxsCount > 0 {
pipeline.lotp.SetOrderBookTxsBlockNumber(blockNumber.Uint64())
pipeline.lotp.SetOrderBookTxsBlockNumber(req.BlockNumber.Uint64())
return true
}
return false
Expand Down
8 changes: 6 additions & 2 deletions plugin/evm/limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ func (lop *limitOrderProcesser) RunMatchingPipeline() {
return
}
executeFuncAndRecoverPanic(func() {
matchesFound := lop.matchingPipeline.Run(new(big.Int).Add(lop.blockChain.CurrentBlock().Number,
big.NewInt(1)), lop.configService.GetActiveMarketsCount(), lop.configService.GetUnderlyingPrices())
matchingRequest := orderbook.MatchingRequest{
BlockNumber: new(big.Int).Add(lop.blockChain.CurrentBlock().Number, big.NewInt(1)),
MarketCount: lop.configService.GetActiveMarketsCount(),
UnderlyingPrices: lop.configService.GetUnderlyingPrices(),
}
matchesFound := lop.matchingPipeline.Run(matchingRequest)
if matchesFound {
lop.blockBuilder.signalTxsReady()
}
Expand Down

0 comments on commit fe4dc2e

Please sign in to comment.