code.vegaprotocol.io/vega@v0.79.0/core/events/position_state.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/libs/num" 22 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 23 ) 24 25 type PositionState struct { 26 *Base 27 partyID string 28 marketID string 29 size int64 30 potentialBuys int64 31 potentialSells int64 32 vwBuyPrice *num.Uint 33 vwSellPrice *num.Uint 34 } 35 36 func NewPositionStateEvent(ctx context.Context, mp MarketPosition, marketID string) *PositionState { 37 return &PositionState{ 38 Base: newBase(ctx, PositionStateEvent), 39 partyID: mp.Party(), 40 marketID: marketID, 41 size: mp.Size(), 42 potentialBuys: mp.Buy(), 43 potentialSells: mp.Sell(), 44 vwBuyPrice: mp.VWBuy(), 45 vwSellPrice: mp.VWSell(), 46 } 47 } 48 49 func (s PositionState) MarketID() string { 50 return s.marketID 51 } 52 53 func (s PositionState) IsParty(id string) bool { 54 return s.partyID == id 55 } 56 57 func (s PositionState) PartyID() string { 58 return s.partyID 59 } 60 61 func (s PositionState) Size() int64 { 62 return s.size 63 } 64 65 func (s PositionState) PotentialBuys() int64 { 66 return s.potentialBuys 67 } 68 69 func (s PositionState) PotentialSells() int64 { 70 return s.potentialSells 71 } 72 73 func (s PositionState) VWBuyPrice() *num.Uint { 74 return s.vwBuyPrice 75 } 76 77 func (s PositionState) VWSellPrice() *num.Uint { 78 return s.vwSellPrice 79 } 80 81 func (s PositionState) Proto() eventspb.PositionStateEvent { 82 return eventspb.PositionStateEvent{ 83 MarketId: s.marketID, 84 PartyId: s.partyID, 85 Size: s.size, 86 PotentialBuys: s.potentialBuys, 87 PotentialSells: s.potentialSells, 88 VwBuyPrice: s.vwBuyPrice.String(), 89 VwSellPrice: s.vwSellPrice.String(), 90 } 91 } 92 93 func (s PositionState) StreamMessage() *eventspb.BusEvent { 94 p := s.Proto() 95 96 busEvent := newBusEventFromBase(s.Base) 97 busEvent.Event = &eventspb.BusEvent_PositionStateEvent{ 98 PositionStateEvent: &p, 99 } 100 101 return busEvent 102 } 103 104 func PositionStateEventFromStream(ctx context.Context, be *eventspb.BusEvent) *PositionState { 105 pse := be.GetPositionStateEvent() 106 107 vwBuy, overflow := num.UintFromString(pse.VwBuyPrice, 10) 108 if overflow { 109 return nil 110 } 111 112 vwSell, overflow := num.UintFromString(pse.VwSellPrice, 10) 113 if overflow { 114 return nil 115 } 116 117 return &PositionState{ 118 Base: newBaseFromBusEvent(ctx, PositionStateEvent, be), 119 partyID: pse.PartyId, 120 marketID: pse.MarketId, 121 size: pse.Size, 122 potentialBuys: pse.PotentialBuys, 123 potentialSells: pse.PotentialSells, 124 vwBuyPrice: vwBuy, 125 vwSellPrice: vwSell, 126 } 127 }