code.vegaprotocol.io/vega@v0.79.0/core/events/trade.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 "code.vegaprotocol.io/vega/core/types" 22 ptypes "code.vegaprotocol.io/vega/protos/vega" 23 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 24 ) 25 26 type Trade struct { 27 *Base 28 t ptypes.Trade 29 } 30 31 func NewTradeEvent(ctx context.Context, t types.Trade) *Trade { 32 p := t.IntoProto() 33 p.Price = t.MarketPrice.String() 34 p.AssetPrice = t.Price.String() 35 return &Trade{ 36 Base: newBase(ctx, TradeEvent), 37 t: *p, 38 } 39 } 40 41 func (t Trade) MarketID() string { 42 return t.t.MarketId 43 } 44 45 func (t Trade) IsParty(id string) bool { 46 return t.t.Buyer == id || t.t.Seller == id 47 } 48 49 func (t *Trade) Trade() ptypes.Trade { 50 return t.t 51 } 52 53 func (t Trade) Proto() ptypes.Trade { 54 return t.t 55 } 56 57 func (t Trade) StreamMessage() *eventspb.BusEvent { 58 busEvent := newBusEventFromBase(t.Base) 59 busEvent.Event = &eventspb.BusEvent_Trade{ 60 Trade: &t.t, 61 } 62 63 return busEvent 64 } 65 66 func TradeEventFromStream(ctx context.Context, be *eventspb.BusEvent) *Trade { 67 return &Trade{ 68 Base: newBaseFromBusEvent(ctx, TradeEvent, be), 69 t: *be.GetTrade(), 70 } 71 }