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

76 lines
1.8 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"
"github.com/cosmos/cosmos-sdk/codec"
2018-02-28 17:57:38 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-05-23 19:26:54 -07:00
"github.com/cosmos/cosmos-sdk/x/auth"
2018-02-28 17:57:38 -08:00
)
2018-08-06 11:11:30 -07:00
// GetAccountCmdDefault invokes the GetAccountCmd for the auth.BaseAccount type.
func GetAccountCmdDefault(storeName string, cdc *codec.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-08-06 11:11:30 -07:00
// GetAccountDecoder gets the account decoder for auth.DefaultAccount.
func GetAccountDecoder(cdc *codec.Codec) auth.AccountDecoder {
2018-05-23 19:26:54 -07:00
return func(accBytes []byte) (acct auth.Account, err error) {
2018-04-07 10:56:49 -07:00
err = cdc.UnmarshalBinaryBare(accBytes, &acct)
2018-03-03 13:03:34 -08:00
if err != nil {
panic(err)
}
2018-08-06 11:11:30 -07:00
2018-02-28 17:57:38 -08:00
return acct, err
}
}
2018-08-06 11:11:30 -07:00
// GetAccountCmd returns a query account that will display the state of the
// account at a given address.
2018-08-31 15:22:37 -07:00
// nolint: unparam
func GetAccountCmd(storeName string, cdc *codec.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 {
// find the key to look up the account
addr := args[0]
2018-07-09 16:06:05 -07:00
key, err := sdk.AccAddressFromBech32(addr)
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-03-30 05:57:53 -07:00
2018-08-06 11:11:30 -07:00
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(decoder)
2018-02-28 17:57:38 -08:00
2018-08-06 11:11:30 -07:00
if err := cliCtx.EnsureAccountExistsFromAddr(key); err != nil {
return err
}
2018-08-06 11:11:30 -07:00
acc, err := cliCtx.GetAccount(key)
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-02-28 17:57:38 -08:00
var output []byte
if cliCtx.Indent {
output, err = cdc.MarshalJSONIndent(acc, "", " ")
} else {
output, err = cdc.MarshalJSON(acc)
}
2018-04-23 08:47:39 -07:00
if err != nil {
return err
}
2018-08-06 11:11:30 -07:00
2018-04-23 08:47:39 -07:00
fmt.Println(string(output))
return nil
},
2018-02-28 17:57:38 -08:00
}
}