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

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/fibonacci-chain/fbc/x/params"
     8  )
     9  
    10  const (
    11  	// default paramspace for params keeper
    12  	DefaultParamspace = ModuleName
    13  )
    14  
    15  // Parameter keys
    16  var (
    17  	ParamStoreKeyCommunityTax            = []byte("communitytax")
    18  	ParamStoreKeyWithdrawAddrEnabled     = []byte("withdrawaddrenabled")
    19  	ParamStoreKeyDistributionType        = []byte("distributiontype")
    20  	ParamStoreKeyWithdrawRewardEnabled   = []byte("withdrawrewardenabled")
    21  	ParamStoreKeyRewardTruncatePrecision = []byte("rewardtruncateprecision")
    22  
    23  	IgnoreInitGenesisList = [][]byte{ParamStoreKeyDistributionType, ParamStoreKeyWithdrawRewardEnabled, ParamStoreKeyRewardTruncatePrecision}
    24  )
    25  
    26  // Params defines the set of distribution parameters.
    27  type Params struct {
    28  	CommunityTax            sdk.Dec `json:"community_tax" yaml:"community_tax"`
    29  	WithdrawAddrEnabled     bool    `json:"withdraw_addr_enabled" yaml:"withdraw_addr_enabled"`
    30  	DistributionType        uint32  `json:"distribution_type" yaml:"distribution_type"`
    31  	WithdrawRewardEnabled   bool    `json:"withdraw_reward_enabled" yaml:"withdraw_reward_enabled"`
    32  	RewardTruncatePrecision int64   `json:"reward_truncate_precision" yaml:"reward_truncate_precision"`
    33  }
    34  
    35  // ParamKeyTable returns the parameter key table.
    36  func ParamKeyTable() params.KeyTable {
    37  	return params.NewKeyTable().RegisterParamSet(&Params{})
    38  }
    39  
    40  // DefaultParams returns default distribution parameters
    41  func DefaultParams() Params {
    42  	return Params{
    43  		CommunityTax:            sdk.NewDecWithPrec(2, 2), // 2%
    44  		WithdrawAddrEnabled:     true,
    45  		WithdrawRewardEnabled:   true,
    46  		RewardTruncatePrecision: 0,
    47  	}
    48  }
    49  
    50  // String returns a human readable string representation of Params
    51  func (p Params) String() string {
    52  	return fmt.Sprintf(`Distribution Params:
    53    Community Tax:          %s
    54    Withdraw Addr Enabled:  %t
    55    Distribution Type: %d
    56    Withdraw Reward Enabled: %t
    57    Reward Truncate Precision: %d`,
    58  		p.CommunityTax, p.WithdrawAddrEnabled, p.DistributionType, p.WithdrawRewardEnabled, p.RewardTruncatePrecision)
    59  }
    60  
    61  // ParamSetPairs returns the parameter set pairs.
    62  func (p *Params) ParamSetPairs() params.ParamSetPairs {
    63  	return params.ParamSetPairs{
    64  		params.NewParamSetPair(ParamStoreKeyCommunityTax, &p.CommunityTax, validateCommunityTax),
    65  		params.NewParamSetPair(ParamStoreKeyWithdrawAddrEnabled, &p.WithdrawAddrEnabled, validateWithdrawAddrEnabled),
    66  		//new params for distribution proposal
    67  		params.NewParamSetPair(ParamStoreKeyDistributionType, &p.DistributionType, validateDistributionType),
    68  		params.NewParamSetPair(ParamStoreKeyWithdrawRewardEnabled, &p.WithdrawRewardEnabled, validateWithdrawRewardEnabled),
    69  		params.NewParamSetPair(ParamStoreKeyRewardTruncatePrecision, &p.RewardTruncatePrecision, validateRewardTruncatePrecision),
    70  	}
    71  }
    72  
    73  // ValidateBasic performs basic validation on distribution parameters.
    74  func (p Params) ValidateBasic() error {
    75  	if p.CommunityTax.IsNegative() || p.CommunityTax.GT(sdk.OneDec()) {
    76  		return fmt.Errorf(
    77  			"community tax should non-negative and less than one: %s", p.CommunityTax,
    78  		)
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func validateCommunityTax(i interface{}) error {
    85  	v, ok := i.(sdk.Dec)
    86  	if !ok {
    87  		return fmt.Errorf("invalid parameter type: %T", i)
    88  	}
    89  
    90  	if v.IsNil() {
    91  		return fmt.Errorf("community tax must be not nil")
    92  	}
    93  	if v.IsNegative() {
    94  		return fmt.Errorf("community tax must be positive: %s", v)
    95  	}
    96  	if v.GT(sdk.OneDec()) {
    97  		return fmt.Errorf("community tax too large: %s", v)
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  func validateWithdrawAddrEnabled(i interface{}) error {
   104  	_, ok := i.(bool)
   105  	if !ok {
   106  		return fmt.Errorf("invalid parameter type: %T", i)
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  func validateDistributionType(i interface{}) error {
   113  	distributionType, ok := i.(uint32)
   114  	if !ok {
   115  		return fmt.Errorf("invalid parameter type: %T", i)
   116  	}
   117  
   118  	if distributionType != DistributionTypeOnChain && distributionType != DistributionTypeOffChain {
   119  		return fmt.Errorf("invalid distribution type: %d", distributionType)
   120  	}
   121  
   122  	return nil
   123  }
   124  
   125  func validateWithdrawRewardEnabled(i interface{}) error {
   126  	_, ok := i.(bool)
   127  	if !ok {
   128  		return fmt.Errorf("invalid parameter type: %T", i)
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func validateRewardTruncatePrecision(i interface{}) error {
   135  	precision, ok := i.(int64)
   136  	if !ok {
   137  		return fmt.Errorf("invalid parameter type: %T", i)
   138  	}
   139  	if precision < 0 || precision > sdk.Precision {
   140  		return fmt.Errorf("invalid parameter precision: %d", precision)
   141  	}
   142  
   143  	return nil
   144  }
   145  
   146  // NewParams creates a new instance of Params
   147  func NewParams(communityTax sdk.Dec, withdrawAddrEnabled bool, distributionType uint32, withdrawRewardEnabled bool, rewardTruncatePrecision int64) Params {
   148  	return Params{
   149  		CommunityTax:            communityTax,
   150  		WithdrawAddrEnabled:     withdrawAddrEnabled,
   151  		DistributionType:        distributionType,
   152  		WithdrawRewardEnabled:   withdrawRewardEnabled,
   153  		RewardTruncatePrecision: rewardTruncatePrecision,
   154  	}
   155  }
   156  
   157  // MarshalYAML implements the text format for yaml marshaling
   158  func (p Params) MarshalYAML() (interface{}, error) {
   159  	return p.String(), nil
   160  }