code.vegaprotocol.io/vega@v0.79.0/core/markets/instrument.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 ) 28 29 // TradableInstrument represent an instrument to be trade in a market. 30 type TradableInstrument struct { 31 Instrument *Instrument 32 MarginCalculator *types.MarginCalculator 33 RiskModel risk.Model 34 assetDP uint32 35 } 36 37 // NewTradableInstrument will instantiate a new tradable instrument 38 // using a market framework configuration for a tradable instrument. 39 func NewTradableInstrument(ctx context.Context, log *logging.Logger, pti *types.TradableInstrument, marketID string, ts common.TimeService, oe products.OracleEngine, broker products.Broker, assetDP uint32) (*TradableInstrument, error) { 40 instrument, err := NewInstrument(ctx, log, pti.Instrument, marketID, ts, oe, broker, assetDP) 41 if err != nil { 42 return nil, err 43 } 44 asset := instrument.Product.GetAsset() 45 riskModel, err := risk.NewModel(pti.RiskModel, asset) 46 if err != nil { 47 return nil, fmt.Errorf("unable to instantiate risk model: %w", err) 48 } 49 return &TradableInstrument{ 50 Instrument: instrument, 51 MarginCalculator: pti.MarginCalculator, 52 RiskModel: riskModel, 53 assetDP: assetDP, // keep it here for the update call 54 }, nil 55 } 56 57 func (i *TradableInstrument) UpdateInstrument(ctx context.Context, log *logging.Logger, ti *types.TradableInstrument, marketID string, oe products.OracleEngine, broker products.Broker) error { 58 i.Instrument.Update(ctx, log, ti.Instrument, oe) 59 60 asset := i.Instrument.Product.GetAsset() 61 62 riskModel, err := risk.NewModel(ti.RiskModel, asset) 63 if err != nil { 64 return fmt.Errorf("unable to instantiate risk model: %w", err) 65 } 66 67 i.RiskModel = riskModel 68 i.MarginCalculator = ti.MarginCalculator 69 return nil 70 } 71 72 // Instrument represent an instrument used in a market. 73 type Instrument struct { 74 ID string 75 Code string 76 Name string 77 Metadata *types.InstrumentMetadata 78 Product products.Product 79 80 Quote string 81 } 82 83 // NewInstrument will instantiate a new instrument 84 // using a market framework configuration for a instrument. 85 func NewInstrument(ctx context.Context, log *logging.Logger, pi *types.Instrument, marketID string, ts common.TimeService, oe products.OracleEngine, broker products.Broker, assetDP uint32) (*Instrument, error) { 86 product, err := products.New(ctx, log, pi.Product, marketID, ts, oe, broker, assetDP) 87 if err != nil { 88 return nil, fmt.Errorf("unable to instantiate product from instrument configuration: %w", err) 89 } 90 return &Instrument{ 91 ID: pi.ID, 92 Code: pi.Code, 93 Name: pi.Name, 94 Metadata: pi.Metadata, 95 Product: product, 96 }, err 97 } 98 99 func (i *Instrument) UpdateAuctionState(ctx context.Context, enter bool) { 100 i.Product.UpdateAuctionState(ctx, enter) 101 } 102 103 func (i *Instrument) UnsubscribeTradingTerminated(ctx context.Context) { 104 i.Product.UnsubscribeTradingTerminated(ctx) 105 } 106 107 func (i *Instrument) UnsubscribeSettlementData(ctx context.Context) { 108 i.Product.UnsubscribeSettlementData(ctx) 109 } 110 111 func (i *Instrument) Unsubscribe(ctx context.Context) { 112 i.UnsubscribeTradingTerminated(ctx) 113 i.UnsubscribeSettlementData(ctx) 114 } 115 116 // NewInstrument will instantiate a new instrument 117 // using a market framework configuration for a instrument. 118 func (i *Instrument) Update(ctx context.Context, log *logging.Logger, pi *types.Instrument, oe products.OracleEngine) error { 119 if err := i.Product.Update(ctx, pi.Product, oe); err != nil { 120 return err 121 } 122 123 i.ID = pi.ID 124 i.Code = pi.Code 125 i.Name = pi.Name 126 i.Metadata = pi.Metadata 127 return nil 128 }