Skip to content
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
14 changes: 8 additions & 6 deletions precompiles/erc20/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,24 @@ 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(
tokenPair erc20types.TokenPair,
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{},
},
Expand Down
11 changes: 4 additions & 7 deletions precompiles/werc20/werc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/precompiles/erc20/test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 14 additions & 0 deletions tests/integration/precompiles/erc20/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/precompiles/werc20/test_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/precompiles/werc20/test_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions x/erc20/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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,
Expand All @@ -52,6 +70,8 @@ func NewKeeper(
evmKeeper: evmKeeper,
stakingKeeper: sk,
transferKeeper: transferKeeper,
erc20ABI: erc20ABI,
werc20ABI: werc20ABI,
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/erc20/keeper/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading