forked from neutron-org/neutron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inactive_limit_order_tranche.go
69 lines (56 loc) · 2.23 KB
/
inactive_limit_order_tranche.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/MonikaCat/neutron/v2/x/dex/types"
)
// SetInactiveLimitOrderTranche set a specific inactiveLimitOrderTranche in the store from its index
func (k Keeper) SetInactiveLimitOrderTranche(ctx sdk.Context, limitOrderTranche *types.LimitOrderTranche) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InactiveLimitOrderTrancheKeyPrefix))
b := k.cdc.MustMarshal(limitOrderTranche)
store.Set(limitOrderTranche.Key.KeyMarshal(), b)
}
// GetInactiveLimitOrderTranche returns a inactiveLimitOrderTranche from its index
func (k Keeper) GetInactiveLimitOrderTranche(
ctx sdk.Context,
limitOrderTrancheKey *types.LimitOrderTrancheKey,
) (val *types.LimitOrderTranche, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InactiveLimitOrderTrancheKeyPrefix))
b := store.Get(limitOrderTrancheKey.KeyMarshal())
if b == nil {
return val, false
}
val = &types.LimitOrderTranche{}
k.cdc.MustUnmarshal(b, val)
return val, true
}
// RemoveInactiveLimitOrderTranche removes a inactiveLimitOrderTranche from the store
func (k Keeper) RemoveInactiveLimitOrderTranche(
ctx sdk.Context,
limitOrderTrancheKey *types.LimitOrderTrancheKey,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InactiveLimitOrderTrancheKeyPrefix))
store.Delete(limitOrderTrancheKey.KeyMarshal())
}
// GetAllInactiveLimitOrderTranche returns all inactiveLimitOrderTranche
func (k Keeper) GetAllInactiveLimitOrderTranche(ctx sdk.Context) (list []*types.LimitOrderTranche) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.InactiveLimitOrderTrancheKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
val := &types.LimitOrderTranche{}
k.cdc.MustUnmarshal(iterator.Value(), val)
list = append(list, val)
}
return
}
func (k Keeper) SaveInactiveTranche(sdkCtx sdk.Context, tranche *types.LimitOrderTranche) {
if tranche.HasTokenIn() || tranche.HasTokenOut() {
k.SetInactiveLimitOrderTranche(sdkCtx, tranche)
} else {
k.RemoveInactiveLimitOrderTranche(
sdkCtx,
tranche.Key,
)
}
}