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

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	yaml "gopkg.in/yaml.v2"
     8  
     9  	sdk "github.com/Finschia/finschia-sdk/types"
    10  	paramtypes "github.com/Finschia/finschia-sdk/x/params/types"
    11  )
    12  
    13  // Default period for deposits & voting
    14  const (
    15  	DefaultPeriod time.Duration = time.Hour * 24 * 2 // 2 days
    16  )
    17  
    18  // Default governance params
    19  var (
    20  	DefaultMinDepositTokens = sdk.NewInt(10000000)
    21  	DefaultQuorum           = sdk.NewDecWithPrec(334, 3)
    22  	DefaultThreshold        = sdk.NewDecWithPrec(5, 1)
    23  	DefaultVetoThreshold    = sdk.NewDecWithPrec(334, 3)
    24  )
    25  
    26  // Parameter store key
    27  var (
    28  	ParamStoreKeyDepositParams = []byte("depositparams")
    29  	ParamStoreKeyVotingParams  = []byte("votingparams")
    30  	ParamStoreKeyTallyParams   = []byte("tallyparams")
    31  )
    32  
    33  // ParamKeyTable - Key declaration for parameters
    34  func ParamKeyTable() paramtypes.KeyTable {
    35  	return paramtypes.NewKeyTable(
    36  		paramtypes.NewParamSetPair(ParamStoreKeyDepositParams, DepositParams{}, validateDepositParams),
    37  		paramtypes.NewParamSetPair(ParamStoreKeyVotingParams, VotingParams{}, validateVotingParams),
    38  		paramtypes.NewParamSetPair(ParamStoreKeyTallyParams, TallyParams{}, validateTallyParams),
    39  	)
    40  }
    41  
    42  // NewDepositParams creates a new DepositParams object
    43  func NewDepositParams(minDeposit sdk.Coins, maxDepositPeriod time.Duration) DepositParams {
    44  	return DepositParams{
    45  		MinDeposit:       minDeposit,
    46  		MaxDepositPeriod: maxDepositPeriod,
    47  	}
    48  }
    49  
    50  // DefaultDepositParams default parameters for deposits
    51  func DefaultDepositParams() DepositParams {
    52  	return NewDepositParams(
    53  		sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, DefaultMinDepositTokens)),
    54  		DefaultPeriod,
    55  	)
    56  }
    57  
    58  // String implements stringer insterface
    59  func (dp DepositParams) String() string {
    60  	out, _ := yaml.Marshal(dp)
    61  	return string(out)
    62  }
    63  
    64  // Equal checks equality of DepositParams
    65  func (dp DepositParams) Equal(dp2 DepositParams) bool {
    66  	return dp.MinDeposit.IsEqual(dp2.MinDeposit) && dp.MaxDepositPeriod == dp2.MaxDepositPeriod
    67  }
    68  
    69  func validateDepositParams(i interface{}) error {
    70  	v, ok := i.(DepositParams)
    71  	if !ok {
    72  		return fmt.Errorf("invalid parameter type: %T", i)
    73  	}
    74  
    75  	if !v.MinDeposit.IsValid() {
    76  		return fmt.Errorf("invalid minimum deposit: %s", v.MinDeposit)
    77  	}
    78  	if v.MaxDepositPeriod <= 0 {
    79  		return fmt.Errorf("maximum deposit period must be positive: %d", v.MaxDepositPeriod)
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  // NewTallyParams creates a new TallyParams object
    86  func NewTallyParams(quorum, threshold, vetoThreshold sdk.Dec) TallyParams {
    87  	return TallyParams{
    88  		Quorum:        quorum,
    89  		Threshold:     threshold,
    90  		VetoThreshold: vetoThreshold,
    91  	}
    92  }
    93  
    94  // DefaultTallyParams default parameters for tallying
    95  func DefaultTallyParams() TallyParams {
    96  	return NewTallyParams(DefaultQuorum, DefaultThreshold, DefaultVetoThreshold)
    97  }
    98  
    99  // Equal checks equality of TallyParams
   100  func (tp TallyParams) Equal(other TallyParams) bool {
   101  	return tp.Quorum.Equal(other.Quorum) && tp.Threshold.Equal(other.Threshold) && tp.VetoThreshold.Equal(other.VetoThreshold)
   102  }
   103  
   104  // String implements stringer insterface
   105  func (tp TallyParams) String() string {
   106  	out, _ := yaml.Marshal(tp)
   107  	return string(out)
   108  }
   109  
   110  func validateTallyParams(i interface{}) error {
   111  	v, ok := i.(TallyParams)
   112  	if !ok {
   113  		return fmt.Errorf("invalid parameter type: %T", i)
   114  	}
   115  
   116  	if v.Quorum.IsNegative() {
   117  		return fmt.Errorf("quorom cannot be negative: %s", v.Quorum)
   118  	}
   119  	if v.Quorum.GT(sdk.OneDec()) {
   120  		return fmt.Errorf("quorom too large: %s", v)
   121  	}
   122  	if !v.Threshold.IsPositive() {
   123  		return fmt.Errorf("vote threshold must be positive: %s", v.Threshold)
   124  	}
   125  	if v.Threshold.GT(sdk.OneDec()) {
   126  		return fmt.Errorf("vote threshold too large: %s", v)
   127  	}
   128  	if !v.VetoThreshold.IsPositive() {
   129  		return fmt.Errorf("veto threshold must be positive: %s", v.Threshold)
   130  	}
   131  	if v.VetoThreshold.GT(sdk.OneDec()) {
   132  		return fmt.Errorf("veto threshold too large: %s", v)
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  // NewVotingParams creates a new VotingParams object
   139  func NewVotingParams(votingPeriod time.Duration) VotingParams {
   140  	return VotingParams{
   141  		VotingPeriod: votingPeriod,
   142  	}
   143  }
   144  
   145  // DefaultVotingParams default parameters for voting
   146  func DefaultVotingParams() VotingParams {
   147  	return NewVotingParams(DefaultPeriod)
   148  }
   149  
   150  // Equal checks equality of TallyParams
   151  func (vp VotingParams) Equal(other VotingParams) bool {
   152  	return vp.VotingPeriod == other.VotingPeriod
   153  }
   154  
   155  // String implements stringer interface
   156  func (vp VotingParams) String() string {
   157  	out, _ := yaml.Marshal(vp)
   158  	return string(out)
   159  }
   160  
   161  func validateVotingParams(i interface{}) error {
   162  	v, ok := i.(VotingParams)
   163  	if !ok {
   164  		return fmt.Errorf("invalid parameter type: %T", i)
   165  	}
   166  
   167  	if v.VotingPeriod <= 0 {
   168  		return fmt.Errorf("voting period must be positive: %s", v.VotingPeriod)
   169  	}
   170  
   171  	return nil
   172  }
   173  
   174  // Params returns all of the governance params
   175  type Params struct {
   176  	VotingParams  VotingParams  `json:"voting_params" yaml:"voting_params"`
   177  	TallyParams   TallyParams   `json:"tally_params" yaml:"tally_params"`
   178  	DepositParams DepositParams `json:"deposit_params" yaml:"deposit_params"`
   179  }
   180  
   181  func (gp Params) String() string {
   182  	return gp.VotingParams.String() + "\n" +
   183  		gp.TallyParams.String() + "\n" + gp.DepositParams.String()
   184  }
   185  
   186  // NewParams creates a new gov Params instance
   187  func NewParams(vp VotingParams, tp TallyParams, dp DepositParams) Params {
   188  	return Params{
   189  		VotingParams:  vp,
   190  		DepositParams: dp,
   191  		TallyParams:   tp,
   192  	}
   193  }
   194  
   195  // DefaultParams default governance params
   196  func DefaultParams() Params {
   197  	return NewParams(DefaultVotingParams(), DefaultTallyParams(), DefaultDepositParams())
   198  }