diff --git a/precompiles/erc20/erc20.go b/precompiles/erc20/erc20.go index 99c77fc26..7a1575f5f 100644 --- a/precompiles/erc20/erc20.go +++ b/precompiles/erc20/erc20.go @@ -56,6 +56,12 @@ type Precompile struct { BankKeeper cmn.BankKeeper } +// LoadABI loads the IERC20Metadata ABI from the embedded abi.json file +// for the erc20 precompile. +func LoadABI() (abi.ABI, error) { + return cmn.LoadABI(f, abiPath) +} + // NewPrecompile creates a new ERC-20 Precompile instance as a // PrecompiledContract interface. func NewPrecompile( @@ -63,15 +69,11 @@ func NewPrecompile( bankKeeper cmn.BankKeeper, erc20Keeper Erc20Keeper, transferKeeper ibcutils.TransferKeeper, + erc20ABI abi.ABI, ) (*Precompile, error) { - newABI, err := cmn.LoadABI(f, abiPath) - if err != nil { - return nil, err - } - p := &Precompile{ Precompile: cmn.Precompile{ - ABI: newABI, + ABI: erc20ABI, KvGasConfig: storetypes.GasConfig{}, TransientKVGasConfig: storetypes.GasConfig{}, }, diff --git a/precompiles/werc20/werc20.go b/precompiles/werc20/werc20.go index 44bb19075..514eb717b 100644 --- a/precompiles/werc20/werc20.go +++ b/precompiles/werc20/werc20.go @@ -52,19 +52,16 @@ func NewPrecompile( bankKeeper cmn.BankKeeper, erc20Keeper Erc20Keeper, transferKeeper ibcutils.TransferKeeper, + erc20ABI abi.ABI, + werc20ABI abi.ABI, ) (*Precompile, error) { - newABI, err := LoadABI() - if err != nil { - return nil, fmt.Errorf("error loading the ABI: %w", err) - } - - erc20Precompile, err := erc20.NewPrecompile(tokenPair, bankKeeper, erc20Keeper, transferKeeper) + erc20Precompile, err := erc20.NewPrecompile(tokenPair, bankKeeper, erc20Keeper, transferKeeper, erc20ABI) if err != nil { return nil, fmt.Errorf("error instantiating the ERC20 precompile: %w", err) } // use the IWERC20 ABI - erc20Precompile.ABI = newABI + erc20Precompile.ABI = werc20ABI return &Precompile{ Precompile: erc20Precompile, diff --git a/tests/integration/precompiles/erc20/test_setup.go b/tests/integration/precompiles/erc20/test_setup.go index fd07dbc49..d6d2fad43 100644 --- a/tests/integration/precompiles/erc20/test_setup.go +++ b/tests/integration/precompiles/erc20/test_setup.go @@ -68,10 +68,13 @@ func (s *PrecompileTestSuite) SetupTest() { s.precompile, err = s.setupERC20Precompile(s.tokenDenom) s.Require().NoError(err) + erc20ABI, err := erc20.LoadABI() + s.Require().NoError(err) + // Instantiate the precompile2 with the bond denom (the token pair was already set up in genesis). tokenPairID := s.network.App.GetErc20Keeper().GetDenomMap(s.network.GetContext(), bondDenom) tokenPair, found := s.network.App.GetErc20Keeper().GetTokenPair(s.network.GetContext(), tokenPairID) s.Require().True(found) - s.precompile2, err = erc20.NewPrecompile(tokenPair, s.network.App.GetBankKeeper(), s.network.App.GetErc20Keeper(), s.network.App.GetTransferKeeper()) + s.precompile2, err = erc20.NewPrecompile(tokenPair, s.network.App.GetBankKeeper(), s.network.App.GetErc20Keeper(), s.network.App.GetTransferKeeper(), erc20ABI) s.Require().NoError(err) } diff --git a/tests/integration/precompiles/erc20/test_utils.go b/tests/integration/precompiles/erc20/test_utils.go index 06d6b3884..31e75c6b3 100644 --- a/tests/integration/precompiles/erc20/test_utils.go +++ b/tests/integration/precompiles/erc20/test_utils.go @@ -171,11 +171,15 @@ func (is *IntegrationTestSuite) setupERC20Precompile(denom string, tokenPairs [] tokenPair = tp } + erc20ABI, err := erc20.LoadABI() + Expect(err).To(BeNil()) + precompile, err := erc20.NewPrecompile( tokenPair, is.network.App.GetBankKeeper(), is.network.App.GetErc20Keeper(), is.network.App.GetTransferKeeper(), + erc20ABI, ) Expect(err).ToNot(HaveOccurred(), "failed to set up %q erc20 precompile", tokenPair.Denom) @@ -188,11 +192,16 @@ func (is *IntegrationTestSuite) setupERC20Precompile(denom string, tokenPairs [] func setupERC20PrecompileForTokenPair( unitNetwork network.UnitTestNetwork, tokenPair erc20types.TokenPair, ) (*erc20.Precompile, error) { + erc20ABI, err := erc20.LoadABI() + if err != nil { + return nil, err + } precompile, err := erc20.NewPrecompile( tokenPair, unitNetwork.App.GetBankKeeper(), unitNetwork.App.GetErc20Keeper(), unitNetwork.App.GetTransferKeeper(), + erc20ABI, ) if err != nil { return nil, errorsmod.Wrapf(err, "failed to create %q erc20 precompile", tokenPair.Denom) @@ -215,11 +224,16 @@ func setupERC20PrecompileForTokenPair( func (is *IntegrationTestSuite) setupNewERC20PrecompileForTokenPair( tokenPair erc20types.TokenPair, ) (*erc20.Precompile, error) { + erc20ABI, err := erc20.LoadABI() + if err != nil { + return nil, err + } precompile, err := erc20.NewPrecompile( tokenPair, is.network.App.GetBankKeeper(), is.network.App.GetErc20Keeper(), is.network.App.GetTransferKeeper(), + erc20ABI, ) if err != nil { return nil, errorsmod.Wrapf(err, "failed to create %q erc20 precompile", tokenPair.Denom) diff --git a/tests/integration/precompiles/werc20/test_events.go b/tests/integration/precompiles/werc20/test_events.go index e4734754d..8d1a3be45 100644 --- a/tests/integration/precompiles/werc20/test_events.go +++ b/tests/integration/precompiles/werc20/test_events.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/suite" cmn "github.com/cosmos/evm/precompiles/common" + "github.com/cosmos/evm/precompiles/erc20" "github.com/cosmos/evm/precompiles/werc20" testconstants "github.com/cosmos/evm/testutil/constants" "github.com/cosmos/evm/testutil/integration/evm/factory" @@ -72,11 +73,18 @@ func (s *PrecompileUnitTestSuite) SetupTest(chainID testconstants.ChainID) { s.Require().True(found, "expected wevmos precompile to be registered in the tokens map") s.Require().Equal(s.precompileAddrHex, tokenPair.Erc20Address, "expected a different address of the contract") + erc20ABI, err := erc20.LoadABI() + s.Require().NoError(err) + werc20ABI, err := werc20.LoadABI() + s.Require().NoError(err) + precompile, err := werc20.NewPrecompile( tokenPair, s.network.App.GetBankKeeper(), s.network.App.GetErc20Keeper(), s.network.App.GetTransferKeeper(), + erc20ABI, + werc20ABI, ) s.Require().NoError(err, "failed to instantiate the werc20 precompile") s.Require().NotNil(precompile) diff --git a/tests/integration/precompiles/werc20/test_integration.go b/tests/integration/precompiles/werc20/test_integration.go index 686bdb65f..3185954d7 100644 --- a/tests/integration/precompiles/werc20/test_integration.go +++ b/tests/integration/precompiles/werc20/test_integration.go @@ -160,11 +160,19 @@ func TestPrecompileIntegrationTestSuite(t *testing.T, create network.CreateEvmAp evmtypes.GetEVMCoinDenom(), erc20types.OWNER_MODULE, ) + + erc20ABI, err := erc20.LoadABI() + Expect(err).To(BeNil()) + werc20ABI, err := werc20.LoadABI() + Expect(err).To(BeNil()) + precompile, err := werc20.NewPrecompile( tokenPair, is.network.App.GetBankKeeper(), is.network.App.GetErc20Keeper(), is.network.App.GetTransferKeeper(), + erc20ABI, + werc20ABI, ) Expect(err).ToNot(HaveOccurred(), "failed to instantiate the werc20 precompile") is.precompile = precompile diff --git a/x/erc20/keeper/keeper.go b/x/erc20/keeper/keeper.go index 1123b25d4..ce800a645 100644 --- a/x/erc20/keeper/keeper.go +++ b/x/erc20/keeper/keeper.go @@ -3,6 +3,10 @@ package keeper import ( "fmt" + "github.com/ethereum/go-ethereum/accounts/abi" + + "github.com/cosmos/evm/precompiles/erc20" + "github.com/cosmos/evm/precompiles/werc20" "github.com/cosmos/evm/x/erc20/types" transferkeeper "github.com/cosmos/evm/x/ibc/transfer/keeper" @@ -25,6 +29,10 @@ type Keeper struct { evmKeeper types.EVMKeeper stakingKeeper types.StakingKeeper transferKeeper *transferkeeper.Keeper + + // cached abis + erc20ABI abi.ABI + werc20ABI abi.ABI } // NewKeeper creates new instances of the erc20 Keeper @@ -43,6 +51,16 @@ func NewKeeper( panic(err) } + erc20ABI, err := erc20.LoadABI() + if err != nil { + panic(err) + } + + werc20ABI, err := werc20.LoadABI() + if err != nil { + panic(err) + } + return Keeper{ authority: authority, storeKey: storeKey, @@ -52,6 +70,8 @@ func NewKeeper( evmKeeper: evmKeeper, stakingKeeper: sk, transferKeeper: transferKeeper, + erc20ABI: erc20ABI, + werc20ABI: werc20ABI, } } diff --git a/x/erc20/keeper/precompiles.go b/x/erc20/keeper/precompiles.go index 6c0312813..9daa52414 100644 --- a/x/erc20/keeper/precompiles.go +++ b/x/erc20/keeper/precompiles.go @@ -62,10 +62,10 @@ func (k Keeper) InstantiateERC20Precompile(ctx sdk.Context, contractAddr common. } if hasWrappedMethods { - return werc20.NewPrecompile(pair, k.bankKeeper, k, *k.transferKeeper) + return werc20.NewPrecompile(pair, k.bankKeeper, k, *k.transferKeeper, k.erc20ABI, k.werc20ABI) } - return erc20.NewPrecompile(pair, k.bankKeeper, k, *k.transferKeeper) + return erc20.NewPrecompile(pair, k.bankKeeper, k, *k.transferKeeper, k.erc20ABI) } // RegisterCodeHash checks if a new precompile already exists and registers the code hash it is not