-
Notifications
You must be signed in to change notification settings - Fork 18
/
grpc_query_staker.go
72 lines (55 loc) · 2.01 KB
/
grpc_query_staker.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
package keeper
import (
"context"
"strings"
"github.com/KYVENetwork/chain/x/query/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) Stakers(c context.Context, req *types.QueryStakersRequest) (*types.QueryStakersResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
data := make([]types.FullStaker, 0)
req.Search = strings.ToLower(req.Search)
accumulator := func(address string, accumulate bool) bool {
fullStaker := k.GetFullStaker(ctx, address)
searchAddress := strings.ToLower(fullStaker.Address)
searchMoniker := strings.ToLower(fullStaker.Metadata.Moniker)
if strings.Contains(searchAddress, req.Search) || strings.Contains(searchMoniker, req.Search) {
if accumulate {
data = append(data, *fullStaker)
}
return true
}
return false
}
var pageRes *query.PageResponse
var err error
switch req.Status {
case types.STAKER_STATUS_ACTIVE:
pageRes, err = k.delegationKeeper.GetPaginatedActiveStakersByDelegation(ctx, req.Pagination, accumulator)
case types.STAKER_STATUS_INACTIVE:
pageRes, err = k.delegationKeeper.GetPaginatedInactiveStakersByDelegation(ctx, req.Pagination, accumulator)
default:
pageRes, err = k.delegationKeeper.GetPaginatedStakersByDelegation(ctx, req.Pagination, accumulator)
}
if err != nil {
return nil, err
}
return &types.QueryStakersResponse{Stakers: data, Pagination: pageRes}, nil
}
func (k Keeper) Staker(c context.Context, req *types.QueryStakerRequest) (*types.QueryStakerResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
if !k.stakerKeeper.DoesStakerExist(ctx, req.Address) {
return nil, sdkerrors.ErrKeyNotFound
}
return &types.QueryStakerResponse{Staker: *k.GetFullStaker(ctx, req.Address)}, nil
}