cosmos-sdk/x/distribution/types/msg.go

155 lines
5.4 KiB
Go

package types
import (
"github.com/cosmos/cosmos-sdk/codec/legacy"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// distribution message types
const (
TypeMsgSetWithdrawAddress = "set_withdraw_address"
TypeMsgWithdrawDelegatorReward = "withdraw_delegator_reward"
TypeMsgWithdrawValidatorCommission = "withdraw_validator_commission"
TypeMsgFundCommunityPool = "fund_community_pool"
)
// Verify interface at compile time
var _, _, _ sdk.Msg = &MsgSetWithdrawAddress{}, &MsgWithdrawDelegatorReward{}, &MsgWithdrawValidatorCommission{}
func NewMsgSetWithdrawAddress(delAddr, withdrawAddr sdk.AccAddress) *MsgSetWithdrawAddress {
return &MsgSetWithdrawAddress{
DelegatorAddress: delAddr.String(),
WithdrawAddress: withdrawAddr.String(),
}
}
func (msg MsgSetWithdrawAddress) Route() string { return ModuleName }
func (msg MsgSetWithdrawAddress) Type() string { return TypeMsgSetWithdrawAddress }
// Return address that must sign over msg.GetSignBytes()
func (msg MsgSetWithdrawAddress) GetSigners() []sdk.AccAddress {
delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress)
return []sdk.AccAddress{delegator}
}
// get the bytes for the message signer to sign on
func (msg MsgSetWithdrawAddress) GetSignBytes() []byte {
bz := legacy.Cdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// quick validity check
func (msg MsgSetWithdrawAddress) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.WithdrawAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid withdraw address: %s", err)
}
return nil
}
func NewMsgWithdrawDelegatorReward(delAddr sdk.AccAddress, valAddr sdk.ValAddress) *MsgWithdrawDelegatorReward {
return &MsgWithdrawDelegatorReward{
DelegatorAddress: delAddr.String(),
ValidatorAddress: valAddr.String(),
}
}
func (msg MsgWithdrawDelegatorReward) Route() string { return ModuleName }
func (msg MsgWithdrawDelegatorReward) Type() string { return TypeMsgWithdrawDelegatorReward }
// Return address that must sign over msg.GetSignBytes()
func (msg MsgWithdrawDelegatorReward) GetSigners() []sdk.AccAddress {
delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress)
return []sdk.AccAddress{delegator}
}
// get the bytes for the message signer to sign on
func (msg MsgWithdrawDelegatorReward) GetSignBytes() []byte {
bz := legacy.Cdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// quick validity check
func (msg MsgWithdrawDelegatorReward) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err)
}
if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err)
}
return nil
}
func NewMsgWithdrawValidatorCommission(valAddr sdk.ValAddress) *MsgWithdrawValidatorCommission {
return &MsgWithdrawValidatorCommission{
ValidatorAddress: valAddr.String(),
}
}
func (msg MsgWithdrawValidatorCommission) Route() string { return ModuleName }
func (msg MsgWithdrawValidatorCommission) Type() string { return TypeMsgWithdrawValidatorCommission }
// Return address that must sign over msg.GetSignBytes()
func (msg MsgWithdrawValidatorCommission) GetSigners() []sdk.AccAddress {
valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress)
return []sdk.AccAddress{sdk.AccAddress(valAddr)}
}
// get the bytes for the message signer to sign on
func (msg MsgWithdrawValidatorCommission) GetSignBytes() []byte {
bz := legacy.Cdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// quick validity check
func (msg MsgWithdrawValidatorCommission) ValidateBasic() error {
if _, err := sdk.ValAddressFromBech32(msg.ValidatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err)
}
return nil
}
// NewMsgFundCommunityPool returns a new MsgFundCommunityPool with a sender and
// a funding amount.
func NewMsgFundCommunityPool(amount sdk.Coins, depositor sdk.AccAddress) *MsgFundCommunityPool {
return &MsgFundCommunityPool{
Amount: amount,
Depositor: depositor.String(),
}
}
// Route returns the MsgFundCommunityPool message route.
func (msg MsgFundCommunityPool) Route() string { return ModuleName }
// Type returns the MsgFundCommunityPool message type.
func (msg MsgFundCommunityPool) Type() string { return TypeMsgFundCommunityPool }
// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes.
func (msg MsgFundCommunityPool) GetSigners() []sdk.AccAddress {
depositor, _ := sdk.AccAddressFromBech32(msg.Depositor)
return []sdk.AccAddress{depositor}
}
// GetSignBytes returns the raw bytes for a MsgFundCommunityPool message that
// the expected signer needs to sign.
func (msg MsgFundCommunityPool) GetSignBytes() []byte {
bz := legacy.Cdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// ValidateBasic performs basic MsgFundCommunityPool message validation.
func (msg MsgFundCommunityPool) ValidateBasic() error {
if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
if _, err := sdk.AccAddressFromBech32(msg.Depositor); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid depositor address: %s", err)
}
return nil
}