code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/market/liquidation_strats.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/liquidation-config/*.json 31 defaultLiquidationConfigs embed.FS 32 defaultLiquidationConfigFileNames = []string{ 33 "defaults/liquidation-config/legacy-liquidation-strategy.json", 34 "defaults/liquidation-config/default-liquidation-strat.json", 35 "defaults/liquidation-config/slow-liquidation-strat.json", 36 "defaults/liquidation-config/AC-013-strat.json", 37 } 38 39 defaultStrat = "legacy-liquidation-strategy" 40 ) 41 42 type liquidationConfig struct { 43 config map[string]*types.LiquidationStrategy 44 } 45 46 func newLiquidationConfig(unmarshaler *defaults.Unmarshaler) *liquidationConfig { 47 config := &liquidationConfig{ 48 config: map[string]*types.LiquidationStrategy{}, 49 } 50 51 contentReaders := helpers.ReadAll(defaultLiquidationConfigs, defaultLiquidationConfigFileNames) 52 for name, contentReader := range contentReaders { 53 liquidationConfig, err := unmarshaler.UnmarshalLiquidationConfig(contentReader) 54 if err != nil { 55 panic(fmt.Errorf("couldn't unmarshal default liquidation config %s: %v", name, err)) 56 } 57 if err := config.Add(name, liquidationConfig); err != nil { 58 panic(fmt.Errorf("failed to add default liquidation config %s: %v", name, err)) 59 } 60 } 61 62 return config 63 } 64 65 func (f *liquidationConfig) Add(name string, strategy *types.LiquidationStrategy) error { 66 f.config[name] = strategy 67 return nil 68 } 69 70 func (f *liquidationConfig) Get(name string) (*types.LiquidationStrategy, error) { 71 if name == "" { 72 // for now, default to the legacy strategy 73 name = defaultStrat 74 } 75 strategy, ok := f.config[name] 76 if !ok { 77 return strategy, fmt.Errorf("no liquidation configuration \"%s\" registered", name) 78 } 79 // Copy to avoid modification between tests. 80 copyConfig := &types.LiquidationStrategy{} 81 if err := copier.Copy(copyConfig, strategy); err != nil { 82 panic(fmt.Errorf("failed to deep copy liquidation config: %v", err)) 83 } 84 return copyConfig, nil 85 }