code.vegaprotocol.io/vega@v0.79.0/core/liquidity/supplied/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 supplied
    17  
    18  import (
    19  	"code.vegaprotocol.io/vega/libs/num"
    20  	snapshotpb "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
    21  )
    22  
    23  func (e *Engine) Payload() *snapshotpb.Payload {
    24  	bidCache := make([]*snapshotpb.LiquidityOffsetProbabilityPair, 0, len(e.pot.bidOffset))
    25  	for i := 0; i < len(e.pot.bidOffset); i++ {
    26  		bidCache = append(bidCache, &snapshotpb.LiquidityOffsetProbabilityPair{Offset: e.pot.bidOffset[i], Probability: e.pot.bidProbability[i].String()})
    27  	}
    28  	askCache := make([]*snapshotpb.LiquidityOffsetProbabilityPair, 0, len(e.pot.askOffset))
    29  	for i := 0; i < len(e.pot.askOffset); i++ {
    30  		askCache = append(askCache, &snapshotpb.LiquidityOffsetProbabilityPair{Offset: e.pot.askOffset[i], Probability: e.pot.askProbability[i].String()})
    31  	}
    32  
    33  	return &snapshotpb.Payload{
    34  		Data: &snapshotpb.Payload_LiquiditySupplied{
    35  			LiquiditySupplied: &snapshotpb.LiquiditySupplied{
    36  				MarketId:         e.marketID,
    37  				BidCache:         bidCache,
    38  				AskCache:         askCache,
    39  				ConsensusReached: e.potInitialised,
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func (e *Engine) Reload(ls *snapshotpb.LiquiditySupplied) error {
    46  	bidOffsets := make([]uint32, 0, len(ls.BidCache))
    47  	bidProbs := make([]num.Decimal, 0, len(ls.BidCache))
    48  	for _, bid := range ls.BidCache {
    49  		bidOffsets = append(bidOffsets, bid.Offset)
    50  		bidProbs = append(bidProbs, num.MustDecimalFromString(bid.Probability))
    51  	}
    52  	askOffsets := make([]uint32, 0, len(ls.AskCache))
    53  	askProbs := make([]num.Decimal, 0, len(ls.AskCache))
    54  	for _, ask := range ls.AskCache {
    55  		askOffsets = append(askOffsets, ask.Offset)
    56  		askProbs = append(askProbs, num.MustDecimalFromString(ask.Probability))
    57  	}
    58  
    59  	e.pot = &probabilityOfTrading{
    60  		bidOffset:      bidOffsets,
    61  		bidProbability: bidProbs,
    62  		askOffset:      askOffsets,
    63  		askProbability: askProbs,
    64  	}
    65  	e.potInitialised = ls.ConsensusReached
    66  	return nil
    67  }