code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/stop_orders.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  
    21  	"code.vegaprotocol.io/vega/core/events"
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	pbevents "code.vegaprotocol.io/vega/protos/vega/events/v1"
    24  
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  type (
    29  	StopOrderEvent interface {
    30  		events.Event
    31  		StopOrder() *pbevents.StopOrderEvent
    32  	}
    33  
    34  	StopOrderStore interface {
    35  		Add(entities.StopOrder) error
    36  		Flush(ctx context.Context) error
    37  	}
    38  
    39  	StopOrder struct {
    40  		subscriber
    41  		store StopOrderStore
    42  	}
    43  )
    44  
    45  func NewStopOrder(store StopOrderStore) *StopOrder {
    46  	return &StopOrder{
    47  		store: store,
    48  	}
    49  }
    50  
    51  func (so *StopOrder) Types() []events.Type {
    52  	return []events.Type{
    53  		events.StopOrderEvent,
    54  	}
    55  }
    56  
    57  func (so *StopOrder) Push(ctx context.Context, evt events.Event) error {
    58  	return so.consume(evt.(StopOrderEvent), evt.Sequence())
    59  }
    60  
    61  func (so *StopOrder) Flush(ctx context.Context) error {
    62  	return so.store.Flush(ctx)
    63  }
    64  
    65  func (so *StopOrder) consume(evt StopOrderEvent, seqNum uint64) error {
    66  	protoOrder := evt.StopOrder()
    67  	stop, err := entities.StopOrderFromProto(protoOrder, so.vegaTime, seqNum, entities.TxHash(evt.TxHash()))
    68  	if err != nil {
    69  		return errors.Wrap(err, "deserializing stop order")
    70  	}
    71  	return errors.Wrap(so.store.Add(stop), "adding stop order")
    72  }
    73  
    74  func (so *StopOrder) Name() string {
    75  	return "StopOrder"
    76  }