-
Notifications
You must be signed in to change notification settings - Fork 44
/
keeper.go
197 lines (165 loc) · 5.5 KB
/
keeper.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package v4
import (
"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"
v4types "github.com/desmos-labs/desmos/v7/x/profiles/legacy/v4/types"
)
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
}
func NewKeeper(storeKey storetypes.StoreKey, cdc codec.BinaryCodec) Keeper {
return Keeper{
storeKey: storeKey,
cdc: cdc,
}
}
func (k Keeper) IterateDTags(ctx sdk.Context, fn func(index int64, dTag string, value []byte) (stop bool)) {
store := ctx.KVStore(k.storeKey)
dTagsStore := prefix.NewStore(store, v4types.DTagPrefix)
iterator := dTagsStore.Iterator(nil, nil)
defer iterator.Close()
var stop = false
var index = int64(0)
for ; iterator.Valid() && !stop; iterator.Next() {
stop = fn(index, string(iterator.Key()), iterator.Value())
index++
}
}
func (k Keeper) IterateDTagTransferRequests(ctx sdk.Context, fn func(index int64, request v4types.DTagTransferRequest) (stop bool)) {
store := ctx.KVStore(k.storeKey)
requestsStore := prefix.NewStore(store, v4types.DTagTransferRequestPrefix)
iterator := requestsStore.Iterator(nil, nil)
defer iterator.Close()
var stop = false
var index = int64(0)
for ; iterator.Valid() && !stop; iterator.Next() {
var request v4types.DTagTransferRequest
err := k.cdc.Unmarshal(iterator.Value(), &request)
if err != nil {
panic(err)
}
stop = fn(index, request)
index++
}
}
func (k Keeper) IterateRelationships(ctx sdk.Context, fn func(index int64, relationship v4types.Relationship) (stop bool)) {
store := ctx.KVStore(k.storeKey)
relationshipsStore := prefix.NewStore(store, v4types.RelationshipsStorePrefix)
iterator := relationshipsStore.Iterator(nil, nil)
defer iterator.Close()
var stop = false
var index = int64(0)
for ; iterator.Valid() && !stop; iterator.Next() {
var relationship v4types.Relationship
err := k.cdc.Unmarshal(iterator.Value(), &relationship)
if err != nil {
panic(err)
}
stop = fn(index, relationship)
index++
}
}
func (k Keeper) GetRelationships(ctx sdk.Context) []v4types.Relationship {
var values []v4types.Relationship
k.IterateRelationships(ctx, func(_ int64, relationship v4types.Relationship) (stop bool) {
values = append(values, relationship)
return false
})
return values
}
func (k Keeper) DeleteRelationships(ctx sdk.Context) {
var keys [][]byte
k.IterateRelationships(ctx, func(_ int64, relationship v4types.Relationship) (stop bool) {
keys = append(keys, v4types.RelationshipsStoreKey(relationship.Creator, relationship.SubspaceID, relationship.Recipient))
return false
})
store := ctx.KVStore(k.storeKey)
for _, key := range keys {
store.Delete(key)
}
}
func (k Keeper) IterateBlocks(ctx sdk.Context, fn func(index int64, relationship v4types.UserBlock) (stop bool)) {
store := ctx.KVStore(k.storeKey)
userBlocksStore := prefix.NewStore(store, v4types.UsersBlocksStorePrefix)
iterator := userBlocksStore.Iterator(nil, nil)
defer iterator.Close()
var stop = false
var index = int64(0)
for ; iterator.Valid() && !stop; iterator.Next() {
var block v4types.UserBlock
err := k.cdc.Unmarshal(iterator.Value(), &block)
if err != nil {
panic(err)
}
stop = fn(index, block)
index++
}
}
func (k Keeper) GetBlocks(ctx sdk.Context) []v4types.UserBlock {
var values []v4types.UserBlock
k.IterateBlocks(ctx, func(_ int64, block v4types.UserBlock) (stop bool) {
values = append(values, block)
return false
})
return values
}
func (k Keeper) DeleteBlocks(ctx sdk.Context) {
var keys [][]byte
k.IterateBlocks(ctx, func(_ int64, block v4types.UserBlock) (stop bool) {
keys = append(keys, v4types.UserBlockStoreKey(block.Blocker, block.SubspaceID, block.Blocked))
return false
})
store := ctx.KVStore(k.storeKey)
for _, key := range keys {
store.Delete(key)
}
}
func (k Keeper) IterateChainLinks(ctx sdk.Context, fn func(index int64, chainLink v4types.ChainLink) (stop bool)) {
store := ctx.KVStore(k.storeKey)
chainLinksStore := prefix.NewStore(store, v4types.ChainLinksPrefix)
iterator := chainLinksStore.Iterator(nil, nil)
defer iterator.Close()
var stop = false
var index = int64(0)
for ; iterator.Valid() && !stop; iterator.Next() {
var chainLink v4types.ChainLink
err := k.cdc.Unmarshal(iterator.Value(), &chainLink)
if err != nil {
panic(err)
}
stop = fn(index, chainLink)
index++
}
}
func (k Keeper) IterateApplicationLinks(ctx sdk.Context, fn func(index int64, applicationLink v4types.ApplicationLink) (stop bool)) {
store := ctx.KVStore(k.storeKey)
applicationLinksStore := prefix.NewStore(store, v4types.UserApplicationLinkPrefix)
iterator := applicationLinksStore.Iterator(nil, nil)
defer iterator.Close()
var stop = false
var index = int64(0)
for ; iterator.Valid() && !stop; iterator.Next() {
var applicationLink v4types.ApplicationLink
err := k.cdc.Unmarshal(iterator.Value(), &applicationLink)
if err != nil {
panic(err)
}
stop = fn(index, applicationLink)
index++
}
}
func (k Keeper) IterateApplicationLinkClientIDKeys(ctx sdk.Context, fn func(index int64, key []byte, value []byte) (stop bool)) {
store := ctx.KVStore(k.storeKey)
clientIDsStore := prefix.NewStore(store, v4types.ApplicationLinkClientIDPrefix)
iterator := clientIDsStore.Iterator(nil, nil)
defer iterator.Close()
var stop = false
var index = int64(0)
for ; iterator.Valid() && !stop; iterator.Next() {
stop = fn(index, append(v4types.ApplicationLinkClientIDPrefix, iterator.Key()...), iterator.Value())
index++
}
}