Skip to content

Commit

Permalink
feat(x/nft): add new queries for nft modules that support query string (
Browse files Browse the repository at this point in the history
#18355)

Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
hieuvubk and julienrbrt committed Nov 17, 2023
1 parent a27cfa0 commit d322b23
Show file tree
Hide file tree
Showing 9 changed files with 12,570 additions and 2,231 deletions.
8,028 changes: 6,526 additions & 1,502 deletions api/cosmos/nft/v1beta1/query.pulsar.go

Large diffs are not rendered by default.

209 changes: 202 additions & 7 deletions api/cosmos/nft/v1beta1/query_grpc.pb.go

Large diffs are not rendered by default.

2,823 changes: 2,440 additions & 383 deletions client/docs/swagger-ui/swagger.yaml

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions proto/cosmos/nft/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,37 @@ service Query {
option (google.api.http).get = "/cosmos/nft/v1beta1/balance/{owner}/{class_id}";
}

// BalancebyQueryString queries the number of NFTs of a given class owned by the owner, same as balanceOf in ERC721
//
// Since: nft v0.1.1
rpc BalanceByQueryString(QueryBalanceByQueryStringRequest) returns (QueryBalanceByQueryStringResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/balance";
}

// Owner queries the owner of the NFT based on its class and id, same as ownerOf in ERC721
rpc Owner(QueryOwnerRequest) returns (QueryOwnerResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/owner/{class_id}/{id}";
}

// OwnerByQueryString queries the owner of the NFT based on its class and id, same as ownerOf in ERC721
//
// Since: nft v0.1.1
rpc OwnerByQueryString(QueryOwnerByQueryStringRequest) returns (QueryOwnerByQueryStringResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/owner";
}

// Supply queries the number of NFTs from the given class, same as totalSupply of ERC721.
rpc Supply(QuerySupplyRequest) returns (QuerySupplyResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/supply/{class_id}";
}

// SupplyByQueryString queries the number of NFTs from the given class, same as totalSupply of ERC721.
//
// Since: nft v0.1.1
rpc SupplyByQueryString(QuerySupplyByQueryStringRequest) returns (QuerySupplyByQueryStringResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/supply";
}

// NFTs queries all NFTs of a given class or owner,choose at least one of the two, similar to tokenByIndex in
// ERC721Enumerable
rpc NFTs(QueryNFTsRequest) returns (QueryNFTsResponse) {
Expand All @@ -35,11 +56,25 @@ service Query {
option (google.api.http).get = "/cosmos/nft/v1beta1/nfts/{class_id}/{id}";
}

// NFTByQueryString queries an NFT based on its class and id.
//
// Since: nft v0.1.1
rpc NFTByQueryString(QueryNFTByQueryStringRequest) returns (QueryNFTByQueryStringResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/nft";
}

// Class queries an NFT class based on its id
rpc Class(QueryClassRequest) returns (QueryClassResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/classes/{class_id}";
}

// Class queries an NFT class based on its id
//
// Since: nft v0.1.1
rpc ClassByQueryString(QueryClassByQueryStringRequest) returns (QueryClassByQueryStringResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/class";
}

// Classes queries all NFT classes
rpc Classes(QueryClassesRequest) returns (QueryClassesResponse) {
option (google.api.http).get = "/cosmos/nft/v1beta1/classes";
Expand All @@ -55,12 +90,31 @@ message QueryBalanceRequest {
string owner = 2;
}

// QueryBalanceByQueryStringRequest is the request type for the Query/Balance RPC method
//
// Since: nft v0.1.1
message QueryBalanceByQueryStringRequest {
// class_id associated with the nft
string class_id = 1;

// owner is the owner address of the nft
string owner = 2;
}

// QueryBalanceResponse is the response type for the Query/Balance RPC method
message QueryBalanceResponse {
// amount is the number of all NFTs of a given class owned by the owner
uint64 amount = 1;
}

// QueryBalanceByQueryStringResponse is the response type for the Query/Balance RPC method
//
// Since: nft v0.1.1
message QueryBalanceByQueryStringResponse {
// amount is the number of all NFTs of a given class owned by the owner
uint64 amount = 1;
}

// QueryOwnerRequest is the request type for the Query/Owner RPC method
message QueryOwnerRequest {
// class_id associated with the nft
Expand All @@ -70,24 +124,59 @@ message QueryOwnerRequest {
string id = 2;
}

// QueryOwnerByQueryStringRequest is the request type for the Query/Owner RPC method
//
// Since: nft v0.1.1
message QueryOwnerByQueryStringRequest {
// class_id associated with the nft
string class_id = 1;

// id is a unique identifier of the NFT
string id = 2;
}

// QueryOwnerResponse is the response type for the Query/Owner RPC method
message QueryOwnerResponse {
// owner is the owner address of the nft
string owner = 1;
}

// QueryOwnerByQueryStringResponse is the response type for the Query/Owner RPC method
//
// Since: nft v0.1.1
message QueryOwnerByQueryStringResponse {
// owner is the owner address of the nft
string owner = 1;
}

// QuerySupplyRequest is the request type for the Query/Supply RPC method
message QuerySupplyRequest {
// class_id associated with the nft
string class_id = 1;
}

// QuerySupplyByQueryStringRequest is the request type for the Query/Supply RPC method
//
// Since: nft v0.1.1
message QuerySupplyByQueryStringRequest {
// class_id associated with the nft
string class_id = 1;
}

// QuerySupplyResponse is the response type for the Query/Supply RPC method
message QuerySupplyResponse {
// amount is the number of all NFTs from the given class
uint64 amount = 1;
}

// QuerySupplyByQueryStringResponse is the response type for the Query/Supply RPC method
//
// Since: nft v0.1.1
message QuerySupplyByQueryStringResponse {
// amount is the number of all NFTs from the given class
uint64 amount = 1;
}

// QueryNFTstRequest is the request type for the Query/NFTs RPC method
message QueryNFTsRequest {
// class_id associated with the nft
Expand Down Expand Up @@ -118,24 +207,59 @@ message QueryNFTRequest {
string id = 2;
}

// QueryNFTByQueryStringRequest is the request type for the Query/NFT RPC method
//
// Since: nft v0.1.1
message QueryNFTByQueryStringRequest {
// class_id associated with the nft
string class_id = 1;

// id is a unique identifier of the NFT
string id = 2;
}

// QueryNFTResponse is the response type for the Query/NFT RPC method
message QueryNFTResponse {
// owner is the owner address of the nft
cosmos.nft.v1beta1.NFT nft = 1;
}

// QueryNFTByQueryStringResponse is the response type for the Query/NFT RPC method
//
// Since: nft v0.1.1
message QueryNFTByQueryStringResponse {
// owner is the owner address of the nft
cosmos.nft.v1beta1.NFT nft = 1;
}

// QueryClassRequest is the request type for the Query/Class RPC method
message QueryClassRequest {
// class_id associated with the nft
string class_id = 1;
}

// QueryClassByQueryStringRequest is the request type for the Query/Class RPC method
//
// Since: nft v0.1.1
message QueryClassByQueryStringRequest {
// class_id associated with the nft
string class_id = 1;
}

// QueryClassResponse is the response type for the Query/Class RPC method
message QueryClassResponse {
// class defines the class of the nft type.
cosmos.nft.v1beta1.Class class = 1;
}

// QueryClassByQueryStringResponse is the response type for the Query/Class RPC method
//
// Since: nft v0.1.1
message QueryClassByQueryStringResponse {
// class defines the class of the nft type.
cosmos.nft.v1beta1.Class class = 1;
}

// QueryClassesRequest is the request type for the Query/Classes RPC method
message QueryClassesRequest {
// pagination defines an optional pagination for the request.
Expand Down
3 changes: 3 additions & 0 deletions x/nft/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Features
* [#18355](https://github.com/cosmos/cosmos-sdk/pull/18355) Addes new versions for `Balance`, `Owner`, `Supply`, `NFT`, `Class` queries that receives request via query string.

## [v0.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/x/nft/v0.1.0) - 2023-11-07
63 changes: 63 additions & 0 deletions x/nft/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ func (k Keeper) Balance(goCtx context.Context, r *nft.QueryBalanceRequest) (*nft
return &nft.QueryBalanceResponse{Amount: balance}, nil
}

// BalanceByQueryString return the number of NFTs of a given class owned by the owner, same as balanceOf in ERC721
// but receives request via query string.
func (k Keeper) BalanceByQueryString(goCtx context.Context, r *nft.QueryBalanceByQueryStringRequest) (*nft.QueryBalanceByQueryStringResponse, error) {
res, err := k.Balance(goCtx, &nft.QueryBalanceRequest{
ClassId: r.ClassId,
Owner: r.Owner,
})
if err != nil {
return nil, err
}
return &nft.QueryBalanceByQueryStringResponse{Amount: res.Amount}, 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 {
Expand All @@ -60,6 +73,19 @@ func (k Keeper) Owner(goCtx context.Context, r *nft.QueryOwnerRequest) (*nft.Que
return &nft.QueryOwnerResponse{Owner: ownerstr}, nil
}

// OwnerByQueryString return the owner of the NFT based on its class and id, same as ownerOf in ERC721
// but receives request via query string.
func (k Keeper) OwnerByQueryString(goCtx context.Context, r *nft.QueryOwnerByQueryStringRequest) (*nft.QueryOwnerByQueryStringResponse, error) {
res, err := k.Owner(goCtx, &nft.QueryOwnerRequest{
ClassId: r.ClassId,
Id: r.Id,
})
if err != nil {
return nil, err
}
return &nft.QueryOwnerByQueryStringResponse{Owner: res.Owner}, 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 {
Expand All @@ -74,6 +100,18 @@ func (k Keeper) Supply(goCtx context.Context, r *nft.QuerySupplyRequest) (*nft.Q
return &nft.QuerySupplyResponse{Amount: supply}, nil
}

// SupplyByQueryString return the number of NFTs from the given class, same as totalSupply of ERC721.
// but receives request via query string.
func (k Keeper) SupplyByQueryString(goCtx context.Context, r *nft.QuerySupplyByQueryStringRequest) (*nft.QuerySupplyByQueryStringResponse, error) {
res, err := k.Supply(goCtx, &nft.QuerySupplyRequest{
ClassId: r.ClassId,
})
if err != nil {
return nil, err
}
return &nft.QuerySupplyByQueryStringResponse{Amount: res.Amount}, 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 {
Expand Down Expand Up @@ -157,6 +195,19 @@ func (k Keeper) NFT(goCtx context.Context, r *nft.QueryNFTRequest) (*nft.QueryNF
return &nft.QueryNFTResponse{Nft: &n}, nil
}

// NFTByQueryString return an NFT based on its class and id.
// but receives request via query string.
func (k Keeper) NFTByQueryString(goCtx context.Context, r *nft.QueryNFTByQueryStringRequest) (*nft.QueryNFTByQueryStringResponse, error) {
res, err := k.NFT(goCtx, &nft.QueryNFTRequest{
ClassId: r.ClassId,
Id: r.Id,
})
if err != nil {
return nil, err
}
return &nft.QueryNFTByQueryStringResponse{Nft: res.Nft}, 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 {
Expand All @@ -175,6 +226,18 @@ func (k Keeper) Class(goCtx context.Context, r *nft.QueryClassRequest) (*nft.Que
return &nft.QueryClassResponse{Class: &class}, nil
}

// ClassByQueryString return an NFT class based on its id
// but receives request via query string.
func (k Keeper) ClassByQueryString(goCtx context.Context, r *nft.QueryClassByQueryStringRequest) (*nft.QueryClassByQueryStringResponse, error) {
res, err := k.Class(goCtx, &nft.QueryClassRequest{
ClassId: r.ClassId,
})
if err != nil {
return nil, err
}
return &nft.QueryClassByQueryStringResponse{Class: res.Class}, nil
}

// Classes return all NFT classes
func (k Keeper) Classes(goCtx context.Context, r *nft.QueryClassesRequest) (*nft.QueryClassesResponse, error) {
if r == nil {
Expand Down
Loading

0 comments on commit d322b23

Please sign in to comment.