code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/party.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/core/types"
    23  	"code.vegaprotocol.io/vega/datanode/entities"
    24  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    25  
    26  	"github.com/pkg/errors"
    27  )
    28  
    29  type PartyEvent interface {
    30  	events.Event
    31  	Party() types.Party
    32  }
    33  
    34  type PartyProfileUpdatedEvent interface {
    35  	events.Event
    36  	PartyProfileUpdated() *eventspb.PartyProfileUpdated
    37  }
    38  
    39  type PartyStore interface {
    40  	Add(context.Context, entities.Party) error
    41  	UpdateProfile(ctx context.Context, updated *entities.PartyProfile) error
    42  }
    43  
    44  type Party struct {
    45  	subscriber
    46  	store PartyStore
    47  }
    48  
    49  func NewParty(store PartyStore) *Party {
    50  	ps := &Party{
    51  		store: store,
    52  	}
    53  	return ps
    54  }
    55  
    56  func (ps *Party) Types() []events.Type {
    57  	return []events.Type{events.PartyEvent, events.PartyProfileUpdatedEvent}
    58  }
    59  
    60  func (ps *Party) Push(ctx context.Context, evt events.Event) error {
    61  	switch e := evt.(type) {
    62  	case PartyEvent:
    63  		return ps.consumeNewParty(ctx, e)
    64  	case PartyProfileUpdatedEvent:
    65  		return ps.consumePartyProfileUpdated(ctx, e)
    66  	default:
    67  		return nil
    68  	}
    69  }
    70  
    71  func (ps *Party) consumeNewParty(ctx context.Context, event PartyEvent) error {
    72  	pp := event.Party().IntoProto()
    73  	p := entities.PartyFromProto(pp, entities.TxHash(event.TxHash()))
    74  	vt := ps.vegaTime
    75  	p.VegaTime = &vt
    76  
    77  	return errors.Wrap(ps.store.Add(ctx, p), "adding party")
    78  }
    79  
    80  func (ps *Party) consumePartyProfileUpdated(ctx context.Context, e PartyProfileUpdatedEvent) error {
    81  	updateEvent := e.PartyProfileUpdated()
    82  	updated := entities.PartyProfileFromProto(updateEvent.UpdatedProfile)
    83  
    84  	return errors.Wrap(ps.store.UpdateProfile(ctx, updated), "updating party profile")
    85  }
    86  
    87  func (ps *Party) Name() string {
    88  	return "Party"
    89  }