Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (ibc): erc20 conversion trough transfer module #1085

Merged
merged 2 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (cmd) [\#974](https://github.com/evmos/evmos/pull/974) Add `prune` command.
- (cmd) [\#1027](https://github.com/evmos/evmos/pull/1027) Apply Google CLI Syntax for required and optional args.
- (ibc) [\#1081](https://github.com/evmos/evmos/pull/1081) Added utils functions for ibc denoms.
- (ibc/erc20) [\#1085](https://github.com/evmos/evmos/pull/1085) Added wrapper for ibc transfer to automatically convert erc20 tokens to cosmos coins.

## [v9.1.0] - 2022-10-25

Expand Down
30 changes: 20 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ import (
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibctestingtypes "github.com/cosmos/ibc-go/v5/testing/types"

"github.com/cosmos/ibc-go/v5/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v5/modules/apps/transfer/keeper"
ibctransfer "github.com/cosmos/ibc-go/v5/modules/apps/transfer"
ibctransfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v5/modules/core"
ibcclient "github.com/cosmos/ibc-go/v5/modules/core/02-client"
Expand Down Expand Up @@ -118,6 +117,7 @@ import (
v82 "github.com/evmos/evmos/v10/app/upgrades/v8_2"
v9 "github.com/evmos/evmos/v10/app/upgrades/v9"
v91 "github.com/evmos/evmos/v10/app/upgrades/v9_1"
evmostypes "github.com/evmos/evmos/v10/types"
"github.com/evmos/evmos/v10/x/claims"
claimskeeper "github.com/evmos/evmos/v10/x/claims/keeper"
claimstypes "github.com/evmos/evmos/v10/x/claims/types"
Expand All @@ -144,6 +144,10 @@ import (
"github.com/evmos/evmos/v10/x/vesting"
vestingkeeper "github.com/evmos/evmos/v10/x/vesting/keeper"
vestingtypes "github.com/evmos/evmos/v10/x/vesting/types"

// NOTE: override ICS20 keeper to support IBC transfers of ERC20 tokens
"github.com/evmos/evmos/v10/x/ibc/transfer"
transferkeeper "github.com/evmos/evmos/v10/x/ibc/transfer/keeper"
)

func init() {
Expand All @@ -161,6 +165,9 @@ func init() {
feemarkettypes.DefaultMinGasMultiplier = MainnetMinGasMultiplier
// modify default min commission to 5%
stakingtypes.DefaultMinCommissionRate = sdk.NewDecWithPrec(5, 2)

// Include the possibility to use an ERC-20 contract address as coin Denom
sdk.SetCoinDenomRegex(evmostypes.EvmosCoinDenomRegex)
}

// Name defines the application binary name
Expand Down Expand Up @@ -197,7 +204,7 @@ var (
feegrantmodule.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
transfer.AppModuleBasic{},
transfer.AppModuleBasic{AppModuleBasic: &ibctransfer.AppModuleBasic{}},
vesting.AppModuleBasic{},
evm.AppModuleBasic{},
feemarket.AppModuleBasic{},
Expand Down Expand Up @@ -271,7 +278,7 @@ type Evmos struct {
AuthzKeeper authzkeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
EvidenceKeeper evidencekeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
TransferKeeper transferkeeper.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -518,11 +525,12 @@ func NewEvmos(
// RecvPacket, message that originates from core IBC and goes down to app, the flow is the otherway
// channel.RecvPacket -> recovery.OnRecvPacket -> claim.OnRecvPacket -> transfer.OnRecvPacket

app.TransferKeeper = ibctransferkeeper.NewKeeper(
app.TransferKeeper = transferkeeper.NewKeeper(
appCodec, keys[ibctransfertypes.StoreKey], app.GetSubspace(ibctransfertypes.ModuleName),
app.ClaimsKeeper, // ICS4 Wrapper: claims IBC middleware
app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper,
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper,
app.Erc20Keeper, // Add ERC20 Keeper for ERC20 transfers
)

app.RecoveryKeeper = recoverykeeper.NewKeeper(
Expand All @@ -534,19 +542,21 @@ func NewEvmos(
app.ClaimsKeeper,
)

// Set the ICS4 wrappers for claims and recovery middlewares
// NOTE: app.Erc20Keeper is already initialized elsewhere

// Set the ICS4 wrappers for custom module middlewares
app.RecoveryKeeper.SetICS4Wrapper(app.IBCKeeper.ChannelKeeper)
app.ClaimsKeeper.SetICS4Wrapper(app.RecoveryKeeper)
// NOTE: ICS4 wrapper for Transfer Keeper already set

// Override the ICS20 app module
transferModule := transfer.NewAppModule(app.TransferKeeper)

// transfer stack contains (from top to bottom):
// transfer stack contains (from bottom to top):
// - Recovery Middleware
// - Airdrop Claims Middleware
// - Transfer
// - IBC Transfer

// create IBC module from bottom to top of stack
// create IBC module from top to bottom of stack
var transferStack porttypes.IBCModule

transferStack = transfer.NewIBCModule(app.TransferKeeper)
Expand Down
19 changes: 19 additions & 0 deletions x/ibc/transfer/ibc_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package transfer

import (
ibctransfer "github.com/cosmos/ibc-go/v5/modules/apps/transfer"
"github.com/evmos/evmos/v10/x/ibc/transfer/keeper"
)

// IBCModule implements the ICS26 interface for transfer given the transfer keeper.
type IBCModule struct {
*ibctransfer.IBCModule
}

// NewIBCModule creates a new IBCModule given the keeper
func NewIBCModule(k keeper.Keeper) IBCModule {
transferModule := ibctransfer.NewIBCModule(*k.Keeper)
return IBCModule{
IBCModule: &transferModule,
}
}
50 changes: 50 additions & 0 deletions x/ibc/transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/cosmos/ibc-go/v5/modules/apps/transfer/keeper"
transfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"

"github.com/evmos/evmos/v10/x/ibc/transfer/types"
)

// Keeper defines the modified IBC transfer keeper that embeds the original one.
// It also contains the bank keeper and the erc20 keeper to support ERC20 tokens
// to be sent via IBC.
type Keeper struct {
*keeper.Keeper
bankKeeper types.BankKeeper
erc20Keeper types.ERC20Keeper
}

// NewKeeper creates a new IBC transfer Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
paramSpace paramtypes.Subspace,

ics4Wrapper transfertypes.ICS4Wrapper,
channelKeeper transfertypes.ChannelKeeper,
portKeeper transfertypes.PortKeeper,
authKeeper transfertypes.AccountKeeper,
bankKeeper types.BankKeeper,
scopedKeeper capabilitykeeper.ScopedKeeper,
erc20Keeper types.ERC20Keeper,
) Keeper {
// create the original IBC transfer keeper for embedding
transferKeeper := keeper.NewKeeper(
cdc, storeKey, paramSpace,
ics4Wrapper, channelKeeper, portKeeper,
authKeeper, bankKeeper, scopedKeeper,
)

return Keeper{
Keeper: &transferKeeper,
bankKeeper: bankKeeper,
erc20Keeper: erc20Keeper,
}
}
Loading