code.vegaprotocol.io/vega@v0.79.0/core/settlement/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 settlement
    17  
    18  import (
    19  	"code.vegaprotocol.io/vega/core/events"
    20  	"code.vegaprotocol.io/vega/core/types"
    21  	"code.vegaprotocol.io/vega/libs/num"
    22  
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  var ErrPartyDoesNotMatch = errors.New("event party and position party do not match")
    27  
    28  // MarketPosition stub event for network position (used in MTM stuff).
    29  type npos struct {
    30  	price *num.Uint
    31  }
    32  
    33  type pos struct {
    34  	events.MarketPosition
    35  	price *num.Uint
    36  }
    37  
    38  type mtmTransfer struct {
    39  	events.MarketPosition
    40  	transfer *types.Transfer
    41  }
    42  
    43  type settlementTrade struct {
    44  	size        int64
    45  	price       *num.Uint
    46  	marketPrice *num.Uint
    47  	newSize     int64 // track this so we can determine when a party switches between long <> short
    48  }
    49  
    50  func (t settlementTrade) Size() int64 {
    51  	return t.size
    52  }
    53  
    54  func (t settlementTrade) Price() *num.Uint {
    55  	return t.price.Clone()
    56  }
    57  
    58  func (t settlementTrade) MarketPrice() *num.Uint {
    59  	return t.marketPrice.Clone()
    60  }
    61  
    62  func newPos(marketPosition events.MarketPosition, price *num.Uint) *pos {
    63  	return &pos{
    64  		MarketPosition: marketPosition,
    65  		price:          price.Clone(),
    66  	}
    67  }
    68  
    69  // Price - part of the MarketPosition interface, used to update position after SettlePreTrade.
    70  func (p pos) Price() *num.Uint {
    71  	return p.price.Clone()
    72  }
    73  
    74  // Transfer - part of the Transfer interface.
    75  func (m mtmTransfer) Transfer() *types.Transfer {
    76  	if m.transfer == nil {
    77  		return nil
    78  	}
    79  	return m.transfer
    80  }
    81  
    82  func (npos) Party() string {
    83  	return types.NetworkParty
    84  }
    85  
    86  func (npos) Size() int64 {
    87  	return 0
    88  }
    89  
    90  func (npos) Buy() int64 {
    91  	return 0
    92  }
    93  
    94  func (npos) Sell() int64 {
    95  	return 0
    96  }
    97  
    98  func (n npos) Price() *num.Uint {
    99  	return n.price.Clone()
   100  }
   101  
   102  func (npos) BuySumProduct() *num.Uint {
   103  	return num.UintZero()
   104  }
   105  
   106  func (npos) SellSumProduct() *num.Uint {
   107  	return num.UintZero()
   108  }
   109  
   110  func (npos) VWBuy() *num.Uint {
   111  	return num.UintZero()
   112  }
   113  
   114  func (npos) VWSell() *num.Uint {
   115  	return num.UintZero()
   116  }
   117  
   118  func (npos) AverageEntryPrice() *num.Uint {
   119  	return num.UintZero()
   120  }