github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/client/cli/utils.go (about) 1 package cli 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "os" 8 9 errorsmod "cosmossdk.io/errors" 10 "cosmossdk.io/math" 11 12 "github.com/cosmos/cosmos-sdk/codec" 13 cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" 14 sdk "github.com/cosmos/cosmos-sdk/types" 15 sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" 16 "github.com/cosmos/cosmos-sdk/x/staking/types" 17 ) 18 19 // validator struct to define the fields of the validator 20 type validator struct { 21 Amount sdk.Coin 22 PubKey cryptotypes.PubKey 23 Moniker string 24 Identity string 25 Website string 26 Security string 27 Details string 28 CommissionRates types.CommissionRates 29 MinSelfDelegation math.Int 30 } 31 32 func parseAndValidateValidatorJSON(cdc codec.Codec, path string) (validator, error) { 33 type internalVal struct { 34 Amount string `json:"amount"` 35 PubKey json.RawMessage `json:"pubkey"` 36 Moniker string `json:"moniker"` 37 Identity string `json:"identity,omitempty"` 38 Website string `json:"website,omitempty"` 39 Security string `json:"security,omitempty"` 40 Details string `json:"details,omitempty"` 41 CommissionRate string `json:"commission-rate"` 42 CommissionMaxRate string `json:"commission-max-rate"` 43 CommissionMaxChange string `json:"commission-max-change-rate"` 44 MinSelfDelegation string `json:"min-self-delegation"` 45 } 46 47 contents, err := os.ReadFile(path) 48 if err != nil { 49 return validator{}, err 50 } 51 52 var v internalVal 53 err = json.Unmarshal(contents, &v) 54 if err != nil { 55 return validator{}, err 56 } 57 58 if v.Amount == "" { 59 return validator{}, fmt.Errorf("must specify amount of coins to bond") 60 } 61 amount, err := sdk.ParseCoinNormalized(v.Amount) 62 if err != nil { 63 return validator{}, err 64 } 65 66 if v.PubKey == nil { 67 return validator{}, fmt.Errorf("must specify the JSON encoded pubkey") 68 } 69 var pk cryptotypes.PubKey 70 if err := cdc.UnmarshalInterfaceJSON(v.PubKey, &pk); err != nil { 71 return validator{}, err 72 } 73 74 if v.Moniker == "" { 75 return validator{}, fmt.Errorf("must specify the moniker name") 76 } 77 78 commissionRates, err := buildCommissionRates(v.CommissionRate, v.CommissionMaxRate, v.CommissionMaxChange) 79 if err != nil { 80 return validator{}, err 81 } 82 83 if v.MinSelfDelegation == "" { 84 return validator{}, fmt.Errorf("must specify minimum self delegation") 85 } 86 minSelfDelegation, ok := math.NewIntFromString(v.MinSelfDelegation) 87 if !ok { 88 return validator{}, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "minimum self delegation must be a positive integer") 89 } 90 91 return validator{ 92 Amount: amount, 93 PubKey: pk, 94 Moniker: v.Moniker, 95 Identity: v.Identity, 96 Website: v.Website, 97 Security: v.Security, 98 Details: v.Details, 99 CommissionRates: commissionRates, 100 MinSelfDelegation: minSelfDelegation, 101 }, nil 102 } 103 104 func buildCommissionRates(rateStr, maxRateStr, maxChangeRateStr string) (commission types.CommissionRates, err error) { 105 if rateStr == "" || maxRateStr == "" || maxChangeRateStr == "" { 106 return commission, errors.New("must specify all validator commission parameters") 107 } 108 109 rate, err := math.LegacyNewDecFromStr(rateStr) 110 if err != nil { 111 return commission, err 112 } 113 114 maxRate, err := math.LegacyNewDecFromStr(maxRateStr) 115 if err != nil { 116 return commission, err 117 } 118 119 maxChangeRate, err := math.LegacyNewDecFromStr(maxChangeRateStr) 120 if err != nil { 121 return commission, err 122 } 123 124 commission = types.NewCommissionRates(rate, maxRate, maxChangeRate) 125 126 return commission, nil 127 }