/
inflation.go
79 lines (61 loc) · 1.54 KB
/
inflation.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
package keeper
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sentinel-official/hub/x/mint/types"
)
func (k *Keeper) SetInflation(ctx sdk.Context, inflation types.Inflation) {
var (
store = k.Store(ctx)
key = types.InflationKey(inflation.Timestamp)
value = k.cdc.MustMarshal(&inflation)
)
store.Set(key, value)
}
func (k *Keeper) GetInflation(ctx sdk.Context, t time.Time) (inflation types.Inflation, found bool) {
var (
store = k.Store(ctx)
key = types.InflationKey(t)
value = store.Get(key)
)
if value == nil {
return inflation, false
}
k.cdc.MustUnmarshal(value, &inflation)
return inflation, true
}
func (k *Keeper) DeleteInflation(ctx sdk.Context, t time.Time) {
var (
store = k.Store(ctx)
key = types.InflationKey(t)
)
store.Delete(key)
}
func (k *Keeper) GetInflations(ctx sdk.Context) (items []types.Inflation) {
var (
store = k.Store(ctx)
iter = sdk.KVStorePrefixIterator(store, types.InflationKeyPrefix)
)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var item types.Inflation
k.cdc.MustUnmarshal(iter.Value(), &item)
items = append(items, item)
}
return items
}
func (k *Keeper) IterateInflations(ctx sdk.Context, fn func(index int, item types.Inflation) (stop bool)) {
var (
store = k.Store(ctx)
iter = sdk.KVStorePrefixIterator(store, types.InflationKeyPrefix)
)
defer iter.Close()
for i := 0; iter.Valid(); iter.Next() {
var item types.Inflation
k.cdc.MustUnmarshal(iter.Value(), &item)
if stop := fn(i, item); stop {
break
}
i++
}
}