feat(mint): add autocli config for mint (#16640)
This commit is contained in:
parent
c63d5fa75e
commit
854978f382
@ -1,16 +1,10 @@
|
||||
package mint
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint/client/cli"
|
||||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||
)
|
||||
|
||||
@ -53,110 +47,3 @@ func (s *E2ETestSuite) TearDownSuite() {
|
||||
s.T().Log("tearing down e2e test suite")
|
||||
s.network.Cleanup()
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestGetCmdQueryParams() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
||||
`{"mint_denom":"stake","inflation_rate_change":"0.130000000000000000","inflation_max":"1.000000000000000000","inflation_min":"1.000000000000000000","goal_bonded":"0.670000000000000000","blocks_per_year":"6311520"}`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
|
||||
`blocks_per_year: "6311520"
|
||||
goal_bonded: "0.670000000000000000"
|
||||
inflation_max: "1.000000000000000000"
|
||||
inflation_min: "1.000000000000000000"
|
||||
inflation_rate_change: "0.130000000000000000"
|
||||
mint_denom: stake`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryParams()
|
||||
clientCtx := val.ClientCtx
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestGetCmdQueryInflation() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
||||
`1.000000000000000000`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
|
||||
`1.000000000000000000`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryInflation()
|
||||
clientCtx := val.ClientCtx
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *E2ETestSuite) TestGetCmdQueryAnnualProvisions() {
|
||||
val := s.network.Validators[0]
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
||||
`500000000.000000000000000000`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
|
||||
`500000000.000000000000000000`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
s.Run(tc.name, func() {
|
||||
cmd := cli.GetCmdQueryAnnualProvisions()
|
||||
clientCtx := val.ClientCtx
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
34
x/mint/autocli.go
Normal file
34
x/mint/autocli.go
Normal file
@ -0,0 +1,34 @@
|
||||
package mint
|
||||
|
||||
import (
|
||||
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
|
||||
mintv1beta1 "cosmossdk.io/api/cosmos/mint/v1beta1"
|
||||
)
|
||||
|
||||
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
|
||||
return &autocliv1.ModuleOptions{
|
||||
Query: &autocliv1.ServiceCommandDescriptor{
|
||||
Service: mintv1beta1.Query_ServiceDesc.ServiceName,
|
||||
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
|
||||
{
|
||||
RpcMethod: "Params",
|
||||
Use: "params",
|
||||
Short: "Query the current minting parameters",
|
||||
},
|
||||
{
|
||||
RpcMethod: "Inflation",
|
||||
Use: "inflation",
|
||||
Short: "Query the current minting inflation value",
|
||||
},
|
||||
{
|
||||
RpcMethod: "AnnualProvisions",
|
||||
Use: "annual-provisions",
|
||||
Short: "Query the current minting annual provisions value",
|
||||
},
|
||||
},
|
||||
},
|
||||
Tx: &autocliv1.ServiceCommandDescriptor{
|
||||
Service: mintv1beta1.Query_ServiceDesc.ServiceName,
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -1,117 +0,0 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||
)
|
||||
|
||||
// GetQueryCmd returns the cli query commands for the minting module.
|
||||
func GetQueryCmd() *cobra.Command {
|
||||
mintingQueryCmd := &cobra.Command{
|
||||
Use: types.ModuleName,
|
||||
Short: "Querying commands for the minting module",
|
||||
DisableFlagParsing: true,
|
||||
SuggestionsMinimumDistance: 2,
|
||||
RunE: client.ValidateCmd,
|
||||
}
|
||||
|
||||
mintingQueryCmd.AddCommand(
|
||||
GetCmdQueryParams(),
|
||||
GetCmdQueryInflation(),
|
||||
GetCmdQueryAnnualProvisions(),
|
||||
)
|
||||
|
||||
return mintingQueryCmd
|
||||
}
|
||||
|
||||
// GetCmdQueryParams implements a command to return the current minting
|
||||
// parameters.
|
||||
func GetCmdQueryParams() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "params",
|
||||
Short: "Query the current minting parameters",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
clientCtx, err := client.GetClientQueryContext(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
queryClient := types.NewQueryClient(clientCtx)
|
||||
|
||||
params := &types.QueryParamsRequest{}
|
||||
res, err := queryClient.Params(cmd.Context(), params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return clientCtx.PrintProto(&res.Params)
|
||||
},
|
||||
}
|
||||
|
||||
flags.AddQueryFlagsToCmd(cmd)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// GetCmdQueryInflation implements a command to return the current minting
|
||||
// inflation value.
|
||||
func GetCmdQueryInflation() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "inflation",
|
||||
Short: "Query the current minting inflation value",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
clientCtx, err := client.GetClientQueryContext(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
queryClient := types.NewQueryClient(clientCtx)
|
||||
|
||||
params := &types.QueryInflationRequest{}
|
||||
res, err := queryClient.Inflation(cmd.Context(), params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return clientCtx.PrintString(fmt.Sprintf("%s\n", res.Inflation))
|
||||
},
|
||||
}
|
||||
|
||||
flags.AddQueryFlagsToCmd(cmd)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// GetCmdQueryAnnualProvisions implements a command to return the current minting
|
||||
// annual provisions value.
|
||||
func GetCmdQueryAnnualProvisions() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "annual-provisions",
|
||||
Short: "Query the current minting annual provisions value",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
clientCtx, err := client.GetClientQueryContext(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
queryClient := types.NewQueryClient(clientCtx)
|
||||
|
||||
params := &types.QueryAnnualProvisionsRequest{}
|
||||
res, err := queryClient.AnnualProvisions(cmd.Context(), params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return clientCtx.PrintString(fmt.Sprintf("%s\n", res.AnnualProvisions))
|
||||
},
|
||||
}
|
||||
|
||||
flags.AddQueryFlagsToCmd(cmd)
|
||||
|
||||
return cmd
|
||||
}
|
||||
@ -1,207 +0,0 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint"
|
||||
mintcli "github.com/cosmos/cosmos-sdk/x/mint/client/cli"
|
||||
)
|
||||
|
||||
func TestGetCmdQueryParams(t *testing.T) {
|
||||
encCfg := testutilmod.MakeTestEncodingConfig(mint.AppModuleBasic{})
|
||||
kr := keyring.NewInMemory(encCfg.Codec)
|
||||
baseCtx := client.Context{}.
|
||||
WithKeyring(kr).
|
||||
WithTxConfig(encCfg.TxConfig).
|
||||
WithCodec(encCfg.Codec).
|
||||
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard).
|
||||
WithChainID("test-chain")
|
||||
|
||||
cmd := mintcli.GetCmdQueryParams()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
flagArgs []string
|
||||
expCmdOutput string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
||||
`[--height=1 --output=json]`,
|
||||
`{"mint_denom":"","inflation_rate_change":"0","inflation_max":"0","inflation_min":"0","goal_bonded":"0","blocks_per_year":"0"}`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
|
||||
`[--height=1 --output=text]`,
|
||||
`blocks_per_year: "0"
|
||||
goal_bonded: "0"
|
||||
inflation_max: "0"
|
||||
inflation_min: "0"
|
||||
inflation_rate_change: "0"
|
||||
mint_denom: ""`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := svrcmd.CreateExecuteContext(context.Background())
|
||||
|
||||
cmd.SetOut(io.Discard)
|
||||
require.NotNil(t, cmd)
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
cmd.SetArgs(tc.flagArgs)
|
||||
|
||||
require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))
|
||||
|
||||
if len(tc.flagArgs) != 0 {
|
||||
require.Contains(t, fmt.Sprint(cmd), "params [] [] Query the current minting parameters")
|
||||
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
|
||||
}
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.flagArgs)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCmdQueryInflation(t *testing.T) {
|
||||
encCfg := testutilmod.MakeTestEncodingConfig(mint.AppModuleBasic{})
|
||||
kr := keyring.NewInMemory(encCfg.Codec)
|
||||
baseCtx := client.Context{}.
|
||||
WithKeyring(kr).
|
||||
WithTxConfig(encCfg.TxConfig).
|
||||
WithCodec(encCfg.Codec).
|
||||
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard).
|
||||
WithChainID("test-chain")
|
||||
|
||||
cmd := mintcli.GetCmdQueryInflation()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
flagArgs []string
|
||||
expCmdOutput string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
||||
`[--height=1 --output=json]`,
|
||||
`<nil>`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
|
||||
`[--height=1 --output=text]`,
|
||||
`<nil>`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := svrcmd.CreateExecuteContext(context.Background())
|
||||
|
||||
cmd.SetOut(io.Discard)
|
||||
require.NotNil(t, cmd)
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
cmd.SetArgs(tc.flagArgs)
|
||||
|
||||
require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))
|
||||
|
||||
if len(tc.flagArgs) != 0 {
|
||||
require.Contains(t, fmt.Sprint(cmd), "inflation [] [] Query the current minting inflation value")
|
||||
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
|
||||
}
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.flagArgs)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCmdQueryAnnualProvisions(t *testing.T) {
|
||||
encCfg := testutilmod.MakeTestEncodingConfig(mint.AppModuleBasic{})
|
||||
kr := keyring.NewInMemory(encCfg.Codec)
|
||||
baseCtx := client.Context{}.
|
||||
WithKeyring(kr).
|
||||
WithTxConfig(encCfg.TxConfig).
|
||||
WithCodec(encCfg.Codec).
|
||||
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard).
|
||||
WithChainID("test-chain")
|
||||
|
||||
cmd := mintcli.GetCmdQueryAnnualProvisions()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
flagArgs []string
|
||||
expCmdOutput string
|
||||
expectedOutput string
|
||||
}{
|
||||
{
|
||||
"json output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
||||
`[--height=1 --output=json]`,
|
||||
`<nil>`,
|
||||
},
|
||||
{
|
||||
"text output",
|
||||
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
|
||||
`[--height=1 --output=text]`,
|
||||
`<nil>`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := svrcmd.CreateExecuteContext(context.Background())
|
||||
|
||||
cmd.SetOut(io.Discard)
|
||||
require.NotNil(t, cmd)
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
cmd.SetArgs(tc.flagArgs)
|
||||
|
||||
require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))
|
||||
|
||||
if len(tc.flagArgs) != 0 {
|
||||
require.Contains(t, fmt.Sprint(cmd), "annual-provisions [] [] Query the current minting annual provisions value")
|
||||
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
|
||||
}
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(baseCtx, cmd, tc.flagArgs)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedOutput, strings.TrimSpace(out.String()))
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -23,7 +23,6 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/types/module"
|
||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint/client/cli"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/mint/simulation"
|
||||
@ -88,7 +87,7 @@ func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil }
|
||||
|
||||
// GetQueryCmd returns the root query command for the mint module.
|
||||
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
|
||||
return cli.GetQueryCmd()
|
||||
return nil
|
||||
}
|
||||
|
||||
// AppModule implements an application module for the mint module.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user