@@ -28,47 +28,39 @@ func NewQueryServer(keeper Keeper) collection.QueryServer {
28
28
}
29
29
}
30
30
31
- func (s queryServer ) validateExistenceOfCollectionGRPC (ctx sdk.Context , id string ) error {
32
- if _ , err := s .keeper .GetContract (ctx , id ); err != nil {
33
- return status .Error (codes .NotFound , err .Error ())
31
+ func (s queryServer ) addressFromBech32GRPC (bech32 string , context string ) (sdk.AccAddress , error ) {
32
+ addr , err := sdk .AccAddressFromBech32 (bech32 )
33
+ if err != nil {
34
+ return nil , status .Error (codes .InvalidArgument , sdkerrors .Wrap (sdkerrors .ErrInvalidAddress .Wrap (bech32 ), context ).Error ())
34
35
}
35
36
36
- return nil
37
+ return addr , nil
37
38
}
38
39
39
- func (s queryServer ) validateExistenceOfFTClassGRPC (ctx sdk.Context , contractID , classID string ) error {
40
+ func (s queryServer ) assertTokenIsFungible (ctx sdk.Context , contractID string , classID string ) error {
40
41
class , err := s .keeper .GetTokenClass (ctx , contractID , classID )
41
42
if err != nil {
42
- return status . Error ( codes . NotFound , err . Error ())
43
+ return err
43
44
}
44
45
45
- _ , ok := class .(* collection.FTClass )
46
- if ! ok {
47
- return status .Error (codes .NotFound , sdkerrors .ErrInvalidType .Wrapf ("not a class of fungible token: %s" , classID ).Error ())
46
+ if _ , ok := class .(* collection.FTClass ); ! ok {
47
+ return collection .ErrTokenNotExist .Wrap (collection .NewFTID (classID ))
48
48
}
49
+
49
50
return nil
50
51
}
51
52
52
- func (s queryServer ) validateExistenceOfNFTClassGRPC (ctx sdk.Context , contractID , classID string ) error {
53
+ func (s queryServer ) assertTokenTypeIsNonFungible (ctx sdk.Context , contractID string , classID string ) error {
53
54
class , err := s .keeper .GetTokenClass (ctx , contractID , classID )
54
55
if err != nil {
55
- return status . Error ( codes . NotFound , err . Error ())
56
+ return err
56
57
}
57
58
58
- _ , ok := class .(* collection.NFTClass )
59
- if ! ok {
60
- return status .Error (codes .NotFound , sdkerrors .ErrInvalidType .Wrapf ("not a class of non-fungible token: %s" , classID ).Error ())
59
+ if _ , ok := class .(* collection.NFTClass ); ! ok {
60
+ return collection .ErrTokenTypeNotExist .Wrap (classID )
61
61
}
62
- return nil
63
- }
64
62
65
- func (s queryServer ) addressFromBech32GRPC (bech32 string , context string ) (sdk.AccAddress , error ) {
66
- addr , err := sdk .AccAddressFromBech32 (bech32 )
67
- if err != nil {
68
- return nil , status .Error (codes .InvalidArgument , sdkerrors .Wrap (sdkerrors .ErrInvalidAddress .Wrap (bech32 ), context ).Error ())
69
- }
70
-
71
- return addr , nil
63
+ return nil
72
64
}
73
65
74
66
var _ collection.QueryServer = queryServer {}
@@ -157,12 +149,8 @@ func (s queryServer) FTSupply(c context.Context, req *collection.QueryFTSupplyRe
157
149
158
150
ctx := sdk .UnwrapSDKContext (c )
159
151
160
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
161
- return nil , err
162
- }
163
-
164
- if err := s .validateExistenceOfFTClassGRPC (ctx , req .ContractId , classID ); err != nil {
165
- return nil , err
152
+ if err := s .assertTokenIsFungible (ctx , req .ContractId , classID ); err != nil {
153
+ return & collection.QueryFTSupplyResponse {Supply : sdk .ZeroInt ()}, nil
166
154
}
167
155
168
156
supply := s .keeper .GetSupply (ctx , req .ContractId , classID )
@@ -187,12 +175,8 @@ func (s queryServer) FTMinted(c context.Context, req *collection.QueryFTMintedRe
187
175
188
176
ctx := sdk .UnwrapSDKContext (c )
189
177
190
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
191
- return nil , err
192
- }
193
-
194
- if err := s .validateExistenceOfFTClassGRPC (ctx , req .ContractId , classID ); err != nil {
195
- return nil , err
178
+ if err := s .assertTokenIsFungible (ctx , req .ContractId , classID ); err != nil {
179
+ return & collection.QueryFTMintedResponse {Minted : sdk .ZeroInt ()}, nil
196
180
}
197
181
198
182
minted := s .keeper .GetMinted (ctx , req .ContractId , classID )
@@ -217,12 +201,8 @@ func (s queryServer) FTBurnt(c context.Context, req *collection.QueryFTBurntRequ
217
201
218
202
ctx := sdk .UnwrapSDKContext (c )
219
203
220
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
221
- return nil , err
222
- }
223
-
224
- if err := s .validateExistenceOfFTClassGRPC (ctx , req .ContractId , classID ); err != nil {
225
- return nil , err
204
+ if err := s .assertTokenIsFungible (ctx , req .ContractId , classID ); err != nil {
205
+ return & collection.QueryFTBurntResponse {Burnt : sdk .ZeroInt ()}, nil
226
206
}
227
207
228
208
burnt := s .keeper .GetBurnt (ctx , req .ContractId , classID )
@@ -246,12 +226,8 @@ func (s queryServer) NFTSupply(c context.Context, req *collection.QueryNFTSupply
246
226
247
227
ctx := sdk .UnwrapSDKContext (c )
248
228
249
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
250
- return nil , err
251
- }
252
-
253
- if err := s .validateExistenceOfNFTClassGRPC (ctx , req .ContractId , classID ); err != nil {
254
- return nil , err
229
+ if err := s .assertTokenTypeIsNonFungible (ctx , req .ContractId , classID ); err != nil {
230
+ return & collection.QueryNFTSupplyResponse {Supply : sdk .ZeroInt ()}, nil
255
231
}
256
232
257
233
supply := s .keeper .GetSupply (ctx , req .ContractId , classID )
@@ -275,12 +251,8 @@ func (s queryServer) NFTMinted(c context.Context, req *collection.QueryNFTMinted
275
251
276
252
ctx := sdk .UnwrapSDKContext (c )
277
253
278
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
279
- return nil , err
280
- }
281
-
282
- if err := s .validateExistenceOfNFTClassGRPC (ctx , req .ContractId , classID ); err != nil {
283
- return nil , err
254
+ if err := s .assertTokenTypeIsNonFungible (ctx , req .ContractId , classID ); err != nil {
255
+ return & collection.QueryNFTMintedResponse {Minted : sdk .ZeroInt ()}, nil
284
256
}
285
257
286
258
minted := s .keeper .GetMinted (ctx , req .ContractId , classID )
@@ -304,12 +276,8 @@ func (s queryServer) NFTBurnt(c context.Context, req *collection.QueryNFTBurntRe
304
276
305
277
ctx := sdk .UnwrapSDKContext (c )
306
278
307
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
308
- return nil , err
309
- }
310
-
311
- if err := s .validateExistenceOfNFTClassGRPC (ctx , req .ContractId , classID ); err != nil {
312
- return nil , err
279
+ if err := s .assertTokenTypeIsNonFungible (ctx , req .ContractId , classID ); err != nil {
280
+ return & collection.QueryNFTBurntResponse {Burnt : sdk .ZeroInt ()}, nil
313
281
}
314
282
315
283
burnt := s .keeper .GetBurnt (ctx , req .ContractId , classID )
@@ -350,11 +318,6 @@ func (s queryServer) TokenClassTypeName(c context.Context, req *collection.Query
350
318
}
351
319
352
320
ctx := sdk .UnwrapSDKContext (c )
353
-
354
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
355
- return nil , err
356
- }
357
-
358
321
class , err := s .keeper .GetTokenClass (ctx , req .ContractId , req .ClassId )
359
322
if err != nil {
360
323
return nil , status .Error (codes .NotFound , err .Error ())
@@ -379,11 +342,6 @@ func (s queryServer) TokenType(c context.Context, req *collection.QueryTokenType
379
342
}
380
343
381
344
ctx := sdk .UnwrapSDKContext (c )
382
-
383
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
384
- return nil , err
385
- }
386
-
387
345
class , err := s .keeper .GetTokenClass (ctx , req .ContractId , classID )
388
346
if err != nil {
389
347
return nil , status .Error (codes .NotFound , err .Error ())
@@ -459,11 +417,6 @@ func (s queryServer) Token(c context.Context, req *collection.QueryTokenRequest)
459
417
}
460
418
461
419
ctx := sdk .UnwrapSDKContext (c )
462
-
463
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
464
- return nil , err
465
- }
466
-
467
420
legacyToken , err := s .getToken (ctx , req .ContractId , req .TokenId )
468
421
if err != nil {
469
422
return nil , status .Error (codes .NotFound , err .Error ())
@@ -491,11 +444,6 @@ func (s queryServer) Root(c context.Context, req *collection.QueryRootRequest) (
491
444
}
492
445
493
446
ctx := sdk .UnwrapSDKContext (c )
494
-
495
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
496
- return nil , err
497
- }
498
-
499
447
if err := s .keeper .hasNFT (ctx , req .ContractId , req .TokenId ); err != nil {
500
448
return nil , status .Error (codes .NotFound , err .Error ())
501
449
}
@@ -514,12 +462,15 @@ func (s queryServer) HasParent(c context.Context, req *collection.QueryHasParent
514
462
return nil , status .Error (codes .InvalidArgument , "empty request" )
515
463
}
516
464
517
- ctx := sdk .UnwrapSDKContext (c )
465
+ if err := collection .ValidateContractID (req .GetContractId ()); err != nil {
466
+ return nil , status .Error (codes .InvalidArgument , err .Error ())
467
+ }
518
468
519
- if err := s . validateGetParentVariants ( ctx , req ); err != nil {
520
- return nil , err
469
+ if err := collection . ValidateNFTID ( req . GetTokenId () ); err != nil {
470
+ return nil , status . Error ( codes . InvalidArgument , err . Error ())
521
471
}
522
472
473
+ ctx := sdk .UnwrapSDKContext (c )
523
474
_ , err := s .keeper .GetParent (ctx , req .ContractId , req .TokenId )
524
475
return & collection.QueryHasParentResponse {HasParent : (err == nil )}, nil
525
476
}
@@ -529,12 +480,15 @@ func (s queryServer) Parent(c context.Context, req *collection.QueryParentReques
529
480
return nil , status .Error (codes .InvalidArgument , "empty request" )
530
481
}
531
482
532
- ctx := sdk .UnwrapSDKContext (c )
483
+ if err := collection .ValidateContractID (req .GetContractId ()); err != nil {
484
+ return nil , status .Error (codes .InvalidArgument , err .Error ())
485
+ }
533
486
534
- if err := s . validateGetParentVariants ( ctx , req ); err != nil {
535
- return nil , err
487
+ if err := collection . ValidateNFTID ( req . GetTokenId () ); err != nil {
488
+ return nil , status . Error ( codes . InvalidArgument , err . Error ())
536
489
}
537
490
491
+ ctx := sdk .UnwrapSDKContext (c )
538
492
parent , err := s .keeper .GetParent (ctx , req .ContractId , req .TokenId )
539
493
if err != nil {
540
494
return nil , status .Error (codes .NotFound , err .Error ())
@@ -548,31 +502,6 @@ func (s queryServer) Parent(c context.Context, req *collection.QueryParentReques
548
502
return & collection.QueryParentResponse {Parent : * token }, nil
549
503
}
550
504
551
- type parentRequest interface {
552
- GetContractId () string
553
- GetTokenId () string
554
- }
555
-
556
- func (s queryServer ) validateGetParentVariants (ctx sdk.Context , req parentRequest ) error {
557
- if err := collection .ValidateContractID (req .GetContractId ()); err != nil {
558
- return status .Error (codes .InvalidArgument , err .Error ())
559
- }
560
-
561
- if err := collection .ValidateNFTID (req .GetTokenId ()); err != nil {
562
- return status .Error (codes .InvalidArgument , err .Error ())
563
- }
564
-
565
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .GetContractId ()); err != nil {
566
- return err
567
- }
568
-
569
- if err := s .keeper .hasNFT (ctx , req .GetContractId (), req .GetTokenId ()); err != nil {
570
- return status .Error (codes .NotFound , err .Error ())
571
- }
572
-
573
- return nil
574
- }
575
-
576
505
func (s queryServer ) Children (c context.Context , req * collection.QueryChildrenRequest ) (* collection.QueryChildrenResponse , error ) {
577
506
if req == nil {
578
507
return nil , status .Error (codes .InvalidArgument , "empty request" )
@@ -587,15 +516,6 @@ func (s queryServer) Children(c context.Context, req *collection.QueryChildrenRe
587
516
}
588
517
589
518
ctx := sdk .UnwrapSDKContext (c )
590
-
591
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
592
- return nil , err
593
- }
594
-
595
- if err := s .keeper .hasNFT (ctx , req .ContractId , req .TokenId ); err != nil {
596
- return nil , status .Error (codes .NotFound , err .Error ())
597
- }
598
-
599
519
store := ctx .KVStore (s .keeper .storeKey )
600
520
childStore := prefix .NewStore (store , childKeyPrefixByTokenID (req .ContractId , req .TokenId ))
601
521
var children []collection.NFT
@@ -631,11 +551,6 @@ func (s queryServer) GranteeGrants(c context.Context, req *collection.QueryGrant
631
551
}
632
552
633
553
ctx := sdk .UnwrapSDKContext (c )
634
-
635
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
636
- return nil , err
637
- }
638
-
639
554
store := ctx .KVStore (s .keeper .storeKey )
640
555
grantStore := prefix .NewStore (store , grantKeyPrefixByGrantee (req .ContractId , granteeAddr ))
641
556
var grants []collection.Grant
@@ -673,11 +588,6 @@ func (s queryServer) IsOperatorFor(c context.Context, req *collection.QueryIsOpe
673
588
}
674
589
675
590
ctx := sdk .UnwrapSDKContext (c )
676
-
677
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
678
- return nil , err
679
- }
680
-
681
591
_ , err = s .keeper .GetAuthorization (ctx , req .ContractId , holder , operator )
682
592
authorized := (err == nil )
683
593
@@ -699,11 +609,6 @@ func (s queryServer) HoldersByOperator(c context.Context, req *collection.QueryH
699
609
}
700
610
701
611
ctx := sdk .UnwrapSDKContext (c )
702
-
703
- if err := s .validateExistenceOfCollectionGRPC (ctx , req .ContractId ); err != nil {
704
- return nil , err
705
- }
706
-
707
612
store := ctx .KVStore (s .keeper .storeKey )
708
613
authorizationStore := prefix .NewStore (store , authorizationKeyPrefixByOperator (req .ContractId , operator ))
709
614
var holders []string
0 commit comments