code.vegaprotocol.io/vega@v0.79.0/core/events/distressed_orders.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  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    22  )
    23  
    24  // DistressedOrders contains the market and parties that needed to have their orders closed in order
    25  // to maintain their open positions on the market.
    26  type DistressedOrders struct {
    27  	*Base
    28  
    29  	pb eventspb.DistressedOrders
    30  }
    31  
    32  func NewDistressedOrdersEvent(ctx context.Context, marketID string, parties []string) *DistressedOrders {
    33  	return &DistressedOrders{
    34  		Base: newBase(ctx, DistressedOrdersClosedEvent),
    35  		pb: eventspb.DistressedOrders{
    36  			MarketId: marketID,
    37  			Parties:  parties,
    38  		},
    39  	}
    40  }
    41  
    42  func (d DistressedOrders) MarketID() string {
    43  	return d.pb.MarketId
    44  }
    45  
    46  func (d DistressedOrders) Parties() []string {
    47  	return d.pb.Parties
    48  }
    49  
    50  func (d DistressedOrders) IsMarket(marketID string) bool {
    51  	return d.pb.MarketId == marketID
    52  }
    53  
    54  func (d DistressedOrders) IsParty(partyID string) bool {
    55  	for _, p := range d.pb.Parties {
    56  		if p == partyID {
    57  			return true
    58  		}
    59  	}
    60  	return false
    61  }
    62  
    63  func (d DistressedOrders) Proto() eventspb.DistressedOrders {
    64  	return d.pb
    65  }
    66  
    67  func (d DistressedOrders) StreamMessage() *eventspb.BusEvent {
    68  	busEvent := newBusEventFromBase(d.Base)
    69  	cpy := d.pb
    70  	busEvent.Event = &eventspb.BusEvent_DistressedOrders{
    71  		DistressedOrders: &cpy,
    72  	}
    73  
    74  	return busEvent
    75  }
    76  
    77  func (d DistressedOrders) StreamMarketMessage() *eventspb.BusEvent {
    78  	return d.StreamMessage()
    79  }
    80  
    81  func DistressedOrdersEventFromStream(ctx context.Context, be *eventspb.BusEvent) *DistressedOrders {
    82  	m := be.GetDistressedOrders()
    83  	return &DistressedOrders{
    84  		Base: newBaseFromBusEvent(ctx, DistressedOrdersClosedEvent, be),
    85  		pb:   *m,
    86  	}
    87  }