-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaims.go
299 lines (254 loc) · 8.72 KB
/
claims.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ingenuity-build/quicksilver/x/claimsmanager/types"
)
func (k Keeper) NewClaim(ctx sdk.Context, address string, chainID string, module types.ClaimType, srcChainID string, amount uint64) types.Claim {
return types.Claim{UserAddress: address, ChainId: chainID, Module: module, SourceChainId: srcChainID, Amount: amount}
}
// GetClaim returns claim
func (k Keeper) GetClaim(ctx sdk.Context, chainID string, address string, module types.ClaimType, srcChainID string) (types.Claim, bool) {
data := types.Claim{}
store := prefix.NewStore(ctx.KVStore(k.storeKey), nil)
key := types.GetKeyClaim(chainID, address, module, srcChainID)
bz := store.Get(key)
if len(bz) == 0 {
return data, false
}
k.cdc.MustUnmarshal(bz, &data)
return data, true
}
// GetLastEpochClaim returns claim from last epoch
func (k Keeper) GetLastEpochClaim(ctx sdk.Context, chainID string, address string, module types.ClaimType, srcChainID string) (types.Claim, bool) {
data := types.Claim{}
store := prefix.NewStore(ctx.KVStore(k.storeKey), nil)
key := types.GetKeyLastEpochClaim(chainID, address, module, srcChainID)
bz := store.Get(key)
if len(bz) == 0 {
return data, false
}
k.cdc.MustUnmarshal(bz, &data)
return data, true
}
// SetClaim sets claim
func (k Keeper) SetClaim(ctx sdk.Context, claim *types.Claim) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), nil)
bz := k.cdc.MustMarshal(claim)
store.Set(types.GetKeyClaim(claim.ChainId, claim.UserAddress, claim.Module, claim.SourceChainId), bz)
}
// SetLastEpochClaim sets claim for last epoch
func (k Keeper) SetLastEpochClaim(ctx sdk.Context, claim *types.Claim) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), nil)
bz := k.cdc.MustMarshal(claim)
store.Set(types.GetKeyLastEpochClaim(claim.ChainId, claim.UserAddress, claim.Module, claim.SourceChainId), bz)
}
// DeleteClaim deletes claim
func (k Keeper) DeleteClaim(ctx sdk.Context, claim *types.Claim) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), nil)
store.Delete(types.GetKeyClaim(claim.ChainId, claim.UserAddress, claim.Module, claim.SourceChainId))
}
// DeleteLastEpochClaim deletes claim for last epoch
func (k Keeper) DeleteLastEpochClaim(ctx sdk.Context, claim *types.Claim) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), nil)
store.Delete(types.GetKeyLastEpochClaim(claim.ChainId, claim.UserAddress, claim.Module, claim.SourceChainId))
}
// IterateClaims iterates through zone claims.
func (k Keeper) IterateClaims(ctx sdk.Context, chainID string, fn func(index int64, data types.Claim) (stop bool)) {
// noop
if fn == nil {
return
}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPrefixClaim(chainID))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
data := types.Claim{}
k.cdc.MustUnmarshal(iterator.Value(), &data)
stop := fn(i, data)
if stop {
break
}
i++
}
}
// IterateUserClaims iterates through zone claims for a given address.
func (k Keeper) IterateUserClaims(ctx sdk.Context, chainID string, address string, fn func(index int64, data types.Claim) (stop bool)) {
// noop
if fn == nil {
return
}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPrefixUserClaim(chainID, address))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
data := types.Claim{}
k.cdc.MustUnmarshal(iterator.Value(), &data)
stop := fn(i, data)
if stop {
break
}
i++
}
}
// IterateLastEpochClaims iterates through zone claims from last epoch.
func (k Keeper) IterateLastEpochClaims(ctx sdk.Context, chainID string, fn func(index int64, data types.Claim) (stop bool)) {
// noop
if fn == nil {
return
}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPrefixLastEpochClaim(chainID))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
data := types.Claim{}
k.cdc.MustUnmarshal(iterator.Value(), &data)
stop := fn(i, data)
if stop {
break
}
i++
}
}
// IterateLastEpochUserClaims iterates through zone claims from last epoch for a given user.
func (k Keeper) IterateLastEpochUserClaims(ctx sdk.Context, chainID string, address string, fn func(index int64, data types.Claim) (stop bool)) {
// noop
if fn == nil {
return
}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPrefixLastEpochUserClaim(chainID, address))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
data := types.Claim{}
k.cdc.MustUnmarshal(iterator.Value(), &data)
stop := fn(i, data)
if stop {
break
}
i++
}
}
// IterateLastEpochUserClaims iterates through zone claims from last epoch for a given user.
func (k Keeper) IterateAllLastEpochClaims(ctx sdk.Context, fn func(index int64, key []byte, data types.Claim) (stop bool)) {
// noop
if fn == nil {
return
}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixLastEpochClaim)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
data := types.Claim{}
k.cdc.MustUnmarshal(iterator.Value(), &data)
stop := fn(i, iterator.Key(), data)
if stop {
break
}
i++
}
}
// IterateAllClaims iterates through all claims.
func (k Keeper) IterateAllClaims(ctx sdk.Context, fn func(index int64, key []byte, data types.Claim) (stop bool)) {
// noop
if fn == nil {
return
}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixClaim)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
data := types.Claim{}
k.cdc.MustUnmarshal(iterator.Value(), &data)
stop := fn(i, iterator.Key(), data)
if stop {
break
}
i++
}
}
// AllClaims returns a slice containing all claims from the store.
func (k Keeper) AllClaims(ctx sdk.Context) []*types.Claim {
claims := []*types.Claim{}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixClaim)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
claim := types.Claim{}
k.cdc.MustUnmarshal(iterator.Value(), &claim)
claims = append(claims, &claim)
}
return claims
}
func (k Keeper) AllZoneClaims(ctx sdk.Context, chainID string) []*types.Claim {
claims := []*types.Claim{}
k.IterateClaims(ctx, chainID, func(_ int64, claim types.Claim) (stop bool) {
claims = append(claims, &claim)
return false
})
return claims
}
func (k Keeper) AllZoneUserClaims(ctx sdk.Context, chainID string, address string) []*types.Claim {
claims := []*types.Claim{}
k.IterateUserClaims(ctx, chainID, address, func(_ int64, claim types.Claim) (stop bool) {
claims = append(claims, &claim)
return false
})
return claims
}
func (k Keeper) AllZoneLastEpochClaims(ctx sdk.Context, chainID string) []*types.Claim {
claims := []*types.Claim{}
k.IterateLastEpochClaims(ctx, chainID, func(_ int64, claim types.Claim) (stop bool) {
claims = append(claims, &claim)
return false
})
return claims
}
func (k Keeper) AllZoneLastEpochUserClaims(ctx sdk.Context, chainID string, address string) []*types.Claim {
claims := []*types.Claim{}
k.IterateLastEpochUserClaims(ctx, chainID, address, func(_ int64, claim types.Claim) (stop bool) {
claims = append(claims, &claim)
return false
})
return claims
}
// ClearClaims deletes all the current epoch claims of the given zone.
func (k Keeper) ClearClaims(ctx sdk.Context, chainID string) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPrefixClaim(chainID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()
store.Delete(key)
}
}
// ClearLastEpochClaims deletes all the last epoch claims of the given zone.
func (k Keeper) ClearLastEpochClaims(ctx sdk.Context, chainID string) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPrefixLastEpochClaim(chainID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()
store.Delete(key)
}
}
// ArchiveAndGarbageCollectClaims deletes all the last epoch claims and moves the current epoch claims to the last epoch store.
func (k Keeper) ArchiveAndGarbageCollectClaims(ctx sdk.Context, chainID string) {
k.ClearLastEpochClaims(ctx, chainID)
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPrefixClaim(chainID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()
store.Delete(key)
newKey := types.KeyPrefixLastEpochClaim
newKey = append(newKey, key[1:]...) // update prefix from KeyPrefixClaim to KeyPrefixLastEpochClaim
store.Set(newKey, iterator.Value())
}
}