github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/types/msg.go (about) 1 package types 2 3 import ( 4 "cosmossdk.io/core/address" 5 errorsmod "cosmossdk.io/errors" 6 "cosmossdk.io/math" 7 8 codectypes "github.com/cosmos/cosmos-sdk/codec/types" 9 cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" 10 sdk "github.com/cosmos/cosmos-sdk/types" 11 sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" 12 ) 13 14 var ( 15 _ sdk.Msg = &MsgCreateValidator{} 16 _ codectypes.UnpackInterfacesMessage = (*MsgCreateValidator)(nil) 17 _ sdk.Msg = &MsgEditValidator{} 18 _ sdk.Msg = &MsgDelegate{} 19 _ sdk.Msg = &MsgUndelegate{} 20 _ sdk.Msg = &MsgBeginRedelegate{} 21 _ sdk.Msg = &MsgCancelUnbondingDelegation{} 22 _ sdk.Msg = &MsgUpdateParams{} 23 ) 24 25 // NewMsgCreateValidator creates a new MsgCreateValidator instance. 26 // Delegator address and validator address are the same. 27 func NewMsgCreateValidator( 28 valAddr string, pubKey cryptotypes.PubKey, 29 selfDelegation sdk.Coin, description Description, commission CommissionRates, minSelfDelegation math.Int, 30 ) (*MsgCreateValidator, error) { 31 var pkAny *codectypes.Any 32 if pubKey != nil { 33 var err error 34 if pkAny, err = codectypes.NewAnyWithValue(pubKey); err != nil { 35 return nil, err 36 } 37 } 38 return &MsgCreateValidator{ 39 Description: description, 40 ValidatorAddress: valAddr, 41 Pubkey: pkAny, 42 Value: selfDelegation, 43 Commission: commission, 44 MinSelfDelegation: minSelfDelegation, 45 }, nil 46 } 47 48 // Validate validates the MsgCreateValidator sdk msg. 49 func (msg MsgCreateValidator) Validate(ac address.Codec) error { 50 // note that unmarshaling from bech32 ensures both non-empty and valid 51 _, err := ac.StringToBytes(msg.ValidatorAddress) 52 if err != nil { 53 return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) 54 } 55 56 if msg.Pubkey == nil { 57 return ErrEmptyValidatorPubKey 58 } 59 60 if !msg.Value.IsValid() || !msg.Value.Amount.IsPositive() { 61 return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid delegation amount") 62 } 63 64 if msg.Description == (Description{}) { 65 return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty description") 66 } 67 68 if msg.Commission == (CommissionRates{}) { 69 return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty commission") 70 } 71 72 if err := msg.Commission.Validate(); err != nil { 73 return err 74 } 75 76 if !msg.MinSelfDelegation.IsPositive() { 77 return errorsmod.Wrap( 78 sdkerrors.ErrInvalidRequest, 79 "minimum self delegation must be a positive integer", 80 ) 81 } 82 83 if msg.Value.Amount.LT(msg.MinSelfDelegation) { 84 return ErrSelfDelegationBelowMinimum 85 } 86 87 return nil 88 } 89 90 // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces 91 func (msg MsgCreateValidator) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { 92 var pubKey cryptotypes.PubKey 93 return unpacker.UnpackAny(msg.Pubkey, &pubKey) 94 } 95 96 // NewMsgEditValidator creates a new MsgEditValidator instance 97 func NewMsgEditValidator(valAddr string, description Description, newRate *math.LegacyDec, newMinSelfDelegation *math.Int) *MsgEditValidator { 98 return &MsgEditValidator{ 99 Description: description, 100 CommissionRate: newRate, 101 ValidatorAddress: valAddr, 102 MinSelfDelegation: newMinSelfDelegation, 103 } 104 } 105 106 // NewMsgDelegate creates a new MsgDelegate instance. 107 func NewMsgDelegate(delAddr, valAddr string, amount sdk.Coin) *MsgDelegate { 108 return &MsgDelegate{ 109 DelegatorAddress: delAddr, 110 ValidatorAddress: valAddr, 111 Amount: amount, 112 } 113 } 114 115 // NewMsgBeginRedelegate creates a new MsgBeginRedelegate instance. 116 func NewMsgBeginRedelegate( 117 delAddr, valSrcAddr, valDstAddr string, amount sdk.Coin, 118 ) *MsgBeginRedelegate { 119 return &MsgBeginRedelegate{ 120 DelegatorAddress: delAddr, 121 ValidatorSrcAddress: valSrcAddr, 122 ValidatorDstAddress: valDstAddr, 123 Amount: amount, 124 } 125 } 126 127 // NewMsgUndelegate creates a new MsgUndelegate instance. 128 func NewMsgUndelegate(delAddr, valAddr string, amount sdk.Coin) *MsgUndelegate { 129 return &MsgUndelegate{ 130 DelegatorAddress: delAddr, 131 ValidatorAddress: valAddr, 132 Amount: amount, 133 } 134 } 135 136 // NewMsgCancelUnbondingDelegation creates a new MsgCancelUnbondingDelegation instance. 137 func NewMsgCancelUnbondingDelegation(delAddr, valAddr string, creationHeight int64, amount sdk.Coin) *MsgCancelUnbondingDelegation { 138 return &MsgCancelUnbondingDelegation{ 139 DelegatorAddress: delAddr, 140 ValidatorAddress: valAddr, 141 Amount: amount, 142 CreationHeight: creationHeight, 143 } 144 }