-
Notifications
You must be signed in to change notification settings - Fork 1
/
market_data.go
78 lines (64 loc) · 2.23 KB
/
market_data.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
package keeper
import (
"encoding/binary"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jim380/Re/x/fix/types"
)
// GetMarketDataCount get the total number of marketData
func (k Keeper) GetMarketDataCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.GetMarketDataCountKey()
bz := store.Get(byteKey)
// Count doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// SetMarketDataCount set the total number of marketData
func (k Keeper) SetMarketDataCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.GetMarketDataCountKey()
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
// SetMarketData set a specific marketData in the store
func (k Keeper) SetMarketData(ctx sdk.Context, mdReqID string, marketData types.MarketData) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetMarketDataKey())
key := []byte(mdReqID)
b := k.cdc.MustMarshal(&marketData)
store.Set(key, b)
}
// GetMarketData returns a marketData from its mdReqID
func (k Keeper) GetMarketData(ctx sdk.Context, mdReqID string) (val types.MarketData, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetMarketDataKey())
key := []byte(mdReqID)
b := store.Get(key)
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveMarketData removes a marketData from the store
func (k Keeper) RemoveMarketData(ctx sdk.Context, mdReqID string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetMarketDataKey())
key := []byte(mdReqID)
store.Delete(key)
}
// GetAllMarketData returns all marketData
func (k Keeper) GetAllMarketData(ctx sdk.Context) []types.MarketData {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetMarketDataKey())
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
list := make([]types.MarketData, 0, k.GetMarketDataCount(ctx))
for ; iterator.Valid(); iterator.Next() {
var val types.MarketData
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return list
}