Skip to content

Commit

Permalink
Merge pull request #11 from hyle-team/feature/migration-nft
Browse files Browse the repository at this point in the history
 nft migrations 1 to 2
  • Loading branch information
olegfomenko authored Aug 14, 2024
2 parents 5afac3d + e87fed4 commit 559cdb6
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
24 changes: 24 additions & 0 deletions x/nft/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
v2 "github.com/cosmos/cosmos-sdk/x/nft/migrations/v2"
)

type Migrator struct {
keeper Keeper
}

func NewMigrator(keeper Keeper) Migrator {
return Migrator{
keeper: keeper,
}
}

func (m Migrator) Migrate1to2(ctx sdk.Context) error {
if err := v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc); err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion x/nft/keeper/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (k Keeper) RemoveOwnerNft(
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.NFTByOwnerKeyPrefix))
ownerBranchStore := prefix.NewStore(store, types.KeyPrefix(owner))
ownerBranchStore.Delete(types.NFTKey(
ownerBranchStore.Delete(types.NFTOwnerKey(
nftAddress,
))
}
Expand Down
38 changes: 38 additions & 0 deletions x/nft/migrations/v2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package v2

import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/nft/types"
)

func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
ctx.Logger().Info(fmt.Sprintf("Performing v12.1.8-rc2 %s module migrations", types.ModuleName))

store := prefix.NewStore(ctx.KVStore(storeKey), types.KeyPrefix(types.NFTKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var nft types.NFT
cdc.MustUnmarshal(iterator.Value(), &nft)

nftOwnerStore := prefix.NewStore(ctx.KVStore(storeKey), types.KeyPrefix(types.NFTByOwnerKeyPrefix))
ownerBranchStore := prefix.NewStore(nftOwnerStore, types.KeyPrefix(nft.Owner))
ownerBranchStore.Delete(types.NFTOwnerKey(
nft.Owner,
))

data := cdc.MustMarshal(&types.Owner{
Address: nft.Owner,
NftAddress: nft.Address,
})
ownerBranchStore.Set(types.NFTOwnerKey(nft.Address), data)
}

return nil
}
12 changes: 9 additions & 3 deletions x/nft/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,21 @@ type AppModule struct {
keeper keeper.Keeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
migrator keeper.Migrator
}

func NewAppModule(
cdc codec.Codec,
keeper keeper.Keeper,
kp keeper.Keeper,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(cdc),
keeper: keeper,
keeper: kp,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
migrator: keeper.NewMigrator(kp),
}
}

Expand All @@ -125,6 +127,10 @@ func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

if err := cfg.RegisterMigration(types.ModuleName, 1, am.migrator.Migrate1to2); err != nil {
panic(err)
}
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
Expand All @@ -148,7 +154,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down

0 comments on commit 559cdb6

Please sign in to comment.