code.vegaprotocol.io/vega@v0.79.0/core/execution/future/market_for_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 future
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/core/execution/common"
    23  	"code.vegaprotocol.io/vega/core/types"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  )
    26  
    27  // UpdateRiskFactorsForTest is a hack for setting the risk factors for tests directly rather than through the consensus engine.
    28  // Never use this for anything functional.
    29  func (m *Market) UpdateRiskFactorsForTest() {
    30  	m.risk.CalculateRiskFactorsForTest()
    31  }
    32  
    33  func (m *Market) EnterAuction(ctx context.Context) {
    34  	m.enterAuction(ctx)
    35  }
    36  
    37  func (m *Market) LeaveAuctionWithIDGen(ctx context.Context, now time.Time, generator common.IDGenerator) {
    38  	m.idgen = generator
    39  	defer func() { m.idgen = nil }()
    40  	m.leaveAuction(ctx, now)
    41  }
    42  
    43  // GetPeggedOrderCount returns the number of pegged orders in the market.
    44  func (m *Market) GetPeggedOrderCount() int {
    45  	return len(m.matching.GetActivePeggedOrderIDs()) + len(m.peggedOrders.GetParkedIDs())
    46  }
    47  
    48  // GetParkedOrderCount returns hte number of parked orders in the market.
    49  func (m *Market) GetParkedOrderCount() int {
    50  	return len(m.peggedOrders.Parked())
    51  }
    52  
    53  // GetPeggedExpiryOrderCount returns the number of pegged order that can expire.
    54  func (m *Market) GetPeggedExpiryOrderCount() int {
    55  	return m.expiringOrders.GetExpiryingOrderCount()
    56  }
    57  
    58  // GetOrdersOnBookCount returns the number of orders on the live book.
    59  func (m *Market) GetOrdersOnBookCount() int64 {
    60  	return m.matching.GetTotalNumberOfOrders()
    61  }
    62  
    63  // GetVolumeOnBook returns the volume of orders on one side of the book.
    64  func (m *Market) GetVolumeOnBook() int64 {
    65  	return m.matching.GetTotalVolume()
    66  }
    67  
    68  // StartPriceAuction initialises the market to handle a price auction.
    69  func (m *Market) StartPriceAuction(now time.Time) {
    70  	end := types.AuctionDuration{
    71  		Duration: 1000,
    72  	}
    73  	// setup auction
    74  	m.as.StartPriceAuction(now, &end)
    75  }
    76  
    77  // TSCalc returns the local tsCalc instance.
    78  func (m *Market) TSCalc() TargetStakeCalculator {
    79  	return m.tsCalc
    80  }
    81  
    82  func (m *Market) State() types.MarketState {
    83  	return m.mkt.State
    84  }
    85  
    86  // Return the number if liquidity provisions in the market.
    87  func (m *Market) GetLPSCount() int {
    88  	return m.equityShares.GetLPSCount()
    89  }
    90  
    91  // Return the state of the LP submission for the given partyID.
    92  func (m *Market) GetLPSState(partyID string) types.LiquidityProvisionStatus {
    93  	lps := m.liquidityEngine.LiquidityProvisionByPartyID(partyID)
    94  
    95  	if lps != nil {
    96  		return lps.Status
    97  	}
    98  	return types.LiquidityProvisionUnspecified
    99  }
   100  
   101  // Returns all the pegged orders for a given party.
   102  func (m *Market) GetPeggedOrders(partyID string) []*types.Order {
   103  	orders := m.matching.GetOrdersPerParty(partyID)
   104  
   105  	peggedOrders := []*types.Order{}
   106  	for _, order := range orders {
   107  		if order.PeggedOrder != nil {
   108  			peggedOrders = append(peggedOrders, order)
   109  		}
   110  	}
   111  	return peggedOrders
   112  }
   113  
   114  // Returns the amount of assets in the bond account (no need to clone in these functions).
   115  func (m *Market) GetBondAccountBalance(ctx context.Context, partyID, marketID, asset string) *num.Uint {
   116  	bondAccount, err := m.collateral.GetOrCreatePartyBondAccount(ctx, partyID, marketID, asset)
   117  	if err == nil {
   118  		return bondAccount.Balance
   119  	}
   120  	return num.UintZero()
   121  }
   122  
   123  // Returns the amount of assets in the general account.
   124  func (m *Market) GetGeneralAccountBalance(partyID, asset string) *num.Uint {
   125  	generalAccount, err := m.collateral.GetPartyGeneralAccount(partyID, asset)
   126  	if err == nil {
   127  		return generalAccount.Balance
   128  	}
   129  	return num.UintZero()
   130  }
   131  
   132  // Returns the amount of assets in the margin account.
   133  func (m *Market) GetMarginAccountBalance(partyID, marketID, asset string) *num.Uint {
   134  	marginAccount, err := m.collateral.GetPartyMarginAccount(marketID, partyID, asset)
   135  	if err == nil {
   136  		return marginAccount.Balance
   137  	}
   138  	return num.UintZero()
   139  }
   140  
   141  // Get the total assets for a party.
   142  func (m *Market) GetTotalAccountBalance(ctx context.Context, partyID, marketID, asset string) *num.Uint {
   143  	return num.Sum(
   144  		m.GetGeneralAccountBalance(partyID, asset),
   145  		m.GetMarginAccountBalance(partyID, marketID, asset),
   146  		m.GetBondAccountBalance(ctx, partyID, marketID, asset),
   147  	)
   148  }
   149  
   150  // Return the current liquidity fee value for a market.
   151  func (m *Market) GetLiquidityFee() num.Decimal {
   152  	return m.fee.GetLiquidityFee()
   153  }
   154  
   155  // Log out orders that don't match.
   156  func (m *Market) ValidateOrder(order *types.Order) bool {
   157  	order2, err := m.matching.GetOrderByID(order.ID)
   158  	if err != nil {
   159  		return false
   160  	}
   161  	return (order.Price.EQ(order2.Price) && order.Size == order2.Size &&
   162  		order.Remaining == order2.Remaining && order.Status == order2.Status)
   163  }
   164  
   165  func (m *Market) DumpBook() {
   166  	m.matching.PrintState("test")
   167  }