code.vegaprotocol.io/vega@v0.79.0/datanode/service/risk.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/logging"
    24  )
    25  
    26  type MarginLevelsStore interface {
    27  	Add(marginLevel entities.MarginLevels) error
    28  	Flush(ctx context.Context) ([]entities.MarginLevels, error)
    29  	GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.MarginLevels, error)
    30  	GetMarginLevelsByIDWithCursorPagination(ctx context.Context, partyID, marketID string, pagination entities.CursorPagination) ([]entities.MarginLevels, entities.PageInfo, error)
    31  }
    32  
    33  type AccountSource interface {
    34  	GetByID(ctx context.Context, id entities.AccountID) (entities.Account, error)
    35  }
    36  
    37  type Risk struct {
    38  	mlStore       MarginLevelsStore
    39  	accountSource AccountSource
    40  	observer      utils.Observer[entities.MarginLevels]
    41  }
    42  
    43  func NewRisk(mlStore MarginLevelsStore, accountSource AccountSource, log *logging.Logger) *Risk {
    44  	return &Risk{
    45  		mlStore:       mlStore,
    46  		accountSource: accountSource,
    47  		observer:      utils.NewObserver[entities.MarginLevels]("margin_levels", log, 0, 0),
    48  	}
    49  }
    50  
    51  func (r *Risk) Add(marginLevel entities.MarginLevels) error {
    52  	return r.mlStore.Add(marginLevel)
    53  }
    54  
    55  func (r *Risk) Flush(ctx context.Context) error {
    56  	flushed, err := r.mlStore.Flush(ctx)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	r.observer.Notify(flushed)
    61  	return nil
    62  }
    63  
    64  func (r *Risk) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.MarginLevels, error) {
    65  	return r.mlStore.GetByTxHash(ctx, txHash)
    66  }
    67  
    68  func (r *Risk) GetMarginLevelsByIDWithCursorPagination(ctx context.Context, partyID, marketID string, pagination entities.CursorPagination) ([]entities.MarginLevels, entities.PageInfo, error) {
    69  	return r.mlStore.GetMarginLevelsByIDWithCursorPagination(ctx, partyID, marketID, pagination)
    70  }
    71  
    72  func (r *Risk) ObserveMarginLevels(
    73  	ctx context.Context, retries int, partyID, marketID string,
    74  ) (accountCh <-chan []entities.MarginLevels, ref uint64) {
    75  	ch, ref := r.observer.Observe(ctx, retries,
    76  		func(ml entities.MarginLevels) bool {
    77  			acc, err := r.accountSource.GetByID(ctx, ml.AccountID)
    78  			if err != nil {
    79  				return false
    80  			}
    81  			return (len(marketID) == 0 || marketID == acc.MarketID.String()) &&
    82  				len(partyID) == 0 || partyID == acc.PartyID.String()
    83  		})
    84  	return ch, ref
    85  }