Skip to content

Commit

Permalink
remove legacyToken[Type]
Browse files Browse the repository at this point in the history
  • Loading branch information
tkxkd0159 committed Feb 27, 2024
1 parent d0f56c5 commit 8f40558
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 118 deletions.
7 changes: 1 addition & 6 deletions x/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TokenClassUnpackInterfaces(any *codectypes.Any, unpacker codectypes.AnyUnpa
}

// ----------------------------------------------------------------------------
// FTClass
// Deprecated: FTClass
var _ TokenClass = (*FTClass)(nil)

func (c *FTClass) SetID(_ *NextClassIDs) {}
Expand Down Expand Up @@ -265,8 +265,3 @@ func TokenFromAny(any *codectypes.Any) Token {
class := any.GetCachedValue().(Token)
return class
}

func TokenUnpackInterfaces(any *codectypes.Any, unpacker codectypes.AnyUnpacker) error {
var token Token
return unpacker.UnpackAny(any, &token)
}
5 changes: 0 additions & 5 deletions x/collection/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *collection.GenesisState, addr
anyv := &contractClasses.Classes[i]
class := collection.TokenClassFromAny(anyv)
k.setTokenClass(ctx, contractID, class)

// legacy
if nftClass, ok := class.(*collection.NFTClass); ok {
k.setLegacyTokenType(ctx, contractID, nftClass.Id)
}
}

reporter.Tick()
Expand Down
55 changes: 0 additions & 55 deletions x/collection/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ var (
mintedKeyPrefix = []byte{0x41}
burntKeyPrefix = []byte{0x42}

legacyTokenKeyPrefix = []byte{0xf0}
legacyTokenTypeKeyPrefix = []byte{0xf1}

classStorePrefix = []byte{0x50}
)

Expand Down Expand Up @@ -383,55 +380,3 @@ func splitStatisticKey(keyPrefix, key []byte) (contractID, classID string) {

return
}

// ----------------------------------------------------------------------------
// legacy keys
func legacyTokenKey(contractID, tokenID string) []byte {
prefix := legacyTokenKeyPrefixByContractID(contractID)
key := make([]byte, len(prefix)+len(tokenID))

copy(key, prefix)
copy(key[len(prefix):], tokenID)

return key
}

func legacyTokenKeyPrefixByContractID(contractID string) []byte {
key := make([]byte, len(legacyTokenKeyPrefix)+1+len(contractID))

begin := 0
copy(key, legacyTokenKeyPrefix)

begin += len(legacyTokenKeyPrefix)
key[begin] = byte(len(contractID))

begin++
copy(key[begin:], contractID)

return key
}

func legacyTokenTypeKey(contractID, tokenType string) []byte {
prefix := legacyTokenTypeKeyPrefixByContractID(contractID)
key := make([]byte, len(prefix)+len(tokenType))

copy(key, prefix)
copy(key[len(prefix):], tokenType)

return key
}

func legacyTokenTypeKeyPrefixByContractID(contractID string) []byte {
key := make([]byte, len(legacyTokenTypeKeyPrefix)+1+len(contractID))

begin := 0
copy(key, legacyTokenTypeKeyPrefix)

begin += len(legacyTokenTypeKeyPrefix)
key[begin] = byte(len(contractID))

begin++
copy(key[begin:], contractID)

return key
}
8 changes: 8 additions & 0 deletions x/collection/keeper/migrations/v3/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func addCoin(store storetypes.KVStore, contractID string, address sdk.AccAddress
panic(err)
}
store.Set(key, bz)

// set owner
key = ownerKey(contractID, amount.TokenId)
bz, err = address.Marshal()
if err != nil {
panic(err)
}
store.Set(key, bz)
}

func removeCoin(store storetypes.KVStore, contractID string, address sdk.AccAddress, ftID string) {
Expand Down
28 changes: 27 additions & 1 deletion x/collection/keeper/migrations/v3/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var (
mintedKeyPrefix = []byte{0x41}
burntKeyPrefix = []byte{0x42}

legacyTokenKeyPrefix = []byte{0xf0}
legacyTokenKeyPrefix = []byte{0xf0}
legacyTokenTypeKeyPrefix = []byte{0xf1}
)

// Deprecated
Expand Down Expand Up @@ -271,3 +272,28 @@ func legacyTokenKeyPrefixByContractID(contractID string) []byte {

return key
}

func legacyTokenTypeKey(contractID, tokenType string) []byte {
prefix := legacyTokenTypeKeyPrefixByContractID(contractID)
key := make([]byte, len(prefix)+len(tokenType))

copy(key, prefix)
copy(key[len(prefix):], tokenType)

return key
}

func legacyTokenTypeKeyPrefixByContractID(contractID string) []byte {
key := make([]byte, len(legacyTokenTypeKeyPrefix)+1+len(contractID))

begin := 0
copy(key, legacyTokenTypeKeyPrefix)

begin += len(legacyTokenTypeKeyPrefix)
key[begin] = byte(len(contractID))

begin++
copy(key[begin:], contractID)

return key
}
3 changes: 3 additions & 0 deletions x/collection/keeper/migrations/v3/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func removeFTs(store storetypes.KVStore, cdc codec.BinaryCodec, contractID strin
panic(err)
}

key := legacyTokenTypeKey(contractID, class.GetId())
store.Delete(key)

if ftClass, ok := class.(*collection.FTClass); ok {
ftID := newFTID(ftClass.Id)
if !validateFTID(ftID) {
Expand Down
2 changes: 1 addition & 1 deletion x/collection/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ func (s msgServer) Modify(c context.Context, req *collection.MsgModify) (*collec
classID := tokenType
tokenID := classID + tokenIndex
if tokenType != "" {
if tokenIndex != "" && collection.ValidateNFTID(tokenID) == nil {
if tokenIndex != "" {
event := collection.EventModifiedNFT{
ContractId: req.ContractId,
Operator: req.Owner,
Expand Down
30 changes: 0 additions & 30 deletions x/collection/keeper/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,3 @@ func (k Keeper) deleteOwner(ctx sdk.Context, contractID, tokenID string) {
panic(err)
}
}

// Deprecated
func (k Keeper) setLegacyToken(ctx sdk.Context, contractID, tokenID string) {
store := k.storeService.OpenKVStore(ctx)
key := legacyTokenKey(contractID, tokenID)
err := store.Set(key, []byte{})
if err != nil {
panic(err)
}
}

// Deprecated
func (k Keeper) deleteLegacyToken(ctx sdk.Context, contractID, tokenID string) {
store := k.storeService.OpenKVStore(ctx)
key := legacyTokenKey(contractID, tokenID)
err := store.Delete(key)
if err != nil {
panic(err)
}
}

// Deprecated
func (k Keeper) setLegacyTokenType(ctx sdk.Context, contractID, tokenType string) {
store := k.storeService.OpenKVStore(ctx)
key := legacyTokenTypeKey(contractID, tokenType)
err := store.Set(key, []byte{})
if err != nil {
panic(err)
}
}
10 changes: 2 additions & 8 deletions x/collection/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func (k Keeper) addCoins(ctx sdk.Context, contractID string, address sdk.AccAddr
balance := k.GetBalance(ctx, contractID, address, coin.TokenId)
newBalance := balance.Add(coin.Amount)
k.setBalance(ctx, contractID, address, coin.TokenId, newBalance)

if err := collection.ValidateNFTID(coin.TokenId); err == nil {
k.setOwner(ctx, contractID, coin.TokenId, address)
}
k.setOwner(ctx, contractID, coin.TokenId, address)
}
}

Expand All @@ -36,10 +33,7 @@ func (k Keeper) subtractCoins(ctx sdk.Context, contractID string, address sdk.Ac
return collection.ErrInsufficientToken.Wrapf("%s is smaller than %s", balance, coin.Amount)
}
k.setBalance(ctx, contractID, address, coin.TokenId, newBalance)

if err := collection.ValidateNFTID(coin.TokenId); err == nil {
k.deleteOwner(ctx, contractID, coin.TokenId)
}
k.deleteOwner(ctx, contractID, coin.TokenId)
}

return nil
Expand Down
13 changes: 1 addition & 12 deletions x/collection/keeper/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ func (k Keeper) CreateTokenClass(ctx sdk.Context, contractID string, class colle

if nftClass, ok := class.(*collection.NFTClass); ok {
k.setNextTokenID(ctx, contractID, nftClass.Id, math.OneUint())

// legacy
k.setLegacyTokenType(ctx, contractID, nftClass.Id)
} else {
panic("TokenClass only supports NFTClass")
}
Expand Down Expand Up @@ -196,9 +193,6 @@ func (k Keeper) MintNFT(ctx sdk.Context, contractID string, to sdk.AccAddress, p
k.setMinted(ctx, contractID, classID, minted.Add(amount))

tokens = append(tokens, token)

// legacy
k.setLegacyToken(ctx, contractID, tokenID)
}

return tokens, nil
Expand All @@ -212,12 +206,7 @@ func (k Keeper) BurnCoins(ctx sdk.Context, contractID string, from sdk.AccAddres
burntAmount := []collection.Coin{}
for _, coin := range amount {
burntAmount = append(burntAmount, coin)
if err := collection.ValidateNFTID(coin.TokenId); err == nil {
k.deleteNFT(ctx, contractID, coin.TokenId)

// legacy
k.deleteLegacyToken(ctx, contractID, coin.TokenId)
}
k.deleteNFT(ctx, contractID, coin.TokenId)
}

// update statistics
Expand Down

0 comments on commit 8f40558

Please sign in to comment.