2018-09-04 23:41:17 -07:00
// nolint
package cli
import (
2019-05-18 16:06:08 -07:00
"fmt"
2019-02-06 16:15:37 -08:00
"strings"
2018-09-04 23:41:17 -07:00
2018-09-19 16:00:21 -07:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2018-11-19 09:02:34 -08:00
amino "github.com/tendermint/go-amino"
2018-09-19 16:00:21 -07:00
2018-11-19 09:02:34 -08: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"
2019-05-18 16:06:08 -07:00
"github.com/cosmos/cosmos-sdk/version"
2018-09-19 21:53:48 -07:00
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
2019-02-04 16:45:18 -08:00
"github.com/cosmos/cosmos-sdk/x/distribution/client/common"
2018-09-19 21:53:48 -07:00
"github.com/cosmos/cosmos-sdk/x/distribution/types"
2018-09-04 23:41:17 -07:00
)
var (
flagOnlyFromValidator = "only-from-validator"
flagIsValidator = "is-validator"
2019-02-08 13:45:41 -08:00
flagComission = "commission"
2018-09-04 23:41:17 -07:00
)
2018-11-19 09:02:34 -08:00
// 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 {
2019-02-06 16:15:37 -08:00
Use : "withdraw-rewards [validator-addr]" ,
2019-05-18 16:06:08 -07:00
Short : "Withdraw rewards from a given delegation address, and optionally withdraw validator commission if the delegation address given is a validator operator" ,
Long : strings . TrimSpace (
fmt . Sprintf ( ` Withdraw rewards from a given delegation address ,
and optionally withdraw validator commission if the delegation address given is a validator operator .
Example :
$ % s tx distr withdraw - rewards cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj -- from mykey
$ % s tx distr withdraw - rewards cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj -- from mykey -- commission
` ,
version . ClientName , version . ClientName ,
) ,
) ,
2019-02-06 16:15:37 -08:00
Args : cobra . ExactArgs ( 1 ) ,
2018-09-04 23:41:17 -07:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2018-12-12 13:29:42 -08:00
txBldr := authtxb . NewTxBuilderFromCLI ( ) . WithTxEncoder ( utils . GetTxEncoder ( cdc ) )
2018-09-04 23:41:17 -07:00
cliCtx := context . NewCLIContext ( ) .
WithCodec ( cdc ) .
2018-11-19 09:02:34 -08:00
WithAccountDecoder ( cdc )
2018-09-04 23:41:17 -07:00
2019-02-06 16:15:37 -08:00
delAddr := cliCtx . GetFromAddress ( )
valAddr , err := sdk . ValAddressFromBech32 ( args [ 0 ] )
if err != nil {
return err
2018-09-04 23:41:17 -07:00
}
2019-02-06 16:15:37 -08:00
msgs := [ ] sdk . Msg { types . NewMsgWithdrawDelegatorReward ( delAddr , valAddr ) }
if viper . GetBool ( flagComission ) {
msgs = append ( msgs , types . NewMsgWithdrawValidatorCommission ( valAddr ) )
2018-12-10 07:29:10 -08:00
}
2019-05-08 13:06:05 -07:00
return utils . GenerateOrBroadcastMsgs ( cliCtx , txBldr , msgs )
2018-09-18 21:00:48 -07:00
} ,
}
2019-02-06 16:15:37 -08:00
cmd . Flags ( ) . Bool ( flagComission , false , "also withdraw validator's commission" )
2018-09-18 21:00:48 -07:00
return cmd
}
2019-02-04 16:45:18 -08:00
// command to withdraw all rewards
func GetCmdWithdrawAllRewards ( cdc * codec . Codec , queryRoute string ) * cobra . Command {
2019-02-06 16:15:37 -08:00
return & cobra . Command {
Use : "withdraw-all-rewards" ,
2019-02-04 16:45:18 -08:00
Short : "withdraw all delegations rewards for a delegator" ,
2019-05-18 16:06:08 -07:00
Long : strings . TrimSpace (
fmt . Sprintf ( ` Withdraw all rewards for a single delegator .
Example :
$ % s tx distr withdraw - all - rewards -- from mykey
` ,
version . ClientName ,
) ,
) ,
2019-02-06 16:15:37 -08:00
Args : cobra . NoArgs ,
2019-02-04 16:45:18 -08:00
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
}
2019-05-08 13:06:05 -07:00
return utils . GenerateOrBroadcastMsgs ( cliCtx , txBldr , msgs )
2019-02-04 16:45:18 -08:00
} ,
}
}
// command to replace a delegator's withdrawal address
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" ,
2019-05-18 16:06:08 -07:00
Long : strings . TrimSpace (
fmt . Sprintf ( ` Set the withdraw address for rewards associated with a delegator address .
Example :
$ % s tx set - withdraw - addr cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p -- from mykey
` ,
version . ClientName ,
) ,
) ,
2019-02-06 16:15:37 -08:00
Args : cobra . ExactArgs ( 1 ) ,
2018-09-18 21:00:48 -07:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2018-12-12 13:29:42 -08:00
txBldr := authtxb . NewTxBuilderFromCLI ( ) . WithTxEncoder ( utils . GetTxEncoder ( cdc ) )
2018-09-18 21:00:48 -07:00
cliCtx := context . NewCLIContext ( ) .
WithCodec ( cdc ) .
2018-11-19 09:02:34 -08:00
WithAccountDecoder ( cdc )
2018-09-18 21:00:48 -07:00
2019-01-29 11:22:47 -08:00
delAddr := cliCtx . GetFromAddress ( )
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 )
2019-05-08 13:06:05 -07:00
return utils . GenerateOrBroadcastMsgs ( cliCtx , txBldr , [ ] sdk . Msg { msg } )
2018-09-04 23:41:17 -07:00
} ,
}
return cmd
}