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