code.vegaprotocol.io/vega@v0.79.0/core/markets/instrument_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 markets
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/core/execution/common"
    23  	"code.vegaprotocol.io/vega/core/products"
    24  	"code.vegaprotocol.io/vega/core/risk"
    25  	"code.vegaprotocol.io/vega/core/types"
    26  	"code.vegaprotocol.io/vega/logging"
    27  	snapshotpb "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
    28  )
    29  
    30  // NewInstrument will instantiate a new instrument
    31  // using a market framework configuration for a instrument.
    32  func NewInstrumentFromSnapshot(
    33  	ctx context.Context,
    34  	log *logging.Logger,
    35  	pi *types.Instrument,
    36  	marketID string,
    37  	ts common.TimeService,
    38  	oe products.OracleEngine,
    39  	broker products.Broker,
    40  	productState *snapshotpb.Product,
    41  	assetDP uint32,
    42  ) (*Instrument, error) {
    43  	product, err := products.NewFromSnapshot(ctx, log, pi.Product, marketID, ts, oe, broker, productState, assetDP)
    44  	if err != nil {
    45  		return nil, fmt.Errorf("unable to instantiate product from instrument configuration: %w", err)
    46  	}
    47  	return &Instrument{
    48  		ID:       pi.ID,
    49  		Code:     pi.Code,
    50  		Name:     pi.Name,
    51  		Metadata: pi.Metadata,
    52  		Product:  product,
    53  	}, err
    54  }
    55  
    56  // NewTradableInstrument will instantiate a new tradable instrument
    57  // using a market framework configuration for a tradable instrument.
    58  func NewTradableInstrumentFromSnapshot(
    59  	ctx context.Context,
    60  	log *logging.Logger,
    61  	pti *types.TradableInstrument,
    62  	marketID string,
    63  	ts common.TimeService,
    64  	oe products.OracleEngine,
    65  	broker products.Broker,
    66  	productState *snapshotpb.Product,
    67  	assetDP uint32,
    68  ) (*TradableInstrument, error) {
    69  	instrument, err := NewInstrumentFromSnapshot(ctx, log, pti.Instrument, marketID, ts, oe, broker, productState, assetDP)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	asset := instrument.Product.GetAsset()
    74  	riskModel, err := risk.NewModel(pti.RiskModel, asset)
    75  	if err != nil {
    76  		return nil, fmt.Errorf("unable to instantiate risk model: %w", err)
    77  	}
    78  	return &TradableInstrument{
    79  		Instrument:       instrument,
    80  		MarginCalculator: pti.MarginCalculator,
    81  		RiskModel:        riskModel,
    82  	}, nil
    83  }