code.vegaprotocol.io/vega@v0.79.0/core/events/settle_position.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 SettlePos struct {
    26  	*Base
    27  	partyID        string
    28  	marketID       string
    29  	positionFactor num.Decimal
    30  	price          *num.Uint
    31  	trades         []TradeSettlement
    32  	ts             int64
    33  }
    34  
    35  func NewSettlePositionEvent(ctx context.Context, partyID, marketID string, price *num.Uint, trades []TradeSettlement, ts int64, positionFactor num.Decimal) *SettlePos {
    36  	return &SettlePos{
    37  		Base:           newBase(ctx, SettlePositionEvent),
    38  		partyID:        partyID,
    39  		marketID:       marketID,
    40  		price:          price.Clone(),
    41  		trades:         trades,
    42  		ts:             ts,
    43  		positionFactor: positionFactor,
    44  	}
    45  }
    46  
    47  func (s SettlePos) MarketID() string {
    48  	return s.marketID
    49  }
    50  
    51  func (s SettlePos) IsParty(id string) bool {
    52  	return s.partyID == id
    53  }
    54  
    55  func (s SettlePos) PartyID() string {
    56  	return s.partyID
    57  }
    58  
    59  func (s SettlePos) Price() *num.Uint {
    60  	return s.price.Clone()
    61  }
    62  
    63  func (s SettlePos) Trades() []TradeSettlement {
    64  	return s.trades
    65  }
    66  
    67  func (s SettlePos) Timestamp() int64 {
    68  	return s.ts
    69  }
    70  
    71  func (s SettlePos) PositionFactor() num.Decimal {
    72  	return s.positionFactor
    73  }
    74  
    75  func (s SettlePos) Proto() eventspb.SettlePosition {
    76  	ts := make([]*eventspb.TradeSettlement, 0, len(s.trades))
    77  	for _, t := range s.trades {
    78  		ts = append(ts, &eventspb.TradeSettlement{
    79  			Size:        t.Size(),
    80  			Price:       t.Price().String(),
    81  			MarketPrice: t.MarketPrice().String(),
    82  		})
    83  	}
    84  	return eventspb.SettlePosition{
    85  		MarketId:         s.marketID,
    86  		PartyId:          s.partyID,
    87  		Price:            s.price.String(),
    88  		PositionFactor:   s.positionFactor.String(),
    89  		TradeSettlements: ts,
    90  	}
    91  }
    92  
    93  func (s SettlePos) StreamMessage() *eventspb.BusEvent {
    94  	p := s.Proto()
    95  
    96  	busEvent := newBusEventFromBase(s.Base)
    97  	busEvent.Event = &eventspb.BusEvent_SettlePosition{
    98  		SettlePosition: &p,
    99  	}
   100  
   101  	return busEvent
   102  }
   103  
   104  type settlement struct {
   105  	SettlementSize        int64
   106  	SettlementPrice       *num.Uint
   107  	SettlementMarketPrice *num.Uint
   108  }
   109  
   110  func (s settlement) Size() int64 {
   111  	return s.SettlementSize
   112  }
   113  
   114  func (s settlement) Price() *num.Uint {
   115  	return s.SettlementPrice
   116  }
   117  
   118  func (s settlement) MarketPrice() *num.Uint {
   119  	return s.SettlementMarketPrice
   120  }
   121  
   122  func SettlePositionEventFromStream(ctx context.Context, be *eventspb.BusEvent) *SettlePos {
   123  	sp := be.GetSettlePosition()
   124  	settlements := make([]TradeSettlement, 0, len(sp.TradeSettlements))
   125  	for _, ts := range sp.TradeSettlements {
   126  		price, _ := num.UintFromString(ts.Price, 10)
   127  		marketPrice, _ := num.UintFromString(ts.MarketPrice, 10)
   128  		settlements = append(settlements, settlement{
   129  			SettlementSize:        ts.Size,
   130  			SettlementPrice:       price,
   131  			SettlementMarketPrice: marketPrice,
   132  		})
   133  	}
   134  	spPrice, _ := num.UintFromString(sp.Price, 10)
   135  	positionFactor := num.MustDecimalFromString(sp.PositionFactor)
   136  
   137  	return &SettlePos{
   138  		Base:           newBaseFromBusEvent(ctx, SettlePositionEvent, be),
   139  		partyID:        sp.PartyId,
   140  		marketID:       sp.MarketId,
   141  		price:          spPrice,
   142  		trades:         settlements,
   143  		positionFactor: positionFactor,
   144  	}
   145  }