forked from evmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_query.go
195 lines (168 loc) · 5.49 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
package keeper
import (
"context"
"github.com/adminoid/cosmos-sdk/store/prefix"
sdk "github.com/adminoid/cosmos-sdk/types"
sdkerrors "github.com/adminoid/cosmos-sdk/types/errors"
"github.com/adminoid/cosmos-sdk/types/query"
"github.com/adminoid/cosmos-sdk/x/nft"
)
var _ nft.QueryServer = Keeper{}
// Balance return the number of NFTs of a given class owned by the owner, same as balanceOf in ERC721
func (k Keeper) Balance(goCtx context.Context, r *nft.QueryBalanceRequest) (*nft.QueryBalanceResponse, error) {
if r == nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}
if len(r.ClassId) == 0 {
return nil, nft.ErrEmptyClassID
}
owner, err := sdk.AccAddressFromBech32(r.Owner)
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(goCtx)
balance := k.GetBalance(ctx, r.ClassId, owner)
return &nft.QueryBalanceResponse{Amount: balance}, nil
}
// Owner return the owner of the NFT based on its class and id, same as ownerOf in ERC721
func (k Keeper) Owner(goCtx context.Context, r *nft.QueryOwnerRequest) (*nft.QueryOwnerResponse, error) {
if r == nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}
if len(r.ClassId) == 0 {
return nil, nft.ErrEmptyClassID
}
if len(r.Id) == 0 {
return nil, nft.ErrEmptyNFTID
}
ctx := sdk.UnwrapSDKContext(goCtx)
owner := k.GetOwner(ctx, r.ClassId, r.Id)
return &nft.QueryOwnerResponse{Owner: owner.String()}, nil
}
// Supply return the number of NFTs from the given class, same as totalSupply of ERC721.
func (k Keeper) Supply(goCtx context.Context, r *nft.QuerySupplyRequest) (*nft.QuerySupplyResponse, error) {
if r == nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}
if len(r.ClassId) == 0 {
return nil, nft.ErrEmptyClassID
}
ctx := sdk.UnwrapSDKContext(goCtx)
supply := k.GetTotalSupply(ctx, r.ClassId)
return &nft.QuerySupplyResponse{Amount: supply}, nil
}
// NFTs queries all NFTs of a given class or owner (at least one must be provided), similar to tokenByIndex in ERC721Enumerable
func (k Keeper) NFTs(goCtx context.Context, r *nft.QueryNFTsRequest) (*nft.QueryNFTsResponse, error) {
if r == nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}
var err error
var owner sdk.AccAddress
if len(r.Owner) > 0 {
owner, err = sdk.AccAddressFromBech32(r.Owner)
if err != nil {
return nil, err
}
}
var nfts []*nft.NFT
var pageRes *query.PageResponse
ctx := sdk.UnwrapSDKContext(goCtx)
switch {
case len(r.ClassId) > 0 && len(r.Owner) > 0:
if pageRes, err = query.Paginate(k.getClassStoreByOwner(ctx, owner, r.ClassId), r.Pagination, func(key []byte, _ []byte) error {
nft, has := k.GetNFT(ctx, r.ClassId, string(key))
if has {
nfts = append(nfts, &nft)
}
return nil
}); err != nil {
return nil, err
}
case len(r.ClassId) > 0 && len(r.Owner) == 0:
nftStore := k.getNFTStore(ctx, r.ClassId)
if pageRes, err = query.Paginate(nftStore, r.Pagination, func(_ []byte, value []byte) error {
var nft nft.NFT
if err := k.cdc.Unmarshal(value, &nft); err != nil {
return err
}
nfts = append(nfts, &nft)
return nil
}); err != nil {
return nil, err
}
case len(r.ClassId) == 0 && len(r.Owner) > 0:
if pageRes, err = query.Paginate(k.prefixStoreNftOfClassByOwner(ctx, owner), r.Pagination, func(key []byte, value []byte) error {
classID, nftID := parseNftOfClassByOwnerStoreKey(key)
if n, has := k.GetNFT(ctx, classID, nftID); has {
nfts = append(nfts, &n)
}
return nil
}); err != nil {
return nil, err
}
default:
return nil, sdkerrors.ErrInvalidRequest.Wrap("must provide at least one of classID or owner")
}
return &nft.QueryNFTsResponse{
Nfts: nfts,
Pagination: pageRes,
}, nil
}
// NFT return an NFT based on its class and id.
func (k Keeper) NFT(goCtx context.Context, r *nft.QueryNFTRequest) (*nft.QueryNFTResponse, error) {
if r == nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}
if len(r.ClassId) == 0 {
return nil, nft.ErrEmptyClassID
}
if len(r.Id) == 0 {
return nil, nft.ErrEmptyNFTID
}
ctx := sdk.UnwrapSDKContext(goCtx)
n, has := k.GetNFT(ctx, r.ClassId, r.Id)
if !has {
return nil, nft.ErrNFTNotExists.Wrapf("not found nft: class: %s, id: %s", r.ClassId, r.Id)
}
return &nft.QueryNFTResponse{Nft: &n}, nil
}
// Class return an NFT class based on its id
func (k Keeper) Class(goCtx context.Context, r *nft.QueryClassRequest) (*nft.QueryClassResponse, error) {
if r == nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}
if len(r.ClassId) == 0 {
return nil, nft.ErrEmptyClassID
}
ctx := sdk.UnwrapSDKContext(goCtx)
class, has := k.GetClass(ctx, r.ClassId)
if !has {
return nil, nft.ErrClassNotExists.Wrapf("not found class: %s", r.ClassId)
}
return &nft.QueryClassResponse{Class: &class}, nil
}
// Classes return all NFT classes
func (k Keeper) Classes(goCtx context.Context, r *nft.QueryClassesRequest) (*nft.QueryClassesResponse, error) {
if r == nil {
return nil, sdkerrors.ErrInvalidRequest.Wrap("empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
store := ctx.KVStore(k.storeKey)
classStore := prefix.NewStore(store, ClassKey)
var classes []*nft.Class
pageRes, err := query.Paginate(classStore, r.Pagination, func(_ []byte, value []byte) error {
var class nft.Class
if err := k.cdc.Unmarshal(value, &class); err != nil {
return err
}
classes = append(classes, &class)
return nil
})
if err != nil {
return nil, err
}
return &nft.QueryClassesResponse{
Classes: classes,
Pagination: pageRes,
}, nil
}