code.vegaprotocol.io/vega@v0.79.0/core/events/statevar.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 events
    17  
    18  import (
    19  	"context"
    20  
    21  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    22  )
    23  
    24  // StateVar is an event for tracking consensus in floating point state variables.
    25  type StateVar struct {
    26  	*Base
    27  	ID      string
    28  	EventID string
    29  	State   string
    30  }
    31  
    32  func NewStateVarEvent(ctx context.Context, ID, eventID, state string) *StateVar {
    33  	return &StateVar{
    34  		Base:    newBase(ctx, StateVarEvent),
    35  		ID:      ID,
    36  		EventID: eventID,
    37  		State:   state,
    38  	}
    39  }
    40  
    41  func (sv StateVar) Proto() eventspb.StateVar {
    42  	return eventspb.StateVar{
    43  		Id:      sv.ID,
    44  		EventId: sv.EventID,
    45  		State:   sv.State,
    46  	}
    47  }
    48  
    49  func (sv StateVar) StreamMessage() *eventspb.BusEvent {
    50  	p := sv.Proto()
    51  	busEvent := newBusEventFromBase(sv.Base)
    52  	busEvent.Event = &eventspb.BusEvent_StateVar{
    53  		StateVar: &p,
    54  	}
    55  
    56  	return busEvent
    57  }
    58  
    59  func StateVarEventFromStream(ctx context.Context, be *eventspb.BusEvent) *StateVar {
    60  	event := be.GetStateVar()
    61  	if event == nil {
    62  		panic("failed to get state var event from event bus")
    63  	}
    64  
    65  	return &StateVar{
    66  		Base:    newBaseFromBusEvent(ctx, StateVarEvent, be),
    67  		ID:      event.GetId(),
    68  		EventID: event.GetEventId(),
    69  		State:   event.GetState(),
    70  	}
    71  }