Skip to content
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

enable data in geneis state to be exported #310

Merged
merged 6 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func NewApp(
incentive.NewAppModule(appCodec, app.incentiveKeeper, app.AccountKeeper, app.BankKeeper, app.cdpKeeper),
ununifidist.NewAppModule(appCodec, app.ununifidistKeeper, app.AccountKeeper, app.BankKeeper),
pricefeed.NewAppModule(appCodec, app.pricefeedKeeper, app.AccountKeeper),
nftmint.NewAppModule(appCodec, app.NftmintKeeper, app.AccountKeeper),
nftmint.NewAppModule(appCodec, app.NftmintKeeper, app.NFTKeeper),
nftmarket.NewAppModule(appCodec, app.NftmarketKeeper, app.AccountKeeper, app.BankKeeper),
// wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper),
)
Expand Down
2 changes: 1 addition & 1 deletion proto/nftmint/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ message GenesisState {
(gogoproto.moretags) = "yaml:\"params\"",
(gogoproto.nullable) = false
];
repeated ClassAttributes class_attributes_list = 2 [ (gogoproto.moretags) = "yaml:\"class_attributes_list\"" ];
}

57 changes: 54 additions & 3 deletions x/nftmint/genesis.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,70 @@
package nftmint

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
nfttypes "github.com/cosmos/cosmos-sdk/x/nft"

"github.com/UnUniFi/chain/x/nftmint/keeper"
"github.com/UnUniFi/chain/x/nftmint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// InitGenesis initializes the store state from a genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, accountKeeper types.AccountKeeper, gs types.GenesisState) {
func InitGenesis(ctx sdk.Context, k keeper.Keeper, nftKeeper types.NftKeeper, gs types.GenesisState) {
k.SetParamSet(ctx, gs.Params)

for _, classAttributes := range gs.ClassAttributesList {
if err := InitClassRelatingData(ctx, k, nftKeeper, *classAttributes); err != nil {
panic(err)
}
}
}

// ExportGenesis export genesis state for nftmarket module
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
classAttributesList := k.GetClassAttributesList(ctx)

return types.GenesisState{
Params: k.GetParamSet(ctx),
Params: k.GetParamSet(ctx),
ClassAttributesList: classAttributesList,
}
}

func InitClassRelatingData(ctx sdk.Context, k keeper.Keeper, nftKeeper types.NftKeeper, classAttributes types.ClassAttributes) error {
class, exists := nftKeeper.GetClass(ctx, classAttributes.ClassId)
if !exists {
return sdkerrors.Wrap(nfttypes.ErrClassNotExists, classAttributes.ClassId)
}

params := k.GetParamSet(ctx)
if err := types.ValidateCreateClass(
params,
class.Name, class.Symbol, classAttributes.BaseTokenUri, class.Description,
classAttributes.MintingPermission,
classAttributes.TokenSupplyCap,
); err != nil {
return err
}

if err := k.SetClassAttributes(ctx, types.NewClassAttributes(
class.Id,
classAttributes.Owner.AccAddress(),
classAttributes.BaseTokenUri,
classAttributes.MintingPermission,
classAttributes.TokenSupplyCap,
)); err != nil {
return err
}

owningClassIdList := k.AddClassIDToOwningClassIdList(ctx, classAttributes.Owner.AccAddress(), class.Id)
if err := k.SetOwningClassIdList(ctx, owningClassIdList); err != nil {
return err
}

classNameIdList := k.AddClassNameIdList(ctx, class.Name, class.Id)
if err := k.SetClassNameIdList(ctx, classNameIdList); err != nil {
return err
}

return nil
}
39 changes: 39 additions & 0 deletions x/nftmint/keeper/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,45 @@ func (k Keeper) GetClassNameIdList(ctx sdk.Context, className string) (types.Cla
return classNameIdList, true
}

func (k Keeper) GetClassAttributesList(ctx sdk.Context) (classAttributesList []*types.ClassAttributes) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixClassAttributes)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var classAttributes types.ClassAttributes
k.cdc.MustUnmarshal(iterator.Value(), &classAttributes)
classAttributesList = append(classAttributesList, &classAttributes)
}

return
}

func (k Keeper) GetOwningClassIdLists(ctx sdk.Context) (owningClassIdLists []*types.OwningClassIdList) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixOwningClassIdList)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var owningClassIdList types.OwningClassIdList
k.cdc.MustUnmarshal(iterator.Value(), &owningClassIdList)
owningClassIdLists = append(owningClassIdLists, &owningClassIdList)
}

return
}

func (k Keeper) GetClassNameIdLists(ctx sdk.Context) (classNameIdLists []*types.ClassNameIdList) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixClassNameIdList)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var classNameIdList types.ClassNameIdList
k.cdc.MustUnmarshal(iterator.Value(), &classNameIdList)
classNameIdLists = append(classNameIdLists, &classNameIdList)
}

return
}

func (k Keeper) AddClassIDToOwningClassIdList(ctx sdk.Context, owner sdk.AccAddress, classID string) types.OwningClassIdList {
owningClassIdList, exists := k.GetOwningClassIdList(ctx, owner)
if !exists {
Expand Down
17 changes: 9 additions & 8 deletions x/nftmint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

"github.com/UnUniFi/chain/x/nftmint/client/cli"
"github.com/UnUniFi/chain/x/nftmint/keeper"
"github.com/UnUniFi/chain/x/nftmint/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/UnUniFi/chain/x/nftmint/client/cli"
"github.com/UnUniFi/chain/x/nftmint/keeper"
"github.com/UnUniFi/chain/x/nftmint/types"
)

var (
Expand Down Expand Up @@ -99,15 +100,15 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
type AppModule struct {
AppModuleBasic

keeper keeper.Keeper
accountKeeper types.AccountKeeper
keeper keeper.Keeper
nftKeeper types.NftKeeper
}

func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, accountKeeper types.AccountKeeper) AppModule {
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, nftKeeper types.NftKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
accountKeeper: accountKeeper,
nftKeeper: nftKeeper,
}
}

Expand Down Expand Up @@ -146,7 +147,7 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

InitGenesis(ctx, am.keeper, am.accountKeeper, genState)
InitGenesis(ctx, am.keeper, am.nftKeeper, genState)

return []abci.ValidatorUpdate{}
}
Expand Down
6 changes: 6 additions & 0 deletions x/nftmint/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func (gs GenesisState) Validate() error {
return err
}

for _, classAttributes := range gs.ClassAttributesList {
if err := ValidateClassAttributes(*classAttributes, gs.Params); err != nil {
return err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think more validations will need to be added here for ClassNameIdLists and OwningClassIdLists.
As well as usage of usage of class ids on OwningClassIdLists that's not on ClassNameIdLists

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know.
was thinking about whether i should add that kind of validation for those.
But, eventually i didn't do that since x/nftmarket doesn't have those type of validation, too, right?
do you even think i should add here now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a mandatory one but would be good to add for completeness.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkXultra
what do you think?
should i do this for this PR or do i just put this as an issue and give someone else this task?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taiki1frsh
How long will it take?

Copy link
Collaborator Author

@taiki1frsh taiki1frsh Dec 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkXultra
without test, it doesn't take an hour.
with out, i guess around four or five hour.


return nil
}

Expand Down
88 changes: 77 additions & 11 deletions x/nftmint/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion x/nftmint/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ func ValidateMintNFT(
return nil
}

func ValidateClassAttributes(classAttributes ClassAttributes, params Params) error {
if err := ValidateUri(params.MinUriLen, params.MaxUriLen, classAttributes.BaseTokenUri); err != nil {
return err
}

if err := ValidateTokenSupplyCap(params.MaxNFTSupplyCap, classAttributes.TokenSupplyCap); err != nil {
return err
}

return nil
}

func ValidateMintingPermission(mintingPermission MintingPermission, owner, minter sdk.AccAddress) error {
switch mintingPermission {
case 0:
Expand All @@ -72,7 +84,6 @@ func ValidateMintingPermission(mintingPermission MintingPermission, owner, minte
}

// Measure length of the string as byte length

func ValidateClassName(minLen, maxLen uint64, className string) error {
len := len(className)
if len < int(minLen) || len > int(maxLen) {
Expand Down