/
query_promoter.go
64 lines (52 loc) · 1.77 KB
/
query_promoter.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
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"
"github.com/sge-network/sge/x/reward/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) Promoters(goCtx context.Context, req *types.QueryPromotersRequest) (*types.QueryPromotersResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
var promoters []types.Promoter
ctx := sdk.UnwrapSDKContext(goCtx)
store := ctx.KVStore(k.storeKey)
promoterStore := prefix.NewStore(store, types.PromoterKeyPrefix)
pageRes, err := query.Paginate(promoterStore, req.Pagination, func(key []byte, value []byte) error {
var promoter types.Promoter
if err := k.cdc.Unmarshal(value, &promoter); err != nil {
return err
}
promoters = append(promoters, promoter)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryPromotersResponse{Promoter: promoters, Pagination: pageRes}, nil
}
func (k Keeper) PromoterByAddress(goCtx context.Context, req *types.QueryPromoterByAddressRequest) (*types.QueryPromoterByAddressResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
promoterByAddress, found := k.GetPromoterByAddress(
ctx,
req.Addr,
)
if !found {
return nil, status.Error(codes.NotFound, "promoter with the provided address not found")
}
promoter, found := k.GetPromoter(
ctx,
promoterByAddress.PromoterUID,
)
if !found {
return nil, status.Error(codes.NotFound, "promoter with the provided uid found")
}
return &types.QueryPromoterByAddressResponse{Promoter: promoter}, nil
}