code.vegaprotocol.io/vega@v0.79.0/core/events/time.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 "time" 21 22 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 23 ) 24 25 // Time event indicating a change in block time (ie time update). 26 type Time struct { 27 *Base 28 blockTime time.Time 29 } 30 31 // NewTime returns a new time Update event. 32 func NewTime(ctx context.Context, t time.Time) *Time { 33 return &Time{ 34 Base: newBase(ctx, TimeUpdate), 35 blockTime: t, 36 } 37 } 38 39 // Time returns the new blocktime. 40 func (t Time) Time() time.Time { 41 return t.blockTime 42 } 43 44 func (t Time) Proto() eventspb.TimeUpdate { 45 return eventspb.TimeUpdate{ 46 Timestamp: t.blockTime.UnixNano(), 47 } 48 } 49 50 func (t Time) StreamMessage() *eventspb.BusEvent { 51 p := t.Proto() 52 busEvent := newBusEventFromBase(t.Base) 53 busEvent.Event = &eventspb.BusEvent_TimeUpdate{ 54 TimeUpdate: &p, 55 } 56 57 return busEvent 58 } 59 60 func TimeEventFromStream(ctx context.Context, be *eventspb.BusEvent) *Time { 61 return &Time{ 62 Base: newBaseFromBusEvent(ctx, TimeUpdate, be), 63 blockTime: time.Unix(0, be.GetTimeUpdate().Timestamp), 64 } 65 }