From dda84c79ab387859f1a88aa3b6e60724b43cc6ca Mon Sep 17 00:00:00 2001 From: Nick DeLuca Date: Wed, 17 Jun 2020 08:04:16 -0500 Subject: [PATCH] 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> --- CHANGELOG.md | 2 ++ x/kavadist/alias.go | 50 +++++++++++++++---------------- x/kavadist/client/cli/query.go | 27 +++++++++++++++++ x/kavadist/keeper/querier.go | 14 +++++++++ x/kavadist/keeper/querier_test.go | 18 ++++++++++- x/kavadist/types/querier.go | 3 +- 6 files changed, 87 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5162c331..6543ea76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +[\#590](https://github.com/Kava-Labs/kava/pull/590) Add CLI query to return kavadist module account balance + [\#584](https://github.com/Kava-Labs/kava/pulls/584) Add REST client and CLI queries for `kavadist` module [\#578](https://github.com/Kava-Labs/kava/pulls/578) Add v0.3 compatible REST client that supports diff --git a/x/kavadist/alias.go b/x/kavadist/alias.go index 04cdf035..c87a02b1 100644 --- a/x/kavadist/alias.go +++ b/x/kavadist/alias.go @@ -1,56 +1,56 @@ package kavadist +// DO NOT EDIT - generated by aliasgen tool (github.com/rhuairahrighairidh/aliasgen) + import ( "github.com/kava-labs/kava/x/kavadist/keeper" "github.com/kava-labs/kava/x/kavadist/types" ) -// autogenerated code using github.com/rigelrozanski/multitool -// aliases generated for the following subdirectories: -// ALIASGEN: github.com/kava-labs/kava/x/kavadist/keeper -// ALIASGEN: github.com/kava-labs/kava/x/kavadist/types - -// nolint const ( - EventTypeKavaDist = types.EventTypeKavaDist - AttributeKeyInflation = types.AttributeKeyInflation - ModuleName = types.ModuleName - StoreKey = types.StoreKey - RouterKey = types.RouterKey - QuerierRoute = types.QuerierRoute - DefaultParamspace = types.DefaultParamspace - KavaDistMacc = types.KavaDistMacc - QueryGetParams = types.QueryGetParams + AttributeKeyInflation = types.AttributeKeyInflation + AttributeKeyStatus = types.AttributeKeyStatus + AttributeValueInactive = types.AttributeValueInactive + DefaultParamspace = types.DefaultParamspace + EventTypeKavaDist = types.EventTypeKavaDist + KavaDistMacc = types.KavaDistMacc + ModuleName = types.ModuleName + QuerierRoute = types.QuerierRoute + QueryGetBalance = types.QueryGetBalance + QueryGetParams = types.QueryGetParams + RouterKey = types.RouterKey + StoreKey = types.StoreKey ) -// nolint var ( - // functions aliases + // function aliases NewKeeper = keeper.NewKeeper - RegisterCodec = types.RegisterCodec - NewGenesisState = types.NewGenesisState + NewQuerier = keeper.NewQuerier DefaultGenesisState = types.DefaultGenesisState - NewParams = types.NewParams DefaultParams = types.DefaultParams + NewGenesisState = types.NewGenesisState + NewParams = types.NewParams + NewPeriod = types.NewPeriod ParamKeyTable = types.ParamKeyTable + RegisterCodec = types.RegisterCodec // variable aliases - ModuleCdc = types.ModuleCdc CurrentDistPeriodKey = types.CurrentDistPeriodKey - PreviousBlockTimeKey = types.PreviousBlockTimeKey - KeyActive = types.KeyActive - KeyPeriods = types.KeyPeriods DefaultActive = types.DefaultActive DefaultPeriods = types.DefaultPeriods DefaultPreviousBlockTime = types.DefaultPreviousBlockTime GovDenom = types.GovDenom + KeyActive = types.KeyActive + KeyPeriods = types.KeyPeriods + ModuleCdc = types.ModuleCdc + PreviousBlockTimeKey = types.PreviousBlockTimeKey ) -// nolint type ( Keeper = keeper.Keeper GenesisState = types.GenesisState Params = types.Params Period = types.Period Periods = types.Periods + SupplyKeeper = types.SupplyKeeper ) diff --git a/x/kavadist/client/cli/query.go b/x/kavadist/client/cli/query.go index 1de62a37..5d01d83f 100644 --- a/x/kavadist/client/cli/query.go +++ b/x/kavadist/client/cli/query.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/kava-labs/kava/x/kavadist/types" ) @@ -21,6 +22,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { kavadistQueryCmd.AddCommand(flags.GetCommands( queryParamsCmd(queryRoute, cdc), + queryBalanceCmd(queryRoute, cdc), )...) return kavadistQueryCmd @@ -53,3 +55,28 @@ func queryParamsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { }, } } + +func queryBalanceCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { + return &cobra.Command{ + Use: "balance", + Short: "get the kavadist module balance", + Long: "Get the current kavadist module account balance.", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + cliCtx := context.NewCLIContext().WithCodec(cdc) + + route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetBalance) + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + return err + } + cliCtx = cliCtx.WithHeight(height) + + var coins sdk.Coins + if err := cdc.UnmarshalJSON(res, &coins); err != nil { + return fmt.Errorf("failed to unmarshal coin balance: %w", err) + } + return cliCtx.PrintOutput(coins) + }, + } +} diff --git a/x/kavadist/keeper/querier.go b/x/kavadist/keeper/querier.go index 77e87e48..fce92840 100644 --- a/x/kavadist/keeper/querier.go +++ b/x/kavadist/keeper/querier.go @@ -16,6 +16,8 @@ func NewQuerier(k Keeper) sdk.Querier { 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) } @@ -34,3 +36,15 @@ func queryGetParams(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, e } 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 +} diff --git a/x/kavadist/keeper/querier_test.go b/x/kavadist/keeper/querier_test.go index e8997e45..481f172e 100644 --- a/x/kavadist/keeper/querier_test.go +++ b/x/kavadist/keeper/querier_test.go @@ -1,13 +1,14 @@ package keeper_test import ( + sdk "github.com/cosmos/cosmos-sdk/types" abci "github.com/tendermint/tendermint/abci/types" "github.com/kava-labs/kava/x/kavadist/keeper" "github.com/kava-labs/kava/x/kavadist/types" ) -func (suite *KeeperTestSuite) TestQuerier() { +func (suite *KeeperTestSuite) TestQuerierGetParams() { querier := keeper.NewQuerier(suite.keeper) bz, err := querier(suite.ctx, []string{types.QueryGetParams}, abci.RequestQuery{}) suite.Require().NoError(err) @@ -18,3 +19,18 @@ func (suite *KeeperTestSuite) TestQuerier() { suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &p)) suite.Require().Equal(testParams, p) } + +func (suite *KeeperTestSuite) TestQuerierGetBalance() { + sk := suite.supplyKeeper + + sk.MintCoins(suite.ctx, types.KavaDistMacc, sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(100e6)))) + + querier := keeper.NewQuerier(suite.keeper) + bz, err := querier(suite.ctx, []string{types.QueryGetBalance}, abci.RequestQuery{}) + suite.Require().NoError(err) + suite.Require().NotNil(bz) + + var coins sdk.Coins + types.ModuleCdc.UnmarshalJSON(bz, &coins) + suite.Require().Equal(sdk.NewInt(100e6), coins.AmountOf("ukava")) +} diff --git a/x/kavadist/types/querier.go b/x/kavadist/types/querier.go index 104ab89a..ae86202b 100644 --- a/x/kavadist/types/querier.go +++ b/x/kavadist/types/querier.go @@ -2,5 +2,6 @@ package types // Querier routes for the kavadist module const ( - QueryGetParams = "params" + QueryGetParams = "params" + QueryGetBalance = "balance" )