-
Notifications
You must be signed in to change notification settings - Fork 45
/
grpc_query.go
279 lines (225 loc) · 9.8 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package keeper
import (
"context"
"strings"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/desmos-labs/desmos/v7/x/profiles/types"
)
var _ types.QueryServer = Keeper{}
// Profile implements the Query/Profile gRPC method
func (k Keeper) Profile(ctx context.Context, request *types.QueryProfileRequest) (*types.QueryProfileResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
dTagOrAddress := request.User
if strings.TrimSpace(dTagOrAddress) == "" {
return nil, errors.Wrap(sdkerrors.ErrInvalidRequest, "DTag or address cannot be empty or blank")
}
sdkAddress, err := sdk.AccAddressFromBech32(dTagOrAddress)
if err != nil {
addr := k.GetAddressFromDTag(sdkCtx, dTagOrAddress)
if addr == "" {
return nil, errors.Wrapf(sdkerrors.ErrInvalidRequest,
"No address related to this DTag: %s", dTagOrAddress)
}
sdkAddress, err = sdk.AccAddressFromBech32(addr)
if err != nil {
return nil, errors.Wrapf(sdkerrors.ErrInvalidAddress, addr)
}
}
account, found, err := k.GetProfile(sdkCtx, sdkAddress.String())
if err != nil {
return nil, err
}
if !found {
return nil, status.Errorf(codes.NotFound, "profile for dtag/address %s not found", dTagOrAddress)
}
accountAny, err := codectypes.NewAnyWithValue(account)
if err != nil {
return nil, err
}
return &types.QueryProfileResponse{Profile: accountAny}, nil
}
func (k Keeper) IncomingDTagTransferRequests(ctx context.Context, request *types.QueryIncomingDTagTransferRequestsRequest) (*types.QueryIncomingDTagTransferRequestsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
// Get user requests prefix store
store := sdkCtx.KVStore(k.storeKey)
reqStore := prefix.NewStore(store, types.DTagTransferRequestPrefix)
if request.Receiver != "" {
reqStore = prefix.NewStore(store, types.IncomingDTagTransferRequestsPrefix(request.Receiver))
}
// Get paginated user requests
var requests []types.DTagTransferRequest
pageRes, err := query.Paginate(reqStore, request.Pagination, func(key []byte, value []byte) error {
var req types.DTagTransferRequest
if err := k.cdc.Unmarshal(value, &req); err != nil {
return status.Error(codes.Internal, err.Error())
}
requests = append(requests, req)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryIncomingDTagTransferRequestsResponse{Requests: requests, Pagination: pageRes}, nil
}
// ChainLinks implements the Query/ChainLinks gRPC method
func (k Keeper) ChainLinks(ctx context.Context, request *types.QueryChainLinksRequest) (*types.QueryChainLinksResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)
// Get user chain links prefix store
linksPrefix := types.ChainLinksPrefix
switch {
case request.User != "" && request.ChainName != "":
linksPrefix = types.ChainLinksStoreKey(request.User, request.ChainName, request.Target)
case request.User != "":
linksPrefix = types.UserChainLinksChainPrefix(request.User, request.ChainName)
}
// Get paginated user chain links
var links []types.ChainLink
linksStore := prefix.NewStore(store, linksPrefix)
pageRes, err := query.Paginate(linksStore, request.Pagination, func(key []byte, value []byte) error {
var link types.ChainLink
if err := k.cdc.Unmarshal(value, &link); err != nil {
return status.Error(codes.Internal, err.Error())
}
links = append(links, link)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryChainLinksResponse{Links: links, Pagination: pageRes}, nil
}
// ChainLinkOwners implements the Query/ChainLinkOwners gRPC method
func (k Keeper) ChainLinkOwners(ctx context.Context, request *types.QueryChainLinkOwnersRequest) (*types.QueryChainLinkOwnersResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)
ownersPrefix := types.ChainLinkChainPrefix
if request.ChainName != "" {
ownersPrefix = types.ChainLinkChainAddressKey(request.ChainName, request.Target)
}
var owners []types.QueryChainLinkOwnersResponse_ChainLinkOwnerDetails
ownersStore := prefix.NewStore(store, ownersPrefix)
pageRes, err := query.Paginate(ownersStore, request.Pagination, func(key []byte, value []byte) error {
// Re-add the prefix because the prefix store trims it out, and we need it to get the data
keyWithPrefix := append([]byte(nil), ownersPrefix...)
keyWithPrefix = append(keyWithPrefix, key...)
chainName, target, user := types.GetChainLinkOwnerData(keyWithPrefix)
owners = append(owners, types.QueryChainLinkOwnersResponse_ChainLinkOwnerDetails{
User: user,
ChainName: chainName,
Target: target,
})
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryChainLinkOwnersResponse{Owners: owners, Pagination: pageRes}, nil
}
// DefaultExternalAddresses implements the Query/DefaultExternalAddresses gRPC method
func (k Keeper) DefaultExternalAddresses(ctx context.Context, request *types.QueryDefaultExternalAddressesRequest) (*types.QueryDefaultExternalAddressesResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)
defaultPrefix := types.DefaultExternalAddressPrefix
if request.Owner != "" {
defaultPrefix = types.DefaultExternalAddressKey(request.Owner, request.ChainName)
}
var links []types.ChainLink
defaultStore := prefix.NewStore(store, defaultPrefix)
pageRes, err := query.Paginate(defaultStore, request.Pagination, func(key []byte, value []byte) error {
// Re-add the prefix because the prefix store trims it out, and we need it to get the data
keyWithPrefix := append([]byte(nil), defaultPrefix...)
keyWithPrefix = append(keyWithPrefix, key...)
owner, chainName := types.GetDefaultExternalAddressData(keyWithPrefix)
link, found := k.GetChainLink(sdkCtx, owner, chainName, string(value))
if !found {
return errors.Wrap(sdkerrors.ErrNotFound, "chain link not found")
}
links = append(links, link)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryDefaultExternalAddressesResponse{Links: links, Pagination: pageRes}, nil
}
// ApplicationLinks implements the Query/ApplicationLinks gRPC method
func (k Keeper) ApplicationLinks(ctx context.Context, request *types.QueryApplicationLinksRequest) (*types.QueryApplicationLinksResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)
// Get user links prefix store
linksPrefix := types.ApplicationLinkPrefix
switch {
case request.User != "" && request.Application != "":
linksPrefix = types.UserApplicationLinkKey(request.User, request.Application, request.Username)
case request.User != "":
linksPrefix = types.UserApplicationLinksApplicationPrefix(request.User, request.Application)
}
// Get paginated user links
var links []types.ApplicationLink
linksStore := prefix.NewStore(store, linksPrefix)
pageRes, err := query.Paginate(linksStore, request.Pagination, func(key []byte, value []byte) error {
var link types.ApplicationLink
if err := k.cdc.Unmarshal(value, &link); err != nil {
return status.Error(codes.Internal, err.Error())
}
links = append(links, link)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryApplicationLinksResponse{Links: links, Pagination: pageRes}, nil
}
// ApplicationLinkByClientID implements the Query/ApplicationLinkByClientID gRPC method
func (k Keeper) ApplicationLinkByClientID(ctx context.Context, request *types.QueryApplicationLinkByClientIDRequest) (*types.QueryApplicationLinkByClientIDResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
link, found, err := k.GetApplicationLinkByClientID(sdkCtx, request.ClientId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if !found {
return nil, status.Errorf(codes.NotFound, "link for client id %s not found", request.ClientId)
}
return &types.QueryApplicationLinkByClientIDResponse{Link: link}, nil
}
// ApplicationLinkOwners implements the Query/ApplicationLinkOwners gRPC method
func (k Keeper) ApplicationLinkOwners(ctx context.Context, request *types.QueryApplicationLinkOwnersRequest) (*types.QueryApplicationLinkOwnersResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)
ownersPrefix := types.ApplicationLinkAppPrefix
if request.Application != "" {
ownersPrefix = types.ApplicationLinkAppUsernameKey(request.Application, request.Username)
}
var owners []types.QueryApplicationLinkOwnersResponse_ApplicationLinkOwnerDetails
ownersStore := prefix.NewStore(store, ownersPrefix)
pageRes, err := query.Paginate(ownersStore, request.Pagination, func(key []byte, value []byte) error {
// Re-add the prefix because the prefix store trims it out, and we need it to get the data
keyWithPrefix := append([]byte(nil), ownersPrefix...)
keyWithPrefix = append(keyWithPrefix, key...)
application, username, user := types.GetApplicationLinkOwnerData(keyWithPrefix)
owners = append(owners, types.QueryApplicationLinkOwnersResponse_ApplicationLinkOwnerDetails{
User: user,
Application: application,
Username: username,
})
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryApplicationLinkOwnersResponse{Owners: owners, Pagination: pageRes}, nil
}
// Params implements the Query/Params gRPC method
func (k Keeper) Params(ctx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
params := k.GetParams(sdkCtx)
return &types.QueryParamsResponse{Params: params}, nil
}