code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/funding_period.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  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    24  
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  type (
    29  	FundingPeriodEvent interface {
    30  		events.Event
    31  		FundingPeriod() *eventspb.FundingPeriod
    32  	}
    33  	FundingPeriodDataPointEvent interface {
    34  		events.Event
    35  		FundingPeriodDataPoint() *eventspb.FundingPeriodDataPoint
    36  	}
    37  	FundingPeriodStore interface {
    38  		AddFundingPeriod(ctx context.Context, period *entities.FundingPeriod) error
    39  		AddDataPoint(ctx context.Context, dataPoint *entities.FundingPeriodDataPoint) error
    40  	}
    41  	FundingPeriod struct {
    42  		subscriber
    43  		store FundingPeriodStore
    44  	}
    45  )
    46  
    47  func NewFundingPeriod(store FundingPeriodStore) *FundingPeriod {
    48  	return &FundingPeriod{
    49  		store: store,
    50  	}
    51  }
    52  
    53  func (fp *FundingPeriod) Types() []events.Type {
    54  	return []events.Type{
    55  		events.FundingPeriodEvent,
    56  		events.FundingPeriodDataPointEvent,
    57  	}
    58  }
    59  
    60  func (fp *FundingPeriod) Push(ctx context.Context, evt events.Event) error {
    61  	switch evt.Type() {
    62  	case events.FundingPeriodEvent:
    63  		return fp.consumeFundingPeriodEvent(ctx, evt.(FundingPeriodEvent))
    64  	case events.FundingPeriodDataPointEvent:
    65  		return fp.consumeFundingPeriodDataPointEvent(ctx, evt.(FundingPeriodDataPointEvent))
    66  	default:
    67  		return nil
    68  	}
    69  }
    70  
    71  func (fp *FundingPeriod) consumeFundingPeriodEvent(ctx context.Context, evt FundingPeriodEvent) error {
    72  	fundingPeriod, err := entities.NewFundingPeriodFromProto(evt.FundingPeriod(), entities.TxHash(evt.TxHash()), fp.vegaTime)
    73  	if err != nil {
    74  		return errors.Wrap(err, "deserializing funding period")
    75  	}
    76  	return errors.Wrap(fp.store.AddFundingPeriod(ctx, fundingPeriod), "adding funding period")
    77  }
    78  
    79  func (fp *FundingPeriod) consumeFundingPeriodDataPointEvent(ctx context.Context, evt FundingPeriodDataPointEvent) error {
    80  	dataPoint, err := entities.NewFundingPeriodDataPointFromProto(evt.FundingPeriodDataPoint(), entities.TxHash(evt.TxHash()), fp.vegaTime)
    81  	if err != nil {
    82  		return errors.Wrap(err, "deserializing funding period data point")
    83  	}
    84  	return errors.Wrap(fp.store.AddDataPoint(ctx, dataPoint), "adding funding period data point")
    85  }
    86  
    87  func (fp *FundingPeriod) Name() string {
    88  	return "FundingPeriod"
    89  }