github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/feesplit/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 "gopkg.in/yaml.v2" 10 ) 11 12 // Parameter store key 13 var ( 14 DefaultEnableFeeSplit = false 15 DefaultDeveloperShares = sdk.NewDecWithPrec(50, 2) // 50% 16 // Cost for executing `crypto.CreateAddress` must be at least 36 gas for the 17 // contained keccak256(word) operation 18 DefaultAddrDerivationCostCreate = uint64(50) 19 20 ParamStoreKeyEnableFeeSplit = []byte("EnableFeeSplit") 21 ParamStoreKeyDeveloperShares = []byte("DeveloperShares") 22 ParamStoreKeyAddrDerivationCostCreate = []byte("AddrDerivationCostCreate") 23 ) 24 25 // ParamKeyTable returns the parameter key table. 26 func ParamKeyTable() params.KeyTable { 27 return params.NewKeyTable().RegisterParamSet(&Params{}) 28 } 29 30 // Params defines the feesplit module params 31 type Params struct { 32 // enable_fee_split defines a parameter to enable the feesplit module 33 EnableFeeSplit bool `json:"enable_fee_split",yaml:"enable_fee_split"` 34 // developer_shares defines the proportion of the transaction fees to be 35 // distributed to the registered contract owner 36 DeveloperShares sdk.Dec `json:"developer_shares",yaml:"developer_shares"` 37 // addr_derivation_cost_create defines the cost of address derivation for 38 // verifying the contract deployer at fee registration 39 AddrDerivationCostCreate uint64 `json:"addr_derivation_cost_create",yaml:"addr_derivation_cost_create"` 40 } 41 42 // NewParams creates a new Params object 43 func NewParams( 44 enableFeeSplit bool, 45 developerShares sdk.Dec, 46 addrDerivationCostCreate uint64, 47 ) Params { 48 return Params{ 49 EnableFeeSplit: enableFeeSplit, 50 DeveloperShares: developerShares, 51 AddrDerivationCostCreate: addrDerivationCostCreate, 52 } 53 } 54 55 func DefaultParams() Params { 56 return Params{ 57 EnableFeeSplit: DefaultEnableFeeSplit, 58 DeveloperShares: DefaultDeveloperShares, 59 AddrDerivationCostCreate: DefaultAddrDerivationCostCreate, 60 } 61 } 62 63 // String implements the fmt.Stringer interface 64 func (p Params) String() string { 65 out, _ := yaml.Marshal(p) 66 return string(out) 67 } 68 69 // ParamSetPairs returns the parameter set pairs. 70 func (p *Params) ParamSetPairs() params.ParamSetPairs { 71 return params.ParamSetPairs{ 72 params.NewParamSetPair(ParamStoreKeyEnableFeeSplit, &p.EnableFeeSplit, validateBool), 73 params.NewParamSetPair(ParamStoreKeyDeveloperShares, &p.DeveloperShares, validateShares), 74 params.NewParamSetPair(ParamStoreKeyAddrDerivationCostCreate, &p.AddrDerivationCostCreate, validateUint64), 75 } 76 } 77 78 func (p Params) Validate() error { 79 if err := validateBool(p.EnableFeeSplit); err != nil { 80 return err 81 } 82 if err := validateShares(p.DeveloperShares); err != nil { 83 return err 84 } 85 return validateUint64(p.AddrDerivationCostCreate) 86 } 87 88 func validateUint64(i interface{}) error { 89 _, ok := i.(uint64) 90 if !ok { 91 return fmt.Errorf("invalid parameter type: %T", i) 92 } 93 94 return nil 95 } 96 97 func validateBool(i interface{}) error { 98 _, ok := i.(bool) 99 if !ok { 100 return fmt.Errorf("invalid parameter type: %T", i) 101 } 102 103 return nil 104 } 105 106 func validateShares(i interface{}) error { 107 v, ok := i.(sdk.Dec) 108 109 if !ok { 110 return fmt.Errorf("invalid parameter type: %T", i) 111 } 112 113 if v.IsNil() { 114 return fmt.Errorf("invalid parameter: nil") 115 } 116 117 if v.IsNegative() { 118 return fmt.Errorf("value cannot be negative: %T", i) 119 } 120 121 if v.GT(sdk.OneDec()) { 122 return fmt.Errorf("value cannot be greater than 1: %T", i) 123 } 124 125 return nil 126 }