package keeper import ( "fmt" abci "github.com/tendermint/tendermint/abci/types" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/bank/internal/types" ) const ( // query balance path QueryBalance = "balances" ) // NewQuerier returns a new sdk.Keeper instance. func NewQuerier(k Keeper) sdk.Querier { return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) { switch path[0] { case QueryBalance: return queryBalance(ctx, req, k) default: return nil, sdk.ErrUnknownRequest("unknown bank query endpoint") } } } // queryBalance fetch an account's balance for the supplied height. // Height and account address are passed as first and second path components respectively. func queryBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) { var params types.QueryBalanceParams if err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms); err != nil { return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err)) } coins := k.GetCoins(ctx, params.Address) if coins == nil { coins = sdk.NewCoins() } bz, err := codec.MarshalJSONIndent(types.ModuleCdc, coins) if err != nil { return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error())) } return bz, nil }