2020-03-13 15:11:31 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
func NewQuerier(keeper Keeper) sdk.Querier {
|
2020-04-27 18:19:05 +00:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
switch path[0] {
|
|
|
|
|
|
|
|
case types.QueryCommittees:
|
|
|
|
return queryCommittees(ctx, path[1:], req, keeper)
|
|
|
|
case types.QueryCommittee:
|
|
|
|
return queryCommittee(ctx, path[1:], req, keeper)
|
|
|
|
case types.QueryProposals:
|
|
|
|
return queryProposals(ctx, path[1:], req, keeper)
|
|
|
|
case types.QueryProposal:
|
|
|
|
return queryProposal(ctx, path[1:], req, keeper)
|
|
|
|
case types.QueryVotes:
|
|
|
|
return queryVotes(ctx, path[1:], req, keeper)
|
|
|
|
case types.QueryVote:
|
|
|
|
return queryVote(ctx, path[1:], req, keeper)
|
|
|
|
case types.QueryTally:
|
|
|
|
return queryTally(ctx, path[1:], req, keeper)
|
2020-06-29 23:39:35 +00:00
|
|
|
case types.QueryNextProposalID:
|
|
|
|
return queryNextProposalID(ctx, req, keeper)
|
2020-06-23 13:17:08 +00:00
|
|
|
case types.QueryRawParams:
|
|
|
|
return queryRawParams(ctx, path[1:], req, keeper)
|
2020-03-13 15:11:31 +00:00
|
|
|
|
|
|
|
default:
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.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
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func queryCommittees(ctx sdk.Context, path []string, _ abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2020-04-27 14:04:47 +00:00
|
|
|
committees := keeper.GetCommittees(ctx)
|
2020-03-13 15:11:31 +00:00
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, committees)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func queryCommittee(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryCommitteeParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
committee, found := keeper.GetCommittee(ctx, params.CommitteeID)
|
|
|
|
if !found {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrUnknownCommittee, "%d", params.CommitteeID)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, committee)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.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
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func queryProposals(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryCommitteeParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.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
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, proposals)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryProposalParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proposal, found := keeper.GetProposal(ctx, params.ProposalID)
|
|
|
|
if !found {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrUnknownProposal, "%d", params.ProposalID)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, proposal)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:39:35 +00:00
|
|
|
func queryNextProposalID(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
|
|
|
nextProposalID, _ := keeper.GetNextProposalID(ctx)
|
|
|
|
|
|
|
|
bz, err := types.ModuleCdc.MarshalJSON(nextProposalID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:06:31 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// Votes
|
|
|
|
// ------------------------------------------
|
2020-03-13 15:11:31 +00:00
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryProposalParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.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
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, votes)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryVoteParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vote, found := keeper.GetVote(ctx, params.ProposalID, params.Voter)
|
|
|
|
if !found {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrUnknownVote, "proposal id: %d, voter: %s", params.ProposalID, params.Voter)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, vote)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.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
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
2020-03-13 15:11:31 +00:00
|
|
|
var params types.QueryProposalParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 16:08:03 +00:00
|
|
|
proposal, found := keeper.GetProposal(ctx, params.ProposalID)
|
2020-03-13 15:11:31 +00:00
|
|
|
if !found {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrapf(types.ErrUnknownProposal, "%d", params.ProposalID)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 16:08:03 +00:00
|
|
|
committee, found := keeper.GetCommittee(ctx, proposal.CommitteeID)
|
|
|
|
if !found {
|
|
|
|
return nil, sdkerrors.Wrapf(types.ErrUnknownCommittee, "%d", proposal.CommitteeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pollingStatus types.ProposalPollingStatus
|
|
|
|
switch com := committee.(type) {
|
|
|
|
case types.MemberCommittee:
|
|
|
|
currVotes := keeper.TallyMemberCommitteeVotes(ctx, params.ProposalID)
|
|
|
|
possibleVotes := sdk.NewDec(int64(len(com.Members)))
|
|
|
|
memberPollingStatus := types.NewProposalPollingStatus(params.ProposalID, currVotes,
|
|
|
|
currVotes, possibleVotes, com.VoteThreshold, sdk.Dec{Int: nil})
|
|
|
|
pollingStatus = memberPollingStatus
|
|
|
|
case types.TokenCommittee:
|
|
|
|
yesVotes, _, currVotes, possibleVotes := keeper.TallyTokenCommitteeVotes(ctx, params.ProposalID, com.TallyDenom)
|
|
|
|
tokenPollingStatus := types.NewProposalPollingStatus(params.ProposalID, yesVotes,
|
|
|
|
currVotes, possibleVotes, com.VoteThreshold, com.Quorum)
|
|
|
|
pollingStatus = tokenPollingStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, pollingStatus)
|
2020-03-13 15:11:31 +00:00
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.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
|
|
|
|
// ------------------------------------------
|
|
|
|
|
|
|
|
func queryRawParams(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
|
|
|
|
var params types.QueryRawParamsParams
|
|
|
|
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
subspace, found := keeper.ParamKeeper.GetSubspace(params.Subspace)
|
|
|
|
if !found {
|
|
|
|
return nil, sdkerrors.Wrapf(types.ErrUnknownSubspace, "subspace: %s", params.Subspace)
|
|
|
|
}
|
|
|
|
rawParams := subspace.GetRaw(ctx, []byte(params.Key))
|
|
|
|
|
|
|
|
// encode the raw params as json, which converts them to a base64 string
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, rawParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|