package keeper
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
bankexported "github.com/cosmos/cosmos-sdk/x/bank/exported"
"github.com/cosmos/cosmos-sdk/x/capability"
channel "github.com/cosmos/cosmos-sdk/x/ibc/04-channel"
channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
porttypes "github.com/cosmos/cosmos-sdk/x/ibc/05-port/types"
"github.com/cosmos/cosmos-sdk/x/ibc/20-transfer/types"
ibctypes "github.com/cosmos/cosmos-sdk/x/ibc/types"
)
// DefaultPacketTimeout is the default packet timeout relative to the current block height
const (
DefaultPacketTimeout = 1000 // NOTE: in blocks
// Keeper defines the IBC transfer keeper
type Keeper struct {
storeKey sdk.StoreKey
cdc *codec.Codec
channelKeeper types.ChannelKeeper
portKeeper types.PortKeeper
bankKeeper types.BankKeeper
scopedKeeper capability.ScopedKeeper
}
// NewKeeper creates a new IBC transfer Keeper instance
func NewKeeper(
cdc *codec.Codec, key sdk.StoreKey,
channelKeeper types.ChannelKeeper, portKeeper types.PortKeeper,
bankKeeper types.BankKeeper, scopedKeeper capability.ScopedKeeper,
) Keeper {
// ensure ibc transfer module account is set
if addr := bankKeeper.GetModuleAddress(types.GetModuleAccountName()); addr == nil {
panic("the IBC transfer module account has not been set")
return Keeper{
storeKey: key,
cdc: cdc,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
bankKeeper: bankKeeper,
scopedKeeper: scopedKeeper,
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s/%s", ibctypes.ModuleName, types.ModuleName))
// GetTransferAccount returns the ICS20 - transfers ModuleAccount
func (k Keeper) GetTransferAccount(ctx sdk.Context) bankexported.ModuleAccountI {
return k.bankKeeper.GetModuleAccount(ctx, types.GetModuleAccountName())
// PacketExecuted defines a wrapper function for the channel Keeper's function
// in order to expose it to the ICS20 transfer handler.
// Keeper retreives channel capability and passes it into channel keeper for authentication
func (k Keeper) PacketExecuted(ctx sdk.Context, packet channelexported.PacketI, acknowledgement []byte) error {
chanCap, ok := k.scopedKeeper.GetCapability(ctx, ibctypes.ChannelCapabilityPath(packet.GetDestPort(), packet.GetDestChannel()))
if !ok {
return sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "channel capability could not be retrieved for packet")
return k.channelKeeper.PacketExecuted(ctx, chanCap, packet, acknowledgement)
// ChanCloseInit defines a wrapper function for the channel Keeper's function
// in order to expose it to the ICS20 trasfer handler.
func (k Keeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error {
capName := ibctypes.ChannelCapabilityPath(portID, channelID)
chanCap, ok := k.scopedKeeper.GetCapability(ctx, capName)
return sdkerrors.Wrapf(channel.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
return k.channelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap)
// 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 {
// Set the portID into our store so we can retrieve it later
store := ctx.KVStore(k.storeKey)
store.Set([]byte(types.PortKey), []byte(portID))
cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, porttypes.PortPath(portID))
// GetPort returns the portID for the transfer module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
return string(store.Get([]byte(types.PortKey)))
// ClaimCapability allows the transfer module that can claim a capability that IBC module
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capability.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)