code.vegaprotocol.io/vega@v0.79.0/core/execution/future/market_checkpoint.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 future
    17  
    18  import (
    19  	"code.vegaprotocol.io/vega/core/types"
    20  	"code.vegaprotocol.io/vega/libs/num"
    21  )
    22  
    23  func (m *Market) GetCPState() *types.CPMarketState {
    24  	id := m.mkt.ID
    25  	shares := m.equityShares.GetCPShares()
    26  	// get all LP accounts, we don't have to sort this slice because we're fetching the balances
    27  	// in the same order as we got the ELS shares (which is already a deterministically sorted slice).
    28  	ipb, ok := m.collateral.GetInsurancePoolBalance(id, m.settlementAsset)
    29  	if !ok {
    30  		ipb = num.UintZero()
    31  	}
    32  	ms := types.CPMarketState{
    33  		ID:               id,
    34  		Shares:           shares,
    35  		InsuranceBalance: ipb,
    36  		LastTradeValue:   m.feeSplitter.TradeValue(),
    37  		State:            m.mkt.State,
    38  	}
    39  	// if the market was closed/settled, include the last valid market definition in the checkpoint
    40  	if m.mkt.State == types.MarketStateSettled || m.mkt.State == types.MarketStateClosed {
    41  		ms.Market = m.mkt.DeepClone()
    42  	}
    43  	return &ms
    44  }
    45  
    46  func (m *Market) LoadCPState(state *types.CPMarketState) {
    47  	m.mkt = state.Market
    48  	m.feeSplitter.SetTradeValue(state.LastTradeValue)
    49  	m.equityShares.SetCPShares(state.Shares)
    50  	// @TODO bond account and insurance account
    51  }
    52  
    53  func (m *Market) SetSuccessorELS(state *types.CPMarketState) {
    54  	// carry over traded value from predecessor
    55  	m.feeSplitter.AddTradeValue(state.LastTradeValue)
    56  	// load equity like shares
    57  	m.equityShares.SetCPShares(state.Shares)
    58  	// @TODO force a recalculation for the LP's who actually are present
    59  }