Skip to content

Commit

Permalink
refactor active markets count
Browse files Browse the repository at this point in the history
  • Loading branch information
debaghtk committed Apr 5, 2024
1 parent ac80606 commit cf1b087
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
13 changes: 6 additions & 7 deletions orderbook/matching_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func (pipeline *MatchingPipeline) RunSanitization() {
pipeline.db.RemoveExpiredSignedOrders()
}

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

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

if len(markets) == 0 {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (pipeline *MatchingPipeline) Run(blockNumber *big.Int) bool {
for _, market := range markets {
orderMap[market] = pipeline.fetchOrders(market, hState.OraclePrices[market], cancellableOrderIds, blockNumber)
}
pipeline.runLiquidations(liquidablePositions, orderMap, hState.OraclePrices, marginMap)
pipeline.runLiquidations(liquidablePositions, orderMap, hState.OraclePrices, marginMap, marketCount)
for _, market := range markets {
// @todo should we prioritize matching in any particular market?
upperBound, _ := pipeline.configService.GetAcceptableBounds(market)
Expand All @@ -111,8 +111,7 @@ type Orders struct {
shortOrders []Order
}

func (pipeline *MatchingPipeline) GetActiveMarkets() []Market {
count := pipeline.configService.GetActiveMarketsCount()
func (pipeline *MatchingPipeline) GetActiveMarkets(count int64) []Market {
markets := make([]Market, count)
for i := int64(0); i < count; i++ {
markets[i] = Market(i)
Expand Down Expand Up @@ -166,15 +165,15 @@ func (pipeline *MatchingPipeline) fetchOrders(market Market, underlyingPrice *bi
return &Orders{longOrders, shortOrders}
}

func (pipeline *MatchingPipeline) runLiquidations(liquidablePositions []LiquidablePosition, orderMap map[Market]*Orders, underlyingPrices map[Market]*big.Int, marginMap map[common.Address]*big.Int) {
func (pipeline *MatchingPipeline) runLiquidations(liquidablePositions []LiquidablePosition, orderMap map[Market]*Orders, underlyingPrices map[Market]*big.Int, marginMap map[common.Address]*big.Int, marketCount int64) {
if len(liquidablePositions) == 0 {
return
}

log.Info("found positions to liquidate", "num", len(liquidablePositions))

// we need to retreive permissible bounds for liquidations in each market
markets := pipeline.GetActiveMarkets()
markets := pipeline.GetActiveMarkets(marketCount)
type S struct {
Upperbound *big.Int
Lowerbound *big.Int
Expand Down
15 changes: 8 additions & 7 deletions orderbook/matching_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestRunLiquidations(t *testing.T) {
traderAddress := common.HexToAddress("0x710bf5f942331874dcbc7783319123679033b63b")
traderAddress1 := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")
market := Market(0)
marketCount := int64(1)
liqUpperBound := big.NewInt(22)
liqLowerBound := big.NewInt(18)

Expand All @@ -27,7 +28,7 @@ func TestRunLiquidations(t *testing.T) {
shortOrders := []Order{getShortOrder()}

orderMap := map[Market]*Orders{market: {longOrders, shortOrders}}
pipeline.runLiquidations([]LiquidablePosition{}, orderMap, underlyingPrices, map[common.Address]*big.Int{})
pipeline.runLiquidations([]LiquidablePosition{}, orderMap, underlyingPrices, map[common.Address]*big.Int{}, marketCount)
assert.Equal(t, longOrders, orderMap[market].longOrders)
assert.Equal(t, shortOrders, orderMap[market].shortOrders)
lotp.AssertNotCalled(t, "ExecuteLiquidation", mock.Anything, mock.Anything, mock.Anything)
Expand All @@ -44,7 +45,7 @@ func TestRunLiquidations(t *testing.T) {
cs.On("GetAcceptableBoundsForLiquidation", market).Return(liqUpperBound, liqLowerBound)
cs.On("GetMinAllowableMargin").Return(big.NewInt(1e5))
cs.On("GetTakerFee").Return(big.NewInt(1e5))
pipeline.runLiquidations([]LiquidablePosition{getLiquidablePos(traderAddress, LONG, 7)}, orderMap, underlyingPrices, map[common.Address]*big.Int{})
pipeline.runLiquidations([]LiquidablePosition{getLiquidablePos(traderAddress, LONG, 7)}, orderMap, underlyingPrices, map[common.Address]*big.Int{}, marketCount)
assert.Equal(t, longOrders, orderMap[market].longOrders)
assert.Equal(t, shortOrders, orderMap[market].shortOrders)
lotp.AssertNotCalled(t, "ExecuteLiquidation", mock.Anything, mock.Anything, mock.Anything)
Expand All @@ -63,7 +64,7 @@ func TestRunLiquidations(t *testing.T) {

orderMap := map[Market]*Orders{market: {[]Order{longOrder}, []Order{shortOrder}}}

pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{})
pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{}, marketCount)

lotp.AssertCalled(t, "ExecuteLiquidation", traderAddress, longOrder, expectedFillAmount)
cs.AssertCalled(t, "GetAcceptableBoundsForLiquidation", market)
Expand All @@ -87,7 +88,7 @@ func TestRunLiquidations(t *testing.T) {

orderMap := map[Market]*Orders{market: {[]Order{longOrder, longOrder2}, []Order{}}}

pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{})
pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{}, marketCount)

lotp.AssertCalled(t, "ExecuteLiquidation", traderAddress, longOrder, expectedFillAmount)
cs.AssertCalled(t, "GetAcceptableBoundsForLiquidation", market)
Expand Down Expand Up @@ -116,7 +117,7 @@ func TestRunLiquidations(t *testing.T) {
lotp.On("ExecuteLiquidation", traderAddress1, orderMap[market].shortOrders[0], big.NewInt(1)).Return(nil)
lotp.On("ExecuteLiquidation", traderAddress1, orderMap[market].shortOrders[1], big.NewInt(1)).Return(nil)

pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{})
pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{}, marketCount)
cs.AssertCalled(t, "GetAcceptableBoundsForLiquidation", market)

lotp.AssertCalled(t, "ExecuteLiquidation", traderAddress, longOrder0, big.NewInt(5))
Expand Down Expand Up @@ -156,7 +157,7 @@ func TestRunLiquidations(t *testing.T) {
cs.On("GetAcceptableBoundsForLiquidation", market).Return(liqUpperBound, liqLowerBound)
cs.On("GetMinAllowableMargin").Return(big.NewInt(1e5))
cs.On("GetTakerFee").Return(big.NewInt(1e5))
pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{})
pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{}, marketCount)
assert.Equal(t, longOrders, orderMap[market].longOrders)
assert.Equal(t, shortOrders, orderMap[market].shortOrders)
lotp.AssertNotCalled(t, "ExecuteLiquidation", mock.Anything, mock.Anything, mock.Anything)
Expand All @@ -173,7 +174,7 @@ func TestRunLiquidations(t *testing.T) {
cs.On("GetTakerFee").Return(big.NewInt(1e5))

orderMap := map[Market]*Orders{market: {[]Order{longOrder}, []Order{shortOrder}}}
pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{})
pipeline.runLiquidations(liquidablePositions, orderMap, underlyingPrices, map[common.Address]*big.Int{}, marketCount)

lotp.AssertCalled(t, "ExecuteLiquidation", traderAddress, shortOrder, expectedFillAmount)
cs.AssertCalled(t, "GetAcceptableBoundsForLiquidation", market)
Expand Down
4 changes: 2 additions & 2 deletions plugin/evm/limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewLimitOrderProcesser(ctx *snow.Context, txPool *txpool.TxPool, shutdownCh
// This is also true for local testing. once contracts are deployed it's mandatory to restart the nodes
hState := &hu.HubbleState{
Assets: matchingPipeline.GetCollaterals(),
ActiveMarkets: matchingPipeline.GetActiveMarkets(),
ActiveMarkets: matchingPipeline.GetActiveMarkets(configService.GetActiveMarketsCount()),
MinAllowableMargin: configService.GetMinAllowableMargin(),
MaintenanceMargin: configService.GetMaintenanceMargin(),
TakerFee: configService.GetTakerFee(),
Expand Down Expand Up @@ -178,7 +178,7 @@ func (lop *limitOrderProcesser) RunMatchingPipeline() {
return
}
executeFuncAndRecoverPanic(func() {
matchesFound := lop.matchingPipeline.Run(new(big.Int).Add(lop.blockChain.CurrentBlock().Number, big.NewInt(1)))
matchesFound := lop.matchingPipeline.Run(new(big.Int).Add(lop.blockChain.CurrentBlock().Number, big.NewInt(1)), lop.configService.GetActiveMarketsCount())
if matchesFound {
lop.blockBuilder.signalTxsReady()
}
Expand Down

0 comments on commit cf1b087

Please sign in to comment.