github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/gov/types/params.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/fibonacci-chain/fbc/x/params/subspace"
     9  )
    10  
    11  // Parameter store key
    12  var (
    13  	ParamStoreKeyDepositParams = []byte("depositparams")
    14  	ParamStoreKeyVotingParams  = []byte("votingparams")
    15  	ParamStoreKeyTallyParams   = []byte("tallyparams")
    16  )
    17  
    18  // Key declaration for parameters
    19  func ParamKeyTable() subspace.KeyTable {
    20  	return subspace.NewKeyTable(
    21  		subspace.ParamSetPairs{
    22  			{ParamStoreKeyDepositParams, DepositParams{}, validateDepositParams},
    23  			{ParamStoreKeyVotingParams, VotingParams{}, validateVotingParams},
    24  			{ParamStoreKeyTallyParams, TallyParams{}, validateTallyParams},
    25  		}...,
    26  	)
    27  }
    28  
    29  // Param around deposits for governance
    30  type DepositParams struct {
    31  	MinDeposit       sdk.SysCoins  `json:"min_deposit,omitempty" yaml:"min_deposit,omitempty"`               //  Minimum deposit for a proposal to enter voting period.
    32  	MaxDepositPeriod time.Duration `json:"max_deposit_period,omitempty" yaml:"max_deposit_period,omitempty"` //  Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months
    33  }
    34  
    35  // NewDepositParams creates a new DepositParams object
    36  func NewDepositParams(minDeposit sdk.SysCoins, maxDepositPeriod time.Duration) DepositParams {
    37  	return DepositParams{
    38  		MinDeposit:       minDeposit,
    39  		MaxDepositPeriod: maxDepositPeriod,
    40  	}
    41  }
    42  
    43  func (dp DepositParams) String() string {
    44  	return fmt.Sprintf(`Deposit Params:
    45    Min Deposit:        %s
    46    Max Deposit Period: %s`, dp.MinDeposit, dp.MaxDepositPeriod)
    47  }
    48  
    49  // Checks equality of DepositParams
    50  func (dp DepositParams) Equal(dp2 DepositParams) bool {
    51  	return dp.MinDeposit.IsEqual(dp2.MinDeposit) && dp.MaxDepositPeriod == dp2.MaxDepositPeriod
    52  }
    53  
    54  func validateDepositParams(i interface{}) error {
    55  	v, ok := i.(DepositParams)
    56  	if !ok {
    57  		return fmt.Errorf("invalid parameter type: %T", i)
    58  	}
    59  
    60  	if !v.MinDeposit.IsValid() {
    61  		return fmt.Errorf("invalid minimum deposit: %s", v.MinDeposit)
    62  	}
    63  	if v.MaxDepositPeriod <= 0 {
    64  		return fmt.Errorf("maximum deposit period must be positive: %d", v.MaxDepositPeriod)
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  // Param around Tallying votes in governance
    71  type TallyParams struct {
    72  	Quorum          sdk.Dec `json:"quorum,omitempty" yaml:"quorum,omitempty"`                         //  Minimum percentage of total stake needed to vote for a result to be considered valid
    73  	Threshold       sdk.Dec `json:"threshold,omitempty" yaml:"threshold,omitempty"`                   //  Minimum proportion of Yes votes for proposal to pass. Initial value: 0.5
    74  	Veto            sdk.Dec `json:"veto,omitempty" yaml:"veto,omitempty"`                             //  Minimum value of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3
    75  	YesInVotePeriod sdk.Dec `json:"yes_in_vote_period,omitempty" yaml:"yes_in_vote_period,omitempty"` //
    76  }
    77  
    78  // NewTallyParams creates a new TallyParams object
    79  func NewTallyParams(quorum, threshold, veto sdk.Dec) TallyParams {
    80  	return TallyParams{
    81  		Quorum:    quorum,
    82  		Threshold: threshold,
    83  		Veto:      veto,
    84  	}
    85  }
    86  
    87  func (tp TallyParams) String() string {
    88  	return fmt.Sprintf(`Tally Params:
    89    Quorum:             %s
    90    Threshold:          %s
    91    Veto:               %s`,
    92  		tp.Quorum, tp.Threshold, tp.Veto)
    93  }
    94  
    95  func validateTallyParams(i interface{}) error {
    96  	v, ok := i.(TallyParams)
    97  	if !ok {
    98  		return fmt.Errorf("invalid parameter type: %T", i)
    99  	}
   100  
   101  	if v.Quorum.IsNegative() {
   102  		return fmt.Errorf("quorom cannot be negative: %s", v.Quorum)
   103  	}
   104  	if v.Quorum.GT(sdk.OneDec()) {
   105  		return fmt.Errorf("quorom too large: %s", v)
   106  	}
   107  	if !v.Threshold.IsPositive() {
   108  		return fmt.Errorf("vote threshold must be positive: %s", v.Threshold)
   109  	}
   110  	if v.Threshold.GT(sdk.OneDec()) {
   111  		return fmt.Errorf("vote threshold too large: %s", v)
   112  	}
   113  	if !v.Veto.IsPositive() {
   114  		return fmt.Errorf("veto threshold must be positive: %s", v.Threshold)
   115  	}
   116  	if v.Veto.GT(sdk.OneDec()) {
   117  		return fmt.Errorf("veto threshold too large: %s", v)
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  // Param around Voting in governance
   124  type VotingParams struct {
   125  	VotingPeriod time.Duration `json:"voting_period,omitempty" yaml:"voting_period,omitempty"` //  Length of the voting period.
   126  }
   127  
   128  // NewVotingParams creates a new VotingParams object
   129  func NewVotingParams(votingPeriod time.Duration) VotingParams {
   130  	return VotingParams{
   131  		VotingPeriod: votingPeriod,
   132  	}
   133  }
   134  
   135  func (vp VotingParams) String() string {
   136  	return fmt.Sprintf(`Voting Params:
   137    Voting Period:      %s`, vp.VotingPeriod)
   138  }
   139  
   140  func validateVotingParams(i interface{}) error {
   141  	v, ok := i.(VotingParams)
   142  	if !ok {
   143  		return fmt.Errorf("invalid parameter type: %T", i)
   144  	}
   145  
   146  	if v.VotingPeriod <= 0 {
   147  		return fmt.Errorf("voting period must be positive: %s", v.VotingPeriod)
   148  	}
   149  
   150  	return nil
   151  }
   152  
   153  // Params returns all of the governance params
   154  type Params struct {
   155  	VotingParams  VotingParams  `json:"voting_params" yaml:"voting_params"`
   156  	TallyParams   TallyParams   `json:"tally_params" yaml:"tally_params"`
   157  	DepositParams DepositParams `json:"deposit_params" yaml:"deposit_parmas"`
   158  }
   159  
   160  func (gp Params) String() string {
   161  	return gp.VotingParams.String() + "\n" +
   162  		gp.TallyParams.String() + "\n" +
   163  		gp.DepositParams.String()
   164  }
   165  
   166  func NewParams(vp VotingParams, tp TallyParams, dp DepositParams) Params {
   167  	return Params{
   168  		VotingParams:  vp,
   169  		DepositParams: dp,
   170  		TallyParams:   tp,
   171  	}
   172  }