code.vegaprotocol.io/vega@v0.79.0/datanode/service/order.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 service
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/datanode/entities"
    22  	"code.vegaprotocol.io/vega/datanode/utils"
    23  	"code.vegaprotocol.io/vega/libs/slice"
    24  	"code.vegaprotocol.io/vega/logging"
    25  )
    26  
    27  //nolint:interfacebloat
    28  type orderStore interface {
    29  	Flush(ctx context.Context) ([]entities.Order, error)
    30  	Add(o entities.Order) error
    31  	GetAll(ctx context.Context) ([]entities.Order, error)
    32  	GetOrder(ctx context.Context, orderID string, version *int32) (entities.Order, error)
    33  	GetByMarketAndID(ctx context.Context, marketIDstr string, orderIDs []string) ([]entities.Order, error)
    34  	GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Order, error)
    35  	GetLiveOrders(ctx context.Context) ([]entities.Order, error)
    36  	ListOrderVersions(ctx context.Context, orderIDStr string, p entities.CursorPagination) ([]entities.Order, entities.PageInfo, error)
    37  	ListOrders(ctx context.Context, p entities.CursorPagination, filter entities.OrderFilter) ([]entities.Order, entities.PageInfo, error)
    38  }
    39  
    40  type Order struct {
    41  	store    orderStore
    42  	observer utils.Observer[entities.Order]
    43  }
    44  
    45  func NewOrder(store orderStore, log *logging.Logger) *Order {
    46  	return &Order{
    47  		store:    store,
    48  		observer: utils.NewObserver[entities.Order]("order", log, 0, 0),
    49  	}
    50  }
    51  
    52  func (o *Order) ObserveOrders(ctx context.Context, retries int, markets []string, parties []string, includeLiquidity bool) (<-chan []entities.Order, uint64) {
    53  	ch, ref := o.observer.Observe(ctx,
    54  		retries,
    55  		func(o entities.Order) bool {
    56  			marketOk := len(markets) == 0 || slice.Contains(markets, o.MarketID.String())
    57  			partyOk := len(parties) == 0 || slice.Contains(parties, o.PartyID.String())
    58  			liqOrder := (o.LpID != nil && includeLiquidity) || !includeLiquidity
    59  			return marketOk && partyOk && liqOrder
    60  		})
    61  	return ch, ref
    62  }
    63  
    64  func (o *Order) Flush(ctx context.Context) error {
    65  	flushed, err := o.store.Flush(ctx)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	o.observer.Notify(flushed)
    70  	return nil
    71  }
    72  
    73  func (o *Order) Add(order entities.Order) error {
    74  	return o.store.Add(order)
    75  }
    76  
    77  func (o *Order) GetAll(ctx context.Context) ([]entities.Order, error) {
    78  	return o.store.GetAll(ctx)
    79  }
    80  
    81  func (o *Order) GetOrder(ctx context.Context, orderID string, version *int32) (entities.Order, error) {
    82  	return o.store.GetOrder(ctx, orderID, version)
    83  }
    84  
    85  func (o *Order) GetByMarketAndID(ctx context.Context, marketIDstr string, orderIDs []string) ([]entities.Order, error) {
    86  	return o.store.GetByMarketAndID(ctx, marketIDstr, orderIDs)
    87  }
    88  
    89  func (o *Order) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Order, error) {
    90  	return o.store.GetByTxHash(ctx, txHash)
    91  }
    92  
    93  func (o *Order) GetLiveOrders(ctx context.Context) ([]entities.Order, error) {
    94  	return o.store.GetLiveOrders(ctx)
    95  }
    96  
    97  func (o *Order) ListOrders(ctx context.Context, p entities.CursorPagination, filter entities.OrderFilter,
    98  ) ([]entities.Order, entities.PageInfo, error) {
    99  	return o.store.ListOrders(ctx, p, filter)
   100  }
   101  
   102  func (o *Order) ListOrderVersions(ctx context.Context, orderID string, p entities.CursorPagination) ([]entities.Order, entities.PageInfo, error) {
   103  	return o.store.ListOrderVersions(ctx, orderID, p)
   104  }