-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_query.go
89 lines (69 loc) · 2.2 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
package internal
import (
"context"
"cosmossdk.io/collections"
"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
testv1alpha1 "github.com/0tech/andromeda/x/test/andromeda/test/v1alpha1"
)
type queryServer struct {
keeper Keeper
}
var _ testv1alpha1.QueryServer = (*queryServer)(nil)
// NewQueryServer returns an implementation of the QueryServer interface
// for the provided Keeper.
func NewQueryServer(keeper Keeper) testv1alpha1.QueryServer {
return &queryServer{
keeper: keeper,
}
}
var errNilRequest = testv1alpha1.ErrUnimplemented.Wrap("nil request")
func (s queryServer) Asset(ctx context.Context, req *testv1alpha1.QueryAssetRequest) (*testv1alpha1.QueryAssetResponse, error) {
if req == nil {
return nil, errNilRequest
}
if req.Account == "" {
return nil, testv1alpha1.ErrUnimplemented.Wrap("nil account")
}
if req.Asset == "" {
return nil, testv1alpha1.ErrUnimplemented.Wrap("nil asset")
}
account, err := s.keeper.addressStringToBytes(req.Account)
if err != nil {
return nil, errors.Wrap(err, "account")
}
if err := s.keeper.HasAsset(ctx, account, req.Asset); err != nil {
return nil, err
}
return &testv1alpha1.QueryAssetResponse{
Asset: &testv1alpha1.QueryAssetResponse_Asset{
Name: req.Asset,
},
}, nil
}
func (s queryServer) Assets(ctx context.Context, req *testv1alpha1.QueryAssetsRequest) (*testv1alpha1.QueryAssetsResponse, error) {
if req == nil {
return nil, errNilRequest
}
if req.Account == "" {
return nil, testv1alpha1.ErrUnimplemented.Wrap("nil account")
}
account, err := s.keeper.addressStringToBytes(req.Account)
if err != nil {
return nil, errors.Wrap(err, "account")
}
assets, pageRes, err := query.CollectionPaginate(ctx, s.keeper.assets, req.Pagination, func(key collections.Pair[sdk.AccAddress, string], _ testv1alpha1.Asset) (*testv1alpha1.QueryAssetsResponse_Asset, error) {
asset := key.K2()
return &testv1alpha1.QueryAssetsResponse_Asset{
Name: asset,
}, nil
}, query.WithCollectionPaginationPairPrefix[sdk.AccAddress, string](account))
if err != nil {
return nil, err
}
return &testv1alpha1.QueryAssetsResponse{
Assets: assets,
Pagination: pageRes,
}, nil
}