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

     1  package types
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"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  	if v.IsNil() {
   124  		return fmt.Errorf("inflation rate change cannot be nil: %s", v)
   125  	}
   126  	if v.IsNegative() {
   127  		return fmt.Errorf("inflation rate change cannot be negative: %s", v)
   128  	}
   129  	if v.GT(sdk.OneDec()) {
   130  		return fmt.Errorf("inflation rate change too large: %s", v)
   131  	}
   132  
   133  	return nil
   134  }
   135  
   136  func validateInflationMax(i interface{}) error {
   137  	v, ok := i.(sdk.Dec)
   138  	if !ok {
   139  		return fmt.Errorf("invalid parameter type: %T", i)
   140  	}
   141  	if v.IsNil() {
   142  		return fmt.Errorf("max inflation cannot be nil: %s", v)
   143  	}
   144  	if v.IsNegative() {
   145  		return fmt.Errorf("max inflation cannot be negative: %s", v)
   146  	}
   147  	if v.GT(sdk.OneDec()) {
   148  		return fmt.Errorf("max inflation too large: %s", v)
   149  	}
   150  
   151  	return nil
   152  }
   153  
   154  func validateInflationMin(i interface{}) error {
   155  	v, ok := i.(sdk.Dec)
   156  	if !ok {
   157  		return fmt.Errorf("invalid parameter type: %T", i)
   158  	}
   159  	if v.IsNil() {
   160  		return fmt.Errorf("min inflation cannot be nil: %s", v)
   161  	}
   162  	if v.IsNegative() {
   163  		return fmt.Errorf("min inflation cannot be negative: %s", v)
   164  	}
   165  	if v.GT(sdk.OneDec()) {
   166  		return fmt.Errorf("min inflation too large: %s", v)
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  func validateGoalBonded(i interface{}) error {
   173  	v, ok := i.(sdk.Dec)
   174  	if !ok {
   175  		return fmt.Errorf("invalid parameter type: %T", i)
   176  	}
   177  	if v.IsNil() {
   178  		return fmt.Errorf("goal bonded cannot be nil: %s", v)
   179  	}
   180  	if v.IsNegative() || v.IsZero() {
   181  		return fmt.Errorf("goal bonded must be positive: %s", v)
   182  	}
   183  	if v.GT(sdk.OneDec()) {
   184  		return fmt.Errorf("goal bonded too large: %s", v)
   185  	}
   186  
   187  	return nil
   188  }
   189  
   190  func validateBlocksPerYear(i interface{}) error {
   191  	v, ok := i.(uint64)
   192  	if !ok {
   193  		return fmt.Errorf("invalid parameter type: %T", i)
   194  	}
   195  
   196  	if v == 0 {
   197  		return fmt.Errorf("blocks per year must be positive: %d", v)
   198  	}
   199  
   200  	return nil
   201  }