code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/market_data.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 sqlsubscribers
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/core/events"
    23  	"code.vegaprotocol.io/vega/datanode/entities"
    24  	types "code.vegaprotocol.io/vega/protos/vega"
    25  
    26  	"github.com/pkg/errors"
    27  )
    28  
    29  type MarketDataEvent interface {
    30  	events.Event
    31  	MarketData() types.MarketData
    32  }
    33  
    34  type MarketDataStore interface {
    35  	Add(*entities.MarketData) error
    36  	Flush(context.Context) error
    37  }
    38  
    39  type MarketData struct {
    40  	subscriber
    41  	store MarketDataStore
    42  }
    43  
    44  func (md *MarketData) Flush(ctx context.Context) error {
    45  	return md.store.Flush(ctx)
    46  }
    47  
    48  func (md *MarketData) Push(ctx context.Context, evt events.Event) error {
    49  	return md.consume(evt.(MarketDataEvent))
    50  }
    51  
    52  func (md *MarketData) Types() []events.Type {
    53  	return []events.Type{events.MarketDataEvent}
    54  }
    55  
    56  func NewMarketData(store MarketDataStore) *MarketData {
    57  	return &MarketData{
    58  		store: store,
    59  	}
    60  }
    61  
    62  func (md *MarketData) consume(event MarketDataEvent) error {
    63  	var record *entities.MarketData
    64  	var err error
    65  	mdProto := event.MarketData()
    66  
    67  	if record, err = md.convertMarketDataProto(&mdProto, event.Sequence(), entities.TxHash(event.TxHash())); err != nil {
    68  		errors.Wrap(err, "converting market data proto for persistence failed")
    69  	}
    70  
    71  	return errors.Wrap(md.store.Add(record), "inserting market data to SQL store failed")
    72  }
    73  
    74  func (md *MarketData) convertMarketDataProto(data *types.MarketData, seqNum uint64, txHash entities.TxHash) (*entities.MarketData, error) {
    75  	record, err := entities.MarketDataFromProto(data, txHash)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	record.VegaTime = md.vegaTime
    81  	record.SeqNum = seqNum
    82  	record.SyntheticTime = md.vegaTime.Add(time.Duration(seqNum) * time.Microsecond)
    83  
    84  	return record, nil
    85  }
    86  
    87  func (md *MarketData) Name() string {
    88  	return "MarketData"
    89  }