From d6dc92de471b657663feeee566650308d91a304f Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Fri, 11 Jun 2021 16:47:58 +0900 Subject: [PATCH] add amino support to authz module with tax computing custom cli --- app/app.go | 3 +- custom/authz/client/cli/tx.go | 101 ++++++++++++++++++++++++++++++++++ custom/authz/module.go | 31 +++++++++++ custom/authz/types/codec.go | 24 ++++++++ 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 custom/authz/client/cli/tx.go create mode 100644 custom/authz/module.go create mode 100644 custom/authz/types/codec.go diff --git a/app/app.go b/app/app.go index f6bb83f56..24a856bfc 100644 --- a/app/app.go +++ b/app/app.go @@ -101,6 +101,7 @@ import ( customauthrest "github.com/terra-money/core/custom/auth/client/rest" customauthsim "github.com/terra-money/core/custom/auth/simulation" customauthtx "github.com/terra-money/core/custom/auth/tx" + customauthz "github.com/terra-money/core/custom/authz" custombank "github.com/terra-money/core/custom/bank" customcrisis "github.com/terra-money/core/custom/crisis" customdistr "github.com/terra-money/core/custom/distribution" @@ -149,6 +150,7 @@ var ( // and genesis verification. ModuleBasics = module.NewBasicManager( customauth.AppModuleBasic{}, + customauthz.AppModuleBasic{}, genutil.AppModuleBasic{}, custombank.AppModuleBasic{}, capability.AppModuleBasic{}, @@ -176,7 +178,6 @@ var ( market.AppModuleBasic{}, treasury.AppModuleBasic{}, wasm.AppModuleBasic{}, - authzmodule.AppModuleBasic{}, ) // module account permissions diff --git a/custom/authz/client/cli/tx.go b/custom/authz/client/cli/tx.go new file mode 100644 index 000000000..9e211f1bc --- /dev/null +++ b/custom/authz/client/cli/tx.go @@ -0,0 +1,101 @@ +package cli + +import ( + "errors" + "fmt" + "strings" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/cosmos/cosmos-sdk/version" + authclient "github.com/cosmos/cosmos-sdk/x/auth/client" + "github.com/cosmos/cosmos-sdk/x/authz" + "github.com/cosmos/cosmos-sdk/x/authz/client/cli" + + feeutils "github.com/terra-money/core/custom/auth/client/utils" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd() *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", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + AuthorizationTxCmd.AddCommand( + cli.NewCmdGrantAuthorization(), + cli.NewCmdRevokeAuthorization(), + NewCmdExecAuthorization(), + ) + + return AuthorizationTxCmd +} + +// NewCmdExecAuthorization execute granted tx +func NewCmdExecAuthorization() *cobra.Command { + cmd := &cobra.Command{ + Use: "exec [msg_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 --from --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 + } + + // Generate transaction factory for gas simulation + txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()) + msg := authz.NewMsgExec(grantee, theTx.GetMsgs()) + if err := msg.ValidateBasic(); err != nil { + return err + } + + if !clientCtx.GenerateOnly && txf.Fees().IsZero() { + // estimate tax and gas + stdFee, err := feeutils.ComputeFeesWithCmd(clientCtx, cmd.Flags(), &msg) + + if err != nil { + return err + } + + // override gas and fees + txf = txf. + WithFees(stdFee.Amount.String()). + WithGas(stdFee.Gas). + WithSimulateAndExecute(false). + WithGasPrices("") + } + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/custom/authz/module.go b/custom/authz/module.go new file mode 100644 index 000000000..7044181e1 --- /dev/null +++ b/custom/authz/module.go @@ -0,0 +1,31 @@ +package authz + +import ( + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + authz "github.com/cosmos/cosmos-sdk/x/authz/module" + + customcli "github.com/terra-money/core/custom/authz/client/cli" + customtypes "github.com/terra-money/core/custom/authz/types" +) + +var ( + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic defines the basic application module used by the distribution module. +type AppModuleBasic struct { + authz.AppModuleBasic +} + +// RegisterLegacyAminoCodec registers the bank module's types for the given codec. +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + customtypes.RegisterLegacyAminoCodec(cdc) +} + +// GetTxCmd returns the root tx command for the bank module. +func (AppModuleBasic) GetTxCmd() *cobra.Command { + return customcli.GetTxCmd() +} diff --git a/custom/authz/types/codec.go b/custom/authz/types/codec.go new file mode 100644 index 000000000..3a6ecd3ed --- /dev/null +++ b/custom/authz/types/codec.go @@ -0,0 +1,24 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" + "github.com/cosmos/cosmos-sdk/x/authz" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +// RegisterLegacyAminoCodec registers the necessary x/authz interfaces and concrete types +// on the provided LegacyAmino codec. These types are used for Amino JSON serialization. +func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + cdc.RegisterInterface((*authz.Authorization)(nil), nil) + + cdc.RegisterConcrete(authz.MsgGrant{}, "msgauth/MsgGrantAuthorization", nil) + cdc.RegisterConcrete(authz.MsgRevoke{}, "msgauth/MsgRevokeAuthorization", nil) + cdc.RegisterConcrete(authz.MsgExec{}, "msgauth/MsgExecAuthorized", nil) + cdc.RegisterConcrete(banktypes.SendAuthorization{}, "msgauth/SendAuthorization", nil) + cdc.RegisterConcrete(authz.GenericAuthorization{}, "msgauth/GenericAuthorization", nil) +} + +func init() { + RegisterLegacyAminoCodec(legacy.Cdc) +}