code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/market/sla_params.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/liquidity-sla-params/*.json 31 defaultLiquiditySLAParams embed.FS 32 defaultLiquiditySLAParamsFileNames = []string{ 33 "defaults/liquidity-sla-params/default-basic.json", 34 "defaults/liquidity-sla-params/default-futures.json", 35 "defaults/liquidity-sla-params/default-st.json", 36 } 37 ) 38 39 type slaParams struct { 40 config map[string]*types.LiquiditySLAParameters 41 } 42 43 func newLiquiditySLAParams(unmarshaler *defaults.Unmarshaler) *slaParams { 44 liquiditySLAParams := &slaParams{ 45 config: map[string]*types.LiquiditySLAParameters{}, 46 } 47 48 contentReaders := helpers.ReadAll(defaultLiquiditySLAParams, defaultLiquiditySLAParamsFileNames) 49 for name, contentReader := range contentReaders { 50 sla, err := unmarshaler.UnmarshalLiquiditySLAParams(contentReader) 51 if err != nil { 52 panic(fmt.Errorf("couldn't unmarshal default SLA params %s: %v", name, err)) 53 } 54 if err := liquiditySLAParams.Add(name, sla); err != nil { 55 panic(fmt.Errorf("failed to add default liquidity SLA params %s: %v", name, err)) 56 } 57 } 58 59 return liquiditySLAParams 60 } 61 62 func (f *slaParams) Add( 63 name string, 64 config *types.LiquiditySLAParameters, 65 ) error { 66 f.config[name] = config 67 return nil 68 } 69 70 func (f *slaParams) Get(name string) (*types.LiquiditySLAParameters, error) { 71 config, ok := f.config[name] 72 if !ok { 73 return config, fmt.Errorf("no liquidity SLA params \"%s\" registered", name) 74 } 75 // Copy to avoid modification between tests. 76 copyConfig := &types.LiquiditySLAParameters{} 77 if err := copier.Copy(copyConfig, config); err != nil { 78 panic(fmt.Errorf("failed to deep copy liquidity SLA params: %v", err)) 79 } 80 return copyConfig, nil 81 }