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

feat: add batch operation for x/nft module #12187

Merged
merged 15 commits into from
Jun 15, 2022
57 changes: 57 additions & 0 deletions x/nft/keeper/nft_batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 {
for _, token := range tokens {
if err := k.Mint(ctx, token, receiver); err != nil {
return err
}
}
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 {
for _, nftID := range nftIDs {
if err := k.Burn(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 {
for _, token := range tokens {
if err := k.Update(ctx, token); err != nil {
return err
}
}
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 {
for _, nftID := range nftIDs {
if err := k.Transfer(ctx, classID, nftID, receiver); err != nil {
return sdkerrors.Wrap(nft.ErrNFTNotExists, nftID)
}
}
return nil
}
Loading