forked from crescent-network/crescent
/
store.go
196 lines (172 loc) 路 5.56 KB
/
store.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
package keeper
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jesse-pinkman-cre/crescent/x/lpfarm/types"
)
func (k Keeper) GetLastBlockTime(ctx sdk.Context) (t time.Time, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.LastBlockTimeKey)
if bz == nil {
return
}
t, err := sdk.ParseTimeBytes(bz)
if err != nil {
panic(err)
}
return t, true
}
func (k Keeper) SetLastBlockTime(ctx sdk.Context, t time.Time) {
store := ctx.KVStore(k.storeKey)
store.Set(types.LastBlockTimeKey, sdk.FormatTimeBytes(t))
}
func (k Keeper) GetLastPlanId(ctx sdk.Context) (id uint64, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.LastPlanIdKey)
if bz == nil {
return
}
return sdk.BigEndianToUint64(bz), true
}
func (k Keeper) SetLastPlanId(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.LastPlanIdKey, sdk.Uint64ToBigEndian(id))
}
func (k Keeper) GetNumPrivatePlans(ctx sdk.Context) (num uint64) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.NumPrivatePlansKey)
if bz == nil {
return 0
}
return sdk.BigEndianToUint64(bz)
}
func (k Keeper) SetNumPrivatePlans(ctx sdk.Context, num uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.NumPrivatePlansKey, sdk.Uint64ToBigEndian(num))
}
func (k Keeper) GetPlan(ctx sdk.Context, id uint64) (plan types.Plan, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetPlanKey(id))
if bz == nil {
return
}
k.cdc.MustUnmarshal(bz, &plan)
return plan, true
}
func (k Keeper) SetPlan(ctx sdk.Context, plan types.Plan) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetPlanKey(plan.Id), k.cdc.MustMarshal(&plan))
}
func (k Keeper) IterateAllPlans(ctx sdk.Context, cb func(plan types.Plan) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.PlanKeyPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var plan types.Plan
k.cdc.MustUnmarshal(iter.Value(), &plan)
if cb(plan) {
break
}
}
}
func (k Keeper) GetFarm(ctx sdk.Context, denom string) (farm types.Farm, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetFarmKey(denom))
if bz == nil {
return
}
k.cdc.MustUnmarshal(bz, &farm)
return farm, true
}
func (k Keeper) SetFarm(ctx sdk.Context, denom string, farm types.Farm) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetFarmKey(denom), k.cdc.MustMarshal(&farm))
}
func (k Keeper) IterateAllFarms(ctx sdk.Context, cb func(denom string, farm types.Farm) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.FarmKeyPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
denom := types.ParseFarmKey(iter.Key())
var farm types.Farm
k.cdc.MustUnmarshal(iter.Value(), &farm)
if cb(denom, farm) {
break
}
}
}
func (k Keeper) GetPosition(ctx sdk.Context, farmerAddr sdk.AccAddress, denom string) (position types.Position, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetPositionKey(farmerAddr, denom))
if bz == nil {
return
}
k.cdc.MustUnmarshal(bz, &position)
return position, true
}
func (k Keeper) SetPosition(ctx sdk.Context, position types.Position) {
store := ctx.KVStore(k.storeKey)
farmerAddr, err := sdk.AccAddressFromBech32(position.Farmer)
if err != nil {
panic(err)
}
store.Set(types.GetPositionKey(farmerAddr, position.Denom), k.cdc.MustMarshal(&position))
}
func (k Keeper) DeletePosition(ctx sdk.Context, farmerAddr sdk.AccAddress, denom string) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetPositionKey(farmerAddr, denom))
}
func (k Keeper) IterateAllPositions(ctx sdk.Context, cb func(position types.Position) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.PositionKeyPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var position types.Position
k.cdc.MustUnmarshal(iter.Value(), &position)
if cb(position) {
break
}
}
}
func (k Keeper) IteratePositionsByFarmer(ctx sdk.Context, farmerAddr sdk.AccAddress, cb func(position types.Position) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.GetPositionsByFarmerKeyPrefix(farmerAddr))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var position types.Position
k.cdc.MustUnmarshal(iter.Value(), &position)
if cb(position) {
break
}
}
}
func (k Keeper) GetHistoricalRewards(ctx sdk.Context, denom string, period uint64) (hist types.HistoricalRewards, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetHistoricalRewardsKey(denom, period))
if bz == nil {
return
}
k.cdc.MustUnmarshal(bz, &hist)
return hist, true
}
func (k Keeper) SetHistoricalRewards(ctx sdk.Context, denom string, period uint64, hist types.HistoricalRewards) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetHistoricalRewardsKey(denom, period), k.cdc.MustMarshal(&hist))
}
func (k Keeper) DeleteHistoricalRewards(ctx sdk.Context, denom string, period uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetHistoricalRewardsKey(denom, period))
}
func (k Keeper) IterateAllHistoricalRewards(ctx sdk.Context, cb func(denom string, period uint64, hist types.HistoricalRewards) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.HistoricalRewardsKeyPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
denom, period := types.ParseHistoricalRewardsKey(iter.Key())
var hist types.HistoricalRewards
k.cdc.MustUnmarshal(iter.Value(), &hist)
if cb(denom, period, hist) {
break
}
}
}