github.com/Finschia/finschia-sdk@v0.48.1/x/foundation/keeper/internal/grpc_query.go (about) 1 package internal 2 3 import ( 4 "context" 5 6 "google.golang.org/grpc/codes" 7 "google.golang.org/grpc/status" 8 9 "github.com/gogo/protobuf/proto" 10 11 codectypes "github.com/Finschia/finschia-sdk/codec/types" 12 "github.com/Finschia/finschia-sdk/store/prefix" 13 sdk "github.com/Finschia/finschia-sdk/types" 14 sdkerrors "github.com/Finschia/finschia-sdk/types/errors" 15 "github.com/Finschia/finschia-sdk/types/query" 16 "github.com/Finschia/finschia-sdk/x/foundation" 17 ) 18 19 type queryServer struct { 20 keeper Keeper 21 } 22 23 func NewQueryServer(keeper Keeper) foundation.QueryServer { 24 return &queryServer{ 25 keeper: keeper, 26 } 27 } 28 29 var _ foundation.QueryServer = (*queryServer)(nil) 30 31 func (s queryServer) Params(c context.Context, req *foundation.QueryParamsRequest) (*foundation.QueryParamsResponse, error) { 32 if req == nil { 33 return nil, status.Error(codes.InvalidArgument, "invalid request") 34 } 35 36 ctx := sdk.UnwrapSDKContext(c) 37 38 return &foundation.QueryParamsResponse{Params: s.keeper.GetParams(ctx)}, nil 39 } 40 41 func (s queryServer) Treasury(c context.Context, req *foundation.QueryTreasuryRequest) (*foundation.QueryTreasuryResponse, error) { 42 if req == nil { 43 return nil, status.Error(codes.InvalidArgument, "invalid request") 44 } 45 46 ctx := sdk.UnwrapSDKContext(c) 47 amount := s.keeper.GetTreasury(ctx) 48 49 return &foundation.QueryTreasuryResponse{Amount: amount}, nil 50 } 51 52 func (s queryServer) FoundationInfo(c context.Context, req *foundation.QueryFoundationInfoRequest) (*foundation.QueryFoundationInfoResponse, error) { 53 if req == nil { 54 return nil, status.Error(codes.InvalidArgument, "invalid request") 55 } 56 57 ctx := sdk.UnwrapSDKContext(c) 58 info := s.keeper.GetFoundationInfo(ctx) 59 60 return &foundation.QueryFoundationInfoResponse{Info: info}, nil 61 } 62 63 func (s queryServer) Member(c context.Context, req *foundation.QueryMemberRequest) (*foundation.QueryMemberResponse, error) { 64 if req == nil { 65 return nil, status.Error(codes.InvalidArgument, "invalid request") 66 } 67 68 ctx := sdk.UnwrapSDKContext(c) 69 addr, err := sdk.AccAddressFromBech32(req.Address) 70 if err != nil { 71 return nil, err 72 } 73 member, err := s.keeper.GetMember(ctx, addr) 74 if err != nil { 75 return nil, err 76 } 77 78 return &foundation.QueryMemberResponse{Member: member}, nil 79 } 80 81 func (s queryServer) Members(c context.Context, req *foundation.QueryMembersRequest) (*foundation.QueryMembersResponse, error) { 82 if req == nil { 83 return nil, status.Error(codes.InvalidArgument, "invalid request") 84 } 85 86 var members []foundation.Member 87 ctx := sdk.UnwrapSDKContext(c) 88 store := ctx.KVStore(s.keeper.storeKey) 89 memberStore := prefix.NewStore(store, memberKeyPrefix) 90 pageRes, err := query.Paginate(memberStore, req.Pagination, func(key []byte, value []byte) error { 91 var member foundation.Member 92 s.keeper.cdc.MustUnmarshal(value, &member) 93 members = append(members, member) 94 return nil 95 }) 96 if err != nil { 97 return nil, err 98 } 99 100 return &foundation.QueryMembersResponse{Members: members, Pagination: pageRes}, nil 101 } 102 103 func (s queryServer) Proposal(c context.Context, req *foundation.QueryProposalRequest) (*foundation.QueryProposalResponse, error) { 104 if req == nil { 105 return nil, status.Error(codes.InvalidArgument, "invalid request") 106 } 107 108 ctx := sdk.UnwrapSDKContext(c) 109 proposal, err := s.keeper.GetProposal(ctx, req.ProposalId) 110 if err != nil { 111 return nil, err 112 } 113 114 return &foundation.QueryProposalResponse{Proposal: proposal}, nil 115 } 116 117 func (s queryServer) Proposals(c context.Context, req *foundation.QueryProposalsRequest) (*foundation.QueryProposalsResponse, error) { 118 if req == nil { 119 return nil, status.Error(codes.InvalidArgument, "invalid request") 120 } 121 122 var proposals []foundation.Proposal 123 ctx := sdk.UnwrapSDKContext(c) 124 store := ctx.KVStore(s.keeper.storeKey) 125 proposalStore := prefix.NewStore(store, proposalKeyPrefix) 126 pageRes, err := query.Paginate(proposalStore, req.Pagination, func(key []byte, value []byte) error { 127 var proposal foundation.Proposal 128 s.keeper.cdc.MustUnmarshal(value, &proposal) 129 proposals = append(proposals, proposal) 130 return nil 131 }) 132 if err != nil { 133 return nil, err 134 } 135 136 return &foundation.QueryProposalsResponse{Proposals: proposals, Pagination: pageRes}, nil 137 } 138 139 func (s queryServer) Vote(c context.Context, req *foundation.QueryVoteRequest) (*foundation.QueryVoteResponse, error) { 140 if req == nil { 141 return nil, status.Error(codes.InvalidArgument, "invalid request") 142 } 143 144 ctx := sdk.UnwrapSDKContext(c) 145 voter, err := sdk.AccAddressFromBech32(req.Voter) 146 if err != nil { 147 return nil, status.Errorf(codes.InvalidArgument, "invalid voter address") 148 } 149 vote, err := s.keeper.GetVote(ctx, req.ProposalId, voter) 150 if err != nil { 151 return nil, err 152 } 153 154 return &foundation.QueryVoteResponse{Vote: vote}, nil 155 } 156 157 func (s queryServer) Votes(c context.Context, req *foundation.QueryVotesRequest) (*foundation.QueryVotesResponse, error) { 158 if req == nil { 159 return nil, status.Error(codes.InvalidArgument, "invalid request") 160 } 161 162 var votes []foundation.Vote 163 ctx := sdk.UnwrapSDKContext(c) 164 store := ctx.KVStore(s.keeper.storeKey) 165 voteStore := prefix.NewStore(store, append(voteKeyPrefix, Uint64ToBytes(req.ProposalId)...)) 166 pageRes, err := query.Paginate(voteStore, req.Pagination, func(key []byte, value []byte) error { 167 var vote foundation.Vote 168 s.keeper.cdc.MustUnmarshal(value, &vote) 169 votes = append(votes, vote) 170 return nil 171 }) 172 if err != nil { 173 return nil, err 174 } 175 176 return &foundation.QueryVotesResponse{Votes: votes, Pagination: pageRes}, nil 177 } 178 179 func (s queryServer) TallyResult(c context.Context, req *foundation.QueryTallyResultRequest) (*foundation.QueryTallyResultResponse, error) { 180 if req == nil { 181 return nil, status.Error(codes.InvalidArgument, "invalid request") 182 } 183 184 ctx := sdk.UnwrapSDKContext(c) 185 proposal, err := s.keeper.GetProposal(ctx, req.ProposalId) 186 if err != nil { 187 return nil, err 188 } 189 190 tally, err := s.keeper.tally(ctx, *proposal) 191 if err != nil { 192 return nil, err 193 } 194 195 return &foundation.QueryTallyResultResponse{Tally: tally}, nil 196 } 197 198 func (s queryServer) Censorships(c context.Context, req *foundation.QueryCensorshipsRequest) (*foundation.QueryCensorshipsResponse, error) { 199 if req == nil { 200 return nil, status.Error(codes.InvalidArgument, "invalid request") 201 } 202 203 var censorships []foundation.Censorship 204 ctx := sdk.UnwrapSDKContext(c) 205 store := ctx.KVStore(s.keeper.storeKey) 206 censorshipStore := prefix.NewStore(store, censorshipKeyPrefix) 207 pageRes, err := query.Paginate(censorshipStore, req.Pagination, func(key []byte, value []byte) error { 208 var censorship foundation.Censorship 209 s.keeper.cdc.MustUnmarshal(value, &censorship) 210 censorships = append(censorships, censorship) 211 return nil 212 }) 213 if err != nil { 214 return nil, err 215 } 216 217 return &foundation.QueryCensorshipsResponse{Censorships: censorships, Pagination: pageRes}, nil 218 } 219 220 func (s queryServer) Grants(c context.Context, req *foundation.QueryGrantsRequest) (*foundation.QueryGrantsResponse, error) { 221 if req == nil { 222 return nil, status.Error(codes.InvalidArgument, "invalid request") 223 } 224 225 grantee, err := sdk.AccAddressFromBech32(req.Grantee) 226 if err != nil { 227 return nil, err 228 } 229 230 ctx := sdk.UnwrapSDKContext(c) 231 store := ctx.KVStore(s.keeper.storeKey) 232 233 if req.MsgTypeUrl != "" { 234 keyPrefix := grantKey(grantee, req.MsgTypeUrl) 235 grantStore := prefix.NewStore(store, keyPrefix) 236 237 var authorizations []*codectypes.Any 238 _, err = query.Paginate(grantStore, req.Pagination, func(key []byte, value []byte) error { 239 var authorization foundation.Authorization 240 if err := s.keeper.cdc.UnmarshalInterface(value, &authorization); err != nil { 241 panic(err) 242 } 243 244 msg, ok := authorization.(proto.Message) 245 if !ok { 246 panic(sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg)) 247 } 248 any, err := codectypes.NewAnyWithValue(msg) 249 if err != nil { 250 panic(err) 251 } 252 authorizations = append(authorizations, any) 253 254 return nil 255 }) 256 if err != nil { 257 return nil, err 258 } 259 260 return &foundation.QueryGrantsResponse{Authorizations: authorizations}, nil 261 262 } 263 264 keyPrefix := grantKeyPrefixByGrantee(grantee) 265 grantStore := prefix.NewStore(store, keyPrefix) 266 267 var authorizations []*codectypes.Any 268 pageRes, err := query.Paginate(grantStore, req.Pagination, func(key []byte, value []byte) error { 269 var authorization foundation.Authorization 270 if err := s.keeper.cdc.UnmarshalInterface(value, &authorization); err != nil { 271 panic(err) 272 } 273 274 msg, ok := authorization.(proto.Message) 275 if !ok { 276 panic(sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg)) 277 } 278 any, err := codectypes.NewAnyWithValue(msg) 279 if err != nil { 280 panic(err) 281 } 282 authorizations = append(authorizations, any) 283 284 return nil 285 }) 286 if err != nil { 287 return nil, err 288 } 289 290 return &foundation.QueryGrantsResponse{Authorizations: authorizations, Pagination: pageRes}, nil 291 }