Skip to content

Commit

Permalink
add amino support to authz module with tax computing custom cli
Browse files Browse the repository at this point in the history
  • Loading branch information
yun-yeo committed Jun 11, 2021
1 parent 3166ae2 commit d6dc92d
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -149,6 +150,7 @@ var (
// and genesis verification.
ModuleBasics = module.NewBasicManager(
customauth.AppModuleBasic{},
customauthz.AppModuleBasic{},
genutil.AppModuleBasic{},
custombank.AppModuleBasic{},
capability.AppModuleBasic{},
Expand Down Expand Up @@ -176,7 +178,6 @@ var (
market.AppModuleBasic{},
treasury.AppModuleBasic{},
wasm.AppModuleBasic{},
authzmodule.AppModuleBasic{},
)

// module account permissions
Expand Down
101 changes: 101 additions & 0 deletions custom/authz/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -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 <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
}

// 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
}
31 changes: 31 additions & 0 deletions custom/authz/module.go
Original file line number Diff line number Diff line change
@@ -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()
}
24 changes: 24 additions & 0 deletions custom/authz/types/codec.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d6dc92d

Please sign in to comment.