code.vegaprotocol.io/vega@v0.79.0/core/events/loss_socialization.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/core/types"
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    24  )
    25  
    26  type LossSoc struct {
    27  	*Base
    28  	partyID  string
    29  	marketID string
    30  	amount   *num.Int
    31  	ts       int64
    32  	lType    types.LossType
    33  }
    34  
    35  func NewLossSocializationEvent(ctx context.Context, partyID, marketID string, amount *num.Uint, neg bool, ts int64, lType types.LossType) *LossSoc {
    36  	signedAmount := num.NewIntFromUint(amount)
    37  	if neg {
    38  		signedAmount.FlipSign()
    39  	}
    40  	return &LossSoc{
    41  		Base:     newBase(ctx, LossSocializationEvent),
    42  		partyID:  partyID,
    43  		marketID: marketID,
    44  		amount:   signedAmount,
    45  		ts:       ts,
    46  		lType:    lType,
    47  	}
    48  }
    49  
    50  func (l LossSoc) LossType() types.LossType {
    51  	return l.lType
    52  }
    53  
    54  func (l LossSoc) IsFunding() bool {
    55  	return l.lType == types.LossTypeFunding
    56  }
    57  
    58  func (l LossSoc) IsParty(id string) bool {
    59  	return l.partyID == id
    60  }
    61  
    62  func (l LossSoc) PartyID() string {
    63  	return l.partyID
    64  }
    65  
    66  func (l LossSoc) MarketID() string {
    67  	return l.marketID
    68  }
    69  
    70  func (l LossSoc) Negative() bool {
    71  	return l.amount.IsNegative()
    72  }
    73  
    74  func (l LossSoc) Amount() *num.Int {
    75  	return l.amount.Clone()
    76  }
    77  
    78  func (l LossSoc) Timestamp() int64 {
    79  	return l.ts
    80  }
    81  
    82  func (l LossSoc) Proto() eventspb.LossSocialization {
    83  	return eventspb.LossSocialization{
    84  		MarketId: l.marketID,
    85  		PartyId:  l.partyID,
    86  		Amount:   l.amount.String(),
    87  		LossType: l.lType,
    88  	}
    89  }
    90  
    91  func (l LossSoc) StreamMessage() *eventspb.BusEvent {
    92  	p := l.Proto()
    93  
    94  	busEvent := newBusEventFromBase(l.Base)
    95  	busEvent.Event = &eventspb.BusEvent_LossSocialization{
    96  		LossSocialization: &p,
    97  	}
    98  
    99  	return busEvent
   100  }
   101  
   102  func LossSocializationEventFromStream(ctx context.Context, be *eventspb.BusEvent) *LossSoc {
   103  	ls := be.GetLossSocialization()
   104  	lse := &LossSoc{
   105  		Base:     newBaseFromBusEvent(ctx, LossSocializationEvent, be),
   106  		partyID:  ls.PartyId,
   107  		marketID: ls.MarketId,
   108  		lType:    ls.LossType,
   109  	}
   110  
   111  	lse.amount, _ = num.IntFromString(ls.Amount, 10)
   112  	return lse
   113  }