-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeper.go
109 lines (88 loc) · 2.41 KB
/
keeper.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/decentrandom/decentrandom/x/rand/internal/types"
)
// Keeper -
type Keeper struct {
CoinKeeper bank.Keeper
storeKey sdk.StoreKey // Unexposed key to access store from sdk.Context
cdc *codec.Codec // The wire codec for binary encoding/decoding.
}
// NewKeeper -
func NewKeeper(coinKeeper bank.Keeper, storeKey sdk.StoreKey, cdc *codec.Codec) Keeper {
return Keeper{
CoinKeeper: coinKeeper,
storeKey: storeKey,
cdc: cdc,
}
}
/*
// GetSeed -
func (k Keeper) GetSeed(ctx sdk.Context, height string) Seed {
store := ctx.KVStore(k.storeKey)
var seed Seed
if !store.Has([]byte(height)) {
return seed
}
bz := store.Get([]byte(height))
k.cdc.MustUnmarshalBinaryBare(bz, &seed)
return seed
}
// SetSeed -
func (k Keeper) SetSeed(ctx sdk.Context, seed Seed) {
store := ctx.KVStore(k.storeKey)
store.Set([]byte(seed.Height), k.cdc.MustMarshalBinaryBare(seed))
}
*/
// SetRound -
func (k Keeper) SetRound(ctx sdk.Context, id string, round types.Round) {
if len(id) == 0 || round.Owner.Empty() {
return
}
store := ctx.KVStore(k.storeKey)
store.Set([]byte(id), k.cdc.MustMarshalBinaryBare(round))
}
// GetRound -
func (k Keeper) GetRound(ctx sdk.Context, id string) types.Round {
store := ctx.KVStore(k.storeKey)
var round types.Round
if !store.Has([]byte(id)) {
return round
}
bz := store.Get([]byte(id))
k.cdc.MustUnmarshalBinaryBare(bz, &round)
return round
}
// IsIDPresent -
func (k Keeper) IsIDPresent(ctx sdk.Context, id string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has([]byte(id))
}
// GetOwner -
func (k Keeper) GetOwner(ctx sdk.Context, id string) sdk.AccAddress {
return k.GetRound(ctx, id).Owner
}
// GetTargets -
func (k Keeper) GetTargets(ctx sdk.Context, id string) []string {
return k.GetRound(ctx, id).Targets
}
// SetTargets -
func (k Keeper) SetTargets(ctx sdk.Context, id string, targets []string) {
round := k.GetRound(ctx, id)
round.Targets = targets
k.SetRound(ctx, id, round)
}
// SetNonce -
func (k Keeper) SetNonce(ctx sdk.Context, id string, nonce string) {
round := k.GetRound(ctx, id)
round.Nonce = nonce
k.SetRound(ctx, id, round)
}
// GetIDsIterator -
func (k Keeper) GetIDsIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, []byte{})
}