-
Notifications
You must be signed in to change notification settings - Fork 200
/
store.go
162 lines (137 loc) · 4.35 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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/pricefeed/types"
)
// OraclesStore maintains the "oracles" KVStore: maps pair name → oracles.
func (k Keeper) OraclesStore() OraclesState {
return (OraclesState)(k)
}
// ActivePairsStore maintains the "active pairs" KVStore: maps pair name → isActive.
// If a pair doesn't have a key in the store, the pair is inactive.
func (k Keeper) ActivePairsStore() ActivePairsState {
return (ActivePairsState)(k)
}
//-----------------------------------------------------------------------------
// OraclesState implements methods for updating the "oracles" sdk.KVStore
type OraclesState Keeper
var oraclesNamespace = []byte("oracles")
func (state OraclesState) getKV(ctx sdk.Context) sdk.KVStore {
return prefix.NewStore(ctx.KVStore(state.storeKey), oraclesNamespace)
}
func (state OraclesState) Get(
ctx sdk.Context, pair common.AssetPair,
) (oracles []sdk.AccAddress) {
kvStore := state.getKV(ctx)
key := []byte(pair.String())
valueBytes := kvStore.Get(key)
if valueBytes == nil {
return []sdk.AccAddress{}
}
oraclesMarshaler := &types.OraclesMarshaler{}
state.cdc.MustUnmarshal(
/*bytes=*/ valueBytes,
/*codec.ProtoMarshaler=*/ oraclesMarshaler)
return oraclesMarshaler.Oracles
}
func (state OraclesState) Set(
ctx sdk.Context, pair common.AssetPair, oracles []sdk.AccAddress,
) {
key := []byte(pair.String())
kvStore := state.getKV(ctx)
oraclesMarshaler := &types.OraclesMarshaler{Oracles: oracles}
kvStore.Set(key, state.cdc.MustMarshal(oraclesMarshaler))
}
func (state OraclesState) AddOracles(
ctx sdk.Context, pair common.AssetPair, oracles []sdk.AccAddress,
) {
startOracles := state.Get(ctx, pair)
endOracles := append(startOracles, oracles...)
state.Set(ctx, pair, endOracles)
}
func (state OraclesState) Iterate(
ctx sdk.Context,
do func(*types.OraclesMarshaler) (stop bool),
) {
kvStore := state.getKV(ctx)
iter := kvStore.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
oraclesMarshaler := &types.OraclesMarshaler{}
state.cdc.MustUnmarshal(iter.Value(), oraclesMarshaler)
if !do(oraclesMarshaler) {
break
}
}
}
//-----------------------------------------------------------------------------
// ActivePairsState implements methods for updating the "active pairs" sdk.KVStore
type ActivePairsState Keeper
var activePairsNamespace = []byte("active pairs")
func (state ActivePairsState) getKV(ctx sdk.Context) sdk.KVStore {
return prefix.NewStore(ctx.KVStore(state.storeKey), activePairsNamespace)
}
func (state ActivePairsState) Get(
ctx sdk.Context, pair common.AssetPair,
) (active bool) {
kvStore := state.getKV(ctx)
key := []byte(pair.String())
valueBytes := kvStore.Get(key)
if valueBytes == nil {
return false
}
activePairsMarshaler := &types.ActivePairMarshaler{}
state.cdc.MustUnmarshal(
/*bytes=*/ valueBytes,
/*codec.ProtoMarshaler=*/ activePairsMarshaler)
isActive := activePairsMarshaler.IsActive
if (valueBytes != nil) && !isActive {
kvStore.Delete(key)
}
return isActive
}
/* ActivePairsState.Set either sets a pair to active or deletes it from the
key-value store (i.e., pairs default to inactive if they don't exist). */
func (state ActivePairsState) Set(
ctx sdk.Context, pair common.AssetPair, active bool,
) {
key := []byte(pair.String())
kvStore := state.getKV(ctx)
if active {
activePairsMarshaler := &types.ActivePairMarshaler{IsActive: active}
kvStore.Set(key, state.cdc.MustMarshal(activePairsMarshaler))
} else if !active && kvStore.Has(key) {
kvStore.Delete(key)
} // else {do nothing}
}
func (state ActivePairsState) SetMany(
ctx sdk.Context, pairs common.AssetPairs, active bool,
) {
for _, pair := range pairs {
state.Set(ctx, pair, active)
}
}
func (state ActivePairsState) AddActivePairs(
ctx sdk.Context, pairs []common.AssetPair,
) {
for _, pair := range pairs {
state.Set(ctx, pair, true)
}
}
func (state ActivePairsState) Iterate(
ctx sdk.Context,
do func(*types.ActivePairMarshaler) (stop bool),
) {
kvStore := state.getKV(ctx)
iter := kvStore.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
activePairsMarshaler := &types.ActivePairMarshaler{}
state.cdc.MustUnmarshal(iter.Value(), activePairsMarshaler)
if !do(activePairsMarshaler) {
break
}
}
}