code.vegaprotocol.io/vega@v0.79.0/core/events/position_resolution_event.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 22 "code.vegaprotocol.io/vega/libs/num" 23 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 24 ) 25 26 type PosRes struct { 27 *Base 28 distressed, closed int 29 marketID string 30 markPrice *num.Uint 31 } 32 33 func NewPositionResolution(ctx context.Context, distressed, closed int, markPrice *num.Uint, marketID string) *PosRes { 34 base := newBase(ctx, PositionResolution) 35 return &PosRes{ 36 Base: base, 37 distressed: distressed, 38 closed: closed, 39 markPrice: markPrice, 40 marketID: marketID, 41 } 42 } 43 44 // MarketEvent implement the MarketEvent interface. 45 func (p PosRes) MarketEvent() string { 46 return fmt.Sprintf("Market %s entered position resolution, %d parties were distressed, %d of which were closed out at mark price %s", p.marketID, p.distressed, p.closed, p.markPrice.String()) 47 } 48 49 func (p PosRes) MarketID() string { 50 return p.marketID 51 } 52 53 func (p PosRes) MarkPrice() *num.Uint { 54 return p.markPrice.Clone() 55 } 56 57 func (p PosRes) Distressed() int { 58 return p.distressed 59 } 60 61 func (p PosRes) Closed() int { 62 return p.closed 63 } 64 65 func (p PosRes) Proto() eventspb.PositionResolution { 66 return eventspb.PositionResolution{ 67 MarketId: p.marketID, 68 Closed: int64(p.closed), 69 Distressed: int64(p.distressed), 70 MarkPrice: p.markPrice.String(), 71 } 72 } 73 74 func (p PosRes) MarketProto() eventspb.MarketEvent { 75 return eventspb.MarketEvent{ 76 MarketId: p.marketID, 77 Payload: p.MarketEvent(), 78 } 79 } 80 81 func (p PosRes) StreamMessage() *eventspb.BusEvent { 82 pr := p.Proto() 83 84 busEvent := newBusEventFromBase(p.Base) 85 busEvent.Event = &eventspb.BusEvent_PositionResolution{ 86 PositionResolution: &pr, 87 } 88 89 return busEvent 90 } 91 92 func (p PosRes) StreamMarketMessage() *eventspb.BusEvent { 93 msg := p.MarketProto() 94 95 busEvent := newBusEventFromBase(p.Base) 96 busEvent.Type = eventspb.BusEventType_BUS_EVENT_TYPE_MARKET 97 busEvent.Event = &eventspb.BusEvent_Market{ 98 Market: &msg, 99 } 100 101 return busEvent 102 } 103 104 func PositionResolutionEventFromStream(ctx context.Context, be *eventspb.BusEvent) *PosRes { 105 base := newBaseFromBusEvent(ctx, PositionResolution, be) 106 mp, _ := num.UintFromString(be.GetPositionResolution().GetMarkPrice(), 10) 107 return &PosRes{ 108 Base: base, 109 distressed: int(be.GetPositionResolution().Distressed), 110 closed: int(be.GetPositionResolution().Closed), 111 markPrice: mp, 112 marketID: be.GetPositionResolution().MarketId, 113 } 114 }