forked from neutron-org/neutron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
106 lines (90 loc) · 3.21 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
package keeper
import (
"fmt"
"cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"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"
"github.com/MonikaCat/neutron/v2/x/interchaintxs/types"
)
const (
LabelSubmitTx = "submit_tx"
LabelHandleAcknowledgment = "handle_ack"
LabelLabelHandleChanOpenAck = "handle_chan_open_ack"
LabelRegisterInterchainAccount = "register_interchain_account"
LabelHandleTimeout = "handle_timeout"
)
type (
Keeper struct {
Codec codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
channelKeeper types.ChannelKeeper
feeKeeper types.FeeRefunderKeeper
icaControllerKeeper types.ICAControllerKeeper
sudoKeeper types.WasmKeeper
bankKeeper types.BankKeeper
getFeeCollectorAddr types.GetFeeCollectorAddr
authority string
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
channelKeeper types.ChannelKeeper,
icaControllerKeeper types.ICAControllerKeeper,
sudoKeeper types.WasmKeeper,
feeKeeper types.FeeRefunderKeeper,
bankKeeper types.BankKeeper,
getFeeCollectorAddr types.GetFeeCollectorAddr,
authority string,
) *Keeper {
return &Keeper{
Codec: cdc,
storeKey: storeKey,
memKey: memKey,
channelKeeper: channelKeeper,
icaControllerKeeper: icaControllerKeeper,
sudoKeeper: sudoKeeper,
feeKeeper: feeKeeper,
bankKeeper: bankKeeper,
getFeeCollectorAddr: getFeeCollectorAddr,
authority: authority,
}
}
func (k *Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k Keeper) ChargeFee(ctx sdk.Context, payer sdk.AccAddress, fee sdk.Coins) error {
k.Logger(ctx).Debug("Trying to change fees", "payer", payer, "fee", fee)
params := k.GetParams(ctx)
if !fee.IsAnyGTE(params.RegisterFee) {
return errors.Wrapf(sdkerrors.ErrInsufficientFee, "provided fee is less than min governance set ack fee: %s < %s", fee, params.RegisterFee)
}
feeCollector := k.getFeeCollectorAddr(ctx)
feeCollectorAddress, err := sdk.AccAddressFromBech32(feeCollector)
if err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "failed to convert fee collector, bech32 to AccAddress: %s: %s", feeCollector, err.Error())
}
err = k.bankKeeper.SendCoins(ctx, payer, feeCollectorAddress, fee)
if err != nil {
return errors.Wrapf(err, "failed send fee(%s) from %s to %s", fee, payer, feeCollectorAddress)
}
return nil
}
func (k Keeper) GetAuthority() string {
return k.authority
}
// GetICARegistrationFeeFirstCodeID returns code id, starting from which we charge fee for ICA registration
func (k Keeper) GetICARegistrationFeeFirstCodeID(ctx sdk.Context) (codeID uint64) {
store := ctx.KVStore(k.storeKey)
bytes := store.Get(types.ICARegistrationFeeFirstCodeID)
if bytes == nil {
k.Logger(ctx).Debug("Fee register ICA code id key don't exists, GetLastCodeID returns 0")
return 0
}
return sdk.BigEndianToUint64(bytes)
}