code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/update_market_configuration_resolver.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 gql
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"strconv"
    22  
    23  	"code.vegaprotocol.io/vega/libs/ptr"
    24  	"code.vegaprotocol.io/vega/protos/vega"
    25  )
    26  
    27  type updateMarketConfigurationResolver VegaResolverRoot
    28  
    29  func (r *updateMarketConfigurationResolver) EnableTxReordering(ctx context.Context, obj *vega.UpdateMarketConfiguration) (bool, error) {
    30  	return obj.EnableTransactionReordering, nil
    31  }
    32  
    33  func (r *updateMarketConfigurationResolver) Instrument(ctx context.Context,
    34  	obj *vega.UpdateMarketConfiguration,
    35  ) (*UpdateInstrumentConfiguration, error) {
    36  	if obj == nil {
    37  		return nil, errors.New("no market configuration update provided")
    38  	}
    39  	protoInstrument := obj.Instrument
    40  
    41  	var product UpdateProductConfiguration
    42  
    43  	switch p := protoInstrument.Product.(type) {
    44  	case *vega.UpdateInstrumentConfiguration_Future:
    45  		product = &vega.UpdateFutureProduct{
    46  			QuoteName:                           p.Future.QuoteName,
    47  			DataSourceSpecForSettlementData:     p.Future.DataSourceSpecForSettlementData,
    48  			DataSourceSpecForTradingTermination: p.Future.DataSourceSpecForTradingTermination,
    49  			DataSourceSpecBinding:               p.Future.DataSourceSpecBinding,
    50  		}
    51  	case *vega.UpdateInstrumentConfiguration_Perpetual:
    52  		product = &vega.UpdatePerpetualProduct{
    53  			QuoteName:                           p.Perpetual.QuoteName,
    54  			MarginFundingFactor:                 p.Perpetual.MarginFundingFactor,
    55  			InterestRate:                        p.Perpetual.InterestRate,
    56  			ClampLowerBound:                     p.Perpetual.ClampLowerBound,
    57  			ClampUpperBound:                     p.Perpetual.ClampUpperBound,
    58  			FundingRateScalingFactor:            p.Perpetual.FundingRateScalingFactor,
    59  			FundingRateLowerBound:               p.Perpetual.FundingRateLowerBound,
    60  			FundingRateUpperBound:               p.Perpetual.FundingRateUpperBound,
    61  			DataSourceSpecForSettlementSchedule: p.Perpetual.DataSourceSpecForSettlementSchedule,
    62  			DataSourceSpecForSettlementData:     p.Perpetual.DataSourceSpecForSettlementData,
    63  			DataSourceSpecBinding:               p.Perpetual.DataSourceSpecBinding,
    64  		}
    65  	default:
    66  		return nil, ErrUnsupportedProduct
    67  	}
    68  
    69  	updateInstrumentConfiguration := &UpdateInstrumentConfiguration{
    70  		Code:    protoInstrument.Code,
    71  		Name:    protoInstrument.Name,
    72  		Product: product,
    73  	}
    74  
    75  	return updateInstrumentConfiguration, nil
    76  }
    77  
    78  func (r *updateMarketConfigurationResolver) PriceMonitoringParameters(ctx context.Context,
    79  	obj *vega.UpdateMarketConfiguration,
    80  ) (*PriceMonitoringParameters, error) {
    81  	if obj == nil {
    82  		return nil, errors.New("no market configuration update provided")
    83  	}
    84  
    85  	if obj.PriceMonitoringParameters == nil {
    86  		return nil, nil
    87  	}
    88  
    89  	triggers := make([]*PriceMonitoringTrigger, 0, len(obj.PriceMonitoringParameters.Triggers))
    90  
    91  	for _, trigger := range obj.PriceMonitoringParameters.Triggers {
    92  		probability, err := strconv.ParseFloat(trigger.Probability, 64)
    93  		if err != nil {
    94  			continue
    95  		}
    96  		triggers = append(triggers, &PriceMonitoringTrigger{
    97  			HorizonSecs:          int(trigger.Horizon),
    98  			Probability:          probability,
    99  			AuctionExtensionSecs: int(trigger.AuctionExtension),
   100  		})
   101  	}
   102  
   103  	params := &PriceMonitoringParameters{
   104  		Triggers: triggers,
   105  	}
   106  
   107  	return params, nil
   108  }
   109  
   110  func (r *updateMarketConfigurationResolver) LiquidityMonitoringParameters(ctx context.Context,
   111  	obj *vega.UpdateMarketConfiguration,
   112  ) (*LiquidityMonitoringParameters, error) {
   113  	if obj == nil {
   114  		return nil, errors.New("no market configuration update provided")
   115  	}
   116  
   117  	if obj.LiquidityMonitoringParameters == nil {
   118  		return nil, nil
   119  	}
   120  
   121  	return &LiquidityMonitoringParameters{
   122  		TargetStakeParameters: &TargetStakeParameters{
   123  			TimeWindow:    int(obj.LiquidityMonitoringParameters.TargetStakeParameters.TimeWindow),
   124  			ScalingFactor: obj.LiquidityMonitoringParameters.TargetStakeParameters.ScalingFactor,
   125  		},
   126  	}, nil
   127  }
   128  
   129  func (r *updateMarketConfigurationResolver) RiskParameters(ctx context.Context,
   130  	obj *vega.UpdateMarketConfiguration,
   131  ) (UpdateMarketRiskParameters, error) {
   132  	if obj == nil {
   133  		return nil, errors.New("no market configuration update provided")
   134  	}
   135  
   136  	if obj.RiskParameters == nil {
   137  		return nil, errors.New("no risk configuration provided")
   138  	}
   139  
   140  	var params UpdateMarketRiskParameters
   141  
   142  	switch rp := obj.RiskParameters.(type) {
   143  	case *vega.UpdateMarketConfiguration_Simple:
   144  		params = rp
   145  	case *vega.UpdateMarketConfiguration_LogNormal:
   146  		params = rp
   147  	default:
   148  		return nil, errors.New("invalid risk configuration provided")
   149  	}
   150  
   151  	return params, nil
   152  }
   153  
   154  func (r *updateMarketConfigurationResolver) AllowedEmptyAMMLevels(ctx context.Context, obj *vega.UpdateMarketConfiguration) (*int, error) {
   155  	v := obj.AllowedEmptyAmmLevels
   156  	if v == nil {
   157  		return nil, nil
   158  	}
   159  	return ptr.From(int(*obj.AllowedEmptyAmmLevels)), nil
   160  }