Skip to content

Commit

Permalink
feat: implement nft module query server (cosmos#10462)
Browse files Browse the repository at this point in the history
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

QueryServer implementation and corresponding keeper methods,refer cosmos#9826

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
Zhiqiang Zhang authored and blewater committed Dec 8, 2021
1 parent d7d5a12 commit 8b381bd
Show file tree
Hide file tree
Showing 5 changed files with 790 additions and 2 deletions.
1 change: 1 addition & 0 deletions x/nft/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ var (
ErrNFTExists = sdkerrors.Register(ModuleName, 5, "nft already exist")
ErrNFTNotExists = sdkerrors.Register(ModuleName, 6, "nft does not exist")
ErrInvalidID = sdkerrors.Register(ModuleName, 7, "invalid id")
ErrInvalidClassID = sdkerrors.Register(ModuleName, 8, "invalid class id")
)
192 changes: 192 additions & 0 deletions x/nft/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package keeper

import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/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.Wrap(sdkerrors.ErrInvalidRequest, "empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
}

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.Wrap(sdkerrors.ErrInvalidRequest, "empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
}

if err := nft.ValidateNFTID(r.Id); err != nil {
return nil, err
}

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.Wrap(sdkerrors.ErrInvalidRequest, "empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(goCtx)
supply := k.GetTotalSupply(ctx, r.ClassId)
return &nft.QuerySupplyResponse{Amount: supply}, nil
}

// NFTsOfClass return all NFTs of a given class or optional owner, similar to tokenByIndex in ERC721Enumerable
func (k Keeper) NFTsOfClass(goCtx context.Context, r *nft.QueryNFTsOfClassRequest) (*nft.QueryNFTsOfClassResponse, error) {
if r == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
}

var nfts []*nft.NFT
ctx := sdk.UnwrapSDKContext(goCtx)

// if owner is not empty, filter nft by owner
if len(r.Owner) > 0 {
owner, err := sdk.AccAddressFromBech32(r.Owner)
if err != nil {
return nil, err
}

ownerStore := k.getClassStoreByOwner(ctx, owner, r.ClassId)
pageRes, err := query.Paginate(ownerStore, r.Pagination, func(key []byte, _ []byte) error {
nft, has := k.GetNFT(ctx, r.ClassId, string(key))
if has {
nfts = append(nfts, &nft)
}
return nil
})

if err != nil {
return nil, err
}
return &nft.QueryNFTsOfClassResponse{
Nfts: nfts,
Pagination: pageRes,
}, nil
}

nftStore := k.getNFTStore(ctx, r.ClassId)
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
})

if err != nil {
return nil, err
}
return &nft.QueryNFTsOfClassResponse{
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.Wrap(sdkerrors.ErrInvalidRequest, "empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
}
if err := nft.ValidateNFTID(r.Id); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)
n, has := k.GetNFT(ctx, r.ClassId, r.Id)
if !has {
return nil, sdkerrors.Wrapf(nft.ErrNFTNotExists, "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.Wrap(sdkerrors.ErrInvalidRequest, "empty request")
}

if err := nft.ValidateClassID(r.ClassId); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)
class, has := k.GetClass(ctx, r.ClassId)
if !has {
return nil, sdkerrors.Wrapf(nft.ErrClassNotExists, "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.Wrap(sdkerrors.ErrInvalidRequest, "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
}
Loading

0 comments on commit 8b381bd

Please sign in to comment.