-
Notifications
You must be signed in to change notification settings - Fork 41
/
query_server.go
86 lines (66 loc) · 2.21 KB
/
query_server.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
package keeper
import (
"context"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
hubtypes "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/provider/types"
)
var (
_ types.QueryServiceServer = (*queryServer)(nil)
)
type queryServer struct {
Keeper
}
func NewQueryServiceServer(keeper Keeper) types.QueryServiceServer {
return &queryServer{Keeper: keeper}
}
func (q *queryServer) QueryProvider(c context.Context, req *types.QueryProviderRequest) (*types.QueryProviderResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
address, err := hubtypes.ProvAddressFromBech32(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address %s", req.Address)
}
ctx := sdk.UnwrapSDKContext(c)
item, found := q.GetProvider(ctx, address)
if !found {
return nil, status.Errorf(codes.NotFound, "provider does not exist for address %s", req.Address)
}
return &types.QueryProviderResponse{Provider: item}, nil
}
func (q *queryServer) QueryProviders(c context.Context, req *types.QueryProvidersRequest) (*types.QueryProvidersResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
var (
items types.Providers
ctx = sdk.UnwrapSDKContext(c)
store = prefix.NewStore(q.Store(ctx), types.ProviderKeyPrefix)
)
pagination, err := query.FilteredPaginate(store, req.Pagination, func(_, value []byte, accumulate bool) (bool, error) {
var item types.Provider
if err := q.cdc.Unmarshal(value, &item); err != nil {
return false, err
}
if accumulate {
items = append(items, item)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryProvidersResponse{Providers: items, Pagination: pagination}, nil
}
func (q *queryServer) QueryParams(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
var (
ctx = sdk.UnwrapSDKContext(c)
params = q.GetParams(ctx)
)
return &types.QueryParamsResponse{Params: params}, nil
}