-
Notifications
You must be signed in to change notification settings - Fork 11
/
keeper.go
184 lines (158 loc) · 6.71 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
"github.com/tendermint/tendermint/libs/log"
"github.com/persistenceOne/pstake-native/x/lscosmos/types"
)
// Keeper of this module maintains the state of whole module
type Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
memKey sdk.StoreKey
paramstore paramtypes.Subspace
bankKeeper types.BankKeeper
accountKeeper types.AccountKeeper
epochKeeper types.EpochKeeper
ics4WrapperKeeper types.ICS4WrapperKeeper
channelKeeper types.ChannelKeeper
portKeeper types.PortKeeper
ibcTransferKeeper types.IBCTransferKeeper
icaControllerKeeper types.ICAControllerKeeper
icqKeeper types.ICQKeeper
lscosmosScopedKeeper types.ScopedKeeper
msgRouter *baseapp.MsgServiceRouter
}
// NewKeeper returns a new instance of ls cosmos module keeper
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey sdk.StoreKey,
ps paramtypes.Subspace,
bankKeeper types.BankKeeper,
accKeeper types.AccountKeeper,
epochKeeper types.EpochKeeper,
ics4WrapperKeeper types.ICS4WrapperKeeper,
channelKeeper types.ChannelKeeper,
portKeeper types.PortKeeper,
ibcTransferKeeper types.IBCTransferKeeper,
icaControllerKeeper types.ICAControllerKeeper,
icqKeeper types.ICQKeeper,
lscosmosScopedKeeper types.ScopedKeeper,
msgRouter *baseapp.MsgServiceRouter,
) Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
bankKeeper: bankKeeper,
accountKeeper: accKeeper,
epochKeeper: epochKeeper,
ics4WrapperKeeper: ics4WrapperKeeper,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
ibcTransferKeeper: ibcTransferKeeper,
icaControllerKeeper: icaControllerKeeper,
icqKeeper: icqKeeper,
lscosmosScopedKeeper: lscosmosScopedKeeper,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
msgRouter: msgRouter,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// ChanCloseInit defines a wrapper function for the channel Keeper's function
func (k Keeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error {
capName := host.ChannelCapabilityPath(portID, channelID)
chanCap, ok := k.lscosmosScopedKeeper.GetCapability(ctx, capName)
if !ok {
return sdkerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
}
return k.channelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap)
}
// IsBound checks if the module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.lscosmosScopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
}
// BindPort defines a wrapper function for the ort Keeper's function in
// order to expose it to module's InitGenesis function
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
capability := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, capability, host.PortPath(portID))
}
// AuthenticateCapability wraps the lscosmosScopedKeeper's AuthenticateCapability function
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.lscosmosScopedKeeper.AuthenticateCapability(ctx, cap, name)
}
// ClaimCapability allows the module that can claim a capability that IBC module passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.lscosmosScopedKeeper.ClaimCapability(ctx, cap, name)
}
// NewCapability allows the module that can initiate and claim a capability that IBC module passes to it
func (k Keeper) NewCapability(ctx sdk.Context, name string) error {
_, err := k.lscosmosScopedKeeper.NewCapability(ctx, name)
return err
}
// GetDepositModuleAccount returns deposit module account interface
func (k Keeper) GetDepositModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.DepositModuleAccount)
}
// GetDelegationModuleAccount returns the delegation module account interface
func (k Keeper) GetDelegationModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.DelegationModuleAccount)
}
// GetRewardModuleAccount returns the reward module account interface
func (k Keeper) GetRewardModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.RewardModuleAccount)
}
// GetUndelegationModuleAccount returns the undelegation module account interface
func (k Keeper) GetUndelegationModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.UndelegationModuleAccount)
}
// GetRewardBoosterModuleAccount returns the rewards booster module account interface
func (k Keeper) GetRewardBoosterModuleAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.accountKeeper.GetModuleAccount(ctx, types.RewardBoosterModuleAccount)
}
// MintTokens in the given account
func (k Keeper) MintTokens(ctx sdk.Context, mintCoin sdk.Coin, delegatorAddress sdk.AccAddress) error {
err := k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(mintCoin))
if err != nil {
return err
}
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, delegatorAddress, sdk.NewCoins(mintCoin))
if err != nil {
return err
}
return nil
}
// SendTokensToDepositModule sends the tokens to DepositModuleAccount
func (k Keeper) SendTokensToDepositModule(ctx sdk.Context, depositCoin sdk.Coins, senderAddress sdk.AccAddress) error {
return k.bankKeeper.SendCoinsFromAccountToModule(ctx, senderAddress, types.DepositModuleAccount, depositCoin)
}
// SendProtocolFee to the community pool
func (k Keeper) SendProtocolFee(ctx sdk.Context, protocolFee sdk.Coins, moduleAccount, pstakeFeeAddressString string) error {
addr, err := sdk.AccAddressFromBech32(pstakeFeeAddressString)
if err != nil {
return err
}
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, moduleAccount, addr, protocolFee)
if err != nil {
return err
}
return nil
}