2020-03-13 15:11:31 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2020-03-13 15:11:31 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-27 18:19:05 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-03-13 15:11:31 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/committee/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier creates a new gov Querier instance
|
2022-01-08 00:39:27 +00:00
|
|
|
func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
|
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
switch path[0] {
|
|
|
|
case types.QueryCommittees:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryCommittees(ctx, req, k, legacyQuerierCdc)
|
2020-03-13 15:11:31 +00:00
|
|
|
case types.QueryCommittee:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryCommittee(ctx, req, k, legacyQuerierCdc)
|
2020-03-13 15:11:31 +00:00
|
|
|
case types.QueryProposals:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryProposals(ctx, req, k, legacyQuerierCdc)
|
2020-03-13 15:11:31 +00:00
|
|
|
case types.QueryProposal:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryProposal(ctx, req, k, legacyQuerierCdc)
|
2020-03-13 15:11:31 +00:00
|
|
|
case types.QueryVotes:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryVotes(ctx, req, k, legacyQuerierCdc)
|
2020-03-13 15:11:31 +00:00
|
|
|
case types.QueryVote:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryVote(ctx, req, k, legacyQuerierCdc)
|
2020-03-13 15:11:31 +00:00
|
|
|
case types.QueryTally:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryTally(ctx, req, k, legacyQuerierCdc)
|
2020-06-29 23:39:35 +00:00
|
|
|
case types.QueryNextProposalID:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryNextProposalID(ctx, req, k, legacyQuerierCdc)
|
2020-06-23 13:17:08 +00:00
|
|
|
case types.QueryRawParams:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryRawParams(ctx, req, k, legacyQuerierCdc)
|
2020-03-13 15:11:31 +00:00
|
|
|
|
|
|
|
default:
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:06:31 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// Committees
|
|
|
|
// ------------------------------------------
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryCommittees(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-04-27 14:04:47 +00:00
|
|
|
committees := keeper.GetCommittees(ctx)
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, committees)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryCommittee(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryCommitteeParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
committee, found := keeper.GetCommittee(ctx, params.CommitteeID)
|
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrapf(types.ErrUnknownCommittee, "%d", params.CommitteeID)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, committee)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:06:31 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// Proposals
|
|
|
|
// ------------------------------------------
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryProposals(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryCommitteeParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:04:47 +00:00
|
|
|
proposals := keeper.GetProposalsByCommittee(ctx, params.CommitteeID)
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposals)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryProposal(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryProposalParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proposal, found := keeper.GetProposal(ctx, params.ProposalID)
|
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrapf(types.ErrUnknownProposal, "%d", params.ProposalID)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposal)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryNextProposalID(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
|
|
|
nextProposalID, _ := k.GetNextProposalID(ctx)
|
2020-06-29 23:39:35 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := types.ModuleCdc.LegacyAmino.MarshalJSON(nextProposalID)
|
2020-06-29 23:39:35 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-06-29 23:39:35 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:06:31 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// Votes
|
|
|
|
// ------------------------------------------
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryVotes(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryProposalParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:04:47 +00:00
|
|
|
votes := keeper.GetVotesByProposal(ctx, params.ProposalID)
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, votes)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryVote(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryVoteParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vote, found := keeper.GetVote(ctx, params.ProposalID, params.Voter)
|
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrapf(types.ErrUnknownVote, "proposal id: %d, voter: %s", params.ProposalID, params.Voter)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, vote)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:06:31 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// Tally
|
|
|
|
// ------------------------------------------
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryTally(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryProposalParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
tally, found := keeper.GetProposalTallyResponse(ctx, params.ProposalID)
|
2020-03-13 15:11:31 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrapf(types.ErrNotFoundProposalTally, "proposal id: %d", params.ProposalID)
|
2021-06-07 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, tally)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-06-23 13:17:08 +00:00
|
|
|
|
|
|
|
// ------------------------------------------
|
|
|
|
// Raw Params
|
|
|
|
// ------------------------------------------
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryRawParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-06-23 13:17:08 +00:00
|
|
|
var params types.QueryRawParamsParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-06-23 13:17:08 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-06-23 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
subspace, found := keeper.paramKeeper.GetSubspace(params.Subspace)
|
2020-06-23 13:17:08 +00:00
|
|
|
if !found {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrapf(types.ErrUnknownSubspace, "subspace: %s", params.Subspace)
|
2020-06-23 13:17:08 +00:00
|
|
|
}
|
|
|
|
rawParams := subspace.GetRaw(ctx, []byte(params.Key))
|
|
|
|
|
|
|
|
// encode the raw params as json, which converts them to a base64 string
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, rawParams)
|
2020-06-23 13:17:08 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-06-23 13:17:08 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|