code.vegaprotocol.io/vega@v0.79.0/core/matching/helpers_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package matching
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/types"
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  	"code.vegaprotocol.io/vega/logging"
    24  )
    25  
    26  type tstOB struct {
    27  	*OrderBook
    28  	log *logging.Logger
    29  }
    30  
    31  func (t *tstOB) Finish() {
    32  	t.log.Sync()
    33  }
    34  
    35  func peggedOrderCounterForTest(int64) {}
    36  
    37  func getTestOrderBook(_ *testing.T, market string) *tstOB {
    38  	tob := tstOB{
    39  		log: logging.NewTestLogger(),
    40  	}
    41  	tob.OrderBook = NewOrderBook(tob.log, NewDefaultConfig(), market, false, peggedOrderCounterForTest)
    42  	// Turn on all the debug levels so we can cover more lines of code
    43  	tob.OrderBook.LogPriceLevelsDebug = true
    44  	tob.OrderBook.LogRemovedOrdersDebug = true
    45  	return &tob
    46  }
    47  
    48  func (b *OrderBook) getNumberOfBuyLevels() int {
    49  	buys := b.buy.getLevels()
    50  	return len(buys)
    51  }
    52  
    53  func (b *OrderBook) getNumberOfSellLevels() int {
    54  	sells := b.sell.getLevels()
    55  	return len(sells)
    56  }
    57  
    58  func (b *OrderBook) getTotalBuyVolume() uint64 {
    59  	var volume uint64
    60  
    61  	buys := b.buy.getLevels()
    62  	for _, pl := range buys {
    63  		volume += pl.volume
    64  	}
    65  	return volume
    66  }
    67  
    68  //nolint:unparam
    69  func (b *OrderBook) getVolumeAtLevel(price uint64, side types.Side) uint64 {
    70  	if side == types.SideBuy {
    71  		priceLevel := b.buy.getPriceLevel(num.NewUint(price))
    72  		if priceLevel != nil {
    73  			return priceLevel.volume
    74  		}
    75  	} else {
    76  		priceLevel := b.sell.getPriceLevel(num.NewUint(price))
    77  		if priceLevel != nil {
    78  			return priceLevel.volume
    79  		}
    80  	}
    81  	return 0
    82  }
    83  
    84  func (b *OrderBook) getTotalSellVolume() uint64 {
    85  	var volume uint64
    86  	sells := b.sell.getLevels()
    87  	for _, pl := range sells {
    88  		volume += pl.volume
    89  	}
    90  	return volume
    91  }