-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add batch operation for x/nft module (#12187)
- Loading branch information
Zhiqiang Zhang
authored
Jun 15, 2022
1 parent
4c3b7af
commit d705a8b
Showing
4 changed files
with
484 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/x/nft" | ||
) | ||
|
||
// BatchMint defines a method for minting a batch of nfts | ||
func (k Keeper) BatchMint(ctx sdk.Context, | ||
tokens []nft.NFT, | ||
receiver sdk.AccAddress, | ||
) error { | ||
checked := make(map[string]bool, len(tokens)) | ||
for _, token := range tokens { | ||
if !checked[token.ClassId] && !k.HasClass(ctx, token.ClassId) { | ||
return sdkerrors.Wrap(nft.ErrClassNotExists, token.ClassId) | ||
} | ||
|
||
if k.HasNFT(ctx, token.ClassId, token.Id) { | ||
return sdkerrors.Wrap(nft.ErrNFTExists, token.Id) | ||
} | ||
|
||
checked[token.ClassId] = true | ||
k.mintWithNoCheck(ctx, token, receiver) | ||
} | ||
return nil | ||
} | ||
|
||
// BatchBurn defines a method for burning a batch of nfts from a specific classID. | ||
// Note: When the upper module uses this method, it needs to authenticate nft | ||
func (k Keeper) BatchBurn(ctx sdk.Context, classID string, nftIDs []string) error { | ||
if !k.HasClass(ctx, classID) { | ||
return sdkerrors.Wrap(nft.ErrClassNotExists, classID) | ||
} | ||
for _, nftID := range nftIDs { | ||
if !k.HasNFT(ctx, classID, nftID) { | ||
return sdkerrors.Wrap(nft.ErrNFTNotExists, nftID) | ||
} | ||
if err := k.burnWithNoCheck(ctx, classID, nftID); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// BatchUpdate defines a method for updating a batch of exist nfts | ||
// Note: When the upper module uses this method, it needs to authenticate nft | ||
func (k Keeper) BatchUpdate(ctx sdk.Context, tokens []nft.NFT) error { | ||
checked := make(map[string]bool, len(tokens)) | ||
for _, token := range tokens { | ||
if !checked[token.ClassId] && !k.HasClass(ctx, token.ClassId) { | ||
return sdkerrors.Wrap(nft.ErrClassNotExists, token.ClassId) | ||
} | ||
|
||
if !k.HasNFT(ctx, token.ClassId, token.Id) { | ||
return sdkerrors.Wrap(nft.ErrNFTNotExists, token.Id) | ||
} | ||
checked[token.ClassId] = true | ||
k.updateWithNoCheck(ctx, token) | ||
} | ||
return nil | ||
} | ||
|
||
// BatchTransfer defines a method for sending a batch of nfts from one account to another account from a specific classID. | ||
// Note: When the upper module uses this method, it needs to authenticate nft | ||
func (k Keeper) BatchTransfer(ctx sdk.Context, | ||
classID string, | ||
nftIDs []string, | ||
receiver sdk.AccAddress, | ||
) error { | ||
if !k.HasClass(ctx, classID) { | ||
return sdkerrors.Wrap(nft.ErrClassNotExists, classID) | ||
} | ||
for _, nftID := range nftIDs { | ||
if !k.HasNFT(ctx, classID, nftID) { | ||
return sdkerrors.Wrap(nft.ErrNFTNotExists, nftID) | ||
} | ||
if err := k.transferWithNoCheck(ctx, classID, nftID, receiver); err != nil { | ||
return sdkerrors.Wrap(nft.ErrNFTNotExists, nftID) | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.