-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
58 lines (50 loc) · 1.68 KB
/
stats.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/furynet/xfury/utils"
"github.com/furynet/xfury/x/market/types"
)
// SetMarketStats sets market statistics in the store
func (k Keeper) SetMarketStats(ctx sdk.Context, stats types.MarketStats) {
store := k.getMarketStatsStore(ctx)
b := k.cdc.MustMarshal(&stats)
store.Set(utils.StrBytes("0"), b)
}
// GetMarketStats returns market stats
func (k Keeper) GetMarketStats(ctx sdk.Context) (val types.MarketStats) {
store := k.getMarketStatsStore(ctx)
b := store.Get(utils.StrBytes("0"))
if b == nil {
return val
}
k.cdc.MustUnmarshal(b, &val)
return val
}
// appendUnsettledResolvedMarket appends market to the unsettled slice
func (k Keeper) appendUnsettledResolvedMarket(ctx sdk.Context, storedMarketUID string) {
stats := k.GetMarketStats(ctx)
stats.ResolvedUnsettled = append(stats.ResolvedUnsettled, storedMarketUID)
k.SetMarketStats(ctx, stats)
}
// GetFirstUnsettledResolvedMarket returns first element of resolved
// markets that have active bets
func (k Keeper) GetFirstUnsettledResolvedMarket(ctx sdk.Context) (string, bool) {
stats := k.GetMarketStats(ctx)
if len(stats.ResolvedUnsettled) > 0 {
return stats.ResolvedUnsettled[0], true
}
return "", false
}
// RemoveUnsettledResolvedMarket removes resolved market
// from the statistics
func (k Keeper) RemoveUnsettledResolvedMarket(ctx sdk.Context, marketUID string) {
stats := k.GetMarketStats(ctx)
if len(stats.ResolvedUnsettled) > 0 {
for i, e := range stats.ResolvedUnsettled {
if e == marketUID {
stats.ResolvedUnsettled = append(stats.ResolvedUnsettled[:i], stats.ResolvedUnsettled[i+1:]...)
}
}
}
k.SetMarketStats(ctx, stats)
}