-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
251 lines (211 loc) · 8.55 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
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
package keeper
import (
"fmt"
"time"
"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"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/incubus-network/fury/x/bep3/types"
)
// Keeper of the bep3 store
type Keeper struct {
key storetypes.StoreKey
cdc codec.Codec
paramSubspace paramtypes.Subspace
bankKeeper types.BankKeeper
accountKeeper types.AccountKeeper
Maccs map[string]bool
}
// NewKeeper creates a bep3 keeper
func NewKeeper(cdc codec.Codec, key storetypes.StoreKey, sk types.BankKeeper, ak types.AccountKeeper,
paramstore paramtypes.Subspace, maccs map[string]bool,
) Keeper {
if !paramstore.HasKeyTable() {
paramstore = paramstore.WithKeyTable(types.ParamKeyTable())
}
keeper := Keeper{
key: key,
cdc: cdc,
paramSubspace: paramstore,
bankKeeper: sk,
accountKeeper: ak,
Maccs: maccs,
}
return keeper
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// ------------------------------------------
// Atomic Swaps
// ------------------------------------------
// SetAtomicSwap puts the AtomicSwap into the store, and updates any indexes.
func (k Keeper) SetAtomicSwap(ctx sdk.Context, atomicSwap types.AtomicSwap) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapKeyPrefix)
bz := k.cdc.MustMarshal(&atomicSwap)
store.Set(atomicSwap.GetSwapID(), bz)
}
// GetAtomicSwap gets an AtomicSwap from the store.
func (k Keeper) GetAtomicSwap(ctx sdk.Context, swapID []byte) (types.AtomicSwap, bool) {
var atomicSwap types.AtomicSwap
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapKeyPrefix)
bz := store.Get(swapID)
if bz == nil {
return atomicSwap, false
}
k.cdc.MustUnmarshal(bz, &atomicSwap)
return atomicSwap, true
}
// RemoveAtomicSwap removes an AtomicSwap from the AtomicSwapKeyPrefix.
func (k Keeper) RemoveAtomicSwap(ctx sdk.Context, swapID []byte) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapKeyPrefix)
store.Delete(swapID)
}
// IterateAtomicSwaps provides an iterator over all stored AtomicSwaps.
// For each AtomicSwap, cb will be called. If cb returns true, the iterator will close and stop.
func (k Keeper) IterateAtomicSwaps(ctx sdk.Context, cb func(atomicSwap types.AtomicSwap) (stop bool)) {
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.key), types.AtomicSwapKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var atomicSwap types.AtomicSwap
k.cdc.MustUnmarshal(iterator.Value(), &atomicSwap)
if cb(atomicSwap) {
break
}
}
}
// GetAllAtomicSwaps returns all AtomicSwaps from the store
func (k Keeper) GetAllAtomicSwaps(ctx sdk.Context) (atomicSwaps types.AtomicSwaps) {
k.IterateAtomicSwaps(ctx, func(atomicSwap types.AtomicSwap) bool {
atomicSwaps = append(atomicSwaps, atomicSwap)
return false
})
return
}
// ------------------------------------------
// Atomic Swap Block Index
// ------------------------------------------
// InsertIntoByBlockIndex adds a swap ID and expiration time into the byBlock index.
func (k Keeper) InsertIntoByBlockIndex(ctx sdk.Context, atomicSwap types.AtomicSwap) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapByBlockPrefix)
store.Set(types.GetAtomicSwapByHeightKey(atomicSwap.ExpireHeight, atomicSwap.GetSwapID()), atomicSwap.GetSwapID())
}
// RemoveFromByBlockIndex removes an AtomicSwap from the byBlock index.
func (k Keeper) RemoveFromByBlockIndex(ctx sdk.Context, atomicSwap types.AtomicSwap) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapByBlockPrefix)
store.Delete(types.GetAtomicSwapByHeightKey(atomicSwap.ExpireHeight, atomicSwap.GetSwapID()))
}
// IterateAtomicSwapsByBlock provides an iterator over AtomicSwaps ordered by AtomicSwap expiration block
// For each AtomicSwap cb will be called. If cb returns true the iterator will close and stop.
func (k Keeper) IterateAtomicSwapsByBlock(ctx sdk.Context, inclusiveCutoffTime uint64, cb func(swapID []byte) (stop bool)) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapByBlockPrefix)
iterator := store.Iterator(
nil, // start at the very start of the prefix store
sdk.PrefixEndBytes(sdk.Uint64ToBigEndian(inclusiveCutoffTime)), // end of range
)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
id := iterator.Value()
if cb(id) {
break
}
}
}
// ------------------------------------------
// Atomic Swap Longterm Storage Index
// ------------------------------------------
// InsertIntoLongtermStorage adds a swap ID and deletion time into the longterm storage index.
// Completed swaps are stored for 1 week.
func (k Keeper) InsertIntoLongtermStorage(ctx sdk.Context, atomicSwap types.AtomicSwap) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapLongtermStoragePrefix)
deletionHeight := uint64(atomicSwap.ClosedBlock) + types.DefaultLongtermStorageDuration
store.Set(types.GetAtomicSwapByHeightKey(deletionHeight, atomicSwap.GetSwapID()), atomicSwap.GetSwapID())
}
// RemoveFromLongtermStorage removes a swap from the into the longterm storage index
func (k Keeper) RemoveFromLongtermStorage(ctx sdk.Context, atomicSwap types.AtomicSwap) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapLongtermStoragePrefix)
deletionHeight := uint64(atomicSwap.ClosedBlock) + types.DefaultLongtermStorageDuration
store.Delete(types.GetAtomicSwapByHeightKey(deletionHeight, atomicSwap.GetSwapID()))
}
// IterateAtomicSwapsLongtermStorage provides an iterator over AtomicSwaps ordered by deletion height.
// For each AtomicSwap cb will be called. If cb returns true the iterator will close and stop.
func (k Keeper) IterateAtomicSwapsLongtermStorage(ctx sdk.Context, inclusiveCutoffTime uint64,
cb func(swapID []byte) (stop bool),
) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapLongtermStoragePrefix)
iterator := store.Iterator(
nil, // start at the very start of the prefix store
sdk.PrefixEndBytes(sdk.Uint64ToBigEndian(inclusiveCutoffTime)), // end of range
)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
id := iterator.Value()
if cb(id) {
break
}
}
}
// ------------------------------------------
// Asset Supplies
// ------------------------------------------
// GetAssetSupply gets an asset's current supply from the store.
func (k Keeper) GetAssetSupply(ctx sdk.Context, denom string) (types.AssetSupply, bool) {
var assetSupply types.AssetSupply
store := prefix.NewStore(ctx.KVStore(k.key), types.AssetSupplyPrefix)
bz := store.Get([]byte(denom))
if bz == nil {
return types.AssetSupply{}, false
}
k.cdc.MustUnmarshal(bz, &assetSupply)
return assetSupply, true
}
// SetAssetSupply updates an asset's supply
func (k Keeper) SetAssetSupply(ctx sdk.Context, supply types.AssetSupply, denom string) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AssetSupplyPrefix)
store.Set([]byte(denom), k.cdc.MustMarshal(&supply))
}
// IterateAssetSupplies provides an iterator over all stored AssetSupplies.
func (k Keeper) IterateAssetSupplies(ctx sdk.Context, cb func(supply types.AssetSupply) (stop bool)) {
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.key), types.AssetSupplyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var supply types.AssetSupply
k.cdc.MustUnmarshal(iterator.Value(), &supply)
if cb(supply) {
break
}
}
}
// GetAllAssetSupplies returns all asset supplies from the store
func (k Keeper) GetAllAssetSupplies(ctx sdk.Context) (supplies types.AssetSupplies) {
k.IterateAssetSupplies(ctx, func(supply types.AssetSupply) bool {
supplies = append(supplies, supply)
return false
})
return
}
// GetPreviousBlockTime get the blocktime for the previous block
func (k Keeper) GetPreviousBlockTime(ctx sdk.Context) (blockTime time.Time, found bool) {
store := prefix.NewStore(ctx.KVStore(k.key), types.PreviousBlockTimeKey)
b := store.Get(types.PreviousBlockTimeKey)
if b == nil {
return time.Time{}, false
}
if err := blockTime.UnmarshalBinary(b); err != nil {
panic(err)
}
return blockTime, true
}
// SetPreviousBlockTime set the time of the previous block
func (k Keeper) SetPreviousBlockTime(ctx sdk.Context, blockTime time.Time) {
store := prefix.NewStore(ctx.KVStore(k.key), types.PreviousBlockTimeKey)
b, err := blockTime.MarshalBinary()
if err != nil {
panic(err)
}
store.Set(types.PreviousBlockTimeKey, b)
}