From f525717054b9f3d09212ba7523ac98a2489d95ad Mon Sep 17 00:00:00 2001 From: Jack Zampolin Date: Mon, 19 Nov 2018 09:02:34 -0800 Subject: [PATCH] Standardize CLI Exports from Modules (#2840) * Move query and tx commands to modules * Move GetAccountDecoder to prevent import cycle and replace calls to it with one call in WithAccountDecoder * Add moduleClients interface and implement in all applicable modules * Use module clients in cli initialization --- PENDING.md | 1 + client/context/context.go | 16 +- client/lcd/root.go | 3 + cmd/gaia/cmd/gaiacli/main.go | 96 +++++- cmd/gaia/cmd/gaiacli/query.go | 83 ----- cmd/gaia/cmd/gaiacli/tx.go | 83 ----- cmd/gaia/init/gentx.go | 9 +- docs/examples/basecoin/cmd/basecli/main.go | 42 ++- docs/examples/democoin/cmd/democli/main.go | 6 +- .../examples/democoin/x/cool/client/cli/tx.go | 5 +- docs/examples/democoin/x/pow/client/cli/tx.go | 3 +- .../x/simplestake/client/cli/commands.go | 3 +- types/module_clients.go | 11 + x/auth/client/cli/account.go | 30 +- x/auth/client/cli/sign.go | 15 +- x/auth/client/rest/query.go | 5 +- x/bank/client/cli/broadcast.go | 3 +- x/bank/client/cli/sendtx.go | 10 +- x/distribution/client/cli/tx.go | 22 +- x/distribution/client/module_client.go | 38 ++ x/gov/client/cli/query.go | 324 ++++++++++++++++++ x/gov/client/cli/tx.go | 324 +----------------- x/gov/client/module_client.go | 55 +++ x/gov/client/rest/rest.go | 8 +- x/gov/client/{ => utils}/utils.go | 2 +- x/ibc/client/cli/ibctx.go | 3 +- x/ibc/client/cli/relay.go | 3 +- x/slashing/client/cli/tx.go | 3 +- x/slashing/client/module_client.go | 47 +++ x/stake/client/cli/query.go | 12 +- x/stake/client/cli/tx.go | 11 +- x/stake/client/cli/utils.go | 3 +- x/stake/client/module_client.go | 61 ++++ 33 files changed, 739 insertions(+), 601 deletions(-) delete mode 100644 cmd/gaia/cmd/gaiacli/query.go delete mode 100644 cmd/gaia/cmd/gaiacli/tx.go create mode 100644 types/module_clients.go create mode 100644 x/distribution/client/module_client.go create mode 100644 x/gov/client/cli/query.go create mode 100644 x/gov/client/module_client.go rename x/gov/client/{ => utils}/utils.go (98%) create mode 100644 x/slashing/client/module_client.go create mode 100644 x/stake/client/module_client.go diff --git a/PENDING.md b/PENDING.md index 4720ec774c1a..1c17731e2b7c 100644 --- a/PENDING.md +++ b/PENDING.md @@ -31,6 +31,7 @@ FEATURES * [gov][cli] [\#2479](https://github.com/cosmos/cosmos-sdk/issues/2479) Added governance parameter query commands. * [stake][cli] [\#2027] Add CLI query command for getting all delegations to a specific validator. + * [\#2840](https://github.com/cosmos/cosmos-sdk/pull/2840) Standardize CLI exports from modules * Gaia * [app] \#2791 Support export at a specific height, with `gaiad export --height=HEIGHT`. diff --git a/client/context/context.go b/client/context/context.go index 9108b3d0bba5..4b440736815a 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -175,10 +175,22 @@ func (ctx CLIContext) WithCodec(cdc *codec.Codec) CLIContext { return ctx } +// GetAccountDecoder gets the account decoder for auth.DefaultAccount. +func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder { + return func(accBytes []byte) (acct auth.Account, err error) { + err = cdc.UnmarshalBinaryBare(accBytes, &acct) + if err != nil { + panic(err) + } + + return acct, err + } +} + // WithAccountDecoder returns a copy of the context with an updated account // decoder. -func (ctx CLIContext) WithAccountDecoder(decoder auth.AccountDecoder) CLIContext { - ctx.AccDecoder = decoder +func (ctx CLIContext) WithAccountDecoder(cdc *codec.Codec) CLIContext { + ctx.AccDecoder = GetAccountDecoder(cdc) return ctx } diff --git a/client/lcd/root.go b/client/lcd/root.go index 3d61816467fc..7081b76c0319 100644 --- a/client/lcd/root.go +++ b/client/lcd/root.go @@ -19,6 +19,9 @@ import ( "github.com/spf13/viper" "github.com/tendermint/tendermint/libs/log" tmserver "github.com/tendermint/tendermint/rpc/lib/server" + + // Import statik for light client stuff + _ "github.com/cosmos/cosmos-sdk/client/lcd/statik" ) // RestServer represents the Light Client Rest server diff --git a/cmd/gaia/cmd/gaiacli/main.go b/cmd/gaia/cmd/gaiacli/main.go index 3dd936f8635a..05c7bc4eca35 100644 --- a/cmd/gaia/cmd/gaiacli/main.go +++ b/cmd/gaia/cmd/gaiacli/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "net/http" "os" "path" @@ -9,6 +10,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + amino "github.com/tendermint/go-amino" "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/client" @@ -19,29 +21,29 @@ import ( "github.com/cosmos/cosmos-sdk/cmd/gaia/app" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" + auth "github.com/cosmos/cosmos-sdk/x/auth/client/rest" bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest" gov "github.com/cosmos/cosmos-sdk/x/gov/client/rest" slashing "github.com/cosmos/cosmos-sdk/x/slashing/client/rest" stake "github.com/cosmos/cosmos-sdk/x/stake/client/rest" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" + distClient "github.com/cosmos/cosmos-sdk/x/distribution/client" + govClient "github.com/cosmos/cosmos-sdk/x/gov/client" + slashingClient "github.com/cosmos/cosmos-sdk/x/slashing/client" + stakeClient "github.com/cosmos/cosmos-sdk/x/stake/client" + _ "github.com/cosmos/cosmos-sdk/client/lcd/statik" ) const ( - storeAcc = "acc" - storeGov = "gov" - storeSlashing = "slashing" - storeStake = "stake" - queryRouteStake = "stake" -) - -// rootCmd is the entry point for this binary -var ( - rootCmd = &cobra.Command{ - Use: "gaiacli", - Short: "Command line interface for interacting with gaiad", - } + storeAcc = "acc" + storeGov = "gov" + storeSlashing = "slashing" + storeStake = "stake" + storeDist = "distr" ) func main() { @@ -68,13 +70,27 @@ func main() { // the below functions and eliminate global vars, like we do // with the cdc + // Module clients hold cli commnads (tx,query) and lcd routes + // TODO: Make the lcd command take a list of ModuleClient + mc := []sdk.ModuleClients{ + govClient.NewModuleClient(storeGov, cdc), + distClient.NewModuleClient(storeDist, cdc), + stakeClient.NewModuleClient(storeStake, cdc), + slashingClient.NewModuleClient(storeSlashing, cdc), + } + + rootCmd := &cobra.Command{ + Use: "gaiacli", + Short: "Command line interface for interacting with gaiad", + } + // Construct Root Command rootCmd.AddCommand( rpc.InitClientCommand(), rpc.StatusCommand(), client.ConfigCmd(), - queryCmd(cdc), - txCmd(cdc), + queryCmd(cdc, mc), + txCmd(cdc, mc), client.LineBreak, rs.ServeCommand(), client.LineBreak, @@ -92,9 +108,53 @@ func main() { err = executor.Execute() if err != nil { - // handle with #870 - panic(err) + fmt.Printf("Failed executing CLI command: %s, exiting...\n", err) + os.Exit(1) + } +} + +func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command { + queryCmd := &cobra.Command{ + Use: "query", + Aliases: []string{"q"}, + Short: "Querying subcommands", } + + queryCmd.AddCommand( + rpc.ValidatorCommand(), + rpc.BlockCommand(), + tx.SearchTxCmd(cdc), + tx.QueryTxCmd(cdc), + client.LineBreak, + authcmd.GetAccountCmd(storeAcc, cdc), + ) + + for _, m := range mc { + queryCmd.AddCommand(m.GetQueryCmd()) + } + + return queryCmd +} + +func txCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command { + txCmd := &cobra.Command{ + Use: "tx", + Short: "Transactions subcommands", + } + + txCmd.AddCommand( + bankcmd.SendTxCmd(cdc), + client.LineBreak, + authcmd.GetSignCommand(cdc), + bankcmd.GetBroadcastCommand(cdc), + client.LineBreak, + ) + + for _, m := range mc { + txCmd.AddCommand(m.GetTxCmd()) + } + + return txCmd } // registerRoutes registers the routes from the different modules for the LCD. @@ -105,7 +165,7 @@ func registerRoutes(rs *lcd.RestServer) { keys.RegisterRoutes(rs.Mux, rs.CliCtx.Indent) rpc.RegisterRoutes(rs.CliCtx, rs.Mux) tx.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc) - auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, "acc") + auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, storeAcc) bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) stake.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) slashing.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) diff --git a/cmd/gaia/cmd/gaiacli/query.go b/cmd/gaia/cmd/gaiacli/query.go deleted file mode 100644 index 3a806d36c262..000000000000 --- a/cmd/gaia/cmd/gaiacli/query.go +++ /dev/null @@ -1,83 +0,0 @@ -package main - -import ( - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/rpc" - "github.com/cosmos/cosmos-sdk/client/tx" - "github.com/spf13/cobra" - - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" - govcmd "github.com/cosmos/cosmos-sdk/x/gov/client/cli" - slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" - stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli" - amino "github.com/tendermint/go-amino" -) - -func queryCmd(cdc *amino.Codec) *cobra.Command { - //Add query commands - queryCmd := &cobra.Command{ - Use: "query", - Aliases: []string{"q"}, - Short: "Querying subcommands", - } - - // Group staking queries under a subcommand - stakeQueryCmd := &cobra.Command{ - Use: "stake", - Short: "Querying commands for the staking module", - } - - stakeQueryCmd.AddCommand(client.GetCommands( - stakecmd.GetCmdQueryDelegation(storeStake, cdc), - stakecmd.GetCmdQueryDelegations(storeStake, cdc), - stakecmd.GetCmdQueryUnbondingDelegation(storeStake, cdc), - stakecmd.GetCmdQueryUnbondingDelegations(storeStake, cdc), - stakecmd.GetCmdQueryRedelegation(storeStake, cdc), - stakecmd.GetCmdQueryRedelegations(storeStake, cdc), - stakecmd.GetCmdQueryValidator(storeStake, cdc), - stakecmd.GetCmdQueryValidators(storeStake, cdc), - stakecmd.GetCmdQueryValidatorDelegations(storeStake, cdc), - stakecmd.GetCmdQueryValidatorUnbondingDelegations(queryRouteStake, cdc), - stakecmd.GetCmdQueryValidatorRedelegations(queryRouteStake, cdc), - stakecmd.GetCmdQueryParams(storeStake, cdc), - stakecmd.GetCmdQueryPool(storeStake, cdc))...) - - // Group gov queries under a subcommand - govQueryCmd := &cobra.Command{ - Use: "gov", - Short: "Querying commands for the governance module", - } - - govQueryCmd.AddCommand(client.GetCommands( - govcmd.GetCmdQueryProposal(storeGov, cdc), - govcmd.GetCmdQueryProposals(storeGov, cdc), - govcmd.GetCmdQueryVote(storeGov, cdc), - govcmd.GetCmdQueryVotes(storeGov, cdc), - govcmd.GetCmdQueryParams(storeGov, cdc), - govcmd.GetCmdQueryDeposit(storeGov, cdc), - govcmd.GetCmdQueryDeposits(storeGov, cdc))...) - - // Group slashing queries under a subcommand - slashingQueryCmd := &cobra.Command{ - Use: "slashing", - Short: "Querying commands for the slashing module", - } - - slashingQueryCmd.AddCommand(client.GetCommands( - slashingcmd.GetCmdQuerySigningInfo(storeSlashing, cdc))...) - - // Query commcmmand structure - queryCmd.AddCommand( - rpc.BlockCommand(), - rpc.ValidatorCommand(), - tx.SearchTxCmd(cdc), - tx.QueryTxCmd(cdc), - client.LineBreak, - client.GetCommands(authcmd.GetAccountCmd(storeAcc, cdc, authcmd.GetAccountDecoder(cdc)))[0], - stakeQueryCmd, - govQueryCmd, - slashingQueryCmd, - ) - - return queryCmd -} diff --git a/cmd/gaia/cmd/gaiacli/tx.go b/cmd/gaia/cmd/gaiacli/tx.go deleted file mode 100644 index fa0abc4ad8c3..000000000000 --- a/cmd/gaia/cmd/gaiacli/tx.go +++ /dev/null @@ -1,83 +0,0 @@ -package main - -import ( - "github.com/cosmos/cosmos-sdk/client" - "github.com/spf13/cobra" - - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" - bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" - distrcmd "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" - govcmd "github.com/cosmos/cosmos-sdk/x/gov/client/cli" - slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" - stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli" - amino "github.com/tendermint/go-amino" -) - -func txCmd(cdc *amino.Codec) *cobra.Command { - //Add transaction generation commands - txCmd := &cobra.Command{ - Use: "tx", - Short: "Transactions subcommands", - } - - stakeTxCmd := &cobra.Command{ - Use: "stake", - Short: "Staking transaction subcommands", - } - - stakeTxCmd.AddCommand(client.PostCommands( - stakecmd.GetCmdCreateValidator(cdc), - stakecmd.GetCmdEditValidator(cdc), - stakecmd.GetCmdDelegate(cdc), - stakecmd.GetCmdRedelegate(storeStake, cdc), - stakecmd.GetCmdUnbond(storeStake, cdc), - )...) - - distTxCmd := &cobra.Command{ - Use: "dist", - Short: "Distribution transactions subcommands", - } - - distTxCmd.AddCommand(client.PostCommands( - distrcmd.GetCmdWithdrawRewards(cdc), - distrcmd.GetCmdSetWithdrawAddr(cdc), - )...) - - govTxCmd := &cobra.Command{ - Use: "gov", - Short: "Governance transactions subcommands", - } - - govTxCmd.AddCommand(client.PostCommands( - govcmd.GetCmdDeposit(cdc), - govcmd.GetCmdVote(cdc), - govcmd.GetCmdSubmitProposal(cdc), - )...) - - slashingTxCmd := &cobra.Command{ - Use: "slashing", - Short: "Slashing transactions subcommands", - } - - slashingTxCmd.AddCommand(client.PostCommands( - slashingcmd.GetCmdUnjail(cdc), - )...) - - txCmd.AddCommand( - //Add auth and bank commands - client.PostCommands( - bankcmd.SendTxCmd(cdc), - bankcmd.GetBroadcastCommand(cdc), - authcmd.GetSignCommand(cdc, authcmd.GetAccountDecoder(cdc)), - )...) - - txCmd.AddCommand( - client.LineBreak, - stakeTxCmd, - distTxCmd, - govTxCmd, - slashingTxCmd, - ) - - return txCmd -} diff --git a/cmd/gaia/init/gentx.go b/cmd/gaia/init/gentx.go index 6d66f2e30688..096097e1883e 100644 --- a/cmd/gaia/init/gentx.go +++ b/cmd/gaia/init/gentx.go @@ -2,6 +2,10 @@ package init import ( "fmt" + "io/ioutil" + "os" + "path/filepath" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/keys" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" @@ -17,9 +21,6 @@ import ( "github.com/tendermint/tendermint/crypto" tmcli "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/libs/common" - "io/ioutil" - "os" - "path/filepath" ) const ( @@ -94,7 +95,7 @@ following delegation and commission default parameters: w.Close() prepareFlagsForTxSign() - signCmd := authcmd.GetSignCommand(cdc, authcmd.GetAccountDecoder(cdc)) + signCmd := authcmd.GetSignCommand(cdc) if w, err = prepareOutputFile(config.RootDir, nodeID); err != nil { return err } diff --git a/docs/examples/basecoin/cmd/basecli/main.go b/docs/examples/basecoin/cmd/basecli/main.go index ad32bf516978..cb0eeba5b93e 100644 --- a/docs/examples/basecoin/cmd/basecli/main.go +++ b/docs/examples/basecoin/cmd/basecli/main.go @@ -8,7 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/rpc" "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/docs/examples/basecoin/app" - "github.com/cosmos/cosmos-sdk/docs/examples/basecoin/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" @@ -24,6 +24,12 @@ import ( "github.com/tendermint/tendermint/libs/cli" ) +const ( + storeAcc = "acc" + storeSlashing = "slashing" + storeStake = "stake" +) + // rootCmd is the entry point for this binary var ( rootCmd = &cobra.Command{ @@ -67,20 +73,20 @@ func main() { // add query/post commands (custom to binary) rootCmd.AddCommand( client.GetCommands( - stakecmd.GetCmdQueryValidator("stake", cdc), - stakecmd.GetCmdQueryValidators("stake", cdc), - stakecmd.GetCmdQueryValidatorUnbondingDelegations("stake", cdc), - stakecmd.GetCmdQueryValidatorRedelegations("stake", cdc), - stakecmd.GetCmdQueryDelegation("stake", cdc), - stakecmd.GetCmdQueryDelegations("stake", cdc), - stakecmd.GetCmdQueryPool("stake", cdc), - stakecmd.GetCmdQueryParams("stake", cdc), - stakecmd.GetCmdQueryUnbondingDelegation("stake", cdc), - stakecmd.GetCmdQueryUnbondingDelegations("stake", cdc), - stakecmd.GetCmdQueryRedelegation("stake", cdc), - stakecmd.GetCmdQueryRedelegations("stake", cdc), - slashingcmd.GetCmdQuerySigningInfo("slashing", cdc), - authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)), + stakecmd.GetCmdQueryValidator(storeStake, cdc), + stakecmd.GetCmdQueryValidators(storeStake, cdc), + stakecmd.GetCmdQueryValidatorUnbondingDelegations(storeStake, cdc), + stakecmd.GetCmdQueryValidatorRedelegations(storeStake, cdc), + stakecmd.GetCmdQueryDelegation(storeStake, cdc), + stakecmd.GetCmdQueryDelegations(storeStake, cdc), + stakecmd.GetCmdQueryPool(storeStake, cdc), + stakecmd.GetCmdQueryParams(storeStake, cdc), + stakecmd.GetCmdQueryUnbondingDelegation(storeStake, cdc), + stakecmd.GetCmdQueryUnbondingDelegations(storeStake, cdc), + stakecmd.GetCmdQueryRedelegation(storeStake, cdc), + stakecmd.GetCmdQueryRedelegations(storeStake, cdc), + slashingcmd.GetCmdQuerySigningInfo(storeSlashing, cdc), + authcmd.GetAccountCmd(storeAcc, cdc), )...) rootCmd.AddCommand( @@ -91,8 +97,8 @@ func main() { stakecmd.GetCmdCreateValidator(cdc), stakecmd.GetCmdEditValidator(cdc), stakecmd.GetCmdDelegate(cdc), - stakecmd.GetCmdUnbond("stake", cdc), - stakecmd.GetCmdRedelegate("stake", cdc), + stakecmd.GetCmdUnbond(storeStake, cdc), + stakecmd.GetCmdRedelegate(storeStake, cdc), slashingcmd.GetCmdUnjail(cdc), )...) @@ -118,7 +124,7 @@ func registerRoutes(rs *lcd.RestServer) { keys.RegisterRoutes(rs.Mux, rs.CliCtx.Indent) rpc.RegisterRoutes(rs.CliCtx, rs.Mux) tx.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc) - auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, "acc") + auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, storeAcc) bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) stake.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) slashing.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) diff --git a/docs/examples/democoin/cmd/democli/main.go b/docs/examples/democoin/cmd/democli/main.go index d101da2d9c38..0c37d9bd0b30 100644 --- a/docs/examples/democoin/cmd/democli/main.go +++ b/docs/examples/democoin/cmd/democli/main.go @@ -18,7 +18,6 @@ import ( bank "github.com/cosmos/cosmos-sdk/x/bank/client/rest" "github.com/cosmos/cosmos-sdk/docs/examples/democoin/app" - "github.com/cosmos/cosmos-sdk/docs/examples/democoin/types" coolcmd "github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/cool/client/cli" powcmd "github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/pow/client/cli" simplestakingcmd "github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/simplestake/client/cli" @@ -32,6 +31,7 @@ var ( Use: "democli", Short: "Democoin light-client", } + storeAcc = "acc" ) func main() { @@ -71,7 +71,7 @@ func main() { // start with commands common to basecoin rootCmd.AddCommand( client.GetCommands( - authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)), + authcmd.GetAccountCmd(storeAcc, cdc), )...) rootCmd.AddCommand( client.PostCommands( @@ -115,6 +115,6 @@ func registerRoutes(rs *lcd.RestServer) { keys.RegisterRoutes(rs.Mux, rs.CliCtx.Indent) rpc.RegisterRoutes(rs.CliCtx, rs.Mux) tx.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc) - auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, "acc") + auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, storeAcc) bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase) } diff --git a/docs/examples/democoin/x/cool/client/cli/tx.go b/docs/examples/democoin/x/cool/client/cli/tx.go index a21685a249f3..aedbd3711490 100644 --- a/docs/examples/democoin/x/cool/client/cli/tx.go +++ b/docs/examples/democoin/x/cool/client/cli/tx.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/cool" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" ) @@ -22,7 +21,7 @@ func QuizTxCmd(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) from, err := cliCtx.GetFromAddress() if err != nil { @@ -46,7 +45,7 @@ func SetTrendTxCmd(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) from, err := cliCtx.GetFromAddress() if err != nil { diff --git a/docs/examples/democoin/x/pow/client/cli/tx.go b/docs/examples/democoin/x/pow/client/cli/tx.go index 548aa9910746..a6827c3aba64 100644 --- a/docs/examples/democoin/x/pow/client/cli/tx.go +++ b/docs/examples/democoin/x/pow/client/cli/tx.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/pow" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/spf13/cobra" @@ -24,7 +23,7 @@ func MineCmd(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) from, err := cliCtx.GetFromAddress() if err != nil { diff --git a/docs/examples/democoin/x/simplestake/client/cli/commands.go b/docs/examples/democoin/x/simplestake/client/cli/commands.go index 3fe9c20c4ac7..582ea33e39a5 100644 --- a/docs/examples/democoin/x/simplestake/client/cli/commands.go +++ b/docs/examples/democoin/x/simplestake/client/cli/commands.go @@ -9,7 +9,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/docs/examples/democoin/x/simplestake" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/spf13/cobra" @@ -32,7 +31,7 @@ func BondTxCmd(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) from, err := cliCtx.GetFromAddress() if err != nil { diff --git a/types/module_clients.go b/types/module_clients.go new file mode 100644 index 000000000000..3b3a9d9a5d37 --- /dev/null +++ b/types/module_clients.go @@ -0,0 +1,11 @@ +package types + +import ( + "github.com/spf13/cobra" +) + +// ModuleClients helps modules provide a standard interface for exporting client functionality +type ModuleClients interface { + GetQueryCmd() *cobra.Command + GetTxCmd() *cobra.Command +} diff --git a/x/auth/client/cli/account.go b/x/auth/client/cli/account.go index f78b5252f2af..922b3e2db202 100644 --- a/x/auth/client/cli/account.go +++ b/x/auth/client/cli/account.go @@ -5,34 +5,17 @@ import ( "github.com/spf13/cobra" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" ) -// GetAccountCmdDefault invokes the GetAccountCmd for the auth.BaseAccount type. -func GetAccountCmdDefault(storeName string, cdc *codec.Codec) *cobra.Command { - return GetAccountCmd(storeName, cdc, GetAccountDecoder(cdc)) -} - -// GetAccountDecoder gets the account decoder for auth.DefaultAccount. -func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder { - return func(accBytes []byte) (acct auth.Account, err error) { - err = cdc.UnmarshalBinaryBare(accBytes, &acct) - if err != nil { - panic(err) - } - - return acct, err - } -} - // GetAccountCmd returns a query account that will display the state of the // account at a given address. // nolint: unparam -func GetAccountCmd(storeName string, cdc *codec.Codec, decoder auth.AccountDecoder) *cobra.Command { - return &cobra.Command{ +func GetAccountCmd(storeName string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ Use: "account [address]", Short: "Query account balance", Args: cobra.ExactArgs(1), @@ -47,9 +30,9 @@ func GetAccountCmd(storeName string, cdc *codec.Codec, decoder auth.AccountDecod cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(decoder) + WithAccountDecoder(cdc) - if err := cliCtx.EnsureAccountExistsFromAddr(key); err != nil { + if err = cliCtx.EnsureAccountExistsFromAddr(key); err != nil { return err } @@ -72,4 +55,7 @@ func GetAccountCmd(storeName string, cdc *codec.Codec, decoder auth.AccountDecod return nil }, } + + // Add the flags here and return the command + return client.GetCommands(cmd)[0] } diff --git a/x/auth/client/cli/sign.go b/x/auth/client/cli/sign.go index f4a4548d4157..9075308e1d0f 100644 --- a/x/auth/client/cli/sign.go +++ b/x/auth/client/cli/sign.go @@ -2,9 +2,10 @@ package cli import ( "fmt" + "io/ioutil" + "github.com/pkg/errors" "github.com/spf13/viper" - "io/ioutil" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" @@ -24,7 +25,7 @@ const ( ) // GetSignCommand returns the sign command -func GetSignCommand(codec *amino.Codec, decoder auth.AccountDecoder) *cobra.Command { +func GetSignCommand(codec *amino.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "sign ", Short: "Sign transactions generated offline", @@ -41,7 +42,7 @@ order. The --offline flag makes sure that the client will not reach out to the local cache. Thus account number or sequence number lookups will not be performed and it is recommended to set such parameters manually.`, - RunE: makeSignCmd(codec, decoder), + RunE: makeSignCmd(codec), Args: cobra.ExactArgs(1), } cmd.Flags().String(client.FlagName, "", "Name of private key with which to sign") @@ -51,10 +52,12 @@ recommended to set such parameters manually.`, cmd.Flags().Bool(flagValidateSigs, false, "Print the addresses that must sign the transaction, "+ "those who have already signed it, and make sure that signatures are in the correct order.") cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.") - return cmd + + // Add the flags here and return the command + return client.PostCommands(cmd)[0] } -func makeSignCmd(cdc *amino.Codec, decoder auth.AccountDecoder) func(cmd *cobra.Command, args []string) error { +func makeSignCmd(cdc *amino.Codec) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) (err error) { stdTx, err := readAndUnmarshalStdTx(cdc, args[0]) if err != nil { @@ -72,7 +75,7 @@ func makeSignCmd(cdc *amino.Codec, decoder auth.AccountDecoder) func(cmd *cobra. if name == "" { return errors.New("required flag \"name\" has not been set") } - cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(decoder) + cliCtx := context.NewCLIContext().WithCodec(cdc).WithAccountDecoder(cdc) txBldr := authtxb.NewTxBuilderFromCLI() // if --signature-only is on, then override --append diff --git a/x/auth/client/rest/query.go b/x/auth/client/rest/query.go index 0629cc939e17..e60e0fdc1d85 100644 --- a/x/auth/client/rest/query.go +++ b/x/auth/client/rest/query.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/gorilla/mux" ) @@ -17,11 +16,11 @@ import ( func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, storeName string) { r.HandleFunc( "/auth/accounts/{address}", - QueryAccountRequestHandlerFn(storeName, cdc, authcmd.GetAccountDecoder(cdc), cliCtx), + QueryAccountRequestHandlerFn(storeName, cdc, context.GetAccountDecoder(cdc), cliCtx), ).Methods("GET") r.HandleFunc( "/bank/balances/{address}", - QueryBalancesRequestHandlerFn(storeName, cdc, authcmd.GetAccountDecoder(cdc), cliCtx), + QueryBalancesRequestHandlerFn(storeName, cdc, context.GetAccountDecoder(cdc), cliCtx), ).Methods("GET") r.HandleFunc( "/tx/sign", diff --git a/x/bank/client/cli/broadcast.go b/x/bank/client/cli/broadcast.go index dd045439e0d8..126668364036 100644 --- a/x/bank/client/cli/broadcast.go +++ b/x/bank/client/cli/broadcast.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/spf13/cobra" @@ -36,7 +37,7 @@ in place of an input filename, the command reads from standard input.`, }, } - return cmd + return client.PostCommands(cmd)[0] } func readAndUnmarshalStdTx(cdc *amino.Codec, filename string) (stdTx auth.StdTx, err error) { diff --git a/x/bank/client/cli/sendtx.go b/x/bank/client/cli/sendtx.go index bae2179766b8..29a101cf73b6 100644 --- a/x/bank/client/cli/sendtx.go +++ b/x/bank/client/cli/sendtx.go @@ -1,13 +1,13 @@ package cli import ( + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" - "github.com/cosmos/cosmos-sdk/x/bank/client" + bankClient "github.com/cosmos/cosmos-sdk/x/bank/client" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -28,7 +28,7 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) if err := cliCtx.EnsureAccountExists(); err != nil { return err @@ -64,7 +64,7 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command { } // build and sign the transaction, then broadcast to Tendermint - msg := client.CreateMsg(from, to, coins) + msg := bankClient.CreateMsg(from, to, coins) if cliCtx.GenerateOnly { return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false) } @@ -78,5 +78,5 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command { cmd.MarkFlagRequired(flagTo) cmd.MarkFlagRequired(flagAmount) - return cmd + return client.PostCommands(cmd)[0] } diff --git a/x/distribution/client/cli/tx.go b/x/distribution/client/cli/tx.go index b88968cda5cd..ee82498e8273 100644 --- a/x/distribution/client/cli/tx.go +++ b/x/distribution/client/cli/tx.go @@ -6,12 +6,13 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + amino "github.com/tendermint/go-amino" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/distribution/types" @@ -22,6 +23,21 @@ var ( flagIsValidator = "is-validator" ) +// GetTxCmd returns the transaction commands for this module +func GetTxCmd(storeKey string, cdc *amino.Codec) *cobra.Command { + distTxCmd := &cobra.Command{ + Use: "dist", + Short: "Distribution transactions subcommands", + } + + distTxCmd.AddCommand(client.PostCommands( + GetCmdWithdrawRewards(cdc), + GetCmdSetWithdrawAddr(cdc), + )...) + + return distTxCmd +} + // command to withdraw rewards func GetCmdWithdrawRewards(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ @@ -41,7 +57,7 @@ func GetCmdWithdrawRewards(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) var msg sdk.Msg switch { @@ -92,7 +108,7 @@ func GetCmdSetWithdrawAddr(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) delAddr, err := cliCtx.GetFromAddress() if err != nil { diff --git a/x/distribution/client/module_client.go b/x/distribution/client/module_client.go new file mode 100644 index 000000000000..ba725e1f87ee --- /dev/null +++ b/x/distribution/client/module_client.go @@ -0,0 +1,38 @@ +package client + +import ( + "github.com/cosmos/cosmos-sdk/client" + distCmds "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" + "github.com/spf13/cobra" + amino "github.com/tendermint/go-amino" +) + +// ModuleClient exports all client functionality from this module +type ModuleClient struct { + storeKey string + cdc *amino.Codec +} + +func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { + return ModuleClient{storeKey, cdc} +} + +// GetQueryCmd returns the cli query commands for this module +func (mc ModuleClient) GetQueryCmd() *cobra.Command { + return &cobra.Command{Hidden: true} +} + +// GetTxCmd returns the transaction commands for this module +func (mc ModuleClient) GetTxCmd() *cobra.Command { + distTxCmd := &cobra.Command{ + Use: "dist", + Short: "Distribution transactions subcommands", + } + + distTxCmd.AddCommand(client.PostCommands( + distCmds.GetCmdWithdrawRewards(mc.cdc), + distCmds.GetCmdSetWithdrawAddr(mc.cdc), + )...) + + return distTxCmd +} diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go new file mode 100644 index 000000000000..227f278a91e8 --- /dev/null +++ b/x/gov/client/cli/query.go @@ -0,0 +1,324 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/gov" + govClientUtils "github.com/cosmos/cosmos-sdk/x/gov/client/utils" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// GetCmdQueryProposal implements the query proposal command. +func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "proposal", + Short: "Query details of a single proposal", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + proposalID := uint64(viper.GetInt64(flagProposalID)) + + params := gov.QueryProposalParams{ + ProposalID: proposalID, + } + + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err + } + + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposal", queryRoute), bz) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + cmd.Flags().String(flagProposalID, "", "proposalID of proposal being queried") + + return cmd +} + +// GetCmdQueryProposals implements a query proposals command. +func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "proposals", + Short: "Query proposals with optional filters", + RunE: func(cmd *cobra.Command, args []string) error { + bechDepositerAddr := viper.GetString(flagDepositer) + bechVoterAddr := viper.GetString(flagVoter) + strProposalStatus := viper.GetString(flagStatus) + numLimit := uint64(viper.GetInt64(flagNumLimit)) + + params := gov.QueryProposalsParams{ + Limit: numLimit, + } + + if len(bechDepositerAddr) != 0 { + depositerAddr, err := sdk.AccAddressFromBech32(bechDepositerAddr) + if err != nil { + return err + } + params.Depositer = depositerAddr + } + + if len(bechVoterAddr) != 0 { + voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr) + if err != nil { + return err + } + params.Voter = voterAddr + } + + if len(strProposalStatus) != 0 { + proposalStatus, err := gov.ProposalStatusFromString(govClientUtils.NormalizeProposalStatus(strProposalStatus)) + if err != nil { + return err + } + params.ProposalStatus = proposalStatus + } + + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err + } + + cliCtx := context.NewCLIContext().WithCodec(cdc) + + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposals", queryRoute), bz) + if err != nil { + return err + } + + var matchingProposals []gov.Proposal + err = cdc.UnmarshalJSON(res, &matchingProposals) + if err != nil { + return err + } + + if len(matchingProposals) == 0 { + fmt.Println("No matching proposals found") + return nil + } + + for _, proposal := range matchingProposals { + fmt.Printf(" %d - %s\n", proposal.GetProposalID(), proposal.GetTitle()) + } + + return nil + }, + } + + cmd.Flags().String(flagNumLimit, "", "(optional) limit to latest [number] proposals. Defaults to all proposals") + cmd.Flags().String(flagDepositer, "", "(optional) filter by proposals deposited on by depositer") + cmd.Flags().String(flagVoter, "", "(optional) filter by proposals voted on by voted") + cmd.Flags().String(flagStatus, "", "(optional) filter proposals by proposal status, status: deposit_period/voting_period/passed/rejected") + + return cmd +} + +// Command to Get a Proposal Information +// GetCmdQueryVote implements the query proposal vote command. +func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "vote", + Short: "Query details of a single vote", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + proposalID := uint64(viper.GetInt64(flagProposalID)) + + voterAddr, err := sdk.AccAddressFromBech32(viper.GetString(flagVoter)) + if err != nil { + return err + } + + params := gov.QueryVoteParams{ + Voter: voterAddr, + ProposalID: proposalID, + } + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err + } + + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/vote", queryRoute), bz) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + cmd.Flags().String(flagProposalID, "", "proposalID of proposal voting on") + cmd.Flags().String(flagVoter, "", "bech32 voter address") + + return cmd +} + +// GetCmdQueryVotes implements the command to query for proposal votes. +func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "votes", + Short: "Query votes on a proposal", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + proposalID := uint64(viper.GetInt64(flagProposalID)) + + params := gov.QueryVotesParams{ + ProposalID: proposalID, + } + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err + } + + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", queryRoute), bz) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + cmd.Flags().String(flagProposalID, "", "proposalID of which proposal's votes are being queried") + + return cmd +} + +// Command to Get a specific Deposit Information +// GetCmdQueryDeposit implements the query proposal deposit command. +func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "deposit", + Short: "Query details of a deposit", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + proposalID := uint64(viper.GetInt64(flagProposalID)) + + depositerAddr, err := sdk.AccAddressFromBech32(viper.GetString(flagDepositer)) + if err != nil { + return err + } + + params := gov.QueryDepositParams{ + Depositer: depositerAddr, + ProposalID: proposalID, + } + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err + } + + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", queryRoute), bz) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + cmd.Flags().String(flagProposalID, "", "proposalID of proposal deposited on") + cmd.Flags().String(flagDepositer, "", "bech32 depositer address") + + return cmd +} + +// GetCmdQueryDeposits implements the command to query for proposal deposits. +func GetCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "deposits", + Short: "Query deposits on a proposal", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + proposalID := uint64(viper.GetInt64(flagProposalID)) + + params := gov.QueryDepositsParams{ + ProposalID: proposalID, + } + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err + } + + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", queryRoute), bz) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + cmd.Flags().String(flagProposalID, "", "proposalID of which proposal's deposits are being queried") + + return cmd +} + +// GetCmdQueryTally implements the command to query for proposal tally result. +func GetCmdQueryTally(queryRoute string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "tally", + Short: "Get the tally of a proposal vote", + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + proposalID := uint64(viper.GetInt64(flagProposalID)) + + params := gov.QueryTallyParams{ + ProposalID: proposalID, + } + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err + } + + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/tally", queryRoute), bz) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + cmd.Flags().String(flagProposalID, "", "proposalID of which proposal is being tallied") + + return cmd +} + +// GetCmdQueryProposal implements the query proposal command. +func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "param [param-type]", + Short: "Query the parameters (voting|tallying|deposit) of the governance process", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + paramType := args[0] + + cliCtx := context.NewCLIContext().WithCodec(cdc) + + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/params/%s", queryRoute, paramType), nil) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + return cmd +} diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 31780f68e74d..e804863d112e 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -7,7 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/gov" @@ -15,7 +14,7 @@ import ( "io/ioutil" "strings" - "github.com/cosmos/cosmos-sdk/x/gov/client" + govClientUtils "github.com/cosmos/cosmos-sdk/x/gov/client/utils" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -80,7 +79,7 @@ $ gaiacli gov submit-proposal --title="Test Proposal" --description="My awesome txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) fromAddr, err := cliCtx.GetFromAddress() if err != nil { @@ -130,7 +129,7 @@ func parseSubmitProposalFlags() (*proposal, error) { if proposalFile == "" { proposal.Title = viper.GetString(flagTitle) proposal.Description = viper.GetString(flagDescription) - proposal.Type = client.NormalizeProposalType(viper.GetString(flagProposalType)) + proposal.Type = govClientUtils.NormalizeProposalType(viper.GetString(flagProposalType)) proposal.Deposit = viper.GetString(flagDeposit) return proposal, nil } @@ -163,7 +162,7 @@ func GetCmdDeposit(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) depositerAddr, err := cliCtx.GetFromAddress() if err != nil { @@ -208,7 +207,7 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) voterAddr, err := cliCtx.GetFromAddress() if err != nil { @@ -218,7 +217,7 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command { proposalID := uint64(viper.GetInt64(flagProposalID)) option := viper.GetString(flagOption) - byteVoteOption, err := gov.VoteOptionFromString(client.NormalizeVoteOption(option)) + byteVoteOption, err := gov.VoteOptionFromString(govClientUtils.NormalizeVoteOption(option)) if err != nil { return err } @@ -248,314 +247,3 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command { return cmd } - -// GetCmdQueryProposal implements the query proposal command. -func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "param [param-type]", - Short: "Query the parameters (voting|tallying|deposit) of the governance process", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - paramType := args[0] - - cliCtx := context.NewCLIContext().WithCodec(cdc) - - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/params/%s", queryRoute, paramType), nil) - if err != nil { - return err - } - - fmt.Println(string(res)) - return nil - }, - } - - return cmd -} - -// GetCmdQueryProposal implements the query proposal command. -func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "proposal", - Short: "Query details of a single proposal", - RunE: func(cmd *cobra.Command, args []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := uint64(viper.GetInt64(flagProposalID)) - - params := gov.QueryProposalParams{ - ProposalID: proposalID, - } - - bz, err := cdc.MarshalJSON(params) - if err != nil { - return err - } - - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposal", queryRoute), bz) - if err != nil { - return err - } - - fmt.Println(string(res)) - return nil - }, - } - - cmd.Flags().String(flagProposalID, "", "proposalID of proposal being queried") - - return cmd -} - -// GetCmdQueryProposals implements a query proposals command. -func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "proposals", - Short: "Query proposals with optional filters", - RunE: func(cmd *cobra.Command, args []string) error { - bechDepositerAddr := viper.GetString(flagDepositer) - bechVoterAddr := viper.GetString(flagVoter) - strProposalStatus := viper.GetString(flagStatus) - numLimit := uint64(viper.GetInt64(flagNumLimit)) - - params := gov.QueryProposalsParams{ - Limit: numLimit, - } - - if len(bechDepositerAddr) != 0 { - depositerAddr, err := sdk.AccAddressFromBech32(bechDepositerAddr) - if err != nil { - return err - } - params.Depositer = depositerAddr - } - - if len(bechVoterAddr) != 0 { - voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr) - if err != nil { - return err - } - params.Voter = voterAddr - } - - if len(strProposalStatus) != 0 { - proposalStatus, err := gov.ProposalStatusFromString(client.NormalizeProposalStatus(strProposalStatus)) - if err != nil { - return err - } - params.ProposalStatus = proposalStatus - } - - bz, err := cdc.MarshalJSON(params) - if err != nil { - return err - } - - cliCtx := context.NewCLIContext().WithCodec(cdc) - - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposals", queryRoute), bz) - if err != nil { - return err - } - - var matchingProposals []gov.Proposal - err = cdc.UnmarshalJSON(res, &matchingProposals) - if err != nil { - return err - } - - if len(matchingProposals) == 0 { - fmt.Println("No matching proposals found") - return nil - } - - for _, proposal := range matchingProposals { - fmt.Printf(" %d - %s\n", proposal.GetProposalID(), proposal.GetTitle()) - } - - return nil - }, - } - - cmd.Flags().String(flagNumLimit, "", "(optional) limit to latest [number] proposals. Defaults to all proposals") - cmd.Flags().String(flagDepositer, "", "(optional) filter by proposals deposited on by depositer") - cmd.Flags().String(flagVoter, "", "(optional) filter by proposals voted on by voted") - cmd.Flags().String(flagStatus, "", "(optional) filter proposals by proposal status, status: deposit_period/voting_period/passed/rejected") - - return cmd -} - -// Command to Get a Proposal Information -// GetCmdQueryVote implements the query proposal vote command. -func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "vote", - Short: "Query details of a single vote", - RunE: func(cmd *cobra.Command, args []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := uint64(viper.GetInt64(flagProposalID)) - - voterAddr, err := sdk.AccAddressFromBech32(viper.GetString(flagVoter)) - if err != nil { - return err - } - - params := gov.QueryVoteParams{ - Voter: voterAddr, - ProposalID: proposalID, - } - bz, err := cdc.MarshalJSON(params) - if err != nil { - return err - } - - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/vote", queryRoute), bz) - if err != nil { - return err - } - - fmt.Println(string(res)) - return nil - }, - } - - cmd.Flags().String(flagProposalID, "", "proposalID of proposal voting on") - cmd.Flags().String(flagVoter, "", "bech32 voter address") - - return cmd -} - -// GetCmdQueryVotes implements the command to query for proposal votes. -func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "votes", - Short: "Query votes on a proposal", - RunE: func(cmd *cobra.Command, args []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := uint64(viper.GetInt64(flagProposalID)) - - params := gov.QueryVotesParams{ - ProposalID: proposalID, - } - bz, err := cdc.MarshalJSON(params) - if err != nil { - return err - } - - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", queryRoute), bz) - if err != nil { - return err - } - - fmt.Println(string(res)) - return nil - }, - } - - cmd.Flags().String(flagProposalID, "", "proposalID of which proposal's votes are being queried") - - return cmd -} - -// Command to Get a specific Deposit Information -// GetCmdQueryDeposit implements the query proposal deposit command. -func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "deposit", - Short: "Query details of a deposit", - RunE: func(cmd *cobra.Command, args []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := uint64(viper.GetInt64(flagProposalID)) - - depositerAddr, err := sdk.AccAddressFromBech32(viper.GetString(flagDepositer)) - if err != nil { - return err - } - - params := gov.QueryDepositParams{ - Depositer: depositerAddr, - ProposalID: proposalID, - } - bz, err := cdc.MarshalJSON(params) - if err != nil { - return err - } - - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", queryRoute), bz) - if err != nil { - return err - } - - fmt.Println(string(res)) - return nil - }, - } - - cmd.Flags().String(flagProposalID, "", "proposalID of proposal deposited on") - cmd.Flags().String(flagDepositer, "", "bech32 depositer address") - - return cmd -} - -// GetCmdQueryDeposits implements the command to query for proposal deposits. -func GetCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "deposits", - Short: "Query deposits on a proposal", - RunE: func(cmd *cobra.Command, args []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := uint64(viper.GetInt64(flagProposalID)) - - params := gov.QueryDepositsParams{ - ProposalID: proposalID, - } - bz, err := cdc.MarshalJSON(params) - if err != nil { - return err - } - - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", queryRoute), bz) - if err != nil { - return err - } - - fmt.Println(string(res)) - return nil - }, - } - - cmd.Flags().String(flagProposalID, "", "proposalID of which proposal's deposits are being queried") - - return cmd -} - -// GetCmdQueryTally implements the command to query for proposal tally result. -func GetCmdQueryTally(queryRoute string, cdc *codec.Codec) *cobra.Command { - cmd := &cobra.Command{ - Use: "tally", - Short: "Get the tally of a proposal vote", - RunE: func(cmd *cobra.Command, args []string) error { - cliCtx := context.NewCLIContext().WithCodec(cdc) - proposalID := uint64(viper.GetInt64(flagProposalID)) - - params := gov.QueryTallyParams{ - ProposalID: proposalID, - } - bz, err := cdc.MarshalJSON(params) - if err != nil { - return err - } - - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/tally", queryRoute), bz) - if err != nil { - return err - } - - fmt.Println(string(res)) - return nil - }, - } - - cmd.Flags().String(flagProposalID, "", "proposalID of which proposal is being tallied") - - return cmd -} diff --git a/x/gov/client/module_client.go b/x/gov/client/module_client.go new file mode 100644 index 000000000000..7cabc6884b3c --- /dev/null +++ b/x/gov/client/module_client.go @@ -0,0 +1,55 @@ +package client + +import ( + "github.com/cosmos/cosmos-sdk/client" + govCli "github.com/cosmos/cosmos-sdk/x/gov/client/cli" + "github.com/spf13/cobra" + amino "github.com/tendermint/go-amino" +) + +// ModuleClient exports all client functionality from this module +type ModuleClient struct { + storeKey string + cdc *amino.Codec +} + +func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { + return ModuleClient{storeKey, cdc} +} + +// GetQueryCmd returns the cli query commands for this module +func (mc ModuleClient) GetQueryCmd() *cobra.Command { + // Group gov queries under a subcommand + govQueryCmd := &cobra.Command{ + Use: "gov", + Short: "Querying commands for the governance module", + } + + govQueryCmd.AddCommand(client.GetCommands( + govCli.GetCmdQueryProposal(mc.storeKey, mc.cdc), + govCli.GetCmdQueryProposals(mc.storeKey, mc.cdc), + govCli.GetCmdQueryVote(mc.storeKey, mc.cdc), + govCli.GetCmdQueryVotes(mc.storeKey, mc.cdc), + govCli.GetCmdQueryParams(mc.storeKey, mc.cdc), + govCli.GetCmdQueryDeposit(mc.storeKey, mc.cdc), + govCli.GetCmdQueryDeposits(mc.storeKey, mc.cdc), + govCli.GetCmdQueryTally(mc.storeKey, mc.cdc))...) + + return govQueryCmd +} + +// GetTxCmd returns the transaction commands for this module +func (mc ModuleClient) GetTxCmd() *cobra.Command { + govTxCmd := &cobra.Command{ + Use: "gov", + Short: "Governance transactions subcommands", + } + + govTxCmd.AddCommand(client.PostCommands( + govCli.GetCmdDeposit(mc.cdc), + govCli.GetCmdVote(mc.cdc), + govCli.GetCmdSubmitProposal(mc.cdc), + )...) + + return govTxCmd +} diff --git a/x/gov/client/rest/rest.go b/x/gov/client/rest/rest.go index 930a39b025c7..c000a34d871c 100644 --- a/x/gov/client/rest/rest.go +++ b/x/gov/client/rest/rest.go @@ -10,7 +10,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/gov" - "github.com/cosmos/cosmos-sdk/x/gov/client" + govClientUtils "github.com/cosmos/cosmos-sdk/x/gov/client/utils" "github.com/gorilla/mux" "github.com/pkg/errors" ) @@ -81,7 +81,7 @@ func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han return } - proposalType, err := gov.ProposalTypeFromString(client.NormalizeProposalType(req.ProposalType)) + proposalType, err := gov.ProposalTypeFromString(govClientUtils.NormalizeProposalType(req.ProposalType)) if err != nil { utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return @@ -165,7 +165,7 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc return } - voteOption, err := gov.VoteOptionFromString(client.NormalizeVoteOption(req.Option)) + voteOption, err := gov.VoteOptionFromString(govClientUtils.NormalizeVoteOption(req.Option)) if err != nil { utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return @@ -460,7 +460,7 @@ func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext) } if len(strProposalStatus) != 0 { - proposalStatus, err := gov.ProposalStatusFromString(client.NormalizeProposalStatus(strProposalStatus)) + proposalStatus, err := gov.ProposalStatusFromString(govClientUtils.NormalizeProposalStatus(strProposalStatus)) if err != nil { utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) return diff --git a/x/gov/client/utils.go b/x/gov/client/utils/utils.go similarity index 98% rename from x/gov/client/utils.go rename to x/gov/client/utils/utils.go index 013f3944a310..e91d7005ceaf 100644 --- a/x/gov/client/utils.go +++ b/x/gov/client/utils/utils.go @@ -1,4 +1,4 @@ -package client +package utils // NormalizeVoteOption - normalize user specified vote option func NormalizeVoteOption(option string) string { diff --git a/x/ibc/client/cli/ibctx.go b/x/ibc/client/cli/ibctx.go index be6dfc940da7..e8b107d9afbd 100644 --- a/x/ibc/client/cli/ibctx.go +++ b/x/ibc/client/cli/ibctx.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/utils" codec "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/ibc" @@ -30,7 +29,7 @@ func IBCTransferCmd(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) from, err := cliCtx.GetFromAddress() if err != nil { diff --git a/x/ibc/client/cli/relay.go b/x/ibc/client/cli/relay.go index 43ad783f4935..217d7cdee8e9 100644 --- a/x/ibc/client/cli/relay.go +++ b/x/ibc/client/cli/relay.go @@ -9,7 +9,6 @@ import ( codec "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/ibc" @@ -42,7 +41,7 @@ type relayCommander struct { func IBCRelayCmd(cdc *codec.Codec) *cobra.Command { cmdr := relayCommander{ cdc: cdc, - decoder: authcmd.GetAccountDecoder(cdc), + decoder: context.GetAccountDecoder(cdc), ibcStore: "ibc", mainStore: "main", accStore: "acc", diff --git a/x/slashing/client/cli/tx.go b/x/slashing/client/cli/tx.go index f70be1871218..7124b3544eeb 100644 --- a/x/slashing/client/cli/tx.go +++ b/x/slashing/client/cli/tx.go @@ -5,7 +5,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/slashing" @@ -22,7 +21,7 @@ func GetCmdUnjail(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) valAddr, err := cliCtx.GetFromAddress() if err != nil { diff --git a/x/slashing/client/module_client.go b/x/slashing/client/module_client.go new file mode 100644 index 000000000000..82efb5afe0cf --- /dev/null +++ b/x/slashing/client/module_client.go @@ -0,0 +1,47 @@ +package client + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" + "github.com/spf13/cobra" + amino "github.com/tendermint/go-amino" +) + +// ModuleClient exports all client functionality from this module +type ModuleClient struct { + storeKey string + cdc *amino.Codec +} + +func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { + return ModuleClient{storeKey, cdc} +} + +// GetQueryCmd returns the cli query commands for this module +func (mc ModuleClient) GetQueryCmd() *cobra.Command { + // Group slashing queries under a subcommand + slashingQueryCmd := &cobra.Command{ + Use: "slashing", + Short: "Querying commands for the slashing module", + } + + slashingQueryCmd.AddCommand(client.GetCommands( + cli.GetCmdQuerySigningInfo(mc.storeKey, mc.cdc))...) + + return slashingQueryCmd + +} + +// GetTxCmd returns the transaction commands for this module +func (mc ModuleClient) GetTxCmd() *cobra.Command { + slashingTxCmd := &cobra.Command{ + Use: "slashing", + Short: "Slashing transactions subcommands", + } + + slashingTxCmd.AddCommand(client.PostCommands( + cli.GetCmdUnjail(mc.cdc), + )...) + + return slashingTxCmd +} diff --git a/x/stake/client/cli/query.go b/x/stake/client/cli/query.go index 24e44999606b..40598fcf8000 100644 --- a/x/stake/client/cli/query.go +++ b/x/stake/client/cli/query.go @@ -115,7 +115,7 @@ func GetCmdQueryValidators(storeName string, cdc *codec.Codec) *cobra.Command { } // GetCmdQueryValidatorUnbondingDelegations implements the query all unbonding delegatations from a validator command. -func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryValidatorUnbondingDelegations(storeKey string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "unbonding-delegations-from [operator-addr]", Short: "Query all unbonding delegatations from a validator", @@ -135,7 +135,7 @@ func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Code } res, err := cliCtx.QueryWithData( - fmt.Sprintf("custom/%s/validatorUnbondingDelegations", queryRoute), + fmt.Sprintf("custom/%s/validatorUnbondingDelegations", storeKey), bz) if err != nil { return err @@ -150,7 +150,7 @@ func GetCmdQueryValidatorUnbondingDelegations(queryRoute string, cdc *codec.Code } // GetCmdQueryValidatorRedelegations implements the query all redelegatations from a validator command. -func GetCmdQueryValidatorRedelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryValidatorRedelegations(storeKey string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "redelegations-from [operator-addr]", Short: "Query all outgoing redelegatations from a validator", @@ -170,7 +170,7 @@ func GetCmdQueryValidatorRedelegations(queryRoute string, cdc *codec.Codec) *cob } res, err := cliCtx.QueryWithData( - fmt.Sprintf("custom/%s/validatorRedelegations", queryRoute), + fmt.Sprintf("custom/%s/validatorRedelegations", storeKey), bz) if err != nil { return err @@ -288,7 +288,7 @@ func GetCmdQueryDelegations(storeName string, cdc *codec.Codec) *cobra.Command { // GetCmdQueryValidatorDelegations implements the command to query all the // delegations to a specific validator. -func GetCmdQueryValidatorDelegations(queryRoute string, cdc *codec.Codec) *cobra.Command { +func GetCmdQueryValidatorDelegations(storeKey string, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "delegations-to [validator-addr]", Short: "Query all delegations made to one validator", @@ -308,7 +308,7 @@ func GetCmdQueryValidatorDelegations(queryRoute string, cdc *codec.Codec) *cobra cliCtx := context.NewCLIContext().WithCodec(cdc) - res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validatorDelegations", queryRoute), bz) + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validatorDelegations", storeKey), bz) if err != nil { return err } diff --git a/x/stake/client/cli/tx.go b/x/stake/client/cli/tx.go index a42fcf6d027a..09b235abb1cd 100644 --- a/x/stake/client/cli/tx.go +++ b/x/stake/client/cli/tx.go @@ -8,7 +8,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder" "github.com/cosmos/cosmos-sdk/x/stake" @@ -25,7 +24,7 @@ func GetCmdCreateValidator(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) amounstStr := viper.GetString(FlagAmount) if amounstStr == "" { @@ -126,7 +125,7 @@ func GetCmdEditValidator(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) valAddr, err := cliCtx.GetFromAddress() if err != nil { @@ -178,7 +177,7 @@ func GetCmdDelegate(cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) amount, err := sdk.ParseCoin(viper.GetString(FlagAmount)) if err != nil { @@ -220,7 +219,7 @@ func GetCmdRedelegate(storeName string, cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) var err error @@ -275,7 +274,7 @@ func GetCmdUnbond(storeName string, cdc *codec.Codec) *cobra.Command { txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) delAddr, err := cliCtx.GetFromAddress() if err != nil { diff --git a/x/stake/client/cli/utils.go b/x/stake/client/cli/utils.go index 9aca2d8995bf..502cb11ec6ee 100644 --- a/x/stake/client/cli/utils.go +++ b/x/stake/client/cli/utils.go @@ -4,7 +4,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" "github.com/cosmos/cosmos-sdk/x/stake" "github.com/cosmos/cosmos-sdk/x/stake/types" "github.com/pkg/errors" @@ -45,7 +44,7 @@ func getShares( key := stake.GetDelegationKey(delAddr, valAddr) cliCtx := context.NewCLIContext(). WithCodec(cdc). - WithAccountDecoder(authcmd.GetAccountDecoder(cdc)) + WithAccountDecoder(cdc) resQuery, err := cliCtx.QueryStore(key, storeName) if err != nil { diff --git a/x/stake/client/module_client.go b/x/stake/client/module_client.go new file mode 100644 index 000000000000..5a08668dc0d3 --- /dev/null +++ b/x/stake/client/module_client.go @@ -0,0 +1,61 @@ +package client + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/x/stake/client/cli" + "github.com/spf13/cobra" + amino "github.com/tendermint/go-amino" +) + +// ModuleClient exports all client functionality from this module +type ModuleClient struct { + storeKey string + cdc *amino.Codec +} + +func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient { + return ModuleClient{storeKey, cdc} +} + +// GetQueryCmd returns the cli query commands for this module +func (mc ModuleClient) GetQueryCmd() *cobra.Command { + stakeQueryCmd := &cobra.Command{ + Use: "stake", + Short: "Querying commands for the staking module", + } + stakeQueryCmd.AddCommand(client.GetCommands( + cli.GetCmdQueryDelegation(mc.storeKey, mc.cdc), + cli.GetCmdQueryDelegations(mc.storeKey, mc.cdc), + cli.GetCmdQueryUnbondingDelegation(mc.storeKey, mc.cdc), + cli.GetCmdQueryUnbondingDelegations(mc.storeKey, mc.cdc), + cli.GetCmdQueryRedelegation(mc.storeKey, mc.cdc), + cli.GetCmdQueryRedelegations(mc.storeKey, mc.cdc), + cli.GetCmdQueryValidator(mc.storeKey, mc.cdc), + cli.GetCmdQueryValidators(mc.storeKey, mc.cdc), + cli.GetCmdQueryValidatorDelegations(mc.storeKey, mc.cdc), + cli.GetCmdQueryValidatorUnbondingDelegations(mc.storeKey, mc.cdc), + cli.GetCmdQueryValidatorRedelegations(mc.storeKey, mc.cdc), + cli.GetCmdQueryParams(mc.storeKey, mc.cdc), + cli.GetCmdQueryPool(mc.storeKey, mc.cdc))...) + + return stakeQueryCmd + +} + +// GetTxCmd returns the transaction commands for this module +func (mc ModuleClient) GetTxCmd() *cobra.Command { + stakeTxCmd := &cobra.Command{ + Use: "stake", + Short: "Staking transaction subcommands", + } + + stakeTxCmd.AddCommand(client.PostCommands( + cli.GetCmdCreateValidator(mc.cdc), + cli.GetCmdEditValidator(mc.cdc), + cli.GetCmdDelegate(mc.cdc), + cli.GetCmdRedelegate(mc.storeKey, mc.cdc), + cli.GetCmdUnbond(mc.storeKey, mc.cdc), + )...) + + return stakeTxCmd +}