code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/market/price_monitoring.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 market
    17  
    18  import (
    19  	"embed"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/core/integration/steps/helpers"
    23  	"code.vegaprotocol.io/vega/core/integration/steps/market/defaults"
    24  	types "code.vegaprotocol.io/vega/protos/vega"
    25  
    26  	"github.com/jinzhu/copier"
    27  )
    28  
    29  var (
    30  	//go:embed defaults/price-monitoring/*.json
    31  	defaultPriceMonitoring          embed.FS
    32  	defaultPriceMonitoringFileNames = []string{
    33  		"defaults/price-monitoring/default-none.json",
    34  		"defaults/price-monitoring/default-basic.json",
    35  	}
    36  )
    37  
    38  type priceMonitoring struct {
    39  	config map[string]*types.PriceMonitoringSettings
    40  }
    41  
    42  func newPriceMonitoring(unmarshaler *defaults.Unmarshaler) *priceMonitoring {
    43  	priceMonitoring := &priceMonitoring{
    44  		config: map[string]*types.PriceMonitoringSettings{},
    45  	}
    46  
    47  	contentReaders := helpers.ReadAll(defaultPriceMonitoring, defaultPriceMonitoringFileNames)
    48  	for name, contentReader := range contentReaders {
    49  		pm, err := unmarshaler.UnmarshalPriceMonitoring(contentReader)
    50  		if err != nil {
    51  			panic(fmt.Errorf("couldn't unmarshal default price monitoring %s: %v", name, err))
    52  		}
    53  		if err := priceMonitoring.Add(name, pm); err != nil {
    54  			panic(fmt.Errorf("failed to add default price monitoring %s: %v", name, err))
    55  		}
    56  	}
    57  
    58  	return priceMonitoring
    59  }
    60  
    61  func (f *priceMonitoring) Add(
    62  	name string,
    63  	config *types.PriceMonitoringSettings,
    64  ) error {
    65  	f.config[name] = config
    66  	return nil
    67  }
    68  
    69  func (f *priceMonitoring) Get(name string) (*types.PriceMonitoringSettings, error) {
    70  	config, ok := f.config[name]
    71  	if !ok {
    72  		return config, fmt.Errorf("no price monitoring \"%s\" registered", name)
    73  	}
    74  	// Copy to avoid modification between tests.
    75  	copyConfig := &types.PriceMonitoringSettings{}
    76  	if err := copier.Copy(copyConfig, config); err != nil {
    77  		panic(fmt.Errorf("failed to deep copy price monitoring: %v", err))
    78  	}
    79  	return copyConfig, nil
    80  }