-
Notifications
You must be signed in to change notification settings - Fork 28
/
listing.go
146 lines (119 loc) · 4.48 KB
/
listing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package keeper
import (
"encoding/binary"
"github.com/OmniFlix/omniflixhub/v3/x/marketplace/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
gogotypes "github.com/cosmos/gogoproto/types"
)
// GetListingCount get the total number of listings
func (k Keeper) GetListingCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.PrefixListingsCount
bz := store.Get(byteKey)
// Count doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// SetListingCount set the total number of listings
func (k Keeper) SetListingCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.PrefixListingsCount
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
// SetListing set a specific listing in the store
func (k Keeper) SetListing(ctx sdk.Context, listing types.Listing) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixListingId)
b := k.cdc.MustMarshal(&listing)
store.Set(types.KeyListingIdPrefix(listing.Id), b)
}
// GetListing returns a listing from its id
func (k Keeper) GetListing(ctx sdk.Context, id string) (val types.Listing, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixListingId)
b := store.Get(types.KeyListingIdPrefix(id))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// GetListing returns a listing from its nft id
func (k Keeper) GetListingIdByNftId(ctx sdk.Context, nftId string) (val string, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixListingNFTID)
b := store.Get(types.KeyListingNFTIDPrefix(nftId))
if b == nil {
return val, false
}
var listingId gogotypes.StringValue
k.cdc.MustUnmarshal(b, &listingId)
return listingId.Value, true
}
// RemoveListing removes a listing from the store
func (k Keeper) RemoveListing(ctx sdk.Context, id string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixListingId)
store.Delete(types.KeyListingIdPrefix(id))
}
// GetAllListings returns all listings
func (k Keeper) GetAllListings(ctx sdk.Context) (list []types.Listing) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixListingId)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Listing
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetListingsByOwner returns all listings of specific owner
func (k Keeper) GetListingsByOwner(ctx sdk.Context, owner sdk.AccAddress) (listings []types.Listing) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, append(types.PrefixListingOwner, owner.Bytes()...))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var id gogotypes.StringValue
k.cdc.MustUnmarshal(iterator.Value(), &id)
listing, found := k.GetListing(ctx, id.Value)
if !found {
continue
}
listings = append(listings, listing)
}
return
}
func (k Keeper) HasListing(ctx sdk.Context, id string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.KeyListingIdPrefix(id))
}
func (k Keeper) SetWithOwner(ctx sdk.Context, owner sdk.AccAddress, id string) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.StringValue{Value: id})
store.Set(types.KeyListingOwnerPrefix(owner, id), bz)
}
func (k Keeper) UnsetWithOwner(ctx sdk.Context, owner sdk.AccAddress, id string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.KeyListingOwnerPrefix(owner, id))
}
func (k Keeper) SetWithNFTID(ctx sdk.Context, nftId, id string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixListingNFTID)
bz := k.cdc.MustMarshal(&gogotypes.StringValue{Value: id})
store.Set(types.KeyListingNFTIDPrefix(nftId), bz)
}
func (k Keeper) UnsetWithNFTID(ctx sdk.Context, nftId string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixListingNFTID)
store.Delete(types.KeyListingNFTIDPrefix(nftId))
}
func (k Keeper) SetWithPriceDenom(ctx sdk.Context, priceDenom, id string) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.StringValue{Value: id})
store.Set(types.KeyListingPriceDenomPrefix(priceDenom, id), bz)
}
func (k Keeper) UnsetWithPriceDenom(ctx sdk.Context, priceDenom, id string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.KeyListingPriceDenomPrefix(priceDenom, id))
}