/
keeper.go
92 lines (79 loc) · 2.68 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
package keeper
import (
"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/provenance-io/provenance/x/ibcratelimit"
)
// Keeper for the ibcratelimit module
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
PermissionedKeeper ibcratelimit.PermissionedKeeper
authority string
}
// NewKeeper Creates a new Keeper for the module.
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
permissionedKeeper ibcratelimit.PermissionedKeeper,
) Keeper {
return Keeper{
storeKey: key,
cdc: cdc,
PermissionedKeeper: permissionedKeeper,
authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
}
}
// Logger Creates a new logger for the module.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+ibcratelimit.ModuleName)
}
// GetParams Gets the params for the module.
func (k Keeper) GetParams(ctx sdk.Context) (params ibcratelimit.Params, err error) {
store := ctx.KVStore(k.storeKey)
key := ibcratelimit.ParamsKey
bz := store.Get(key)
if len(bz) == 0 {
return ibcratelimit.Params{}, nil
}
err = k.cdc.Unmarshal(bz, ¶ms)
return params, err
}
// SetParams Sets the params for the module.
func (k Keeper) SetParams(ctx sdk.Context, params ibcratelimit.Params) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(¶ms)
store.Set(ibcratelimit.ParamsKey, bz)
}
// GetContractAddress Gets the current value of the module's contract address.
func (k Keeper) GetContractAddress(ctx sdk.Context) (contract string) {
params, _ := k.GetParams(ctx)
return params.ContractAddress
}
// IsContractConfigured Checks if the contract has been configured for the module.
func (k Keeper) IsContractConfigured(ctx sdk.Context) bool {
params, err := k.GetParams(ctx)
if err != nil {
return false
}
return params.ContractAddress != ""
}
// ValidateAuthority returns an error if the provided address is not the authority.
func (k Keeper) ValidateAuthority(addr string) error {
if k.authority != addr {
return govtypes.ErrInvalidSigner.Wrapf("expected %q got %q", k.authority, addr)
}
return nil
}
// emitEvent emits the provided event and writes any error to the error log.
func (k Keeper) emitEvent(ctx sdk.Context, event proto.Message) {
err := ctx.EventManager().EmitTypedEvent(event)
if err != nil {
k.Logger(ctx).Error("error emitting event %#v: %v", event, err)
}
}