code.vegaprotocol.io/vega@v0.79.0/core/execution/common/equity_shares_snapshot.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 common
    17  
    18  import (
    19  	"sort"
    20  
    21  	"code.vegaprotocol.io/vega/core/types"
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  )
    24  
    25  func NewEquitySharesFromSnapshot(state *types.EquityShare) *EquityShares {
    26  	lps := map[string]*lp{}
    27  
    28  	totalPStake, totalVStake := num.DecimalZero(), num.DecimalZero()
    29  	for _, slp := range state.Lps {
    30  		lps[slp.ID] = &lp{
    31  			stake:  slp.Stake,
    32  			share:  slp.Share,
    33  			avg:    slp.Avg,
    34  			vStake: slp.VStake,
    35  		}
    36  		totalPStake = totalPStake.Add(slp.Stake)
    37  		totalVStake = totalVStake.Add(slp.VStake)
    38  	}
    39  
    40  	return &EquityShares{
    41  		mvp:                 state.Mvp,
    42  		r:                   state.R,
    43  		totalVStake:         totalVStake,
    44  		totalPStake:         totalPStake,
    45  		openingAuctionEnded: state.OpeningAuctionEnded,
    46  		lps:                 lps,
    47  	}
    48  }
    49  
    50  func (es EquityShares) Changed() bool {
    51  	return true
    52  }
    53  
    54  func (es *EquityShares) GetState() *types.EquityShare {
    55  	lps := make([]*types.EquityShareLP, 0, len(es.lps))
    56  	for id, lp := range es.lps {
    57  		lps = append(lps, &types.EquityShareLP{
    58  			ID:     id,
    59  			Stake:  lp.stake,
    60  			Share:  lp.share,
    61  			Avg:    lp.avg,
    62  			VStake: lp.vStake,
    63  		})
    64  	}
    65  
    66  	// Need to make sure the items are correctly sorted to make this deterministic
    67  	sort.Slice(lps, func(i, j int) bool {
    68  		return lps[i].ID < lps[j].ID
    69  	})
    70  
    71  	return &types.EquityShare{
    72  		Mvp:                 es.mvp,
    73  		R:                   es.r,
    74  		OpeningAuctionEnded: es.openingAuctionEnded,
    75  		Lps:                 lps,
    76  	}
    77  }