Skip to content

Commit

Permalink
refactor(cli): use clientCtx adress codecs in cli (#18070)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Oct 11, 2023
1 parent 6009891 commit f28a93a
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 79 deletions.
22 changes: 10 additions & 12 deletions x/auth/vesting/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"github.com/spf13/cobra"

"cosmossdk.io/core/address"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -24,7 +22,7 @@ const (
)

// GetTxCmd returns vesting module's transaction commands.
func GetTxCmd(ac address.Codec) *cobra.Command {
func GetTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Vesting transaction subcommands",
Expand All @@ -34,17 +32,17 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
}

txCmd.AddCommand(
NewMsgCreateVestingAccountCmd(ac),
NewMsgCreatePermanentLockedAccountCmd(ac),
NewMsgCreatePeriodicVestingAccountCmd(ac),
NewMsgCreateVestingAccountCmd(),
NewMsgCreatePermanentLockedAccountCmd(),
NewMsgCreatePeriodicVestingAccountCmd(),
)

return txCmd
}

// NewMsgCreateVestingAccountCmd returns a CLI command handler for creating a
// MsgCreateVestingAccount transaction.
func NewMsgCreateVestingAccountCmd(ac address.Codec) *cobra.Command {
func NewMsgCreateVestingAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-vesting-account [to_address] [amount] [end_time]",
Short: "Create a new vesting account funded with an allocation of tokens.",
Expand All @@ -59,7 +57,7 @@ timestamp.`,
if err != nil {
return err
}
toAddr, err := ac.StringToBytes(args[0])
toAddr, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -93,7 +91,7 @@ timestamp.`,

// NewMsgCreatePermanentLockedAccountCmd returns a CLI command handler for creating a
// MsgCreatePermanentLockedAccount transaction.
func NewMsgCreatePermanentLockedAccountCmd(ac address.Codec) *cobra.Command {
func NewMsgCreatePermanentLockedAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-permanent-locked-account [to_address] [amount]",
Short: "Create a new permanently locked account funded with an allocation of tokens.",
Expand All @@ -106,7 +104,7 @@ tokens.`,
if err != nil {
return err
}
toAddr, err := ac.StringToBytes(args[0])
toAddr, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -142,7 +140,7 @@ type InputPeriod struct {

// NewMsgCreatePeriodicVestingAccountCmd returns a CLI command handler for creating a
// MsgCreatePeriodicVestingAccountCmd transaction.
func NewMsgCreatePeriodicVestingAccountCmd(ac address.Codec) *cobra.Command {
func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-periodic-vesting-account [to_address] [periods_json_file]",
Short: "Create a new vesting account funded with an allocation of tokens.",
Expand Down Expand Up @@ -170,7 +168,7 @@ func NewMsgCreatePeriodicVestingAccountCmd(ac address.Codec) *cobra.Command {
return err
}

toAddr, err := ac.StringToBytes(args[0])
toAddr, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions x/auth/vesting/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *CLITestSuite) SetupSuite() {

func (s *CLITestSuite) TestNewMsgCreateVestingAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreateVestingAccountCmd(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewMsgCreateVestingAccountCmd()
cmd.SetOutput(io.Discard)

extraArgs := []string{
Expand Down Expand Up @@ -143,7 +143,7 @@ func (s *CLITestSuite) TestNewMsgCreateVestingAccountCmd() {

func (s *CLITestSuite) TestNewMsgCreatePermanentLockedAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreatePermanentLockedAccountCmd(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewMsgCreatePermanentLockedAccountCmd()
cmd.SetOutput(io.Discard)

extraArgs := []string{
Expand Down Expand Up @@ -222,7 +222,7 @@ func (s *CLITestSuite) TestNewMsgCreatePermanentLockedAccountCmd() {

func (s *CLITestSuite) TestNewMsgCreatePeriodicVestingAccountCmd() {
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
cmd := cli.NewMsgCreatePeriodicVestingAccountCmd(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewMsgCreatePeriodicVestingAccountCmd()
cmd.SetOutput(io.Discard)

extraArgs := []string{
Expand Down
9 changes: 3 additions & 6 deletions x/auth/vesting/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"google.golang.org/grpc"

modulev1 "cosmossdk.io/api/cosmos/vesting/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"

Expand All @@ -33,9 +32,7 @@ var (
// AppModuleBasic defines the basic application module used by the sub-vesting
// module. The module itself contain no special logic or state other than message
// handling.
type AppModuleBasic struct {
ac address.Codec
}
type AppModuleBasic struct{}

// Name returns the module's name.
func (AppModuleBasic) Name() string {
Expand Down Expand Up @@ -69,7 +66,7 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *gwruntime.S

// GetTxCmd returns the root tx command for the auth module.
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
return cli.GetTxCmd()
}

// AppModule extends the AppModuleBasic implementation by implementing the
Expand All @@ -83,7 +80,7 @@ type AppModule struct {

func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{ac: ak.AddressCodec()},
AppModuleBasic: AppModuleBasic{},
accountKeeper: ak,
bankKeeper: bk,
}
Expand Down
37 changes: 16 additions & 21 deletions x/authz/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

"github.com/spf13/cobra"

"cosmossdk.io/core/address"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -35,7 +33,7 @@ const (
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd(ac address.Codec) *cobra.Command {
func GetTxCmd() *cobra.Command {
AuthorizationTxCmd := &cobra.Command{
Use: authz.ModuleName,
Short: "Authorization transactions subcommands",
Expand All @@ -46,28 +44,25 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
}

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

return AuthorizationTxCmd
}

// NewCmdGrantAuthorization returns a CLI command handler for creating a MsgGrant transaction.
//
// cannot give autocli support, can be CLI breaking
func NewCmdGrantAuthorization(ac address.Codec) *cobra.Command {
// Migrating this command to AutoCLI is possible but would be CLI breaking.
func NewCmdGrantAuthorization() *cobra.Command {
cmd := &cobra.Command{
Use: "grant <grantee> <authorization_type=\"send\"|\"generic\"|\"delegate\"|\"unbond\"|\"redelegate\"> --from <granter>",
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:
Examples:
$ %s tx %s grant cosmos1skjw.. send --spend-limit=1000stake --from=cosmos1skl..
$ %s tx %s grant cosmos1skjw.. generic --msg-type=/cosmos.gov.v1.MsgVote --from=cosmos1sk..
`, version.AppName, authz.ModuleName, version.AppName, authz.ModuleName),
),
$ %[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)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -79,7 +74,7 @@ Examples:
return errors.New("grantee and granter should be different")
}

grantee, err := ac.StringToBytes(args[0])
grantee, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -115,7 +110,7 @@ Examples:
}
}

allowed, err := bech32toAccAddresses(allowList, ac)
allowed, err := bech32toAccAddresses(clientCtx, allowList)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,12 +163,12 @@ Examples:
delegateLimit = &spendLimit
}

allowed, err := bech32toValAddresses(allowValidators)
allowed, err := bech32toValAddresses(clientCtx, allowValidators)
if err != nil {
return err
}

denied, err := bech32toValAddresses(denyValidators)
denied, err := bech32toValAddresses(clientCtx, denyValidators)
if err != nil {
return err
}
Expand Down Expand Up @@ -271,10 +266,10 @@ Example:
}

// bech32toValAddresses returns []ValAddress from a list of Bech32 string addresses.
func bech32toValAddresses(validators []string) ([]sdk.ValAddress, error) {
func bech32toValAddresses(clientCtx client.Context, validators []string) ([]sdk.ValAddress, error) {
vals := make([]sdk.ValAddress, len(validators))
for i, validator := range validators {
addr, err := sdk.ValAddressFromBech32(validator)
addr, err := clientCtx.ValidatorAddressCodec.StringToBytes(validator)
if err != nil {
return nil, err
}
Expand All @@ -284,10 +279,10 @@ func bech32toValAddresses(validators []string) ([]sdk.ValAddress, error) {
}

// bech32toAccAddresses returns []AccAddress from a list of Bech32 string addresses.
func bech32toAccAddresses(accAddrs []string, ac address.Codec) ([]sdk.AccAddress, error) {
func bech32toAccAddresses(clientCtx client.Context, accAddrs []string) ([]sdk.AccAddress, error) {
addrs := make([]sdk.AccAddress, len(accAddrs))
for i, addr := range accAddrs {
accAddr, err := ac.StringToBytes(addr)
accAddr, err := clientCtx.AddressCodec.StringToBytes(addr)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions x/authz/client/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package authz

import (
"github.com/cosmos/cosmos-sdk/client"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/x/authz/client/cli"
)

func CreateGrant(clientCtx client.Context, args []string) (testutil.BufferWriter, error) {
cmd := cli.NewCmdGrantAuthorization(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdGrantAuthorization()
return clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
}
2 changes: 1 addition & 1 deletion x/authz/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux

// GetTxCmd returns the transaction commands for the authz module
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
return cli.GetTxCmd()
}

// AppModule implements the sdk.AppModule interface
Expand Down
11 changes: 5 additions & 6 deletions x/feegrant/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/spf13/cobra"

"cosmossdk.io/core/address"
"cosmossdk.io/x/feegrant"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -27,7 +26,7 @@ const (
)

// GetTxCmd returns the transaction commands for feegrant module
func GetTxCmd(ac address.Codec) *cobra.Command {
func GetTxCmd() *cobra.Command {
feegrantTxCmd := &cobra.Command{
Use: feegrant.ModuleName,
Short: "Feegrant transactions sub-commands",
Expand All @@ -38,15 +37,15 @@ func GetTxCmd(ac address.Codec) *cobra.Command {
}

feegrantTxCmd.AddCommand(
NewCmdFeeGrant(ac),
NewCmdFeeGrant(),
)

return feegrantTxCmd
}

// NewCmdFeeGrant returns a CLI command handler to create a MsgGrantAllowance transaction.
// This command is more powerful than AutoCLI generated command as it allows a better input validation.
func NewCmdFeeGrant(ac address.Codec) *cobra.Command {
func NewCmdFeeGrant() *cobra.Command {
cmd := &cobra.Command{
Use: "grant [granter_key_or_address] [grantee]",
Aliases: []string{"grant-allowance"},
Expand Down Expand Up @@ -75,13 +74,13 @@ Examples:
return err
}

_, err = ac.StringToBytes(args[1])
_, err = clientCtx.AddressCodec.StringToBytes(args[1])
if err != nil {
return err
}

granter := clientCtx.GetFromAddress()
granterStr, err := ac.BytesToString(granter)
granterStr, err := clientCtx.AddressCodec.BytesToString(granter)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions x/feegrant/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *CLITestSuite) createGrant(granter, grantee sdk.Address) {
commonFlags...,
)

cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, args)
s.Require().NoError(err)

Expand Down Expand Up @@ -423,7 +423,7 @@ func (s *CLITestSuite) TestNewCmdFeeGrant() {
tc := tc

s.Run(tc.name, func() {
cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)

if tc.expectErr {
Expand Down Expand Up @@ -466,7 +466,7 @@ func (s *CLITestSuite) TestTxWithFeeGrant() {
commonFlags...,
)

cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()

var res sdk.TxResponse
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
Expand Down Expand Up @@ -619,7 +619,7 @@ func (s *CLITestSuite) TestFilteredFeeAllowance() {
tc := tc

s.Run(tc.name, func() {
cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErrMsg != "" {
s.Require().Error(err)
Expand Down Expand Up @@ -669,7 +669,7 @@ func (s *CLITestSuite) TestFilteredFeeAllowance() {
commonFlags...,
)

cmd := cli.NewCmdFeeGrant(addresscodec.NewBech32Codec("cosmos"))
cmd := cli.NewCmdFeeGrant()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &sdk.TxResponse{}), out.String())

Expand Down
Loading

0 comments on commit f28a93a

Please sign in to comment.