github.com/cosmos/cosmos-sdk@v0.50.10/x/mint/types/params.go (about) 1 package types 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "cosmossdk.io/math" 9 10 sdk "github.com/cosmos/cosmos-sdk/types" 11 ) 12 13 // NewParams returns Params instance with the given values. 14 func NewParams(mintDenom string, inflationRateChange, inflationMax, inflationMin, goalBonded math.LegacyDec, blocksPerYear uint64) Params { 15 return Params{ 16 MintDenom: mintDenom, 17 InflationRateChange: inflationRateChange, 18 InflationMax: inflationMax, 19 InflationMin: inflationMin, 20 GoalBonded: goalBonded, 21 BlocksPerYear: blocksPerYear, 22 } 23 } 24 25 // DefaultParams returns default x/mint module parameters. 26 func DefaultParams() Params { 27 return Params{ 28 MintDenom: sdk.DefaultBondDenom, 29 InflationRateChange: math.LegacyNewDecWithPrec(13, 2), 30 InflationMax: math.LegacyNewDecWithPrec(20, 2), 31 InflationMin: math.LegacyNewDecWithPrec(7, 2), 32 GoalBonded: math.LegacyNewDecWithPrec(67, 2), 33 BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5 second block times 34 } 35 } 36 37 // Validate does the sanity check on the params. 38 func (p Params) Validate() error { 39 if err := validateMintDenom(p.MintDenom); err != nil { 40 return err 41 } 42 if err := validateInflationRateChange(p.InflationRateChange); err != nil { 43 return err 44 } 45 if err := validateInflationMax(p.InflationMax); err != nil { 46 return err 47 } 48 if err := validateInflationMin(p.InflationMin); err != nil { 49 return err 50 } 51 if err := validateGoalBonded(p.GoalBonded); err != nil { 52 return err 53 } 54 if err := validateBlocksPerYear(p.BlocksPerYear); err != nil { 55 return err 56 } 57 if p.InflationMax.LT(p.InflationMin) { 58 return fmt.Errorf( 59 "max inflation (%s) must be greater than or equal to min inflation (%s)", 60 p.InflationMax, p.InflationMin, 61 ) 62 } 63 64 return nil 65 } 66 67 func validateMintDenom(i interface{}) error { 68 v, ok := i.(string) 69 if !ok { 70 return fmt.Errorf("invalid parameter type: %T", i) 71 } 72 73 if strings.TrimSpace(v) == "" { 74 return errors.New("mint denom cannot be blank") 75 } 76 if err := sdk.ValidateDenom(v); err != nil { 77 return err 78 } 79 80 return nil 81 } 82 83 func validateInflationRateChange(i interface{}) error { 84 v, ok := i.(math.LegacyDec) 85 if !ok { 86 return fmt.Errorf("invalid parameter type: %T", i) 87 } 88 89 if v.IsNil() { 90 return fmt.Errorf("inflation rate change cannot be nil: %s", v) 91 } 92 if v.IsNegative() { 93 return fmt.Errorf("inflation rate change cannot be negative: %s", v) 94 } 95 if v.GT(math.LegacyOneDec()) { 96 return fmt.Errorf("inflation rate change too large: %s", v) 97 } 98 99 return nil 100 } 101 102 func validateInflationMax(i interface{}) error { 103 v, ok := i.(math.LegacyDec) 104 if !ok { 105 return fmt.Errorf("invalid parameter type: %T", i) 106 } 107 108 if v.IsNil() { 109 return fmt.Errorf("max inflation cannot be nil: %s", v) 110 } 111 if v.IsNegative() { 112 return fmt.Errorf("max inflation cannot be negative: %s", v) 113 } 114 if v.GT(math.LegacyOneDec()) { 115 return fmt.Errorf("max inflation too large: %s", v) 116 } 117 118 return nil 119 } 120 121 func validateInflationMin(i interface{}) error { 122 v, ok := i.(math.LegacyDec) 123 if !ok { 124 return fmt.Errorf("invalid parameter type: %T", i) 125 } 126 127 if v.IsNil() { 128 return fmt.Errorf("min inflation cannot be nil: %s", v) 129 } 130 if v.IsNegative() { 131 return fmt.Errorf("min inflation cannot be negative: %s", v) 132 } 133 if v.GT(math.LegacyOneDec()) { 134 return fmt.Errorf("min inflation too large: %s", v) 135 } 136 137 return nil 138 } 139 140 func validateGoalBonded(i interface{}) error { 141 v, ok := i.(math.LegacyDec) 142 if !ok { 143 return fmt.Errorf("invalid parameter type: %T", i) 144 } 145 146 if v.IsNil() { 147 return fmt.Errorf("goal bonded cannot be nil: %s", v) 148 } 149 if v.IsNegative() || v.IsZero() { 150 return fmt.Errorf("goal bonded must be positive: %s", v) 151 } 152 if v.GT(math.LegacyOneDec()) { 153 return fmt.Errorf("goal bonded too large: %s", v) 154 } 155 156 return nil 157 } 158 159 func validateBlocksPerYear(i interface{}) error { 160 v, ok := i.(uint64) 161 if !ok { 162 return fmt.Errorf("invalid parameter type: %T", i) 163 } 164 165 if v == 0 { 166 return fmt.Errorf("blocks per year must be positive: %d", v) 167 } 168 169 return nil 170 }