Lorenz Herzberger 468fbb5305
402 bug asset query does not repsect lookupperiodinmin value (#405)
* chore: remove unused asset.proto and asset.pb.go files
* feat: add AssetByAddress store functionality
* fix: asset query now returns numElements passed in req
* chore: add migration for new store mechanics
* chore: set upgradehandler for assetmodule migration
* chore: removed obsolete GetCIDsByAddress function
* chore: adjust cmd usage
---------

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
2024-06-06 14:39:05 +02:00

74 lines
2.6 KiB
Go

package keeper
import (
"github.com/planetmint/planetmint-go/util"
"github.com/planetmint/planetmint-go/x/asset/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k Keeper) setAddresAssetCount(ctx sdk.Context, address string, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
store.Set(types.AddressCountKey(address), util.SerializeUint64(count))
}
func (k Keeper) GetAddressAssetCount(ctx sdk.Context, address string) (count uint64, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
countBytes := store.Get(types.AddressCountKey(address))
if countBytes == nil {
return 0, false
}
return util.DeserializeUint64(countBytes), true
}
func (k Keeper) incrementAddressAssetCount(ctx sdk.Context, address string) uint64 {
count, _ := k.GetAddressAssetCount(ctx, address)
k.setAddresAssetCount(ctx, address, count+1)
return count + 1
}
func (k Keeper) StoreAddressAsset(ctx sdk.Context, msg types.MsgNotarizeAsset) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AddressPrefix(msg.GetCreator()))
count := k.incrementAddressAssetCount(ctx, msg.GetCreator())
store.Set(util.SerializeUint64(count), []byte(msg.GetCid()))
}
func (k Keeper) StoreAsset(ctx sdk.Context, msg types.MsgNotarizeAsset) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
store.Set(util.SerializeString(msg.GetCid()), []byte(msg.GetCreator()))
k.StoreAddressAsset(ctx, msg)
}
func (k Keeper) GetAsset(ctx sdk.Context, cid string) (msg types.MsgNotarizeAsset, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AssetKey))
creatorBytes := store.Get(util.SerializeString(cid))
if creatorBytes == nil {
return msg, false
}
msg.Cid = cid
msg.Creator = string(creatorBytes)
return msg, true
}
func (k Keeper) GetAssetByAddressAndID(ctx sdk.Context, address string, id uint64) (cid string, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AddressPrefix(address))
cidBytes := store.Get(util.SerializeUint64(id))
if cidBytes == nil {
return cid, false
}
return string(cidBytes), true
}
func (k Keeper) GetAssetsByAddress(ctx sdk.Context, address string, start []byte, end []byte) (cids []string, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AddressPrefix(address))
iterator := store.ReverseIterator(start, end)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
cidBytes := iterator.Value()
cids = append(cids, string(cidBytes))
}
return cids, len(cids) > 0
}