Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: split commands for withdraw rewards and commission due to eip712 #175

Merged
merged 4 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewTxCmd() *cobra.Command {

distTxCmd.AddCommand(
NewWithdrawRewardsCmd(),
NewWithdrawCommission(),
NewWithdrawAllRewardsCmd(),
NewSetWithdrawAddrCmd(),
NewFundCommunityPoolCmd(),
Expand Down Expand Up @@ -76,20 +77,16 @@ func newSplitAndApply(

// NewWithdrawRewardsCmd returns a CLI command handler for creating a MsgWithdrawDelegatorReward transaction.
func NewWithdrawRewardsCmd() *cobra.Command {
bech32PrefixValAddr := sdk.GetConfig().GetBech32ValidatorAddrPrefix()

cmd := &cobra.Command{
Use: "withdraw-rewards [validator-addr]",
Short: "Withdraw rewards from a given delegation address, and optionally withdraw validator commission if the delegation address given is a validator operator",
Short: "Withdraw rewards from a given delegation address",
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.
fmt.Sprintf(`Withdraw rewards from a given delegation address.

Example:
$ %s tx distribution withdraw-rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj --from mykey
$ %s tx distribution withdraw-rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj --from mykey --commission
$ %s tx distribution withdraw-rewards 0x91D7d.. --from mykey
`,
version.AppName, bech32PrefixValAddr, version.AppName, bech32PrefixValAddr,
version.AppName,
),
),
Args: cobra.ExactArgs(1),
Expand All @@ -106,15 +103,46 @@ $ %s tx distribution withdraw-rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj

msgs := []sdk.Msg{types.NewMsgWithdrawDelegatorReward(delAddr, valAddr)}

if commission, _ := cmd.Flags().GetBool(FlagCommission); commission {
msgs = append(msgs, types.NewMsgWithdrawValidatorCommission(valAddr))
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgs...)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

// NewWithdrawCommission returns a CLI command handler for creating a MsgWithdrawValidatorCommission transaction.
func NewWithdrawCommission() *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-commission [validator-addr]",
Short: "Withdraw validator commission",
Long: strings.TrimSpace(
fmt.Sprintf(`Withdraw validator commission if the delegation address given is a validator operator.

Example:
$ %s tx distribution withdraw-commission 0x91D7d.. --from mykey
`,
version.AppName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
valAddr, err := sdk.AccAddressFromHexUnsafe(args[0])
if err != nil {
return err
}

msgs := []sdk.Msg{types.NewMsgWithdrawValidatorCommission(valAddr)}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgs...)
},
}

cmd.Flags().Bool(FlagCommission, false, "Withdraw the validator's commission in addition to the rewards")
flags.AddTxFlagsToCmd(cmd)

return cmd
Expand Down
22 changes: 22 additions & 0 deletions x/distribution/client/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,25 @@ func MsgWithdrawDelegatorRewardExec(clientCtx client.Context, valAddr fmt.String

return buf.Bytes(), nil
}

func MsgWithdrawCommissionExec(clientCtx client.Context, valAddr fmt.Stringer, extraArgs ...string) ([]byte, error) {
buf := new(bytes.Buffer)
clientCtx = clientCtx.WithOutput(buf)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

args := []string{valAddr.String()}
args = append(args, extraArgs...)

cmd := distrcli.NewWithdrawCommission()
cmd.SetErr(buf)
cmd.SetOut(buf)
cmd.SetArgs(args)

if err := cmd.ExecuteContext(ctx); err != nil {
return nil, err
}

return buf.Bytes(), nil
}
2 changes: 1 addition & 1 deletion x/distribution/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func SimulateMsgWithdrawValidatorCommission(txConfig client.TxConfig, ak types.A
account := ak.GetAccount(ctx, simAccount.Address)
spendable := bk.SpendableCoins(ctx, account.GetAddress())

msg := types.NewMsgWithdrawValidatorCommission(validator.GetOperator())
msg := types.NewMsgWithdrawValidatorCommission(sdk.AccAddress(validator.GetOperator()))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, in staking module validator's operator is using ValAddress type. Need to change in the following prs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I have found some places in other modules too


txCtx := simulation.OperationInput{
R: r,
Expand Down
6 changes: 3 additions & 3 deletions x/distribution/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ func (msg MsgWithdrawDelegatorReward) ValidateBasic() error {
return nil
}

func NewMsgWithdrawValidatorCommission(valAddr sdk.ValAddress) *MsgWithdrawValidatorCommission {
func NewMsgWithdrawValidatorCommission(valAddr sdk.AccAddress) *MsgWithdrawValidatorCommission {
return &MsgWithdrawValidatorCommission{
ValidatorAddress: valAddr.String(),
}
}

// Return address that must sign over msg.GetSignBytes()
func (msg MsgWithdrawValidatorCommission) GetSigners() []sdk.AccAddress {
valAddr, _ := sdk.ValAddressFromHex(msg.ValidatorAddress)
return []sdk.AccAddress{sdk.AccAddress(valAddr)}
valAddr, _ := sdk.AccAddressFromHexUnsafe(msg.ValidatorAddress)
return []sdk.AccAddress{valAddr}
}

// get the bytes for the message signer to sign on
Expand Down