code.vegaprotocol.io/vega@v0.79.0/core/events/market_tick.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  	"fmt"
    21  	"time"
    22  
    23  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    24  )
    25  
    26  type MarketTick struct {
    27  	*Base
    28  	id string
    29  	t  time.Time
    30  }
    31  
    32  func NewMarketTick(ctx context.Context, id string, t time.Time) *MarketTick {
    33  	return &MarketTick{
    34  		Base: newBase(ctx, MarketTickEvent),
    35  		id:   id,
    36  		t:    t,
    37  	}
    38  }
    39  
    40  func (m MarketTick) MarketID() string {
    41  	return m.id
    42  }
    43  
    44  func (m MarketTick) Time() time.Time {
    45  	return m.t
    46  }
    47  
    48  func (m MarketTick) MarketEvent() string {
    49  	return fmt.Sprintf("Market %s on time %s", m.id, m.t.String())
    50  }
    51  
    52  func (m MarketTick) Proto() eventspb.MarketTick {
    53  	return eventspb.MarketTick{
    54  		Id:   m.id,
    55  		Time: m.t.UTC().Unix(),
    56  	}
    57  }
    58  
    59  func (m MarketTick) MarketProto() eventspb.MarketEvent {
    60  	return eventspb.MarketEvent{
    61  		MarketId: m.id,
    62  		Payload:  m.MarketEvent(),
    63  	}
    64  }
    65  
    66  func (m MarketTick) StreamMessage() *eventspb.BusEvent {
    67  	p := m.Proto()
    68  	busEvent := newBusEventFromBase(m.Base)
    69  	busEvent.Event = &eventspb.BusEvent_MarketTick{
    70  		MarketTick: &p,
    71  	}
    72  
    73  	return busEvent
    74  }
    75  
    76  func (m MarketTick) StreamMarketMessage() *eventspb.BusEvent {
    77  	return m.StreamMessage()
    78  }
    79  
    80  func MarketTickEventFromStream(ctx context.Context, be *eventspb.BusEvent) *MarketTick {
    81  	return &MarketTick{
    82  		Base: newBaseFromBusEvent(ctx, MarketTickEvent, be),
    83  		id:   be.GetMarketTick().GetId(),
    84  		t:    time.Unix(be.GetMarketTick().GetTime(), 0).UTC(),
    85  	}
    86  }