0g-chain/x/kavadist/keeper/querier.go
Nick DeLuca dda84c79ab
Add CLI Command to Query Kavadist Balance (#590)
* add command to view balance of kavadist module account

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
2020-06-17 09:04:16 -04:00

51 lines
1.4 KiB
Go

package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/x/kavadist/types"
)
// NewQuerier is the module level router for state queries
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
switch path[0] {
case types.QueryGetParams:
return queryGetParams(ctx, req, k)
case types.QueryGetBalance:
return queryGetBalance(ctx, req, k)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
}
}
}
// query params in the store
func queryGetParams(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
// Get params
params := k.GetParams(ctx)
// Encode results
bz, err := codec.MarshalJSONIndent(k.cdc, params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// queryGetBalance returns current balance of kavadist module account
func queryGetBalance(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
acc := k.supplyKeeper.GetModuleAccount(ctx, types.KavaDistMacc)
bz, err := codec.MarshalJSONIndent(k.cdc, acc.GetCoins())
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}