-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeper.go
268 lines (223 loc) · 9.47 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package keeper
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/e-money/bep3/module/types"
"github.com/tendermint/tendermint/libs/log"
)
// Keeper of the bep3 store
type Keeper struct {
key sdk.StoreKey
cdc codec.BinaryMarshaler
paramSubspace paramtypes.Subspace
// bankKeeper
bankKeeper types.BankKeeper
// authKeeper
accountKeeper types.AccountKeeper
Maccs map[string]bool
}
// NewKeeper creates a bep3 keeper
func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey, bk 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: bk,
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))
}
// EnsureModuleAccountPermissions syncs the bep3 module account's permissions with those in the supply keeper.
func (k Keeper) EnsureModuleAccountPermissions(ctx sdk.Context) error {
maccI := k.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
macc, ok := maccI.(*authtypes.ModuleAccount)
if !ok {
return fmt.Errorf("expected %s account to be a module account type", types.ModuleName)
}
_, perms := k.accountKeeper.GetModuleAddressAndPermissions(types.ModuleName)
macc.Permissions = perms
k.accountKeeper.SetModuleAccount(ctx, macc)
return nil
}
// ------------------------------------------
// 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.MustMarshalBinaryLengthPrefixed(&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.MustUnmarshalBinaryLengthPrefixed(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.MustUnmarshalBinaryLengthPrefixed(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 Timestamp
// ------------------------------------------
// InsertIntoByTimestamp adds a swap ID and expiration time into the byTimestamp index.
func (k Keeper) InsertIntoByTimestamp(ctx sdk.Context, atomicSwap types.AtomicSwap) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapByBlockPrefix)
swapKey := types.GetAtomicSwapByTimestampKey(
atomicSwap.ExpireTimestamp, atomicSwap.GetSwapID())
store.Set(swapKey, atomicSwap.GetSwapID())
}
// RemoveFromByTimestamp removes an AtomicSwap from the byTimestamp index.
func (k Keeper) RemoveFromByTimestamp(ctx sdk.Context, atomicSwap types.AtomicSwap) {
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapByBlockPrefix)
store.Delete(types.GetAtomicSwapByTimestampKey(atomicSwap.ExpireTimestamp, 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 int64, 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(types.GetTimestampSortableKey(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 roughly 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.MustUnmarshalBinaryBare(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.MustMarshalBinaryBare(&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.MustUnmarshalBinaryBare(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.AssetSupplies = append(supplies.AssetSupplies, supply)
return false
})
return
}
// GetPreviousBlockTime get the block time 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)
// use the same id as null is not allowed anymore
b := store.Get(types.PreviousBlockTimeKey)
if b == nil {
return time.Time{}, false
}
var prevBlockTime types.PrevBlockTime
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &prevBlockTime)
return prevBlockTime.Val, 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)
prevBlockTime := &types.PrevBlockTime{Val: blockTime}
// use the same id as null is not allowed anymore
store.Set(types.PreviousBlockTimeKey, k.cdc.MustMarshalBinaryLengthPrefixed(prevBlockTime))
}