2019-06-05 16:26:17 -07:00
|
|
|
package types
|
2018-10-19 11:36:00 -07:00
|
|
|
|
|
|
|
import (
|
2019-12-10 08:48:57 -08:00
|
|
|
"errors"
|
2018-10-19 11:36:00 -07:00
|
|
|
"fmt"
|
2019-12-10 08:48:57 -08:00
|
|
|
"strings"
|
2018-11-26 04:13:47 -08:00
|
|
|
|
2020-02-24 08:05:21 -08:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2018-10-19 11:36:00 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-19 03:52:20 -08:00
|
|
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
2019-05-02 13:50:01 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parameter store keys
|
|
|
|
var (
|
|
|
|
KeyMintDenom = []byte("MintDenom")
|
|
|
|
KeyInflationRateChange = []byte("InflationRateChange")
|
|
|
|
KeyInflationMax = []byte("InflationMax")
|
|
|
|
KeyInflationMin = []byte("InflationMin")
|
|
|
|
KeyGoalBonded = []byte("GoalBonded")
|
|
|
|
KeyBlocksPerYear = []byte("BlocksPerYear")
|
2018-10-19 11:36:00 -07:00
|
|
|
)
|
|
|
|
|
2019-05-02 13:50:01 -07:00
|
|
|
// ParamTable for minting module.
|
2020-02-19 03:52:20 -08:00
|
|
|
func ParamKeyTable() paramtypes.KeyTable {
|
|
|
|
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
|
2019-05-02 13:50:01 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
func NewParams(
|
|
|
|
mintDenom string, inflationRateChange, inflationMax, inflationMin, goalBonded sdk.Dec, blocksPerYear uint64,
|
|
|
|
) Params {
|
2018-11-26 04:13:47 -08:00
|
|
|
|
|
|
|
return Params{
|
|
|
|
MintDenom: mintDenom,
|
|
|
|
InflationRateChange: inflationRateChange,
|
|
|
|
InflationMax: inflationMax,
|
|
|
|
InflationMin: inflationMin,
|
|
|
|
GoalBonded: goalBonded,
|
|
|
|
BlocksPerYear: blocksPerYear,
|
|
|
|
}
|
2018-10-19 11:36:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// default minting module parameters
|
|
|
|
func DefaultParams() Params {
|
|
|
|
return Params{
|
2019-02-13 15:01:50 -08:00
|
|
|
MintDenom: sdk.DefaultBondDenom,
|
2018-10-19 11:36:00 -07:00
|
|
|
InflationRateChange: sdk.NewDecWithPrec(13, 2),
|
|
|
|
InflationMax: sdk.NewDecWithPrec(20, 2),
|
|
|
|
InflationMin: sdk.NewDecWithPrec(7, 2),
|
|
|
|
GoalBonded: sdk.NewDecWithPrec(67, 2),
|
2018-11-26 04:13:47 -08:00
|
|
|
BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5 second block times
|
2018-10-19 11:36:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
// validate params
|
2019-12-10 08:48:57 -08:00
|
|
|
func (p Params) Validate() error {
|
|
|
|
if err := validateMintDenom(p.MintDenom); err != nil {
|
|
|
|
return err
|
2018-10-19 11:36:00 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateInflationRateChange(p.InflationRateChange); err != nil {
|
|
|
|
return err
|
2018-10-19 11:36:00 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateInflationMax(p.InflationMax); err != nil {
|
|
|
|
return err
|
2018-10-19 11:36:00 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateInflationMin(p.InflationMin); err != nil {
|
|
|
|
return err
|
2018-10-19 11:36:00 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
if err := validateGoalBonded(p.GoalBonded); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := validateBlocksPerYear(p.BlocksPerYear); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.InflationMax.LT(p.InflationMin) {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"max inflation (%s) must be greater than or equal to min inflation (%s)",
|
|
|
|
p.InflationMax, p.InflationMin,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:36:00 -07:00
|
|
|
return nil
|
2019-12-10 08:48:57 -08:00
|
|
|
|
2018-10-19 11:36:00 -07:00
|
|
|
}
|
2019-04-01 10:28:36 -07:00
|
|
|
|
2020-02-24 08:05:21 -08:00
|
|
|
// String implements the Stringer interface.
|
2019-04-01 10:28:36 -07:00
|
|
|
func (p Params) String() string {
|
2020-02-24 08:05:21 -08:00
|
|
|
out, _ := yaml.Marshal(p)
|
|
|
|
return string(out)
|
2019-04-01 10:28:36 -07:00
|
|
|
}
|
2019-05-02 13:50:01 -07:00
|
|
|
|
|
|
|
// Implements params.ParamSet
|
2020-02-19 03:52:20 -08:00
|
|
|
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
|
|
|
|
return paramtypes.ParamSetPairs{
|
|
|
|
paramtypes.NewParamSetPair(KeyMintDenom, &p.MintDenom, validateMintDenom),
|
|
|
|
paramtypes.NewParamSetPair(KeyInflationRateChange, &p.InflationRateChange, validateInflationRateChange),
|
|
|
|
paramtypes.NewParamSetPair(KeyInflationMax, &p.InflationMax, validateInflationMax),
|
|
|
|
paramtypes.NewParamSetPair(KeyInflationMin, &p.InflationMin, validateInflationMin),
|
|
|
|
paramtypes.NewParamSetPair(KeyGoalBonded, &p.GoalBonded, validateGoalBonded),
|
|
|
|
paramtypes.NewParamSetPair(KeyBlocksPerYear, &p.BlocksPerYear, validateBlocksPerYear),
|
2019-12-10 08:48:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateMintDenom(i interface{}) error {
|
|
|
|
v, ok := i.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.TrimSpace(v) == "" {
|
|
|
|
return errors.New("mint denom cannot be blank")
|
|
|
|
}
|
|
|
|
if err := sdk.ValidateDenom(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateInflationRateChange(i interface{}) error {
|
|
|
|
v, ok := i.(sdk.Dec)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsNegative() {
|
|
|
|
return fmt.Errorf("inflation rate change cannot be negative: %s", v)
|
|
|
|
}
|
|
|
|
if v.GT(sdk.OneDec()) {
|
|
|
|
return fmt.Errorf("inflation rate change too large: %s", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateInflationMax(i interface{}) error {
|
|
|
|
v, ok := i.(sdk.Dec)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsNegative() {
|
|
|
|
return fmt.Errorf("max inflation cannot be negative: %s", v)
|
2019-05-02 13:50:01 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
if v.GT(sdk.OneDec()) {
|
|
|
|
return fmt.Errorf("max inflation too large: %s", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateInflationMin(i interface{}) error {
|
|
|
|
v, ok := i.(sdk.Dec)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsNegative() {
|
|
|
|
return fmt.Errorf("min inflation cannot be negative: %s", v)
|
|
|
|
}
|
|
|
|
if v.GT(sdk.OneDec()) {
|
|
|
|
return fmt.Errorf("min inflation too large: %s", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateGoalBonded(i interface{}) error {
|
|
|
|
v, ok := i.(sdk.Dec)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsNegative() {
|
|
|
|
return fmt.Errorf("goal bonded cannot be negative: %s", v)
|
|
|
|
}
|
|
|
|
if v.GT(sdk.OneDec()) {
|
|
|
|
return fmt.Errorf("goal bonded too large: %s", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateBlocksPerYear(i interface{}) error {
|
|
|
|
v, ok := i.(uint64)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid parameter type: %T", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == 0 {
|
|
|
|
return fmt.Errorf("blocks per year must be positive: %d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-05-02 13:50:01 -07:00
|
|
|
}
|