-
Notifications
You must be signed in to change notification settings - Fork 324
/
grpc_query.go
139 lines (109 loc) · 4.39 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
package keeper
import (
"context"
"encoding/json"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/dymensionxyz/dymension/v3/x/streamer/types"
)
var _ types.QueryServer = Querier{}
// Querier defines a wrapper around the streamer module keeper providing gRPC method handlers.
type Querier struct {
Keeper
}
// NewQuerier creates a new Querier struct.
func NewQuerier(k Keeper) Querier {
return Querier{Keeper: k}
}
// ModuleToDistributeCoins returns coins that are going to be distributed.
func (q Querier) ModuleToDistributeCoins(goCtx context.Context, _ *types.ModuleToDistributeCoinsRequest) (*types.ModuleToDistributeCoinsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
return &types.ModuleToDistributeCoinsResponse{Coins: q.Keeper.GetModuleToDistributeCoins(ctx)}, nil
}
// StreamByID takes a streamID and returns its respective stream.
func (q Querier) StreamByID(goCtx context.Context, req *types.StreamByIDRequest) (*types.StreamByIDResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
stream, err := q.Keeper.GetStreamByID(ctx, req.Id)
if err != nil {
return nil, err
}
return &types.StreamByIDResponse{Stream: stream}, nil
}
// Streams returns all upcoming and active streams.
func (q Querier) Streams(goCtx context.Context, req *types.StreamsRequest) (*types.StreamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
pageRes, streams, err := q.filterByPrefixAndDenom(ctx, types.KeyPrefixStreams, "", req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.StreamsResponse{Data: streams, Pagination: pageRes}, nil
}
// ActiveStreams returns all active streams.
func (q Querier) ActiveStreams(goCtx context.Context, req *types.ActiveStreamsRequest) (*types.ActiveStreamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
pageRes, streams, err := q.filterByPrefixAndDenom(ctx, types.KeyPrefixActiveStreams, "", req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.ActiveStreamsResponse{Data: streams, Pagination: pageRes}, nil
}
// UpcomingStreams returns all upcoming streams.
func (q Querier) UpcomingStreams(goCtx context.Context, req *types.UpcomingStreamsRequest) (*types.UpcomingStreamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
pageRes, streams, err := q.filterByPrefixAndDenom(ctx, types.KeyPrefixUpcomingStreams, "", req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.UpcomingStreamsResponse{Data: streams, Pagination: pageRes}, nil
}
// getStreamFromIDJsonBytes returns streams from the json bytes of streamIDs.
func (q Querier) getStreamFromIDJsonBytes(ctx sdk.Context, refValue []byte) ([]types.Stream, error) {
streams := []types.Stream{}
streamIDs := []uint64{}
err := json.Unmarshal(refValue, &streamIDs)
if err != nil {
return streams, err
}
for _, streamID := range streamIDs {
stream, err := q.Keeper.GetStreamByID(ctx, streamID)
if err != nil {
return []types.Stream{}, err
}
streams = append(streams, *stream)
}
return streams, nil
}
// filterByPrefixAndDenom filters streams based on a given key prefix and denom
func (q Querier) filterByPrefixAndDenom(ctx sdk.Context, prefixType []byte, denom string, pagination *query.PageRequest) (*query.PageResponse, []types.Stream, error) {
streams := []types.Stream{}
store := ctx.KVStore(q.Keeper.storeKey)
valStore := prefix.NewStore(store, prefixType)
pageRes, err := query.FilteredPaginate(valStore, pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
// this may return multiple streams at once if two streams start at the same time.
// for now this is treated as an edge case that is not of importance
newStreams, err := q.getStreamFromIDJsonBytes(ctx, value)
if err != nil {
return false, err
}
if accumulate {
streams = append(streams, newStreams...)
}
return true, nil
})
return pageRes, streams, err
}