github.com/Finschia/finschia-sdk@v0.48.1/x/mint/types/params.go (about)

     1  package types
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	yaml "gopkg.in/yaml.v2"
     9  
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  	paramtypes "github.com/Finschia/finschia-sdk/x/params/types"
    12  )
    13  
    14  // Parameter store keys
    15  var (
    16  	KeyMintDenom           = []byte("MintDenom")
    17  	KeyInflationRateChange = []byte("InflationRateChange")
    18  	KeyInflationMax        = []byte("InflationMax")
    19  	KeyInflationMin        = []byte("InflationMin")
    20  	KeyGoalBonded          = []byte("GoalBonded")
    21  	KeyBlocksPerYear       = []byte("BlocksPerYear")
    22  )
    23  
    24  // ParamTable for minting module.
    25  func ParamKeyTable() paramtypes.KeyTable {
    26  	return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
    27  }
    28  
    29  func NewParams(
    30  	mintDenom string, inflationRateChange, inflationMax, inflationMin, goalBonded sdk.Dec, blocksPerYear uint64,
    31  ) Params {
    32  	return Params{
    33  		MintDenom:           mintDenom,
    34  		InflationRateChange: inflationRateChange,
    35  		InflationMax:        inflationMax,
    36  		InflationMin:        inflationMin,
    37  		GoalBonded:          goalBonded,
    38  		BlocksPerYear:       blocksPerYear,
    39  	}
    40  }
    41  
    42  // default minting module parameters
    43  func DefaultParams() Params {
    44  	return Params{
    45  		MintDenom:           sdk.DefaultBondDenom,
    46  		InflationRateChange: sdk.NewDecWithPrec(13, 2),
    47  		InflationMax:        sdk.NewDecWithPrec(20, 2),
    48  		InflationMin:        sdk.NewDecWithPrec(7, 2),
    49  		GoalBonded:          sdk.NewDecWithPrec(67, 2),
    50  		BlocksPerYear:       uint64(60 * 60 * 8766 / 5), // assuming 5 second block times
    51  	}
    52  }
    53  
    54  // validate params
    55  func (p Params) Validate() error {
    56  	if err := validateMintDenom(p.MintDenom); err != nil {
    57  		return err
    58  	}
    59  	if err := validateInflationRateChange(p.InflationRateChange); err != nil {
    60  		return err
    61  	}
    62  	if err := validateInflationMax(p.InflationMax); err != nil {
    63  		return err
    64  	}
    65  	if err := validateInflationMin(p.InflationMin); err != nil {
    66  		return err
    67  	}
    68  	if err := validateGoalBonded(p.GoalBonded); err != nil {
    69  		return err
    70  	}
    71  	if err := validateBlocksPerYear(p.BlocksPerYear); err != nil {
    72  		return err
    73  	}
    74  	if p.InflationMax.LT(p.InflationMin) {
    75  		return fmt.Errorf(
    76  			"max inflation (%s) must be greater than or equal to min inflation (%s)",
    77  			p.InflationMax, p.InflationMin,
    78  		)
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  // String implements the Stringer interface.
    85  func (p Params) String() string {
    86  	out, _ := yaml.Marshal(p)
    87  	return string(out)
    88  }
    89  
    90  // Implements params.ParamSet
    91  func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
    92  	return paramtypes.ParamSetPairs{
    93  		paramtypes.NewParamSetPair(KeyMintDenom, &p.MintDenom, validateMintDenom),
    94  		paramtypes.NewParamSetPair(KeyInflationRateChange, &p.InflationRateChange, validateInflationRateChange),
    95  		paramtypes.NewParamSetPair(KeyInflationMax, &p.InflationMax, validateInflationMax),
    96  		paramtypes.NewParamSetPair(KeyInflationMin, &p.InflationMin, validateInflationMin),
    97  		paramtypes.NewParamSetPair(KeyGoalBonded, &p.GoalBonded, validateGoalBonded),
    98  		paramtypes.NewParamSetPair(KeyBlocksPerYear, &p.BlocksPerYear, validateBlocksPerYear),
    99  	}
   100  }
   101  
   102  func validateMintDenom(i interface{}) error {
   103  	v, ok := i.(string)
   104  	if !ok {
   105  		return fmt.Errorf("invalid parameter type: %T", i)
   106  	}
   107  
   108  	if strings.TrimSpace(v) == "" {
   109  		return errors.New("mint denom cannot be blank")
   110  	}
   111  	if err := sdk.ValidateDenom(v); err != nil {
   112  		return err
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  func validateInflationRateChange(i interface{}) error {
   119  	v, ok := i.(sdk.Dec)
   120  	if !ok {
   121  		return fmt.Errorf("invalid parameter type: %T", i)
   122  	}
   123  
   124  	if v.IsNegative() {
   125  		return fmt.Errorf("inflation rate change cannot be negative: %s", v)
   126  	}
   127  	if v.GT(sdk.OneDec()) {
   128  		return fmt.Errorf("inflation rate change too large: %s", v)
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func validateInflationMax(i interface{}) error {
   135  	v, ok := i.(sdk.Dec)
   136  	if !ok {
   137  		return fmt.Errorf("invalid parameter type: %T", i)
   138  	}
   139  
   140  	if v.IsNegative() {
   141  		return fmt.Errorf("max inflation cannot be negative: %s", v)
   142  	}
   143  	if v.GT(sdk.OneDec()) {
   144  		return fmt.Errorf("max inflation too large: %s", v)
   145  	}
   146  
   147  	return nil
   148  }
   149  
   150  func validateInflationMin(i interface{}) error {
   151  	v, ok := i.(sdk.Dec)
   152  	if !ok {
   153  		return fmt.Errorf("invalid parameter type: %T", i)
   154  	}
   155  
   156  	if v.IsNegative() {
   157  		return fmt.Errorf("min inflation cannot be negative: %s", v)
   158  	}
   159  	if v.GT(sdk.OneDec()) {
   160  		return fmt.Errorf("min inflation too large: %s", v)
   161  	}
   162  
   163  	return nil
   164  }
   165  
   166  func validateGoalBonded(i interface{}) error {
   167  	v, ok := i.(sdk.Dec)
   168  	if !ok {
   169  		return fmt.Errorf("invalid parameter type: %T", i)
   170  	}
   171  
   172  	if v.IsNegative() || v.IsZero() {
   173  		return fmt.Errorf("goal bonded must be positive: %s", v)
   174  	}
   175  	if v.GT(sdk.OneDec()) {
   176  		return fmt.Errorf("goal bonded too large: %s", v)
   177  	}
   178  
   179  	return nil
   180  }
   181  
   182  func validateBlocksPerYear(i interface{}) error {
   183  	v, ok := i.(uint64)
   184  	if !ok {
   185  		return fmt.Errorf("invalid parameter type: %T", i)
   186  	}
   187  
   188  	if v == 0 {
   189  		return fmt.Errorf("blocks per year must be positive: %d", v)
   190  	}
   191  
   192  	return nil
   193  }