-
Notifications
You must be signed in to change notification settings - Fork 351
/
keeper.go
180 lines (145 loc) · 4.75 KB
/
keeper.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2016-2021 Shanghai Bianjie AI Technology Inc. (licensed under the Apache License, Version 2.0)
// Modifications Copyright (c) 2021-present Crypto.org (licensed under the Apache License, Version 2.0)
package keeper
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crypto-org-chain/chain-main/v4/x/nft/types"
)
// Keeper maintains the link to data storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
storeKey storetypes.StoreKey // Unexposed key to access store from sdk.Context
cdc codec.Codec
}
// NewKeeper creates a new instance of the NFT Keeper
func NewKeeper(cdc codec.Codec, storeKey storetypes.StoreKey) Keeper {
return Keeper{
storeKey: storeKey,
cdc: cdc,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("chainmain/%s", types.ModuleName))
}
// IssueDenom issues a denom according to the given params
func (k Keeper) IssueDenom(ctx sdk.Context,
id, name, schema, uri string,
creator sdk.AccAddress) error {
return k.SetDenom(ctx, types.NewDenom(id, name, schema, uri, creator))
}
// MintNFTUnverified mints an NFT without verifying if the owner is the creator of denom
// Needed during genesis initialization
func (k Keeper) MintNFTUnverified(ctx sdk.Context, denomID, tokenID, tokenNm, tokenURI, tokenData string, owner sdk.AccAddress) error {
if !k.HasDenomID(ctx, denomID) {
return sdkerrors.Wrapf(types.ErrInvalidDenom, "denom ID %s not exists", denomID)
}
if k.HasNFT(ctx, denomID, tokenID) {
return sdkerrors.Wrapf(types.ErrNFTAlreadyExists, "NFT %s already exists in collection %s", tokenID, denomID)
}
k.setNFT(
ctx, denomID,
types.NewBaseNFT(
tokenID,
tokenNm,
owner,
tokenURI,
tokenData,
),
)
k.setOwner(ctx, denomID, tokenID, owner)
k.increaseSupply(ctx, denomID)
return nil
}
// MintNFT mints an NFT and manages the NFT's existence within Collections and Owners
func (k Keeper) MintNFT(
ctx sdk.Context, denomID, tokenID, tokenNm,
tokenURI, tokenData string, sender, owner sdk.AccAddress,
) error {
_, err := k.IsDenomCreator(ctx, denomID, sender)
if err != nil {
return err
}
return k.MintNFTUnverified(ctx, denomID, tokenID, tokenNm, tokenURI, tokenData, owner)
}
// EditNFT updates an already existing NFT
func (k Keeper) EditNFT(
ctx sdk.Context, denomID, tokenID, tokenNm,
tokenURI, tokenData string, owner sdk.AccAddress,
) error {
if !k.HasDenomID(ctx, denomID) {
return sdkerrors.Wrapf(types.ErrInvalidDenom, "denom ID %s not exists", denomID)
}
nft, err := k.IsOwner(ctx, denomID, tokenID, owner)
if err != nil {
return err
}
_, err = k.IsDenomCreator(ctx, denomID, owner)
if err != nil {
return err
}
if types.Modified(tokenNm) {
nft.Name = tokenNm
}
if types.Modified(tokenURI) {
nft.URI = tokenURI
}
if types.Modified(tokenData) {
nft.Data = tokenData
}
k.setNFT(ctx, denomID, nft)
return nil
}
// TransferOwner transfers the ownership of the given NFT to the new owner
func (k Keeper) TransferOwner(
ctx sdk.Context, denomID, tokenID string, srcOwner, dstOwner sdk.AccAddress,
) error {
if !k.HasDenomID(ctx, denomID) {
return sdkerrors.Wrapf(types.ErrInvalidDenom, "denom ID %s not exists", denomID)
}
nft, err := k.IsOwner(ctx, denomID, tokenID, srcOwner)
if err != nil {
return err
}
nft.Owner = dstOwner.String()
k.setNFT(ctx, denomID, nft)
k.swapOwner(ctx, denomID, tokenID, srcOwner, dstOwner)
return nil
}
// BurnNFT deletes a specified NFT
func (k Keeper) BurnNFT(ctx sdk.Context, denomID, tokenID string, owner sdk.AccAddress) error {
if !k.HasDenomID(ctx, denomID) {
return sdkerrors.Wrapf(types.ErrInvalidDenom, "denom ID %s not exists", denomID)
}
nft, err := k.IsOwner(ctx, denomID, tokenID, owner)
if err != nil {
return err
}
_, err = k.IsDenomCreator(ctx, denomID, owner)
if err != nil {
return err
}
k.deleteNFT(ctx, denomID, nft)
k.deleteOwner(ctx, denomID, tokenID, owner)
k.decreaseSupply(ctx, denomID)
return nil
}
// BurnNFTUnverified deletes a specified NFT without verifying if the owner is the creator of denom
// Needed for IBC transfer of NFT
func (k Keeper) BurnNFTUnverified(ctx sdk.Context, denomID, tokenID string, owner sdk.AccAddress) error {
if !k.HasDenomID(ctx, denomID) {
return sdkerrors.Wrapf(types.ErrInvalidDenom, "denom ID %s not exists", denomID)
}
nft, err := k.IsOwner(ctx, denomID, tokenID, owner)
if err != nil {
return err
}
k.deleteNFT(ctx, denomID, nft)
k.deleteOwner(ctx, denomID, tokenID, owner)
k.decreaseSupply(ctx, denomID)
return nil
}