code.vegaprotocol.io/vega@v0.79.0/core/products/products_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 products
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/logging"
    24  	snapshotpb "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
    25  )
    26  
    27  var ErrNoStateProvidedForPerpsWithSnapshot = errors.New("no state provided to restore perps from a snapshot")
    28  
    29  // New instance a new product from a Market framework product configuration.
    30  func NewFromSnapshot(
    31  	ctx context.Context,
    32  	log *logging.Logger,
    33  	pp interface{},
    34  	marketID string,
    35  	ts TimeService,
    36  	oe OracleEngine,
    37  	broker Broker,
    38  	state *snapshotpb.Product,
    39  	assetDP uint32,
    40  ) (Product, error) {
    41  	if pp == nil {
    42  		return nil, ErrNilProduct
    43  	}
    44  
    45  	switch p := pp.(type) {
    46  	case *types.InstrumentFuture: // no state in the future, so all OK
    47  		return NewFuture(ctx, log, p.Future, oe, assetDP)
    48  	case *types.InstrumentPerps:
    49  		perpsState := state.GetPerps()
    50  		if perpsState == nil {
    51  			return nil, ErrNoStateProvidedForPerpsWithSnapshot
    52  		}
    53  		return NewPerpetualFromSnapshot(ctx, log, p.Perps, marketID, ts, oe, broker, perpsState, assetDP)
    54  	default:
    55  		return nil, ErrUnimplementedProduct
    56  	}
    57  }