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

refactor(x/authz): add exec as autocli cmd #18039

Merged
merged 7 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
89 changes: 42 additions & 47 deletions x/authz/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (

// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
AuthorizationTxCmd := &cobra.Command{
authorizationTxCmd := &cobra.Command{
Use: authz.ModuleName,
Short: "Authorization transactions subcommands",
Long: "Authorize and revoke access to execute transactions on behalf of your address",
Expand All @@ -43,12 +43,49 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

AuthorizationTxCmd.AddCommand(
authorizationTxCmd.AddCommand(
NewCmdGrantAuthorization(),
NewCmdExecAuthorization(),
)

return AuthorizationTxCmd
return authorizationTxCmd
}

// NewCmdExecAuthorization returns a CLI command handler for creating a MsgExec transaction.
// Deprecated: This command is deprecated in favor for the AutoCLI exec command.
// It stays here for backward compatibility, as the AutoCLI command has a small breaking change,
// but it will be removed in future versions.
func NewCmdExecAuthorization() *cobra.Command {
cmd := &cobra.Command{
Deprecated: "Use the AutoCLI exec command instead",
Use: "legacy-exec [tx-json-file] --from [grantee]",
Short: "Execute tx on behalf of granter account",
Example: fmt.Sprintf("$ %s tx authz exec tx.json --from grantee\n $ %[1]s tx bank send [granter] [recipient] [amount] --generate-only tx.json && %[1]s tx authz exec tx.json --from grantee", version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
grantee := clientCtx.GetFromAddress()

if offline, _ := cmd.Flags().GetBool(flags.FlagOffline); offline {
return errors.New("cannot broadcast tx during offline mode")
}

theTx, err := authclient.ReadTxFromFile(clientCtx, args[0])
if err != nil {
return err
}
msg := authz.NewMsgExec(grantee, theTx.GetMsgs())

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

// NewCmdGrantAuthorization returns a CLI command handler for creating a MsgGrant transaction.
Expand All @@ -57,12 +94,11 @@ func NewCmdGrantAuthorization() *cobra.Command {
cmd := &cobra.Command{
Use: "grant [grantee] <authorization_type=\"send\"|\"generic\"|\"delegate\"|\"unbond\"|\"redelegate\"> --from [granter]",
Short: "Grant authorization to an address",
Long: strings.TrimSpace(
fmt.Sprintf(`create a new grant authorization to an address to execute a transaction on your behalf:
Long: fmt.Sprintf(`create a new grant authorization to an address to execute a transaction on your behalf:
Examples:
$ %[1]s tx authz grant cosmos1skjw.. send --spend-limit=1000stake --from=cosmos1skl..
$ %[1]s tx authz grant cosmos1skjw.. generic --msg-type=/cosmos.gov.v1.MsgVote --from=cosmos1sk..
`, version.AppName)),
`, version.AppName),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
Expand Down Expand Up @@ -224,47 +260,6 @@ func getExpireTime(cmd *cobra.Command) (*time.Time, error) {
return &e, nil
}

// NewCmdExecAuthorization returns a CLI command handler for creating a MsgExec transaction.
//
// cannot give autocli support, can be CLI breaking
func NewCmdExecAuthorization() *cobra.Command {
cmd := &cobra.Command{
Use: "exec [tx-json-file] --from [grantee]",
Short: "execute tx on behalf of granter account",
Long: strings.TrimSpace(
fmt.Sprintf(`execute tx on behalf of granter account:
Example:
$ %s tx %s exec tx.json --from grantee
$ %s tx bank send <granter> <recipient> --from <granter> --chain-id <chain-id> --generate-only > tx.json && %s tx %s exec tx.json --from grantee
`, version.AppName, authz.ModuleName, version.AppName, version.AppName, authz.ModuleName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
grantee := clientCtx.GetFromAddress()

if offline, _ := cmd.Flags().GetBool(flags.FlagOffline); offline {
return errors.New("cannot broadcast tx during offline mode")
}

theTx, err := authclient.ReadTxFromFile(clientCtx, args[0])
if err != nil {
return err
}
msg := authz.NewMsgExec(grantee, theTx.GetMsgs())

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

// bech32toValAddresses returns []ValAddress from a list of Bech32 string addresses.
func bech32toValAddresses(clientCtx client.Context, validators []string) ([]sdk.ValAddress, error) {
vals := make([]sdk.ValAddress, len(validators))
Expand Down
Loading
Loading