cosmos-sdk/x/nft/keeper/genesis.go

66 lines
1.4 KiB
Go
Raw Normal View History

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)
2021-10-27 07:43:47 -07:00
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,
}
}