basecli refactor
This commit is contained in:
parent
94948746a0
commit
65f27f2daa
@ -2,6 +2,7 @@ package client
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
// nolint
|
||||
const (
|
||||
FlagChainID = "chain-id"
|
||||
FlagNode = "node"
|
||||
|
||||
71
client/helpers.go
Normal file
71
client/helpers.go
Normal file
@ -0,0 +1,71 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
)
|
||||
|
||||
// GetNode prepares a simple rpc.Client from the flags
|
||||
func GetNode() (rpcclient.Client, error) {
|
||||
uri := viper.GetString(FlagNode)
|
||||
if uri == "" {
|
||||
return nil, errors.New("Must define node using --node")
|
||||
}
|
||||
return rpcclient.NewHTTP(uri, "/websocket"), nil
|
||||
}
|
||||
|
||||
// Broadcast the transaction bytes to Tendermint
|
||||
func BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
|
||||
|
||||
node, err := GetNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := node.BroadcastTxCommit(tx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
if res.CheckTx.Code != uint32(0) {
|
||||
return res, errors.Errorf("CheckTx failed: (%d) %s",
|
||||
res.CheckTx.Code,
|
||||
res.CheckTx.Log)
|
||||
}
|
||||
if res.DeliverTx.Code != uint32(0) {
|
||||
return res, errors.Errorf("DeliverTx failed: (%d) %s",
|
||||
res.DeliverTx.Code,
|
||||
res.DeliverTx.Log)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Query from Tendermint with the provided key and storename
|
||||
func Query(key cmn.HexBytes, storeName string) (res []byte, err error) {
|
||||
|
||||
path := fmt.Sprintf("/%s/key", storeName)
|
||||
node, err := GetNode()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
opts := rpcclient.ABCIQueryOptions{
|
||||
Height: viper.GetInt64(FlagHeight),
|
||||
Trusted: viper.GetBool(FlagTrustNode),
|
||||
}
|
||||
result, err := node.ABCIQueryWithOptions(path, key, opts)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
resp := result.Response
|
||||
if resp.Code != uint32(0) {
|
||||
return res, errors.Errorf("Query failed: (%d) %s", resp.Code, resp.Log)
|
||||
}
|
||||
return resp.Value, nil
|
||||
}
|
||||
@ -102,7 +102,8 @@ func printCreate(info keys.Info, seed string) {
|
||||
// print seed unless requested not to.
|
||||
if !viper.GetBool(flagNoBackup) {
|
||||
fmt.Println("**Important** write this seed phrase in a safe place.")
|
||||
fmt.Println("It is the only way to recover your account if you ever forget your password.\n")
|
||||
fmt.Println("It is the only way to recover your account if you ever forget your password.")
|
||||
fmt.Println()
|
||||
fmt.Println(seed)
|
||||
}
|
||||
case "json":
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
package client
|
||||
|
||||
import rpcclient "github.com/tendermint/tendermint/rpc/client"
|
||||
|
||||
// GetNode prepares a simple rpc.Client from the flags
|
||||
func GetNode(uri string) rpcclient.Client {
|
||||
return rpcclient.NewHTTP(uri, "/websocket")
|
||||
}
|
||||
@ -5,7 +5,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
tmwire "github.com/tendermint/tendermint/wire"
|
||||
@ -43,8 +42,10 @@ func getBlock(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
// get the node
|
||||
uri := viper.GetString(client.FlagNode)
|
||||
node := client.GetNode(uri)
|
||||
node, err := client.GetNode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: actually honor the --select flag!
|
||||
// header -> BlockchainInfo
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
tmwire "github.com/tendermint/tendermint/wire"
|
||||
@ -22,8 +21,10 @@ func statusCommand() *cobra.Command {
|
||||
|
||||
func checkStatus(cmd *cobra.Command, args []string) error {
|
||||
// get the node
|
||||
uri := viper.GetString(client.FlagNode)
|
||||
node := client.GetNode(uri)
|
||||
node, err := client.GetNode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := node.Status()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@ -5,7 +5,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
tmwire "github.com/tendermint/tendermint/wire"
|
||||
@ -38,8 +37,10 @@ func getValidators(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
// get the node
|
||||
uri := viper.GetString(client.FlagNode)
|
||||
node := client.GetNode(uri)
|
||||
node, err := client.GetNode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := node.Validators(height)
|
||||
if err != nil {
|
||||
|
||||
@ -41,11 +41,10 @@ func searchTx(cmd *cobra.Command, args []string) error {
|
||||
query := strings.Join(tags, " AND ")
|
||||
|
||||
// get the node
|
||||
uri := viper.GetString(client.FlagNode)
|
||||
if uri == "" {
|
||||
return errors.New("Must define which node to query with --node")
|
||||
node, err := client.GetNode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
node := client.GetNode(uri)
|
||||
|
||||
prove := !viper.GetBool(client.FlagTrustNode)
|
||||
res, err := node.TxSearch(query, prove)
|
||||
|
||||
@ -28,6 +28,7 @@ func txCommand() *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
// command to query for a transaction
|
||||
func queryTx(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 || len(args[0]) == 0 {
|
||||
return errors.New("You must provide a tx hash")
|
||||
@ -41,11 +42,10 @@ func queryTx(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
// get the node
|
||||
uri := viper.GetString(client.FlagNode)
|
||||
if uri == "" {
|
||||
return errors.New("Must define which node to query with --node")
|
||||
node, err := client.GetNode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
node := client.GetNode(uri)
|
||||
prove := !viper.GetBool(client.FlagTrustNode)
|
||||
|
||||
res, err := node.Tx(hash, prove)
|
||||
|
||||
@ -178,5 +178,5 @@ func GetGenesisJSON(pubkey, chainID, denom, addr string, options string) string
|
||||
"plugin_options": [
|
||||
"coin/issuer", {"app": "sigs", "addr": "%s"}%s
|
||||
]
|
||||
}`, chainID, pubkey, addr, denom, addr, options)
|
||||
}`, addr, denom, addr, options)
|
||||
}
|
||||
|
||||
@ -7,33 +7,37 @@ import (
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/examples/basecoin/app" // XXX: not good
|
||||
"github.com/cosmos/cosmos-sdk/examples/basecoin/types" // XXX: not good
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||
)
|
||||
|
||||
// GetAccountCmd returns a query account that will display the
|
||||
// state of the account at a given address
|
||||
func GetAccountCmd(storeName string) *cobra.Command {
|
||||
cmd := acctCmd{storeName}
|
||||
|
||||
return &cobra.Command{
|
||||
Use: "account <address>",
|
||||
Short: "Query account balance",
|
||||
RunE: cmd.get,
|
||||
RunE: newRunner(storeName).cmd,
|
||||
}
|
||||
}
|
||||
|
||||
type acctCmd struct {
|
||||
type runner struct {
|
||||
storeName string
|
||||
}
|
||||
|
||||
func (a acctCmd) get(cmd *cobra.Command, args []string) error {
|
||||
func newRunner(storeName string) runner {
|
||||
return runner{
|
||||
storeName: storeName,
|
||||
}
|
||||
}
|
||||
|
||||
func (r runner) cmd(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 || len(args[0]) == 0 {
|
||||
return errors.New("You must provide an account name")
|
||||
}
|
||||
@ -45,41 +49,25 @@ func (a acctCmd) get(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
key := crypto.Address(bz)
|
||||
path := fmt.Sprintf("/%s/key", a.storeName)
|
||||
|
||||
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(client.FlagHeight),
|
||||
Trusted: viper.GetBool(client.FlagTrustNode),
|
||||
}
|
||||
result, err := node.ABCIQueryWithOptions(path, key, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp := result.Response
|
||||
if resp.Code != uint32(0) {
|
||||
return errors.Errorf("Query failed: (%d) %s", resp.Code, resp.Log)
|
||||
}
|
||||
res, err := client.Query(key, r.storeName)
|
||||
|
||||
// parse out the value
|
||||
acct := new(types.AppAccount)
|
||||
cdc := app.MakeTxCodec()
|
||||
err = cdc.UnmarshalBinary(resp.Value, acct)
|
||||
cdc := wire.NewCodec()
|
||||
bank.RegisterWire(cdc)
|
||||
|
||||
err = cdc.UnmarshalBinary(res, acct)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// print out whole account or just coins?
|
||||
// print out whole account
|
||||
output, err := json.MarshalIndent(acct, "", " ")
|
||||
// output, err := json.MarshalIndent(acct.BaseAccount.Coins, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(output))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -8,12 +8,14 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/keys"
|
||||
"github.com/cosmos/cosmos-sdk/examples/basecoin/app" // XXX: not good
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -43,29 +45,12 @@ func sendTx(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
uri := viper.GetString(client.FlagNode)
|
||||
if uri == "" {
|
||||
return errors.New("Must define which node to query with --node")
|
||||
}
|
||||
node := client.GetNode(uri)
|
||||
|
||||
result, err := node.BroadcastTxCommit(txBytes)
|
||||
res, err := client.BroadcastTx(txBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result.CheckTx.Code != uint32(0) {
|
||||
return errors.Errorf("CheckTx failed: (%d) %s",
|
||||
result.CheckTx.Code,
|
||||
result.CheckTx.Log)
|
||||
}
|
||||
if result.DeliverTx.Code != uint32(0) {
|
||||
return errors.Errorf("DeliverTx failed: (%d) %s",
|
||||
result.DeliverTx.Code,
|
||||
result.DeliverTx.Log)
|
||||
}
|
||||
|
||||
fmt.Printf("Committed at block %d. Hash: %s\n", result.Height, result.Hash.String())
|
||||
fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -107,7 +92,9 @@ func buildTx() ([]byte, error) {
|
||||
|
||||
// marshal bytes
|
||||
tx := sdk.NewStdTx(msg, sigs)
|
||||
cdc := app.MakeTxCodec()
|
||||
cdc := wire.NewCodec()
|
||||
bank.RegisterWire(cdc)
|
||||
|
||||
txBytes, err := cdc.MarshalBinary(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -116,6 +103,7 @@ func buildTx() ([]byte, error) {
|
||||
}
|
||||
|
||||
func buildMsg(from crypto.Address) (sdk.Msg, error) {
|
||||
|
||||
// parse coins
|
||||
amount := viper.GetString(flagAmount)
|
||||
coins, err := sdk.ParseCoins(amount)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user