code.vegaprotocol.io/vega@v0.79.0/core/types/plugins.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 types
    17  
    18  import (
    19  	"code.vegaprotocol.io/vega/libs/num"
    20  	proto "code.vegaprotocol.io/vega/protos/vega"
    21  )
    22  
    23  type Position struct {
    24  	// Market identifier
    25  	MarketID string
    26  	// Party identifier
    27  	PartyID string
    28  	// Open volume for the position, value is signed +ve for long and -ve for short
    29  	OpenVolume int64
    30  	// Realised profit and loss for the position, value is signed +ve for long and -ve for short
    31  	RealisedPnl num.Decimal
    32  	// Unrealised profit and loss for the position, value is signed +ve for long and -ve for short
    33  	UnrealisedPnl num.Decimal
    34  	// Average entry price for the position, the price is an integer, for example `123456` is a correctly
    35  	// formatted price of `1.23456` assuming market configured to 5 decimal places
    36  	AverageEntryPrice *num.Uint
    37  	// Timestamp for the latest time the position was updated
    38  	UpdatedAt                 int64
    39  	TakerFeesPaid             *num.Uint
    40  	TakerFeesPaidSince        *num.Uint
    41  	MakerFeesReceived         *num.Uint
    42  	MakerFeesReceivedSince    *num.Uint
    43  	FeesPaid                  *num.Uint
    44  	FeesPaidSince             *num.Uint
    45  	FundingPaymentAmount      *num.Int
    46  	FundingPaymentAmountSince *num.Int
    47  }
    48  
    49  func NewPosition(marketID, partyID string) Position {
    50  	return Position{
    51  		MarketID:                  marketID,
    52  		PartyID:                   partyID,
    53  		AverageEntryPrice:         num.UintZero(),
    54  		TakerFeesPaid:             num.UintZero(),
    55  		MakerFeesReceived:         num.UintZero(),
    56  		FeesPaid:                  num.UintZero(),
    57  		TakerFeesPaidSince:        num.UintZero(),
    58  		MakerFeesReceivedSince:    num.UintZero(),
    59  		FeesPaidSince:             num.UintZero(),
    60  		FundingPaymentAmount:      num.IntZero(),
    61  		FundingPaymentAmountSince: num.IntZero(),
    62  	}
    63  }
    64  
    65  func (p *Position) IntoProto() *proto.Position {
    66  	if p.FeesPaid == nil {
    67  		p.FeesPaid = num.UintZero()
    68  	}
    69  	if p.FeesPaidSince == nil {
    70  		p.FeesPaidSince = num.UintZero()
    71  	}
    72  	if p.TakerFeesPaid == nil {
    73  		p.TakerFeesPaid = num.UintZero()
    74  	}
    75  	if p.TakerFeesPaidSince == nil {
    76  		p.TakerFeesPaidSince = num.UintZero()
    77  	}
    78  	if p.MakerFeesReceived == nil {
    79  		p.MakerFeesReceived = num.UintZero()
    80  	}
    81  	if p.MakerFeesReceivedSince == nil {
    82  		p.MakerFeesReceivedSince = num.UintZero()
    83  	}
    84  	if p.FundingPaymentAmount == nil {
    85  		p.FundingPaymentAmount = num.IntZero()
    86  	}
    87  	if p.FundingPaymentAmountSince == nil {
    88  		p.FundingPaymentAmountSince = num.IntZero()
    89  	}
    90  	return &proto.Position{
    91  		MarketId:                  p.MarketID,
    92  		PartyId:                   p.PartyID,
    93  		OpenVolume:                p.OpenVolume,
    94  		RealisedPnl:               p.RealisedPnl.BigInt().String(),
    95  		UnrealisedPnl:             p.UnrealisedPnl.BigInt().String(),
    96  		AverageEntryPrice:         num.UintToString(p.AverageEntryPrice),
    97  		UpdatedAt:                 p.UpdatedAt,
    98  		TakerFeesPaid:             p.TakerFeesPaid.String(),
    99  		MakerFeesReceived:         p.MakerFeesReceived.String(),
   100  		FeesPaid:                  p.FeesPaid.String(),
   101  		TakerFeesPaidSince:        p.TakerFeesPaidSince.String(),
   102  		MakerFeesReceivedSince:    p.MakerFeesReceivedSince.String(),
   103  		FundingPaymentAmount:      p.FundingPaymentAmount.String(),
   104  		FundingPaymentAmountSince: p.FundingPaymentAmountSince.String(),
   105  	}
   106  }
   107  
   108  type Positions []*Position
   109  
   110  func (p Positions) IntoProto() []*proto.Position {
   111  	out := make([]*proto.Position, 0, len(p))
   112  	for _, v := range p {
   113  		out = append(out, v.IntoProto())
   114  	}
   115  	return out
   116  }
   117  
   118  func (p *Position) ResetSince() {
   119  	p.TakerFeesPaidSince = num.UintZero()
   120  	p.MakerFeesReceivedSince = num.UintZero()
   121  	p.FeesPaidSince = num.UintZero()
   122  	p.FundingPaymentAmountSince = num.IntZero()
   123  }