Incentive module: accounts should only be able to claim their own rewards (#826)

* make rewards only claimable by sender

* remove 'owner' from cli for incentive claims
This commit is contained in:
Denali Marsh 2021-02-15 16:29:00 +01:00 committed by GitHub
parent 15da55b451
commit 94999b6b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 36 deletions

View File

@ -11,7 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils" "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
@ -32,38 +31,30 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
)...) )...)
return incentiveTxCmd return incentiveTxCmd
} }
func getCmdClaimCdp(cdc *codec.Codec) *cobra.Command { func getCmdClaimCdp(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "claim-cdp [owner] [multiplier]", Use: "claim-cdp [multiplier]",
Short: "claim CDP rewards for cdp owner and collateral-type", Short: "claim CDP rewards using a given multiplier",
Long: strings.TrimSpace( Long: strings.TrimSpace(
fmt.Sprintf(`Claim any outstanding CDP rewards owned by owner for the input collateral-type and multiplier, fmt.Sprintf(`Claim sender's outstanding CDP rewards using a given multiplier
Example: Example:
$ %s tx %s claim-cdp kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw large $ %s tx %s claim-cdp large
`, version.ClientName, types.ModuleName), `, version.ClientName, types.ModuleName),
), ),
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin()) inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
owner, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
sender := cliCtx.GetFromAddress() sender := cliCtx.GetFromAddress()
if !sender.Equals(owner) { multiplier := args[0]
return sdkerrors.Wrapf(types.ErrInvalidClaimOwner, "tx sender %s does not match claim owner %s", sender, owner)
}
msg := types.NewMsgClaimUSDXMintingReward(owner, args[1]) msg := types.NewMsgClaimUSDXMintingReward(sender, multiplier)
err = msg.ValidateBasic() err := msg.ValidateBasic()
if err != nil { if err != nil {
return err return err
} }
@ -74,33 +65,26 @@ func getCmdClaimCdp(cdc *codec.Codec) *cobra.Command {
func getCmdClaimHard(cdc *codec.Codec) *cobra.Command { func getCmdClaimHard(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{ return &cobra.Command{
Use: "claim-hard [owner] [multiplier]", Use: "claim-hard [multiplier]",
Short: "claim Hard rewards for deposit/borrow and delegating", Short: "claim sender's Hard module rewards using a given multiplier",
Long: strings.TrimSpace( Long: strings.TrimSpace(
fmt.Sprintf(`Claim owner's outstanding Hard rewards using given multiplier multiplier, fmt.Sprintf(`Claim sender's outstanding Hard rewards for deposit/borrow/delegate using given multiplier
Example: Example:
$ %s tx %s claim-hard kava15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw large $ %s tx %s claim-hard large
`, version.ClientName, types.ModuleName), `, version.ClientName, types.ModuleName),
), ),
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin()) inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContextWithInputAndFrom(inBuf, args[0]).WithCodec(cdc) cliCtx := context.NewCLIContext().WithCodec(cdc)
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc)) txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
owner, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
sender := cliCtx.GetFromAddress() sender := cliCtx.GetFromAddress()
if !sender.Equals(owner) { multiplier := args[0]
return sdkerrors.Wrapf(types.ErrInvalidClaimOwner, "tx sender %s does not match claim owner %s", sender, owner)
}
msg := types.NewMsgClaimHardLiquidityProviderReward(owner, args[1]) msg := types.NewMsgClaimHardLiquidityProviderReward(sender, multiplier)
err = msg.ValidateBasic() err := msg.ValidateBasic()
if err != nil { if err != nil {
return err return err
} }

View File

@ -8,10 +8,10 @@ import (
// Incentive module errors // Incentive module errors
var ( var (
ErrClaimNotFound = sdkerrors.Register(ModuleName, 2, "no claim with input id found for owner and collateral type") ErrClaimNotFound = sdkerrors.Register(ModuleName, 2, "no claimable rewards found for user")
ErrRewardPeriodNotFound = sdkerrors.Register(ModuleName, 3, "no reward period found for collateral type") ErrRewardPeriodNotFound = sdkerrors.Register(ModuleName, 3, "no reward period found for collateral type")
ErrInvalidAccountType = sdkerrors.Register(ModuleName, 4, "account type not supported") ErrInvalidAccountType = sdkerrors.Register(ModuleName, 4, "account type not supported")
ErrNoClaimsFound = sdkerrors.Register(ModuleName, 5, "no claims with collateral type found for address") ErrNoClaimsFound = sdkerrors.Register(ModuleName, 5, "no claimable rewards found")
ErrInsufficientModAccountBalance = sdkerrors.Register(ModuleName, 6, "module account has insufficient balance to pay claim") ErrInsufficientModAccountBalance = sdkerrors.Register(ModuleName, 6, "module account has insufficient balance to pay claim")
ErrAccountNotFound = sdkerrors.Register(ModuleName, 7, "account not found") ErrAccountNotFound = sdkerrors.Register(ModuleName, 7, "account not found")
ErrInvalidMultiplier = sdkerrors.Register(ModuleName, 8, "invalid rewards multiplier") ErrInvalidMultiplier = sdkerrors.Register(ModuleName, 8, "invalid rewards multiplier")