code.vegaprotocol.io/vega@v0.79.0/core/events/settle_market.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 SettleMarket struct {
    26  	*Base
    27  	marketID       string
    28  	settledPrice   *num.Uint
    29  	positionFactor num.Decimal
    30  	ts             int64
    31  }
    32  
    33  func NewMarketSettled(ctx context.Context, marketID string, ts int64, settledPrice *num.Uint, positionFactor num.Decimal) *SettleMarket {
    34  	return &SettleMarket{
    35  		Base:           newBase(ctx, SettleMarketEvent),
    36  		marketID:       marketID,
    37  		settledPrice:   settledPrice.Clone(),
    38  		positionFactor: positionFactor,
    39  		ts:             ts,
    40  	}
    41  }
    42  
    43  func (m SettleMarket) MarketID() string {
    44  	return m.marketID
    45  }
    46  
    47  // PartyID will return an empty string as this is only required to satisfy an interface
    48  // for identifying events that can affect positions in the data-node.
    49  func (m SettleMarket) PartyID() string {
    50  	return ""
    51  }
    52  
    53  func (m SettleMarket) SettledPrice() *num.Uint {
    54  	return m.settledPrice.Clone()
    55  }
    56  
    57  func (m SettleMarket) PositionFactor() num.Decimal {
    58  	return m.positionFactor
    59  }
    60  
    61  func (m SettleMarket) Timestamp() int64 {
    62  	return m.ts
    63  }
    64  
    65  func (m SettleMarket) Proto() *eventspb.SettleMarket {
    66  	return &eventspb.SettleMarket{
    67  		MarketId:       m.marketID,
    68  		Price:          m.settledPrice.String(),
    69  		PositionFactor: m.positionFactor.String(),
    70  	}
    71  }
    72  
    73  func (m SettleMarket) StreamMessage() *eventspb.BusEvent {
    74  	p := m.Proto()
    75  	busEvent := newBusEventFromBase(m.Base)
    76  	busEvent.Event = &eventspb.BusEvent_SettleMarket{SettleMarket: p}
    77  	return busEvent
    78  }
    79  
    80  func SettleMarketEventFromStream(ctx context.Context, be *eventspb.BusEvent) *SettleMarket {
    81  	sm := be.GetSettleMarket()
    82  	smPrice, _ := num.UintFromString(sm.Price, 10)
    83  	positionFactor := num.MustDecimalFromString(sm.PositionFactor)
    84  
    85  	return &SettleMarket{
    86  		Base:           newBaseFromBusEvent(ctx, SettleMarketEvent, be),
    87  		marketID:       sm.MarketId,
    88  		settledPrice:   smPrice,
    89  		positionFactor: positionFactor,
    90  	}
    91  }