Skip to content

Commit

Permalink
refactor(x/authz): add exec as autocli cmd (#18039)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Oct 12, 2023
1 parent cf49217 commit c3bc5c8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 373 deletions.
88 changes: 41 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,48 @@ 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{
Use: "legacy-exec [tx-json-file] --from [grantee]",
Short: "Execute tx on behalf of granter account. Deprecated, use exec instead.",
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 +93,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 +259,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

0 comments on commit c3bc5c8

Please sign in to comment.