-
Notifications
You must be signed in to change notification settings - Fork 16
/
keeper.go
126 lines (94 loc) · 3.24 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package keeper
import (
log "github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/larry0x/abstract-account/x/abstractaccount/types"
)
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
ak authkeeper.AccountKeeperI
ck wasmtypes.ContractOpsKeeper
authority string
}
func NewKeeper(
cdc codec.BinaryCodec, storeKey storetypes.StoreKey,
ak authkeeper.AccountKeeperI, ck wasmtypes.ContractOpsKeeper,
authority string,
) Keeper {
if ak == nil {
panic("AccountKeeperI cannot be nil")
}
if ck == nil {
panic("ContractOpsKeeper cannot be nil")
}
return Keeper{cdc, storeKey, ak, ck, authority}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
func (k Keeper) ContractKeeper() wasmtypes.ContractOpsKeeper {
return k.ck
}
// ---------------------------------- Params -----------------------------------
func (k Keeper) GetParams(ctx sdk.Context) (*types.Params, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyParams)
if bz == nil {
return nil, sdkerrors.ErrNotFound.Wrap("x/abstractaccount module params")
}
var params types.Params
if err := k.cdc.Unmarshal(bz, ¶ms); err != nil {
return nil, types.ErrParsingParams.Wrap(err.Error())
}
return ¶ms, nil
}
func (k Keeper) SetParams(ctx sdk.Context, params *types.Params) error {
store := ctx.KVStore(k.storeKey)
// params must be valid before we save it
// there are two instances where SetParams is called - in Keeper.InitGenesis,
// and in msgServer.UpdateParams
// we can either perform the validation in those two functions, or do it
// together here. doing it here seems cleaner.
if err := params.Validate(); err != nil {
return err
}
bz, err := k.cdc.Marshal(params)
if err != nil {
return types.ErrParsingParams.Wrap(err.Error())
}
store.Set(types.KeyParams, bz)
return nil
}
// ------------------------------- NextAccountId -------------------------------
func (k Keeper) GetAndIncrementNextAccountID(ctx sdk.Context) uint64 {
id := k.GetNextAccountID(ctx)
k.SetNextAccountID(ctx, id+1)
return id
}
func (k Keeper) GetNextAccountID(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
return sdk.BigEndianToUint64(store.Get(types.KeyNextAccountID))
}
func (k Keeper) SetNextAccountID(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyNextAccountID, sdk.Uint64ToBigEndian(id))
}
// ------------------------------- SignerAddress -------------------------------
func (k Keeper) GetSignerAddress(ctx sdk.Context) sdk.AccAddress {
store := ctx.KVStore(k.storeKey)
return sdk.AccAddress(store.Get(types.KeySignerAddress))
}
func (k Keeper) SetSignerAddress(ctx sdk.Context, signerAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeySignerAddress, signerAddr)
}
func (k Keeper) DeleteSignerAddress(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.KeySignerAddress)
}