Skip to content

Commit

Permalink
Move sendtx and query account commands into x/bank
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey authored and rigelrozanski committed Mar 1, 2018
1 parent 00304dd commit 356baf6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 48 deletions.
33 changes: 33 additions & 0 deletions client/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package client

import "github.com/spf13/cobra"

const (
FlagChainID = "chain-id"
FlagNode = "node"
FlagHeight = "height"
FlagTrustNode = "trust-node"
FlagName = "name"
)

// GetCommands adds common flags to query commands
func GetCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
// TODO: make this default false when we support proofs
c.Flags().Bool(FlagTrustNode, true, "Don't verify proofs for responses")
c.Flags().String(FlagChainID, "", "Chain ID of tendermint node")
c.Flags().String(FlagNode, "", "<host>:<port> to tendermint rpc interface for this chain")
c.Flags().Int64(FlagHeight, 0, "block height to query, omit to get most recent provable block")
}
return cmds
}

// PostCommands adds common flags for commands to post tx
func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
c.Flags().String(FlagName, "", "Name of private key with which to sign")
c.Flags().String(FlagChainID, "", "Chain ID of tendermint node")
c.Flags().String(FlagNode, "", "<host>:<port> to tendermint rpc interface for this chain")
}
return cmds
}
45 changes: 9 additions & 36 deletions examples/basecoin/cmd/basecli/client.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package main

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"

const (
// these are needed for every init
flagChainID = "chain-id"
flagNode = "node"
"github.com/cosmos/cosmos-sdk/client"
)

const (
// one of the following should be provided to verify the connection
flagGenesis = "genesis"
flagCommit = "commit"
flagValHash = "validator-set"

flagHeight = "height"
flagSelect = "select"
flagTags = "tag"
flagAny = "any"

flagBind = "bind"
flagCORS = "cors"
flagTrustNode = "trust-node"

// this is for signing
flagName = "name"
flagBind = "bind"
flagCORS = "cors"
)

var (
Expand All @@ -49,36 +44,14 @@ func AddClientCommands(cmd *cobra.Command) {
)
}

// GetCommands adds common flags to query commands
func GetCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
// TODO: make this default false when we support proofs
c.Flags().Bool(flagTrustNode, true, "Don't verify proofs for responses")
c.Flags().String(flagChainID, "", "Chain ID of tendermint node")
c.Flags().String(flagNode, "", "<host>:<port> to tendermint rpc interface for this chain")
c.Flags().Int64(flagHeight, 0, "block height to query, omit to get most recent provable block")
}
return cmds
}

// PostCommands adds common flags for commands to post tx
func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
c.Flags().String(flagName, "", "Name of private key with which to sign")
c.Flags().String(flagChainID, "", "Chain ID of tendermint node")
c.Flags().String(flagNode, "", "<host>:<port> to tendermint rpc interface for this chain")
}
return cmds
}

func initClientCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize light client",
RunE: todoNotImplemented,
}
cmd.Flags().StringP(flagChainID, "c", "", "ID of chain we connect to")
cmd.Flags().StringP(flagNode, "n", "tcp://localhost:46657", "Node to connect to")
cmd.Flags().StringP(client.FlagChainID, "c", "", "ID of chain we connect to")
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")
cmd.Flags().String(flagGenesis, "", "Genesis file to verify header validity")
cmd.Flags().String(flagCommit, "", "File with trusted and signed header")
cmd.Flags().String(flagValHash, "", "Hash of trusted validator set (hex-encoded)")
Expand Down
6 changes: 4 additions & 2 deletions examples/basecoin/cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

"github.com/tendermint/tmlibs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/version"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands"
)

// gaiacliCmd is the entry point for this binary
Expand All @@ -35,9 +37,9 @@ func main() {

// query/post commands (custom to binary)
basecliCmd.AddCommand(
GetCommands(getAccountCmd())...)
client.GetCommands(bankcmd.GetAccountCmd())...)
basecliCmd.AddCommand(
PostCommands(postSendCommand())...)
client.PostCommands(bankcmd.SendTxCommand())...)

// add proxy, version and key info
basecliCmd.AddCommand(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package commands

import (
"encoding/hex"
Expand All @@ -17,7 +17,9 @@ import (
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
)

func getAccountCmd() *cobra.Command {
// GetAccountCmd returns a query account that will display the
// state of the account at a given address
func GetAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "account <address>",
Short: "Query account balance",
Expand All @@ -42,16 +44,15 @@ func getAccount(cmd *cobra.Command, args []string) error {
// TODO: make the store name a variable in getAccountCmd?
path := "/main/key"

uri := viper.GetString(flagNode)
uri := viper.GetString(client.FlagNode)
if uri == "" {
return errors.New("Must define which node to query with --node")
}
node := client.GetNode(uri)

opts := rpcclient.ABCIQueryOptions{
Height: viper.GetInt64(flagHeight),
// Trusted: viper.GetBool(flagTrustNode),
Trusted: true,
Height: viper.GetInt64(client.FlagHeight),
Trusted: viper.GetBool(client.FlagTrustNode),
}
result, err := node.ABCIQueryWithOptions(path, key, opts)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package commands

import (
"encoding/hex"
Expand All @@ -23,7 +23,8 @@ const (
flagSequence = "seq"
)

func postSendCommand() *cobra.Command {
// SendTxCommand will create a send tx and sign it with the given key
func SendTxCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "send",
Short: "Create and sign a send tx",
Expand All @@ -42,7 +43,7 @@ func sendTx(cmd *cobra.Command, args []string) error {
return err
}

uri := viper.GetString(flagNode)
uri := viper.GetString(client.FlagNode)
if uri == "" {
return errors.New("Must define which node to query with --node")
}
Expand Down Expand Up @@ -75,7 +76,7 @@ func buildTx() ([]byte, error) {
return nil, err
}

name := viper.GetString(flagName)
name := viper.GetString(client.FlagName)
info, err := keybase.Get(name)
if err != nil {
return nil, errors.WithMessage(err, "No key for name")
Expand Down

0 comments on commit 356baf6

Please sign in to comment.