-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add amino support to authz module with tax computing custom cli
- Loading branch information
Showing
4 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |