code.vegaprotocol.io/vega@v0.79.0/core/events/settle_distressed.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 SettleDistressed struct { 26 *Base 27 partyID string 28 marketID string 29 margin *num.Uint 30 price *num.Uint 31 ts int64 32 } 33 34 func NewSettleDistressed(ctx context.Context, partyID, marketID string, price, margin *num.Uint, ts int64) *SettleDistressed { 35 return &SettleDistressed{ 36 Base: newBase(ctx, SettleDistressedEvent), 37 partyID: partyID, 38 marketID: marketID, 39 margin: margin.Clone(), 40 price: price.Clone(), 41 ts: ts, 42 } 43 } 44 45 func (s SettleDistressed) IsParty(id string) bool { 46 return s.partyID == id 47 } 48 49 func (s SettleDistressed) PartyID() string { 50 return s.partyID 51 } 52 53 func (s SettleDistressed) MarketID() string { 54 return s.marketID 55 } 56 57 func (s SettleDistressed) Margin() *num.Uint { 58 return s.margin.Clone() 59 } 60 61 func (s SettleDistressed) Price() *num.Uint { 62 return s.price.Clone() 63 } 64 65 func (s SettleDistressed) Timestamp() int64 { 66 return s.ts 67 } 68 69 func (s SettleDistressed) Proto() eventspb.SettleDistressed { 70 return eventspb.SettleDistressed{ 71 MarketId: s.marketID, 72 PartyId: s.partyID, 73 Margin: s.margin.String(), 74 Price: s.price.String(), 75 } 76 } 77 78 func (s SettleDistressed) StreamMessage() *eventspb.BusEvent { 79 p := s.Proto() 80 81 busEvent := newBusEventFromBase(s.Base) 82 busEvent.Event = &eventspb.BusEvent_SettleDistressed{ 83 SettleDistressed: &p, 84 } 85 86 return busEvent 87 } 88 89 func SettleDistressedEventFromStream(ctx context.Context, be *eventspb.BusEvent) *SettleDistressed { 90 sd := be.GetSettleDistressed() 91 sdMargin, marginOverflow := num.UintFromString(sd.Margin, 10) 92 sdPrice, priceOverflow := num.UintFromString(sd.Price, 10) 93 94 if marginOverflow || priceOverflow { 95 return nil 96 } 97 98 return &SettleDistressed{ 99 Base: newBaseFromBusEvent(ctx, SettleDistressedEvent, be), 100 partyID: sd.PartyId, 101 marketID: sd.MarketId, 102 margin: sdMargin, 103 price: sdPrice, 104 } 105 }