-
Notifications
You must be signed in to change notification settings - Fork 7
/
querier.go
156 lines (122 loc) · 4.33 KB
/
querier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package keeper
import (
abci "github.com/tendermint/tendermint/abci/types"
"strconv"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/anathatech/project-anatha/x/governance/internal/types"
)
const (
QueryParams = "params"
QueryProposals = "proposals"
QueryProposal = "proposal"
QueryVotes = "votes"
QueryVote = "vote"
QueryGovernors = "governors"
ParamVoting = "voting"
ParamTallying = "tallying"
)
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case QueryParams:
return queryParams(ctx, path[1:], req, keeper)
case QueryProposals:
return queryProposals(ctx, path[1:], req, keeper)
case QueryProposal:
return queryProposal(ctx, path[1:], req, keeper)
case QueryVotes:
return queryVotes(ctx, path[1:], req, keeper)
case QueryVote:
return queryVote(ctx, path[1:], req, keeper)
case QueryGovernors:
return queryGovernors(ctx, path[1:], req, keeper)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
}
}
}
func queryParams(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
switch path[0] {
case ParamVoting:
bz, err := codec.MarshalJSONIndent(keeper.cdc, keeper.GetVotingParams(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
case ParamTallying:
bz, err := codec.MarshalJSONIndent(keeper.cdc, keeper.GetTallyParams(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "%s is not a valid query request path", req.Path)
}
}
func queryProposals(ctx sdk.Context, _ []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
proposals := keeper.GetProposals(ctx)
if proposals == nil {
proposals = types.Proposals{}
}
bz, err := codec.MarshalJSONIndent(keeper.cdc, proposals)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
proposalID, err := strconv.ParseUint(path[0], 10, 64)
if err != nil {
return nil, sdkerrors.Wrapf(err, "proposal-id %s not a valid int, please input a valid proposal-id", path[0])
}
proposal, err := keeper.GetProposal(ctx, proposalID)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrUnknownProposal, "%d", proposalID)
}
bz, err := codec.MarshalJSONIndent(keeper.cdc, proposal)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
proposalID, err := strconv.ParseUint(path[0], 10, 64)
if err != nil {
return nil, sdkerrors.Wrapf(err, "proposal-id %s not a valid int, please input a valid proposal-id", path[0])
}
votes := keeper.GetVotes(ctx, proposalID)
bz, err := codec.MarshalJSONIndent(keeper.cdc, votes)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
proposalID, err := strconv.ParseUint(path[0], 10, 64)
if err != nil {
return nil, sdkerrors.Wrapf(err, "proposal-id %s not a valid int, please input a valid proposal-id", path[0])
}
voter, err := sdk.AccAddressFromBech32(path[1])
if err != nil {
return nil, sdkerrors.Wrap(err, "Failed to parse the voter address.")
}
vote, err := keeper.GetVote(ctx, proposalID, voter)
if err != nil {
return nil, sdkerrors.Wrap(err, "Failed to find specified vote")
}
bz, err := codec.MarshalJSONIndent(keeper.cdc, vote)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryGovernors(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
governors := keeper.GetGovernors(ctx)
bz, err := codec.MarshalJSONIndent(keeper.cdc, governors)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}