-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement nft module msg server (#10074)
<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description MsgServer implementation and corresponding keeper methods, refer #9826 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
- Loading branch information
Zhiqiang Zhang
authored
Oct 27, 2021
1 parent
0a3660d
commit 1d1fcc9
Showing
20 changed files
with
1,175 additions
and
21 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
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 { | ||
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{} | ||
} |
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.