/
keeper.go
83 lines (71 loc) · 2.1 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
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/eco/longy/x/longy/internal/types"
)
// Keeper maintains the link to data storage
type Keeper struct {
contextStoreKey sdk.StoreKey
accountKeeper auth.AccountKeeper
coinKeeper bank.Keeper
Cdc *codec.Codec
}
// NewKeeper is a creator for `Keeper`
//nolint:gocritic
func NewKeeper(cdc *codec.Codec, longyStoreKey sdk.StoreKey, accKeeper auth.AccountKeeper,
coinKeeper bank.Keeper) Keeper {
return Keeper{
contextStoreKey: longyStoreKey,
accountKeeper: accKeeper,
coinKeeper: coinKeeper,
Cdc: cdc,
}
}
// AccountKeeper returns the auth module's account keeper composed with this module
//nolint: gocritic
func (k Keeper) AccountKeeper() auth.AccountKeeper {
return k.accountKeeper
}
// CoinKeeper returns the module's bank keeper
//nolint: gocritic
func (k Keeper) CoinKeeper() bank.Keeper {
return k.coinKeeper
}
// Set sets the key value pair in the store
//nolint:gocritic
func (k Keeper) Set(ctx sdk.Context, key []byte, value []byte) {
store := ctx.KVStore(k.contextStoreKey)
store.Set(key, value)
}
// Get returns the value for the provided key from the store
//nolint:gocritic
func (k Keeper) Get(ctx sdk.Context, key []byte) (v []byte, err sdk.Error) {
store := ctx.KVStore(k.contextStoreKey)
//return store.Get(key)
v = store.Get(key)
if len(v) == 0 {
err = types.ErrItemNotFound("invalid key passed for item %s", key)
return
}
return
}
// Delete removes the provided key value pair from the store
//nolint:gocritic
func (k Keeper) Delete(ctx sdk.Context, key []byte) {
store := ctx.KVStore(k.contextStoreKey)
store.Delete(key)
}
// Has returns whether the key exists in the store
//nolint:gocritic
func (k Keeper) Has(ctx sdk.Context, key []byte) bool {
store := ctx.KVStore(k.contextStoreKey)
return store.Has(key)
}
// KVStore returns the key value store
//nolint:gocritic
func (k Keeper) KVStore(ctx sdk.Context) sdk.KVStore {
return ctx.KVStore(k.contextStoreKey)
}