code.vegaprotocol.io/vega@v0.79.0/core/products/products.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  	"time"
    22  
    23  	dscommon "code.vegaprotocol.io/vega/core/datasource/common"
    24  	"code.vegaprotocol.io/vega/core/datasource/spec"
    25  	"code.vegaprotocol.io/vega/core/events"
    26  	"code.vegaprotocol.io/vega/core/types"
    27  	"code.vegaprotocol.io/vega/libs/num"
    28  	"code.vegaprotocol.io/vega/logging"
    29  	snapshotpb "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
    30  )
    31  
    32  var (
    33  	// ErrNilProduct signals the product passed in the constructor was nil.
    34  	ErrNilProduct = errors.New("nil product")
    35  	// ErrUnimplementedProduct signal that the product passed to the
    36  	// constructor was not nil, but the code as no knowledge of it.
    37  	ErrUnimplementedProduct = errors.New("unimplemented product")
    38  )
    39  
    40  // OracleEngine ...
    41  //
    42  //go:generate go run github.com/golang/mock/mockgen -destination mocks/mock.go -package mocks code.vegaprotocol.io/vega/core/products OracleEngine,Broker
    43  type OracleEngine interface {
    44  	ListensToSigners(dscommon.Data) bool
    45  	Subscribe(context.Context, spec.Spec, spec.OnMatchedData) (spec.SubscriptionID, spec.Unsubscriber, error)
    46  	Unsubscribe(context.Context, spec.SubscriptionID)
    47  }
    48  
    49  type Broker interface {
    50  	Send(e events.Event)
    51  	SendBatch(es []events.Event)
    52  }
    53  
    54  // Product is the interface provided by all product in vega.
    55  type Product interface {
    56  	Settle(*num.Uint, *num.Uint, num.Decimal) (amt *types.FinancialAmount, neg bool, rounding num.Decimal, err error)
    57  	Value(markPrice *num.Uint) (*num.Uint, error)
    58  	GetAsset() string
    59  	IsTradingTerminated() bool
    60  	ScaleSettlementDataToDecimalPlaces(price *num.Numeric, dp uint32) (*num.Uint, error)
    61  	NotifyOnTradingTerminated(listener func(context.Context, bool))
    62  	NotifyOnSettlementData(listener func(context.Context, *num.Numeric))
    63  	NotifyOnDataSourcePropagation(listener func(context.Context, *num.Uint))
    64  	Update(ctx context.Context, pp interface{}, oe OracleEngine) error
    65  	UnsubscribeTradingTerminated(ctx context.Context)
    66  	UnsubscribeSettlementData(ctx context.Context)
    67  	RestoreSettlementData(*num.Numeric)
    68  	UpdateAuctionState(context.Context, bool)
    69  
    70  	// tell the product about an internal data-point such as a the current mark-price
    71  	SubmitDataPoint(context.Context, *num.Uint, int64) error
    72  
    73  	// snapshot specific
    74  	Serialize() *snapshotpb.Product
    75  	GetMarginIncrease(int64) num.Decimal
    76  	GetData(t int64) *types.ProductData
    77  	GetCurrentPeriod() uint64
    78  }
    79  
    80  // TimeService ...
    81  type TimeService interface {
    82  	GetTimeNow() time.Time
    83  }
    84  
    85  // New instance a new product from a Market framework product configuration.
    86  func New(ctx context.Context, log *logging.Logger, pp interface{}, marketID string, ts TimeService, oe OracleEngine, broker Broker, assetDP uint32) (Product, error) {
    87  	if pp == nil {
    88  		return nil, ErrNilProduct
    89  	}
    90  
    91  	switch p := pp.(type) {
    92  	case *types.InstrumentFuture:
    93  		return NewFuture(ctx, log, p.Future, oe, assetDP)
    94  	case *types.InstrumentPerps:
    95  		return NewPerpetual(ctx, log, p.Perps, marketID, ts, oe, broker, assetDP)
    96  	default:
    97  		return nil, ErrUnimplementedProduct
    98  	}
    99  }