code.vegaprotocol.io/vega@v0.79.0/datanode/service/trade.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  type tradeStore interface {
    28  	Flush(ctx context.Context) ([]*entities.Trade, error)
    29  	Add(t *entities.Trade) error
    30  	List(context.Context, []entities.MarketID, []entities.PartyID, []entities.OrderID, entities.CursorPagination, entities.DateRange) ([]entities.Trade, entities.PageInfo, error)
    31  	GetLastTradeByMarket(ctx context.Context, market string) ([]entities.Trade, error)
    32  	GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Trade, error)
    33  }
    34  
    35  type Trade struct {
    36  	store    tradeStore
    37  	observer utils.Observer[*entities.Trade]
    38  }
    39  
    40  func NewTrade(store tradeStore, log *logging.Logger) *Trade {
    41  	return &Trade{
    42  		store:    store,
    43  		observer: utils.NewObserver[*entities.Trade]("trade", log, 0, 0),
    44  	}
    45  }
    46  
    47  func (t *Trade) Flush(ctx context.Context) error {
    48  	flushed, err := t.store.Flush(ctx)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	t.observer.Notify(flushed)
    53  	return nil
    54  }
    55  
    56  func (t *Trade) Add(trade *entities.Trade) error {
    57  	return t.store.Add(trade)
    58  }
    59  
    60  func (t *Trade) List(ctx context.Context,
    61  	marketIDs []entities.MarketID,
    62  	partyIDs []entities.PartyID,
    63  	orderIDs []entities.OrderID,
    64  	pagination entities.CursorPagination,
    65  	dateRange entities.DateRange,
    66  ) ([]entities.Trade, entities.PageInfo, error) {
    67  	return t.store.List(ctx, marketIDs, partyIDs, orderIDs, pagination, dateRange)
    68  }
    69  
    70  func (t *Trade) GetLastTradeByMarket(ctx context.Context, market string) ([]entities.Trade, error) {
    71  	return t.store.GetLastTradeByMarket(ctx, market)
    72  }
    73  
    74  func (t *Trade) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Trade, error) {
    75  	return t.store.GetByTxHash(ctx, txHash)
    76  }
    77  
    78  func (t *Trade) Observe(ctx context.Context, retries int, marketIDs []string, partyIDs []string) (<-chan []*entities.Trade, uint64) {
    79  	ch, ref := t.observer.Observe(ctx,
    80  		retries,
    81  		func(trade *entities.Trade) bool {
    82  			// match market filter if any, or if no filter is provided
    83  			marketsOk := len(marketIDs) == 0 || slice.Contains(marketIDs, trade.MarketID.String())
    84  			// match party filter if any, or if no filter is provided
    85  			partiesOk := len(partyIDs) == 0 || slice.Contains(partyIDs, trade.Buyer.String()) || slice.Contains(partyIDs, trade.Seller.String())
    86  
    87  			return marketsOk && partiesOk
    88  		})
    89  	return ch, ref
    90  }