Skip to content

Commit

Permalink
Implement cli withdraw-all-rewards command as multi-message tx
Browse files Browse the repository at this point in the history
ref #3476
  • Loading branch information
Alessio Treglia committed Feb 2, 2019
1 parent 1763d99 commit 8d58622
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"

"github.com/cosmos/cosmos-sdk/x/distribution/client/common"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)

Expand Down Expand Up @@ -89,6 +90,38 @@ func GetCmdWithdrawRewards(cdc *codec.Codec) *cobra.Command {
return cmd
}

// command to withdraw all rewards
func GetCmdWithdrawAllRewards(cdc *codec.Codec, queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-all-rewards [delegator-addr]",
Short: "withdraw all delegations rewards for a delegator",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {

txBldr := authtxb.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc)

delAddr := cliCtx.GetFromAddress()
msgs, err := common.WithdrawAllDelegatorRewards(cliCtx, cdc, queryRoute, delAddr)
if err != nil {
return err
}

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(os.Stdout, txBldr, cliCtx, msgs, false)
}

// build and sign the transaction, then broadcast to Tendermint
return utils.CompleteAndBroadcastTxCLI(txBldr, cliCtx, msgs)
},
}
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.
func GetCmdSetWithdrawAddr(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Expand Down
1 change: 1 addition & 0 deletions x/distribution/client/module_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (mc ModuleClient) GetTxCmd() *cobra.Command {
distTxCmd.AddCommand(client.PostCommands(
distCmds.GetCmdWithdrawRewards(mc.cdc),
distCmds.GetCmdSetWithdrawAddr(mc.cdc),
distCmds.GetCmdWithdrawAllRewards(mc.cdc, mc.storeKey),
)...)

return distTxCmd
Expand Down

0 comments on commit 8d58622

Please sign in to comment.