-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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: implement nft module msg server #10074
Merged
Merged
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
59ce1a2
implement nft msgServer
36a50b1
add comment
fcb7fba
remove legacy support
cd881c8
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into im…
5692305
apply comments from github
844453d
rename variable
7ed9d6d
apply comment from github
906acb2
resolve conflict
8791b08
resolve conflict
da47700
resolve conflict
7b30a6c
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into im…
f0f2750
apply comment from github
ecc2585
fix conflict
22aa3d5
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into im…
d6c965f
fix conflict
58130f9
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into im…
20ed66b
merge from master
d523160
Merge branch 'master' into implement-nft-msgServer
aaronc c698e31
Merge branch 'master' into implement-nft-msgServer
aaronc 8133e0c
fix test error
59e566c
Merge branch 'master' into implement-nft-msgServer
aaronc a44e772
Merge branch 'master' into implement-nft-msgServer
aaronc e38a1fe
Merge branch 'master' into implement-nft-msgServer
aaronc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,14 @@ | ||
package nft | ||
|
||
import ( | ||
types "github.com/cosmos/cosmos-sdk/codec/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/msgservice" | ||
) | ||
|
||
func RegisterInterfaces(registry types.InterfaceRegistry) { | ||
registry.RegisterImplementations((*sdk.Msg)(nil), | ||
&MsgSend{}, | ||
) | ||
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) | ||
} |
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,15 @@ | ||
package nft | ||
|
||
import ( | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
// x/nft module sentinel errors | ||
var ( | ||
ErrInvalidNFT = sdkerrors.Register(ModuleName, 2, "invalid nft") | ||
ErrClassExists = sdkerrors.Register(ModuleName, 3, "nft class already exist") | ||
ErrClassNotExists = sdkerrors.Register(ModuleName, 4, "nft class does not exist") | ||
ErrNFTExists = sdkerrors.Register(ModuleName, 5, "nft already exist") | ||
ErrNFTNotExists = sdkerrors.Register(ModuleName, 6, "nft does not exist") | ||
ErrInvalidID = sdkerrors.Register(ModuleName, 7, "invalid id") | ||
) |
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,18 @@ | ||
package nft | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
// BankKeeper defines the contract needed to be fulfilled for banking and supply | ||
// dependencies. | ||
type BankKeeper interface { | ||
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins | ||
} | ||
|
||
// AccountKeeper defines the contract required for account APIs. | ||
type AccountKeeper interface { | ||
GetModuleAddress(name string) sdk.AccAddress | ||
GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI | ||
} | ||
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,30 @@ | ||
package nft | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// ValidateGenesis check the given genesis state has no integrity issues | ||
func ValidateGenesis(data GenesisState) error { | ||
for _, class := range data.Classes { | ||
if err := ValidateClassID(class.Id); err != nil { | ||
return err | ||
} | ||
} | ||
for _, entry := range data.Entries { | ||
for _, nft := range entry.Nfts { | ||
if err := ValidateNFTID(nft.Id); err != nil { | ||
dreamer-zq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
if _, err := sdk.AccAddressFromBech32(entry.Owner); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// DefaultGenesisState - Return a default genesis state | ||
func DefaultGenesisState() *GenesisState { | ||
return &GenesisState{} | ||
dreamer-zq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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,67 @@ | ||
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" | ||
) | ||
|
||
// SaveClass defines a method for creating a new nft class | ||
func (k Keeper) SaveClass(ctx sdk.Context, class nft.Class) error { | ||
if k.HasClass(ctx, class.Id) { | ||
return sdkerrors.Wrap(nft.ErrClassExists, class.Id) | ||
} | ||
bz, err := k.cdc.Marshal(&class) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "Marshal nft.Class failed") | ||
} | ||
store := ctx.KVStore(k.storeKey) | ||
store.Set(classStoreKey(class.Id), bz) | ||
return nil | ||
} | ||
|
||
// UpdateClass defines a method for updating a exist nft class | ||
func (k Keeper) UpdateClass(ctx sdk.Context, class nft.Class) error { | ||
if !k.HasClass(ctx, class.Id) { | ||
return sdkerrors.Wrap(nft.ErrClassNotExists, class.Id) | ||
} | ||
bz, err := k.cdc.Marshal(&class) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "Marshal nft.Class failed") | ||
} | ||
store := ctx.KVStore(k.storeKey) | ||
store.Set(classStoreKey(class.Id), bz) | ||
return nil | ||
} | ||
|
||
// GetClass defines a method for returning the class information of the specified id | ||
func (k Keeper) GetClass(ctx sdk.Context, classID string) (nft.Class, bool) { | ||
store := ctx.KVStore(k.storeKey) | ||
bz := store.Get(classStoreKey(classID)) | ||
|
||
var class nft.Class | ||
if len(bz) == 0 { | ||
return class, false | ||
} | ||
k.cdc.MustUnmarshal(bz, &class) | ||
return class, true | ||
} | ||
|
||
// GetClasses defines a method for returning all classes information | ||
func (k Keeper) GetClasses(ctx sdk.Context) (classes []*nft.Class) { | ||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, ClassKey) | ||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
var class nft.Class | ||
k.cdc.MustUnmarshal(iterator.Value(), &class) | ||
classes = append(classes, &class) | ||
} | ||
return | ||
} | ||
|
||
// HasClass determines whether the specified classID exist | ||
func (k Keeper) HasClass(ctx sdk.Context, classID string) bool { | ||
store := ctx.KVStore(k.storeKey) | ||
return store.Has(classStoreKey(classID)) | ||
} |
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,65 @@ | ||
package keeper | ||
|
||
import ( | ||
"sort" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/nft" | ||
) | ||
|
||
// InitGenesis new nft genesis | ||
func (k Keeper) InitGenesis(ctx sdk.Context, data *nft.GenesisState) { | ||
for _, class := range data.Classes { | ||
if err := k.SaveClass(ctx, *class); err != nil { | ||
panic(err) | ||
} | ||
|
||
} | ||
for _, entry := range data.Entries { | ||
for _, nft := range entry.Nfts { | ||
owner, err := sdk.AccAddressFromBech32(entry.Owner) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := k.Mint(ctx, *nft, owner); err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// ExportGenesis returns a GenesisState for a given context. | ||
func (k Keeper) ExportGenesis(ctx sdk.Context) *nft.GenesisState { | ||
classes := k.GetClasses(ctx) | ||
nftMap := make(map[string][]*nft.NFT) | ||
for _, class := range classes { | ||
nfts := k.GetNFTsOfClass(ctx, class.Id) | ||
for i, n := range nfts { | ||
owner := k.GetOwner(ctx, n.ClassId, n.Id) | ||
nftArr, ok := nftMap[owner.String()] | ||
if !ok { | ||
nftArr = make([]*nft.NFT, 0) | ||
} | ||
nftMap[owner.String()] = append(nftArr, &nfts[i]) | ||
} | ||
} | ||
|
||
owners := make([]string, 0, len(nftMap)) | ||
for owner := range nftMap { | ||
owners = append(owners, owner) | ||
} | ||
sort.Strings(owners) | ||
|
||
entries := make([]*nft.Entry, 0, len(nftMap)) | ||
for _, owner := range owners { | ||
entries = append(entries, &nft.Entry{ | ||
Owner: owner, | ||
Nfts: nftMap[owner], | ||
}) | ||
} | ||
return &nft.GenesisState{ | ||
Classes: classes, | ||
Entries: entries, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be good to depend on
Query
proto services rather than keepers directly, but I think we will firstly need to clean other parts of SDK before requesting that changes (and finalize adr-33).