feat(x/feegrant): add autocli options for tx (#17959)

This commit is contained in:
Julien Robert 2023-10-09 13:38:34 +02:00 committed by GitHub
parent b170ccc459
commit 1d03d890cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 139 deletions

View File

@ -11,12 +11,19 @@ import (
// findSubCommand finds a sub-command of the provided command whose Use
// string is or begins with the provided subCmdName.
// It verifies the command's aliases as well.
func findSubCommand(cmd *cobra.Command, subCmdName string) *cobra.Command {
for _, subCmd := range cmd.Commands() {
use := subCmd.Use
if use == subCmdName || strings.HasPrefix(use, subCmdName+" ") {
return subCmd
}
for _, alias := range subCmd.Aliases {
if alias == subCmdName || strings.HasPrefix(alias, subCmdName+" ") {
return subCmd
}
}
}
return nil
}

View File

@ -39,17 +39,18 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
feegrantTxCmd.AddCommand(
NewCmdFeeGrant(ac),
NewCmdRevokeFeegrant(ac),
)
return feegrantTxCmd
}
// NewCmdFeeGrant returns a CLI command handler to create a MsgGrantAllowance transaction.
// This command is more powerful than AutoCLI generated command as it allows a better input validation.
func NewCmdFeeGrant(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "grant [granter_key_or_address] [grantee]",
Short: "Grant Fee allowance to an address",
Use: "grant [granter_key_or_address] [grantee]",
Aliases: []string{"grant-allowance"},
Short: "Grant Fee allowance to an address",
Long: strings.TrimSpace(
fmt.Sprintf(
`Grant authorization to pay fees from your address. Note, the '--from' flag is
@ -190,49 +191,6 @@ Examples:
return cmd
}
// NewCmdRevokeFeegrant returns a CLI command handler to create a MsgRevokeAllowance transaction.
func NewCmdRevokeFeegrant(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "revoke [granter] [grantee]",
Short: "revoke fee-grant",
Long: strings.TrimSpace(
fmt.Sprintf(`revoke fee grant from a granter to a grantee. Note, the '--from' flag is
ignored as it is implied from [granter].
Example:
$ %s tx %s revoke cosmos1skj.. cosmos1skj..
`, version.AppName, feegrant.ModuleName),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Flags().Set(flags.FlagFrom, args[0]); err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
_, err = ac.StringToBytes(args[1])
if err != nil {
return err
}
granter, err := ac.BytesToString(clientCtx.GetFromAddress())
if err != nil {
return err
}
msg := feegrant.NewMsgRevokeAllowance(granter, args[1])
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func getPeriodReset(duration int64) time.Time {
return time.Now().Add(getPeriod(duration))
}

View File

@ -436,99 +436,6 @@ func (s *CLITestSuite) TestNewCmdFeeGrant() {
}
}
func (s *CLITestSuite) TestNewCmdRevokeFeegrant() {
granter := s.addedGranter
grantee := s.addedGrantee
clientCtx := s.clientCtx
commonFlags := []string{
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(10))).String()),
}
addressCodec := addresscodec.NewBech32Codec("cosmos")
// Create new fee grant specifically to test amino.
encodedGrantee := "cosmos16ydaqh0fcnh4qt7a3jme4mmztm2qel5axcpw00"
aminoGrantee, err := addressCodec.StringToBytes(encodedGrantee)
s.Require().NoError(err)
s.createGrant(granter, sdk.AccAddress(aminoGrantee))
testCases := []struct {
name string
args []string
expectErr bool
expectedCode uint32
respType proto.Message
}{
{
"invalid granter",
append(
[]string{
"wrong_granter",
grantee.String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, granter),
},
commonFlags...,
),
true, 0, nil,
},
{
"invalid grantee",
append(
[]string{
granter.String(),
"wrong_grantee",
fmt.Sprintf("--%s=%s", flags.FlagFrom, granter),
},
commonFlags...,
),
true, 0, nil,
},
{
"Valid revoke",
append(
[]string{
granter.String(),
grantee.String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, granter),
},
commonFlags...,
),
false, 0, &sdk.TxResponse{},
},
{
"Valid revoke with amino",
append(
[]string{
granter.String(),
encodedGrantee,
fmt.Sprintf("--%s=%s", flags.FlagFrom, granter),
fmt.Sprintf("--%s=%s", flags.FlagSignMode, flags.SignModeLegacyAminoJSON),
},
commonFlags...,
),
false, 0, &sdk.TxResponse{},
},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := cli.NewCmdRevokeFeegrant(addresscodec.NewBech32Codec("cosmos"))
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
}
})
}
}
func (s *CLITestSuite) TestTxWithFeeGrant() {
clientCtx := s.clientCtx
granter := s.addedGranter

View File

@ -51,6 +51,20 @@ You can find the fee-grant of a granter and grantee.`),
},
Tx: &autocliv1.ServiceCommandDescriptor{
Service: feegrantv1beta1.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "RevokeAllowance",
Use: "revoke [granter] [grantee]",
Short: "Revoke a fee grant",
Long: "Revoke fee grant from a granter to a grantee. Note, the '--from' flag is ignored as it is implied from [granter]",
Example: fmt.Sprintf(`$ %s tx feegrant revoke [granter] [grantee]`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "granter"},
{ProtoField: "grantee"},
},
},
},
EnhanceCustomCommand: true,
},
}
}