diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index 676d80814..760a85a84 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -64,7 +64,6 @@ import ( "github.com/functionx/fx-core/v6/x/erc20" erc20keeper "github.com/functionx/fx-core/v6/x/erc20/keeper" erc20types "github.com/functionx/fx-core/v6/x/erc20/types" - ethkeeper "github.com/functionx/fx-core/v6/x/eth/keeper" ethtypes "github.com/functionx/fx-core/v6/x/eth/types" fxevmkeeper "github.com/functionx/fx-core/v6/x/evm/keeper" fxgovkeeper "github.com/functionx/fx-core/v6/x/gov/keeper" @@ -447,7 +446,7 @@ func NewAppKeeper( AddRoute(bsctypes.ModuleName, crosschainkeeper.NewModuleHandler(appKeepers.BscKeeper)). AddRoute(polygontypes.ModuleName, crosschainkeeper.NewModuleHandler(appKeepers.PolygonKeeper)). AddRoute(avalanchetypes.ModuleName, crosschainkeeper.NewModuleHandler(appKeepers.AvalancheKeeper)). - AddRoute(ethtypes.ModuleName, ethkeeper.NewModuleHandler(appKeepers.EthKeeper)). + AddRoute(ethtypes.ModuleName, crosschainkeeper.NewModuleHandler(appKeepers.EthKeeper)). AddRoute(arbitrumtypes.ModuleName, crosschainkeeper.NewModuleHandler(appKeepers.ArbitrumKeeper)). AddRoute(optimismtypes.ModuleName, crosschainkeeper.NewModuleHandler(appKeepers.OptimismKeeper)). AddRoute(layer2types.ModuleName, crosschainkeeper.NewModuleHandler(appKeepers.Layer2Keeper)). @@ -494,8 +493,8 @@ func NewAppKeeper( ) ibcTransferRouter := fxtypes.NewRouter(). - AddRoute(gravitytypes.ModuleName, appKeepers.Layer2Keeper). - AddRoute(ethtypes.ModuleName, appKeepers.Layer2Keeper). + AddRoute(gravitytypes.ModuleName, appKeepers.EthKeeper). + AddRoute(ethtypes.ModuleName, appKeepers.EthKeeper). AddRoute(bsctypes.ModuleName, appKeepers.BscKeeper). AddRoute(polygontypes.ModuleName, appKeepers.PolygonKeeper). AddRoute(trontypes.ModuleName, appKeepers.TronKeeper). diff --git a/app/keepers/keys.go b/app/keepers/keys.go index 146b3be81..9cd3422ae 100644 --- a/app/keepers/keys.go +++ b/app/keepers/keys.go @@ -89,7 +89,7 @@ func (appKeepers *AppKeepers) EvmPrecompiled() { // cross chain precompile transferRouter := fxtypes.NewRouter(). - // AddRoute(ethtypes.ModuleName, appKeepers.EthKeeper). + AddRoute(ethtypes.ModuleName, appKeepers.EthKeeper). AddRoute(bsctypes.ModuleName, appKeepers.BscKeeper). AddRoute(polygontypes.ModuleName, appKeepers.PolygonKeeper). AddRoute(trontypes.ModuleName, appKeepers.TronKeeper). diff --git a/x/eth/keeper/keeper_router.go b/x/eth/keeper/keeper_router.go deleted file mode 100644 index 25ebf60dc..000000000 --- a/x/eth/keeper/keeper_router.go +++ /dev/null @@ -1,11 +0,0 @@ -package keeper - -import crosschainkeeper "github.com/functionx/fx-core/v6/x/crosschain/keeper" - -func NewModuleHandler(keeper crosschainkeeper.Keeper) *crosschainkeeper.ModuleHandler { - return &crosschainkeeper.ModuleHandler{ - QueryServer: keeper, - MsgServer: NewMsgServerImpl(keeper), - ProposalServer: keeper, - } -} diff --git a/x/eth/keeper/msg_server.go b/x/eth/keeper/msg_server.go deleted file mode 100644 index d9e7fa21e..000000000 --- a/x/eth/keeper/msg_server.go +++ /dev/null @@ -1,27 +0,0 @@ -package keeper - -import ( - "context" - - "cosmossdk.io/errors" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - - crosschainkeeper "github.com/functionx/fx-core/v6/x/crosschain/keeper" - crosschaintypes "github.com/functionx/fx-core/v6/x/crosschain/types" -) - -var _ crosschaintypes.MsgServer = MsgServer{} - -type MsgServer struct { - crosschainkeeper.MsgServer -} - -func NewMsgServerImpl(keeper crosschainkeeper.Keeper) crosschaintypes.MsgServer { - return MsgServer{ - MsgServer: crosschainkeeper.MsgServer{Keeper: keeper}, - } -} - -func (s MsgServer) SendToExternal(_ context.Context, _ *crosschaintypes.MsgSendToExternal) (*crosschaintypes.MsgSendToExternalResponse, error) { - return nil, errors.Wrap(sdkerrors.ErrInvalidRequest, "not supported") -} diff --git a/x/evm/precompiles/crosschain/cancel_sendtoexternal_test.go b/x/evm/precompiles/crosschain/cancel_sendtoexternal_test.go index c76a0b451..e63af3e30 100644 --- a/x/evm/precompiles/crosschain/cancel_sendtoexternal_test.go +++ b/x/evm/precompiles/crosschain/cancel_sendtoexternal_test.go @@ -20,6 +20,7 @@ import ( bsctypes "github.com/functionx/fx-core/v6/x/bsc/types" crosschaintypes "github.com/functionx/fx-core/v6/x/crosschain/types" "github.com/functionx/fx-core/v6/x/erc20/types" + ethtypes "github.com/functionx/fx-core/v6/x/eth/types" "github.com/functionx/fx-core/v6/x/evm/precompiles/crosschain" ) @@ -139,7 +140,7 @@ func (suite *PrecompileTestSuite) TestCancelSendToExternal() { { name: "ok - fip20 contract + evm token", prepare: func(_ *types.TokenPair, _ string, signer *helpers.Signer, randMint *big.Int) (*types.TokenPair, string, string) { - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName pair, found := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, fxtypes.DefaultDenom) suite.Require().True(found) suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), pair.GetDenom()) @@ -159,7 +160,7 @@ func (suite *PrecompileTestSuite) TestCancelSendToExternal() { { name: "ok - address + evm token", prepare: func(_ *types.TokenPair, _ string, signer *helpers.Signer, randMint *big.Int) (*types.TokenPair, string, string) { - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) @@ -210,7 +211,7 @@ func (suite *PrecompileTestSuite) TestCancelSendToExternal() { { name: "ok - address + wrapper origin token", prepare: func(_ *types.TokenPair, _ string, signer *helpers.Signer, randMint *big.Int) (*types.TokenPair, string, string) { - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName pair, found := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, fxtypes.DefaultDenom) suite.Require().True(found) diff --git a/x/evm/precompiles/crosschain/contract_test.go b/x/evm/precompiles/crosschain/contract_test.go index 3b87c1573..349fd4d4b 100644 --- a/x/evm/precompiles/crosschain/contract_test.go +++ b/x/evm/precompiles/crosschain/contract_test.go @@ -181,7 +181,7 @@ func (suite *PrecompileTestSuite) CrossChainKeepers() map[string]CrossChainKeepe cck := value.Field(i).Interface().(CrossChainKeeper) if chainName == "bridge-eth" { // keepers["gravity"] = cck - // keepers["eth"] = cck + keepers["eth"] = cck } else { keepers[chainName] = cck } diff --git a/x/evm/precompiles/crosschain/crosschain_test.go b/x/evm/precompiles/crosschain/crosschain_test.go index 630e6c4fa..a4070d061 100644 --- a/x/evm/precompiles/crosschain/crosschain_test.go +++ b/x/evm/precompiles/crosschain/crosschain_test.go @@ -114,6 +114,111 @@ func (suite *PrecompileTestSuite) TestCrossChain() { }, result: true, }, + { + name: "ok - address - origin token", + malleate: func(_ *types.TokenPair, _ Metadata, signer *helpers.Signer, randMint *big.Int) ([]byte, *types.TokenPair, *big.Int, string, []string) { + helpers.AddTestAddr(suite.app, suite.ctx, signer.AccAddress(), + sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(randMint)))) + + balance := suite.app.BankKeeper.GetBalance(suite.ctx, signer.AccAddress(), fxtypes.DefaultDenom) + suite.Require().Equal(randMint.String(), balance.Amount.BigInt().String()) + moduleName := ethtypes.ModuleName + + suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) + + data, err := crosschain.GetABI().Pack( + "crossChain", + common.HexToAddress(fxtypes.EmptyEvmAddress), + helpers.GenerateAddressByModule(moduleName), + randMint, + big.NewInt(0), + fxtypes.MustStrToByte32(moduleName), + "", + ) + suite.Require().NoError(err) + + pair, found := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, fxtypes.DefaultDenom) + suite.Require().True(found) + + return data, &pair, randMint, moduleName, nil + }, + result: true, + }, + { + name: "ok - address - origin erc20 token", + malleate: func(_ *types.TokenPair, _ Metadata, signer *helpers.Signer, randMint *big.Int) ([]byte, *types.TokenPair, *big.Int, string, []string) { + moduleName := ethtypes.ModuleName + denomAddr := helpers.GenerateAddress().String() + alias := fmt.Sprintf("%s%s", moduleName, denomAddr) + + suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, denomAddr, alias) + + token, err := suite.DeployContract(signer.Address()) + suite.Require().NoError(err) + + suite.MintERC20Token(signer, token, signer.Address(), randMint) + balOf := suite.BalanceOf(token, signer.Address()) + suite.Require().Equal(randMint.String(), balOf.String()) + + pair, err := suite.app.Erc20Keeper.RegisterNativeERC20(suite.ctx, token, alias) + suite.Require().NoError(err) + + suite.ERC20Approve(signer, token, crosschain.GetAddress(), randMint) + + data, err := crosschain.GetABI().Pack( + "crossChain", + pair.GetERC20Contract(), + helpers.GenerateAddressByModule(moduleName), + randMint, + big.NewInt(0), + fxtypes.MustStrToByte32(moduleName), + "", + ) + suite.Require().NoError(err) + + return data, pair, big.NewInt(0), moduleName, nil + }, + result: true, + }, + { + name: "ok - address - wrapper origin token", + malleate: func(_ *types.TokenPair, _ Metadata, signer *helpers.Signer, randMint *big.Int) ([]byte, *types.TokenPair, *big.Int, string, []string) { + pair, found := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, fxtypes.DefaultDenom) + suite.Require().True(found) + + moduleName := ethtypes.ModuleName + suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) + + coin := sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(randMint)) + helpers.AddTestAddr(suite.app, suite.ctx, signer.AccAddress(), sdk.NewCoins(coin)) + + balance := suite.app.BankKeeper.GetBalance(suite.ctx, signer.AccAddress(), fxtypes.DefaultDenom) + suite.Require().Equal(randMint.String(), balance.Amount.BigInt().String()) + + _, err := suite.app.Erc20Keeper.ConvertCoin(sdk.WrapSDKContext(suite.ctx), &types.MsgConvertCoin{ + Coin: coin, + Receiver: signer.Address().Hex(), + Sender: signer.AccAddress().String(), + }) + suite.Require().NoError(err) + + suite.ERC20Approve(signer, pair.GetERC20Contract(), crosschain.GetAddress(), randMint) + + data, err := crosschain.GetABI().Pack( + "crossChain", + pair.GetERC20Contract(), + helpers.GenerateAddressByModule(moduleName), + randMint, + big.NewInt(0), + fxtypes.MustStrToByte32(moduleName), + "", + ) + suite.Require().NoError(err) + + return data, &pair, big.NewInt(0), moduleName, nil + }, + result: true, + }, { name: "ok - ibc token", malleate: func(_ *types.TokenPair, _ Metadata, signer *helpers.Signer, randMint *big.Int) ([]byte, *types.TokenPair, *big.Int, string, []string) { @@ -174,7 +279,7 @@ func (suite *PrecompileTestSuite) TestCrossChain() { helpers.AddTestAddr(suite.app, suite.ctx, signer.AccAddress(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(randMint)))) - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) data, err := crosschain.GetABI().Pack( "crossChain", @@ -326,6 +431,72 @@ func (suite *PrecompileTestSuite) TestCrossChain() { }, result: true, }, + { + name: "contract - ok - origin token", + malleate: func(_ *types.TokenPair, _ Metadata, signer *helpers.Signer, randMint *big.Int) ([]byte, *types.TokenPair, *big.Int, string, []string) { + helpers.AddTestAddr(suite.app, suite.ctx, signer.AccAddress(), + sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(randMint)))) + + moduleName := ethtypes.ModuleName + suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) + + data, err := fxtypes.MustABIJson(testcontract.CrossChainTestMetaData.ABI).Pack( + "crossChain", + common.HexToAddress(fxtypes.EmptyEvmAddress), + helpers.GenerateAddressByModule(moduleName), + randMint, + big.NewInt(0), + fxtypes.MustStrToByte32(moduleName), + "", + ) + suite.Require().NoError(err) + + pair, found := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, fxtypes.DefaultDenom) + suite.Require().True(found) + + return data, &pair, randMint, moduleName, nil + }, + result: true, + }, + { + name: "contract - ok - address - wrapper origin token", + malleate: func(_ *types.TokenPair, _ Metadata, signer *helpers.Signer, randMint *big.Int) ([]byte, *types.TokenPair, *big.Int, string, []string) { + pair, found := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, fxtypes.DefaultDenom) + suite.Require().True(found) + + moduleName := ethtypes.ModuleName + suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) + + coin := sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(randMint)) + helpers.AddTestAddr(suite.app, suite.ctx, signer.AccAddress(), sdk.NewCoins(coin)) + + balance := suite.app.BankKeeper.GetBalance(suite.ctx, signer.AccAddress(), fxtypes.DefaultDenom) + suite.Require().Equal(randMint.String(), balance.Amount.BigInt().String()) + + _, err := suite.app.Erc20Keeper.ConvertCoin(sdk.WrapSDKContext(suite.ctx), &types.MsgConvertCoin{ + Coin: coin, + Receiver: signer.Address().Hex(), + Sender: signer.AccAddress().String(), + }) + suite.Require().NoError(err) + + suite.ERC20Approve(signer, pair.GetERC20Contract(), suite.crosschain, randMint) + + data, err := fxtypes.MustABIJson(testcontract.CrossChainTestMetaData.ABI).Pack( + "crossChain", + pair.GetERC20Contract(), + helpers.GenerateAddressByModule(moduleName), + randMint, + big.NewInt(0), + fxtypes.MustStrToByte32(moduleName), + "", + ) + suite.Require().NoError(err) + + return data, &pair, big.NewInt(0), moduleName, nil + }, + result: true, + }, { name: "contract - ok - ibc token", malleate: func(_ *types.TokenPair, _ Metadata, signer *helpers.Signer, randMint *big.Int) ([]byte, *types.TokenPair, *big.Int, string, []string) { @@ -386,7 +557,7 @@ func (suite *PrecompileTestSuite) TestCrossChain() { helpers.AddTestAddr(suite.app, suite.ctx, signer.AccAddress(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(randMint)))) - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) data, err := fxtypes.MustABIJson(testcontract.CrossChainTestMetaData.ABI).Pack( "crossChain", diff --git a/x/evm/precompiles/crosschain/fip20crosschain_test.go b/x/evm/precompiles/crosschain/fip20crosschain_test.go index 31f44a230..11d7dbed3 100644 --- a/x/evm/precompiles/crosschain/fip20crosschain_test.go +++ b/x/evm/precompiles/crosschain/fip20crosschain_test.go @@ -22,6 +22,7 @@ import ( bsctypes "github.com/functionx/fx-core/v6/x/bsc/types" crosschaintypes "github.com/functionx/fx-core/v6/x/crosschain/types" "github.com/functionx/fx-core/v6/x/erc20/types" + ethtypes "github.com/functionx/fx-core/v6/x/eth/types" "github.com/functionx/fx-core/v6/x/evm/precompiles/crosschain" ) @@ -78,7 +79,7 @@ func (suite *PrecompileTestSuite) TestFIP20CrossChain() { coin := sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(randMint)) helpers.AddTestAddr(suite.app, suite.ctx, signer.AccAddress().Bytes(), sdk.NewCoins(coin)) - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) fee := big.NewInt(1) @@ -710,7 +711,7 @@ func (suite *PrecompileTestSuite) TestFIP20CrossChainIBC() { coin := sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(randMint)) helpers.AddTestAddr(suite.app, suite.ctx, signer.AccAddress().Bytes(), sdk.NewCoins(coin)) - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) fee := big.NewInt(1) diff --git a/x/evm/precompiles/crosschain/increase_bridgefee_test.go b/x/evm/precompiles/crosschain/increase_bridgefee_test.go index f219a78b5..c951c8af1 100644 --- a/x/evm/precompiles/crosschain/increase_bridgefee_test.go +++ b/x/evm/precompiles/crosschain/increase_bridgefee_test.go @@ -19,6 +19,7 @@ import ( bsctypes "github.com/functionx/fx-core/v6/x/bsc/types" crosschaintypes "github.com/functionx/fx-core/v6/x/crosschain/types" "github.com/functionx/fx-core/v6/x/erc20/types" + ethtypes "github.com/functionx/fx-core/v6/x/eth/types" "github.com/functionx/fx-core/v6/x/evm/precompiles/crosschain" ) @@ -154,7 +155,7 @@ func (suite *PrecompileTestSuite) TestIncreaseBridgeFee() { { name: "ok - fip20 contract + evm token", prepare: func(_ *types.TokenPair, _ string, signer *helpers.Signer, randMint *big.Int) (*types.TokenPair, string, string) { - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName pair, found := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, fxtypes.DefaultDenom) suite.Require().True(found) suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), pair.GetDenom()) @@ -174,7 +175,7 @@ func (suite *PrecompileTestSuite) TestIncreaseBridgeFee() { { name: "ok - address + evm token", prepare: func(_ *types.TokenPair, _ string, signer *helpers.Signer, randMint *big.Int) (*types.TokenPair, string, string) { - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName suite.CrossChainKeepers()[moduleName].AddBridgeToken(suite.ctx, helpers.GenerateAddress().String(), fxtypes.DefaultDenom) @@ -225,7 +226,7 @@ func (suite *PrecompileTestSuite) TestIncreaseBridgeFee() { { name: "ok - address + wrapper origin token", prepare: func(_ *types.TokenPair, _ string, signer *helpers.Signer, randMint *big.Int) (*types.TokenPair, string, string) { - moduleName := bsctypes.ModuleName + moduleName := ethtypes.ModuleName pair, found := suite.app.Erc20Keeper.GetTokenPair(suite.ctx, fxtypes.DefaultDenom) suite.Require().True(found) diff --git a/x/gravity/keeper/msg_server.go b/x/gravity/keeper/msg_server.go index a7efe6643..e398c2978 100644 --- a/x/gravity/keeper/msg_server.go +++ b/x/gravity/keeper/msg_server.go @@ -11,7 +11,7 @@ import ( ) type msgServer struct { - crosschaintypes.MsgServer + crosschainkeeper.MsgServer } func NewMsgServerImpl(keeper crosschainkeeper.Keeper) types.MsgServer {