github.com/Finschia/finschia-sdk@v0.48.1/x/staking/types/commission.go (about) 1 package types 2 3 import ( 4 "time" 5 6 yaml "gopkg.in/yaml.v2" 7 8 sdk "github.com/Finschia/finschia-sdk/types" 9 ) 10 11 // NewCommissionRates returns an initialized validator commission rates. 12 func NewCommissionRates(rate, maxRate, maxChangeRate sdk.Dec) CommissionRates { 13 return CommissionRates{ 14 Rate: rate, 15 MaxRate: maxRate, 16 MaxChangeRate: maxChangeRate, 17 } 18 } 19 20 // NewCommission returns an initialized validator commission. 21 func NewCommission(rate, maxRate, maxChangeRate sdk.Dec) Commission { 22 return Commission{ 23 CommissionRates: NewCommissionRates(rate, maxRate, maxChangeRate), 24 UpdateTime: time.Unix(0, 0).UTC(), 25 } 26 } 27 28 // NewCommissionWithTime returns an initialized validator commission with a specified 29 // update time which should be the current block BFT time. 30 func NewCommissionWithTime(rate, maxRate, maxChangeRate sdk.Dec, updatedAt time.Time) Commission { 31 return Commission{ 32 CommissionRates: NewCommissionRates(rate, maxRate, maxChangeRate), 33 UpdateTime: updatedAt, 34 } 35 } 36 37 // String implements the Stringer interface for a Commission object. 38 func (c Commission) String() string { 39 out, _ := yaml.Marshal(c) 40 return string(out) 41 } 42 43 // String implements the Stringer interface for a CommissionRates object. 44 func (cr CommissionRates) String() string { 45 out, _ := yaml.Marshal(cr) 46 return string(out) 47 } 48 49 // Validate performs basic sanity validation checks of initial commission 50 // parameters. If validation fails, an SDK error is returned. 51 func (cr CommissionRates) Validate() error { 52 switch { 53 case cr.MaxRate.IsNegative(): 54 // max rate cannot be negative 55 return ErrCommissionNegative 56 57 case cr.MaxRate.GT(sdk.OneDec()): 58 // max rate cannot be greater than 1 59 return ErrCommissionHuge 60 61 case cr.Rate.IsNegative(): 62 // rate cannot be negative 63 return ErrCommissionNegative 64 65 case cr.Rate.GT(cr.MaxRate): 66 // rate cannot be greater than the max rate 67 return ErrCommissionGTMaxRate 68 69 case cr.MaxChangeRate.IsNegative(): 70 // change rate cannot be negative 71 return ErrCommissionChangeRateNegative 72 73 case cr.MaxChangeRate.GT(cr.MaxRate): 74 // change rate cannot be greater than the max rate 75 return ErrCommissionChangeRateGTMaxRate 76 } 77 78 return nil 79 } 80 81 // ValidateNewRate performs basic sanity validation checks of a new commission 82 // rate. If validation fails, an SDK error is returned. 83 func (c Commission) ValidateNewRate(newRate sdk.Dec, blockTime time.Time) error { 84 switch { 85 case blockTime.Sub(c.UpdateTime).Hours() < 24: 86 // new rate cannot be changed more than once within 24 hours 87 return ErrCommissionUpdateTime 88 89 case newRate.IsNegative(): 90 // new rate cannot be negative 91 return ErrCommissionNegative 92 93 case newRate.GT(c.MaxRate): 94 // new rate cannot be greater than the max rate 95 return ErrCommissionGTMaxRate 96 97 case newRate.Sub(c.Rate).GT(c.MaxChangeRate): 98 // new rate % points change cannot be greater than the max change rate 99 return ErrCommissionGTMaxChangeRate 100 } 101 102 return nil 103 }