/
grpc_query.go
141 lines (118 loc) · 3.9 KB
/
grpc_query.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
package keeper
import (
"context"
"time"
"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/OmniFlix/omniflixhub/v3/x/itc/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var _ types.QueryServer = Keeper{}
// Params returns params of the itc module.
func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
// Campaign returns details of the campaign
func (k Keeper) Campaign(goCtx context.Context,
req *types.QueryCampaignRequest,
) (*types.QueryCampaignResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
campaign, found := k.GetCampaign(ctx, req.CampaignId)
if !found {
return nil, status.Errorf(codes.NotFound, "campaign %d not found", req.CampaignId)
}
return &types.QueryCampaignResponse{Campaign: campaign}, nil
}
func (k Keeper) Campaigns(goCtx context.Context,
req *types.QueryCampaignsRequest,
) (*types.QueryCampaignsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
var filteredCampaigns []types.Campaign
var pageRes *query.PageResponse
store := ctx.KVStore(k.storeKey)
campaignStore := prefix.NewStore(store, types.PrefixCampaignId)
pageRes, err := query.FilteredPaginate(campaignStore,
req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var al types.Campaign
k.cdc.MustUnmarshal(value, &al)
matchCreator, matchStatus := true, true
// match status (if supplied/valid)
if len(req.Status.String()) > 0 && types.ValidCampaignStatus(req.Status) {
if req.Status == types.CAMPAIGN_STATUS_ACTIVE {
matchStatus = al.StartTime.Before(time.Now())
} else if req.Status == types.CAMPAIGN_STATUS_INACTIVE {
matchStatus = al.StartTime.After(time.Now())
}
}
// match creator address (if supplied)
if len(req.Creator) > 0 {
creator, err := sdk.AccAddressFromBech32(req.Creator)
if err != nil {
return false, err
}
matchCreator = al.Creator == creator.String()
}
if matchCreator && matchStatus {
if accumulate {
filteredCampaigns = append(filteredCampaigns, al)
}
return true, nil
}
return false, nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
return &types.QueryCampaignsResponse{Campaigns: filteredCampaigns, Pagination: pageRes}, nil
}
func (k Keeper) Claims(goCtx context.Context,
req *types.QueryClaimsRequest,
) (*types.QueryClaimsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
var filteredClaims []types.Claim
var pageRes *query.PageResponse
store := ctx.KVStore(k.storeKey)
claimStore := prefix.NewStore(store, types.PrefixClaimByNftId)
pageRes, err := query.FilteredPaginate(claimStore,
req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var al types.Claim
k.cdc.MustUnmarshal(value, &al)
matchClaimer := true
matchCampaignId := al.CampaignId == req.CampaignId
// match claimer address (if supplied)
if len(req.Address) > 0 {
address, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return false, err
}
matchClaimer = al.Address == address.String()
}
if matchCampaignId && matchClaimer {
if accumulate {
filteredClaims = append(filteredClaims, al)
}
return true, nil
}
return false, nil
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
return &types.QueryClaimsResponse{
Claims: filteredClaims,
Pagination: pageRes,
}, nil
}