cosmos-sdk/x/stake/client/cli/query.go

172 lines
4.3 KiB
Go
Raw Normal View History

2018-04-25 07:18:06 -07:00
package cli
2018-02-24 05:19:32 -08:00
import (
2018-02-25 15:45:20 -08:00
"encoding/hex"
"fmt"
2018-02-24 05:19:32 -08:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
crypto "github.com/tendermint/go-crypto"
"github.com/cosmos/cosmos-sdk/client/context"
2018-03-17 11:18:04 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" // XXX fix
2018-02-25 15:45:20 -08:00
"github.com/cosmos/cosmos-sdk/x/stake"
2018-02-24 05:19:32 -08:00
)
// get the command to query a candidate
2018-04-06 11:35:54 -07:00
func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
2018-05-04 18:29:12 -07:00
Use: "candidate [candidate-addr]",
Short: "Query a validator-candidate account",
2018-05-04 18:29:12 -07:00
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
2018-05-04 18:29:12 -07:00
addr, err := sdk.GetAddress(args[0])
if err != nil {
return err
}
2018-04-16 16:17:09 -07:00
key := stake.GetCandidateKey(addr)
ctx := context.NewCoreContextFromViper()
2018-03-30 05:57:53 -07:00
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
// parse out the candidate
candidate := new(stake.Candidate)
cdc.MustUnmarshalBinary(res, candidate)
2018-04-09 13:18:56 -07:00
output, err := wire.MarshalJSONIndent(cdc, candidate)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
return cmd
2018-02-24 05:19:32 -08:00
}
// get the command to query a candidate
func GetCmdQueryCandidates(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "candidates",
Short: "Query for all validator-candidate accounts",
RunE: func(cmd *cobra.Command, args []string) error {
key := stake.CandidatesKey
ctx := context.NewCoreContextFromViper()
resKVs, err := ctx.QuerySubspace(cdc, key, storeName)
if err != nil {
return err
}
// parse out the candidates
var candidates []stake.Candidate
for _, KV := range resKVs {
var candidate stake.Candidate
cdc.MustUnmarshalBinary(KV.Value, &candidate)
candidates = append(candidates, candidate)
}
output, err := wire.MarshalJSONIndent(cdc, candidates)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
return cmd
}
// get the command to query a single delegator bond
2018-04-06 11:35:54 -07:00
func GetCmdQueryDelegatorBond(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "delegator-bond",
Short: "Query a delegators bond based on address and candidate pubkey",
RunE: func(cmd *cobra.Command, args []string) error {
addr, err := sdk.GetAddress(viper.GetString(FlagAddressCandidate))
if err != nil {
return err
}
bz, err := hex.DecodeString(viper.GetString(FlagAddressDelegator))
if err != nil {
return err
}
delegator := crypto.Address(bz)
2018-04-16 16:17:09 -07:00
key := stake.GetDelegatorBondKey(delegator, addr, cdc)
ctx := context.NewCoreContextFromViper()
2018-03-30 05:57:53 -07:00
res, err := ctx.Query(key, storeName)
if err != nil {
return err
}
// parse out the bond
bond := new(stake.DelegatorBond)
cdc.MustUnmarshalBinary(res, bond)
2018-04-09 13:18:56 -07:00
output, err := wire.MarshalJSONIndent(cdc, bond)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
cmd.Flags().AddFlagSet(fsCandidate)
cmd.Flags().AddFlagSet(fsDelegator)
return cmd
}
2018-02-24 05:19:32 -08:00
// get the command to query all the candidates bonded to a delegator
func GetCmdQueryDelegatorBonds(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "delegator-candidates",
Short: "Query all delegators bonds based on delegator-address",
RunE: func(cmd *cobra.Command, args []string) error {
delegatorAddr, err := sdk.GetAddress(viper.GetString(FlagAddressDelegator))
if err != nil {
return err
}
key := stake.GetDelegatorBondsKey(delegatorAddr, cdc)
ctx := context.NewCoreContextFromViper()
resKVs, err := ctx.QuerySubspace(cdc, key, storeName)
if err != nil {
return err
}
// parse out the candidates
var delegators []stake.DelegatorBond
for _, KV := range resKVs {
var delegator stake.DelegatorBond
cdc.MustUnmarshalBinary(KV.Value, &delegator)
delegators = append(delegators, delegator)
}
output, err := wire.MarshalJSONIndent(cdc, delegators)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
cmd.Flags().AddFlagSet(fsDelegator)
return cmd
}