/
msg_server_burn_nft.go
50 lines (41 loc) · 1.38 KB
/
msg_server_burn_nft.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/likecoin/likecoin-chain/v3/x/likenft/types"
)
func (k msgServer) BurnNFT(goCtx context.Context, msg *types.MsgBurnNFT) (*types.MsgBurnNFTResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Check nft exists
exists := k.nftKeeper.HasNFT(ctx, msg.ClassId, msg.NftId)
if !exists {
return nil, types.ErrNftNotFound.Wrapf("Class %s NFT %s does not exist", msg.ClassId, msg.NftId)
}
// Check user is owner
owner := k.nftKeeper.GetOwner(ctx, msg.ClassId, msg.NftId)
if err := k.assertBech32EqualsAccAddress(msg.Creator, owner); err != nil {
return nil, err
}
// Check class is set to burnable
class, classData, err := k.GetClass(ctx, msg.ClassId)
if err != nil {
return nil, err
}
if !classData.Config.Burnable {
return nil, types.ErrNftNotBurnable.Wrapf("NFT of class %s is not burnable", class.Id)
}
// Burn NFT
err = k.nftKeeper.Burn(ctx, msg.ClassId, msg.NftId)
if err != nil {
return nil, types.ErrFailedToBurnNFT.Wrapf("%s", err.Error())
}
// Emit event
ctx.EventManager().EmitTypedEvent(&types.EventBurnNFT{
ClassId: class.Id,
NftId: msg.NftId,
Owner: owner.String(),
ClassParentIscnIdPrefix: classData.Parent.IscnIdPrefix,
ClassParentAccount: classData.Parent.Account,
})
return &types.MsgBurnNFTResponse{}, nil
}