Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor nft with cosmos-sdk nft module #309

Merged
merged 14 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Bug Fixes
* [\#304](https://github.com/irisnet/irismod/pull/304) Fix nft module import error.

### Improvements
* [\#305](https://github.com/irisnet/irismod/pull/305) Remove ibc-go from project.
* [\#306](https://github.com/irisnet/irismod/pull/306) Bump up cosmos-sdk to v0.46.1.
* [\#307](https://github.com/irisnet/irismod/pull/307) Refactor proto-gen with docker.
* [\#308](https://github.com/irisnet/irismod/pull/308) Coinswap module adds unilateral injection liquidity function.
* [\#309](https://github.com/irisnet/irismod/pull/309) Refactor nft with cosmos-sdk nft module.

### API Breaking Changes
* [\#309](https://github.com/irisnet/irismod/pull/309) GRPC method `Owner` rename to `NFTsOfOwner`, Remove deprecated `Queries` api

## [v1.6.0] - 2022-08-08

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/tidwall/gjson v1.14.0
google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -64,7 +65,6 @@ require (
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/api v0.81.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

Expand Down
2 changes: 1 addition & 1 deletion modules/mt/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewTxCmd() *cobra.Command {
return txCmd
}

// GetCmdIssueDenom is the CLI command for an IssueDenom transaction
// GetCmdIssueDenom is the CLI command for an SaveDenom transaction
func GetCmdIssueDenom() *cobra.Command {
cmd := &cobra.Command{
Use: "issue",
Expand Down
17 changes: 9 additions & 8 deletions modules/mt/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions modules/nft/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ func (s *IntegrationTestSuite) TestNft() {
s.Require().Equal(from.String(), nftItem.Owner)

//------test GetCmdQueryOwner()-------------
respType = proto.Message(&nfttypes.QueryOwnerResponse{})
respType = proto.Message(&nfttypes.QueryNFTsOfOwnerResponse{})
bz, err = nfttestutil.QueryOwnerExec(val.ClientCtx, from.String())
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType))
ownerResp := respType.(*nfttypes.QueryOwnerResponse)
ownerResp := respType.(*nfttypes.QueryNFTsOfOwnerResponse)
s.Require().Equal(from.String(), ownerResp.Owner.Address)
s.Require().Equal(denom, ownerResp.Owner.IDCollections[0].DenomId)
s.Require().Equal(tokenID, ownerResp.Owner.IDCollections[0].TokenIds[0])
Expand Down
2 changes: 1 addition & 1 deletion modules/nft/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func GetCmdQueryOwner() *cobra.Command {
return err
}
queryClient := types.NewQueryClient(clientCtx)
resp, err := queryClient.Owner(context.Background(), &types.QueryOwnerRequest{
resp, err := queryClient.NFTsOfOwner(context.Background(), &types.QueryNFTsOfOwnerRequest{
DenomId: denomID,
Owner: args[0],
Pagination: pageReq,
Expand Down
6 changes: 3 additions & 3 deletions modules/nft/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewTxCmd() *cobra.Command {
return txCmd
}

// GetCmdIssueDenom is the CLI command for an IssueDenom transaction
// GetCmdIssueDenom is the CLI command for an SaveDenom transaction
func GetCmdIssueDenom() *cobra.Command {
cmd := &cobra.Command{
Use: "issue [denom-id]",
Expand Down Expand Up @@ -134,7 +134,7 @@ func GetCmdIssueDenom() *cobra.Command {
return cmd
}

// GetCmdMintNFT is the CLI command for a MintNFT transaction
// GetCmdMintNFT is the CLI command for a SaveNFT transaction
func GetCmdMintNFT() *cobra.Command {
cmd := &cobra.Command{
Use: "mint [denom-id] [nft-id]",
Expand Down Expand Up @@ -332,7 +332,7 @@ func GetCmdTransferNFT() *cobra.Command {
return cmd
}

// GetCmdBurnNFT is the CLI command for sending a BurnNFT transaction
// GetCmdBurnNFT is the CLI command for sending a RemoveNFT transaction
func GetCmdBurnNFT() *cobra.Command {
cmd := &cobra.Command{
Use: "burn [denom-id] [nft-id]",
Expand Down
34 changes: 0 additions & 34 deletions modules/nft/genesis.go

This file was deleted.

47 changes: 0 additions & 47 deletions modules/nft/handler.go

This file was deleted.

106 changes: 21 additions & 85 deletions modules/nft/keeper/collection.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package keeper

import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"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/irisnet/irismod/modules/nft/exported"
"github.com/irisnet/irismod/modules/nft/types"
)

// SetCollection saves all NFTs and returns an error if there already exists
func (k Keeper) SetCollection(ctx sdk.Context, collection types.Collection) error {
// SaveCollection saves all NFTs and returns an error if there already exists
func (k Keeper) SaveCollection(ctx sdk.Context, collection types.Collection) error {
for _, nft := range collection.NFTs {
if err := k.MintNFT(
if err := k.SaveNFT(
ctx,
collection.Denom.Id,
nft.GetID(),
Expand All @@ -32,87 +25,30 @@ func (k Keeper) SetCollection(ctx sdk.Context, collection types.Collection) erro
return nil
}

// GetCollection returns the collection by the specified denom ID
func (k Keeper) GetCollection(ctx sdk.Context, denomID string) (types.Collection, error) {
denom, found := k.GetDenom(ctx, denomID)
if !found {
return types.Collection{}, sdkerrors.Wrapf(types.ErrInvalidDenom, "denomID %s not existed ", denomID)
}

nfts := k.GetNFTs(ctx, denomID)
return types.NewCollection(denom, nfts), nil
}

// GetPaginateCollection returns the collection by the specified denom ID
func (k Keeper) GetPaginateCollection(ctx sdk.Context, request *types.QueryCollectionRequest, denomID string) (types.Collection, *query.PageResponse, error) {
denom, found := k.GetDenom(ctx, denomID)
if !found {
return types.Collection{}, nil, sdkerrors.Wrapf(types.ErrInvalidDenom, "denomID %s not existed ", denomID)
}
var nfts []exported.NFT
store := ctx.KVStore(k.storeKey)
nftStore := prefix.NewStore(store, types.KeyNFT(denomID, ""))
pageRes, err := query.Paginate(nftStore, shapePageRequest(request.Pagination), func(key []byte, value []byte) error {
var baseNFT types.BaseNFT
k.cdc.MustUnmarshal(value, &baseNFT)
nfts = append(nfts, baseNFT)
return nil
})
if err != nil {
return types.Collection{}, nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
return types.NewCollection(denom, nfts), pageRes, nil
}

// GetCollections returns all the collections
func (k Keeper) GetCollections(ctx sdk.Context) (cs []types.Collection) {
for _, denom := range k.GetDenoms(ctx) {
nfts := k.GetNFTs(ctx, denom.Id)
cs = append(cs, types.NewCollection(denom, nfts))
}
return cs
}
func (k Keeper) GetCollections(ctx sdk.Context) (cs []types.Collection, err error) {
for _, class := range k.nk.GetClasses(ctx) {
nfts, err := k.GetNFTs(ctx, class.Id)
if err != nil {
return nil, err
}

// GetDenomSupply returns the number of NFTs by the specified denom ID
func (k Keeper) GetTotalSupply(ctx sdk.Context, denomID string) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyCollection(denomID))
if len(bz) == 0 {
return 0
}
return types.MustUnMarshalSupply(k.cdc, bz)
}
denom, err := k.GetDenomInfo(ctx, class.Id)
if err != nil {
return nil, err
}

// GetTotalSupplyOfOwner returns the amount of NFTs by the specified conditions
func (k Keeper) GetTotalSupplyOfOwner(ctx sdk.Context, id string, owner sdk.AccAddress) (supply uint64) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyOwner(owner, id, ""))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
supply++
cs = append(cs, types.NewCollection(*denom, nfts))
}
return supply
return cs, nil
}

func (k Keeper) increaseSupply(ctx sdk.Context, denomID string) {
supply := k.GetTotalSupply(ctx, denomID)
supply++

store := ctx.KVStore(k.storeKey)
bz := types.MustMarshalSupply(k.cdc, supply)
store.Set(types.KeyCollection(denomID), bz)
// GetTotalSupply returns the number of NFTs by the specified denom ID
func (k Keeper) GetTotalSupply(ctx sdk.Context, denomID string) uint64 {
return k.nk.GetTotalSupply(ctx, denomID)
}

func (k Keeper) decreaseSupply(ctx sdk.Context, denomID string) {
supply := k.GetTotalSupply(ctx, denomID)
supply--

store := ctx.KVStore(k.storeKey)
if supply == 0 {
store.Delete(types.KeyCollection(denomID))
return
}

bz := types.MustMarshalSupply(k.cdc, supply)
store.Set(types.KeyCollection(denomID), bz)
// GetBalance returns the amount of NFTs by the specified conditions
func (k Keeper) GetBalance(ctx sdk.Context, id string, owner sdk.AccAddress) (supply uint64) {
return k.nk.GetBalance(ctx, id, owner)
}
Loading