cosmos-sdk/x/auth/client/cli/account.go

77 lines
1.9 KiB
Go
Raw Normal View History

2018-04-25 07:18:06 -07:00
package cli
2018-02-28 17:57:38 -08:00
import (
"fmt"
"github.com/spf13/cobra"
"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]
2018-06-01 07:23:58 -07:00
key, err := sdk.GetAccAddressBech32(addr)
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-03-30 05:57:53 -07:00
2018-04-23 08:47:39 -07:00
// perform query
ctx := context.NewCoreContextFromViper()
res, err := ctx.QueryStore(auth.AddressStoreKey(key), storeName)
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-02-28 17:57:38 -08:00
// Check if account was found
if res == nil {
return sdk.ErrUnknownAddress("No account with address " + addr +
" was found in the state.\nAre you sure there has been a transaction involving it?")
}
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
}
}