-
Notifications
You must be signed in to change notification settings - Fork 51
/
pool.go
96 lines (78 loc) · 2.3 KB
/
pool.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/leveragelp/types"
)
// RemovePool removes a pool from the store
func (k Keeper) RemovePool(ctx sdk.Context, index uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoolKeyPrefix))
store.Delete(types.PoolKey(index))
}
// GetAllPool returns all pool
func (k Keeper) GetAllPools(ctx sdk.Context) (list []types.Pool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoolKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Pool
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// SetPool set a specific pool in the store from its index
func (k Keeper) SetPool(ctx sdk.Context, pool types.Pool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoolKeyPrefix))
b := k.cdc.MustMarshal(&pool)
store.Set(types.PoolKey(pool.AmmPoolId), b)
}
// GetPool returns a pool from its index
func (k Keeper) GetPool(ctx sdk.Context, poolId uint64) (val types.Pool, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoolKeyPrefix))
b := store.Get(types.PoolKey(
poolId,
))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
func (k Keeper) GetEnabledPools(ctx sdk.Context) []uint64 {
poolIds := make([]uint64, 0)
pools := k.GetAllPools(ctx)
for _, p := range pools {
if p.Enabled {
poolIds = append(poolIds, p.AmmPoolId)
}
}
return poolIds
}
func (k Keeper) SetEnabledPools(ctx sdk.Context, pools []uint64) {
for _, poolId := range pools {
pool, found := k.GetPool(ctx, poolId)
if !found {
pool = types.NewPool(poolId)
k.SetPool(ctx, pool)
}
pool.Enabled = true
k.SetPool(ctx, pool)
}
}
func (k Keeper) IsPoolEnabled(ctx sdk.Context, poolId uint64) bool {
pool, found := k.GetPool(ctx, poolId)
if !found {
pool = types.NewPool(poolId)
k.SetPool(ctx, pool)
}
return pool.Enabled
}
func (k Keeper) IsPoolClosed(ctx sdk.Context, poolId uint64) bool {
pool, found := k.GetPool(ctx, poolId)
if !found {
pool = types.NewPool(poolId)
k.SetPool(ctx, pool)
}
return pool.Closed
}