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