cosmos-sdk/x/distribution/client/cli/tx.go

131 lines
3.5 KiB
Go
Raw Normal View History

2018-09-04 23:41:17 -07:00
// nolint
package cli
import (
2018-09-18 21:00:48 -07:00
"fmt"
2018-09-04 23:41:17 -07:00
2018-09-19 16:00:21 -07:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
amino "github.com/tendermint/go-amino"
2018-09-19 16:00:21 -07:00
"github.com/cosmos/cosmos-sdk/client"
2018-09-04 23:41:17 -07:00
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
2018-09-19 16:00:21 -07:00
"github.com/cosmos/cosmos-sdk/codec"
2018-09-04 23:41:17 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-09-19 21:53:48 -07:00
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
2018-09-04 23:41:17 -07:00
)
var (
flagOnlyFromValidator = "only-from-validator"
flagIsValidator = "is-validator"
)
// GetTxCmd returns the transaction commands for this module
func GetTxCmd(storeKey string, cdc *amino.Codec) *cobra.Command {
distTxCmd := &cobra.Command{
Use: "dist",
Short: "Distribution transactions subcommands",
}
distTxCmd.AddCommand(client.PostCommands(
GetCmdWithdrawRewards(cdc),
GetCmdSetWithdrawAddr(cdc),
)...)
return distTxCmd
}
2018-09-18 21:00:48 -07:00
// command to withdraw rewards
2018-09-19 21:53:48 -07:00
func GetCmdWithdrawRewards(cdc *codec.Codec) *cobra.Command {
2018-09-04 23:41:17 -07:00
cmd := &cobra.Command{
2018-09-18 21:00:48 -07:00
Use: "withdraw-rewards",
Short: "withdraw rewards for either: all-delegations, a delegation, or a validator",
Args: cobra.NoArgs,
2018-09-04 23:41:17 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
2018-09-18 21:00:48 -07:00
onlyFromVal := viper.GetString(flagOnlyFromValidator)
isVal := viper.GetBool(flagIsValidator)
if onlyFromVal != "" && isVal {
return fmt.Errorf("cannot use --%v, and --%v flags together",
flagOnlyFromValidator, flagIsValidator)
}
2018-09-19 21:53:48 -07:00
txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc)
2018-09-04 23:41:17 -07:00
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc)
2018-09-04 23:41:17 -07:00
2018-09-18 21:00:48 -07:00
var msg sdk.Msg
switch {
case isVal:
addr, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
2018-09-19 21:53:48 -07:00
valAddr := sdk.ValAddress(addr.Bytes())
msg = types.NewMsgWithdrawValidatorRewardsAll(valAddr)
2018-09-18 21:00:48 -07:00
case onlyFromVal != "":
delAddr, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
valAddr, err := sdk.ValAddressFromBech32(onlyFromVal)
if err != nil {
return err
}
2018-09-19 21:53:48 -07:00
msg = types.NewMsgWithdrawDelegatorReward(delAddr, valAddr)
2018-09-18 21:00:48 -07:00
default:
delAddr, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
2018-09-19 21:53:48 -07:00
msg = types.NewMsgWithdrawDelegatorRewardsAll(delAddr)
2018-09-04 23:41:17 -07:00
}
2018-09-18 21:00:48 -07:00
// build and sign the transaction, then broadcast to Tendermint
2018-09-28 00:29:52 -07:00
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
2018-09-18 21:00:48 -07:00
},
}
cmd.Flags().String(flagOnlyFromValidator, "", "only withdraw from this validator address (in bech)")
cmd.Flags().Bool(flagIsValidator, false, "also withdraw validator's commission")
return cmd
}
// GetCmdDelegate implements the delegate command.
2018-09-19 16:00:21 -07:00
func GetCmdSetWithdrawAddr(cdc *codec.Codec) *cobra.Command {
2018-09-18 21:00:48 -07:00
cmd := &cobra.Command{
Use: "set-withdraw-addr [withdraw-addr]",
Short: "change the default withdraw address for rewards associated with an address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
2018-09-19 21:53:48 -07:00
txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc)
2018-09-18 21:00:48 -07:00
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc)
2018-09-18 21:00:48 -07:00
2018-09-04 23:41:17 -07:00
delAddr, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
2018-09-18 21:00:48 -07:00
withdrawAddr, err := sdk.AccAddressFromBech32(args[0])
2018-09-04 23:41:17 -07:00
if err != nil {
return err
}
2018-09-19 21:53:48 -07:00
msg := types.NewMsgSetWithdrawAddress(delAddr, withdrawAddr)
2018-09-04 23:41:17 -07:00
// build and sign the transaction, then broadcast to Tendermint
2018-09-28 00:29:52 -07:00
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
2018-09-04 23:41:17 -07:00
},
}
return cmd
}