2018-04-25 07:18:06 -07:00
|
|
|
package cli
|
2018-02-28 17:57:38 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-02-28 17:57:38 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-03-02 01:24:07 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
2018-05-23 19:26:54 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-02-28 17:57:38 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetAccountCmd for the auth.BaseAccount type
|
|
|
|
func GetAccountCmdDefault(storeName string, cdc *wire.Codec) *cobra.Command {
|
2018-03-20 18:22:15 -07:00
|
|
|
return GetAccountCmd(storeName, cdc, GetAccountDecoder(cdc))
|
2018-02-28 17:57:38 -08:00
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// Get account decoder for auth.DefaultAccount
|
2018-05-23 19:26:54 -07:00
|
|
|
func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder {
|
|
|
|
return func(accBytes []byte) (acct auth.Account, err error) {
|
2018-04-07 10:56:49 -07:00
|
|
|
// acct := new(auth.BaseAccount)
|
|
|
|
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
|
2018-03-03 13:03:34 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-02-28 17:57:38 -08:00
|
|
|
return acct, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountCmd returns a query account that will display the
|
|
|
|
// state of the account at a given address
|
2018-05-23 19:26:54 -07:00
|
|
|
func GetAccountCmd(storeName string, cdc *wire.Codec, decoder auth.AccountDecoder) *cobra.Command {
|
2018-02-28 17:57:38 -08:00
|
|
|
return &cobra.Command{
|
2018-04-23 08:47:39 -07:00
|
|
|
Use: "account [address]",
|
2018-02-28 17:57:38 -08:00
|
|
|
Short: "Query account balance",
|
2018-04-23 08:47:39 -07:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2018-02-28 17:57:38 -08:00
|
|
|
|
2018-04-23 08:47:39 -07:00
|
|
|
// find the key to look up the account
|
|
|
|
addr := args[0]
|
|
|
|
bz, err := hex.DecodeString(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := sdk.Address(bz)
|
2018-03-30 05:57:53 -07:00
|
|
|
|
2018-04-23 08:47:39 -07:00
|
|
|
// perform query
|
|
|
|
ctx := context.NewCoreContextFromViper()
|
|
|
|
res, err := ctx.Query(key, storeName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-28 17:57:38 -08:00
|
|
|
|
2018-04-23 08:47:39 -07:00
|
|
|
// decode the value
|
|
|
|
account, err := decoder(res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-28 17:57:38 -08:00
|
|
|
|
2018-04-23 08:47:39 -07:00
|
|
|
// print out whole account
|
|
|
|
output, err := wire.MarshalJSONIndent(cdc, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(output))
|
|
|
|
return nil
|
|
|
|
},
|
2018-02-28 17:57:38 -08:00
|
|
|
}
|
|
|
|
}
|