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>
This commit is contained in:
Nick DeLuca 2020-06-17 08:04:16 -05:00 committed by GitHub
parent bfb9f7e20a
commit dda84c79ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 27 deletions

View File

@ -36,6 +36,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased] ## [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 [\#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 [\#578](https://github.com/Kava-Labs/kava/pulls/578) Add v0.3 compatible REST client that supports

View File

@ -1,56 +1,56 @@
package kavadist package kavadist
// DO NOT EDIT - generated by aliasgen tool (github.com/rhuairahrighairidh/aliasgen)
import ( import (
"github.com/kava-labs/kava/x/kavadist/keeper" "github.com/kava-labs/kava/x/kavadist/keeper"
"github.com/kava-labs/kava/x/kavadist/types" "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 ( const (
EventTypeKavaDist = types.EventTypeKavaDist AttributeKeyInflation = types.AttributeKeyInflation
AttributeKeyInflation = types.AttributeKeyInflation AttributeKeyStatus = types.AttributeKeyStatus
ModuleName = types.ModuleName AttributeValueInactive = types.AttributeValueInactive
StoreKey = types.StoreKey DefaultParamspace = types.DefaultParamspace
RouterKey = types.RouterKey EventTypeKavaDist = types.EventTypeKavaDist
QuerierRoute = types.QuerierRoute KavaDistMacc = types.KavaDistMacc
DefaultParamspace = types.DefaultParamspace ModuleName = types.ModuleName
KavaDistMacc = types.KavaDistMacc QuerierRoute = types.QuerierRoute
QueryGetParams = types.QueryGetParams QueryGetBalance = types.QueryGetBalance
QueryGetParams = types.QueryGetParams
RouterKey = types.RouterKey
StoreKey = types.StoreKey
) )
// nolint
var ( var (
// functions aliases // function aliases
NewKeeper = keeper.NewKeeper NewKeeper = keeper.NewKeeper
RegisterCodec = types.RegisterCodec NewQuerier = keeper.NewQuerier
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState DefaultGenesisState = types.DefaultGenesisState
NewParams = types.NewParams
DefaultParams = types.DefaultParams DefaultParams = types.DefaultParams
NewGenesisState = types.NewGenesisState
NewParams = types.NewParams
NewPeriod = types.NewPeriod
ParamKeyTable = types.ParamKeyTable ParamKeyTable = types.ParamKeyTable
RegisterCodec = types.RegisterCodec
// variable aliases // variable aliases
ModuleCdc = types.ModuleCdc
CurrentDistPeriodKey = types.CurrentDistPeriodKey CurrentDistPeriodKey = types.CurrentDistPeriodKey
PreviousBlockTimeKey = types.PreviousBlockTimeKey
KeyActive = types.KeyActive
KeyPeriods = types.KeyPeriods
DefaultActive = types.DefaultActive DefaultActive = types.DefaultActive
DefaultPeriods = types.DefaultPeriods DefaultPeriods = types.DefaultPeriods
DefaultPreviousBlockTime = types.DefaultPreviousBlockTime DefaultPreviousBlockTime = types.DefaultPreviousBlockTime
GovDenom = types.GovDenom GovDenom = types.GovDenom
KeyActive = types.KeyActive
KeyPeriods = types.KeyPeriods
ModuleCdc = types.ModuleCdc
PreviousBlockTimeKey = types.PreviousBlockTimeKey
) )
// nolint
type ( type (
Keeper = keeper.Keeper Keeper = keeper.Keeper
GenesisState = types.GenesisState GenesisState = types.GenesisState
Params = types.Params Params = types.Params
Period = types.Period Period = types.Period
Periods = types.Periods Periods = types.Periods
SupplyKeeper = types.SupplyKeeper
) )

View File

@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/x/kavadist/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( kavadistQueryCmd.AddCommand(flags.GetCommands(
queryParamsCmd(queryRoute, cdc), queryParamsCmd(queryRoute, cdc),
queryBalanceCmd(queryRoute, cdc),
)...) )...)
return kavadistQueryCmd 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)
},
}
}

View File

@ -16,6 +16,8 @@ func NewQuerier(k Keeper) sdk.Querier {
switch path[0] { switch path[0] {
case types.QueryGetParams: case types.QueryGetParams:
return queryGetParams(ctx, req, k) return queryGetParams(ctx, req, k)
case types.QueryGetBalance:
return queryGetBalance(ctx, req, k)
default: default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName) 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 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
}

View File

@ -1,13 +1,14 @@
package keeper_test package keeper_test
import ( import (
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types" abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/x/kavadist/keeper" "github.com/kava-labs/kava/x/kavadist/keeper"
"github.com/kava-labs/kava/x/kavadist/types" "github.com/kava-labs/kava/x/kavadist/types"
) )
func (suite *KeeperTestSuite) TestQuerier() { func (suite *KeeperTestSuite) TestQuerierGetParams() {
querier := keeper.NewQuerier(suite.keeper) querier := keeper.NewQuerier(suite.keeper)
bz, err := querier(suite.ctx, []string{types.QueryGetParams}, abci.RequestQuery{}) bz, err := querier(suite.ctx, []string{types.QueryGetParams}, abci.RequestQuery{})
suite.Require().NoError(err) suite.Require().NoError(err)
@ -18,3 +19,18 @@ func (suite *KeeperTestSuite) TestQuerier() {
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &p)) suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &p))
suite.Require().Equal(testParams, 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"))
}

View File

@ -2,5 +2,6 @@ package types
// Querier routes for the kavadist module // Querier routes for the kavadist module
const ( const (
QueryGetParams = "params" QueryGetParams = "params"
QueryGetBalance = "balance"
) )