/
grpc_query_listing.go
126 lines (99 loc) · 3.56 KB
/
grpc_query_listing.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
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/likecoin/likecoin-chain/v4/x/likenft/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) ListingIndex(c context.Context, req *types.QueryListingIndexRequest) (*types.QueryListingIndexResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
var listings []types.Listing
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
listingStore := prefix.NewStore(store, types.KeyPrefix(types.ListingKeyPrefix))
pageRes, err := query.Paginate(listingStore, req.Pagination, func(key []byte, value []byte) error {
var storeRecord types.ListingStoreRecord
if err := k.cdc.Unmarshal(value, &storeRecord); err != nil {
return err
}
listings = append(listings, storeRecord.ToPublicRecord())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryListingIndexResponse{Listings: listings, Pagination: pageRes}, nil
}
func (k Keeper) Listing(c context.Context, req *types.QueryListingRequest) (*types.QueryListingResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
seller, err := sdk.AccAddressFromBech32(req.Seller)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
val, found := k.GetListing(
ctx,
req.ClassId,
req.NftId,
seller,
)
if !found {
return nil, status.Error(codes.NotFound, "not found")
}
return &types.QueryListingResponse{Listing: val.ToPublicRecord()}, nil
}
func (k Keeper) ListingsByClass(goCtx context.Context, req *types.QueryListingsByClassRequest) (*types.QueryListingsByClassResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
var listings []types.Listing
ctx := sdk.UnwrapSDKContext(goCtx)
store := ctx.KVStore(k.storeKey)
subStore := prefix.NewStore(store, append(types.KeyPrefix(types.ListingKeyPrefix), types.ListingsByClassKey(req.ClassId)...))
pageRes, err := query.Paginate(subStore, req.Pagination, func(key []byte, value []byte) error {
var storeRecord types.ListingStoreRecord
if err := k.cdc.Unmarshal(value, &storeRecord); err != nil {
return err
}
listings = append(listings, storeRecord.ToPublicRecord())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryListingsByClassResponse{
Listings: listings,
Pagination: pageRes,
}, nil
}
func (k Keeper) ListingsByNFT(goCtx context.Context, req *types.QueryListingsByNFTRequest) (*types.QueryListingsByNFTResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
var listings []types.Listing
ctx := sdk.UnwrapSDKContext(goCtx)
store := ctx.KVStore(k.storeKey)
subStore := prefix.NewStore(store, append(types.KeyPrefix(types.ListingKeyPrefix), types.ListingsByNFTKey(req.ClassId, req.NftId)...))
pageRes, err := query.Paginate(subStore, req.Pagination, func(key []byte, value []byte) error {
var storeRecord types.ListingStoreRecord
if err := k.cdc.Unmarshal(value, &storeRecord); err != nil {
return err
}
listings = append(listings, storeRecord.ToPublicRecord())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryListingsByNFTResponse{
Listings: listings,
Pagination: pageRes,
}, nil
}