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]
[\#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

View File

@ -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
)

View File

@ -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)
},
}
}

View File

@ -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
}

View File

@ -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"))
}

View File

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