diff --git a/x/evm/keeper/context.go b/x/evm/keeper/context.go index 0045f95..fbbb945 100644 --- a/x/evm/keeper/context.go +++ b/x/evm/keeper/context.go @@ -3,9 +3,11 @@ package keeper import ( "context" "encoding/json" + "errors" "math" "math/big" + "cosmossdk.io/collections" "github.com/holiman/uint256" "github.com/ethereum/go-ethereum/common" @@ -45,17 +47,14 @@ func (k Keeper) buildBlockContext(ctx context.Context, evm callableEVM) (vm.Bloc headerHash = make([]byte, 32) } - var contractAddr common.Address - if !k.initializing { - params, err := k.Params.Get(ctx) - if err != nil { - return vm.BlockContext{}, err - } + params, err := k.Params.Get(ctx) + if err != nil { + return vm.BlockContext{}, err + } - contractAddr, err = types.DenomToContractAddr(ctx, k, params.FeeDenom) - if err != nil { - return vm.BlockContext{}, err - } + contractAddr, err := types.DenomToContractAddr(ctx, k, params.FeeDenom) + if err != nil && !errors.Is(err, collections.ErrNotFound) { + return vm.BlockContext{}, err } // TODO: should we charge gas for CanTransfer and Transfer? @@ -72,6 +71,11 @@ func (k Keeper) buildBlockContext(ctx context.Context, evm callableEVM) (vm.Bloc return true } + // if the contract is not found, return false + if (contractAddr == common.Address{}) { + return false + } + inputBz, err := k.erc20Keeper.GetERC20ABI().Pack("balanceOf", a) if err != nil { return false diff --git a/x/evm/keeper/erc20.go b/x/evm/keeper/erc20.go index 0a90937..bd8a270 100644 --- a/x/evm/keeper/erc20.go +++ b/x/evm/keeper/erc20.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "errors" "math/big" "strings" @@ -99,7 +100,9 @@ func (k ERC20Keeper) GetBalance(ctx context.Context, addr sdk.AccAddress, denom } contractAddr, err := types.DenomToContractAddr(ctx, k, denom) - if err != nil { + if err != nil && errors.Is(err, collections.ErrNotFound) { + return math.ZeroInt(), nil + } else if err != nil { return math.ZeroInt(), err } @@ -225,7 +228,9 @@ func (k ERC20Keeper) GetSupply(ctx context.Context, denom string) (math.Int, err // HasSupply implements IERC20Keeper. func (k ERC20Keeper) HasSupply(ctx context.Context, denom string) (bool, error) { contractAddr, err := types.DenomToContractAddr(ctx, k, denom) - if err != nil { + if err != nil && errors.Is(err, collections.ErrNotFound) { + return false, nil + } else if err != nil { return false, err } diff --git a/x/evm/keeper/genesis.go b/x/evm/keeper/genesis.go index aaf3f51..6b2e093 100644 --- a/x/evm/keeper/genesis.go +++ b/x/evm/keeper/genesis.go @@ -15,18 +15,12 @@ import ( ) func (k Keeper) Initialize(ctx context.Context) error { + // 1. deploy erc20 factory contract code, err := hexutil.Decode(erc20_factory.Erc20FactoryBin) if err != nil { return err } - // This is temporal alive flag to prevent checking fee denom erc20 coin existence - // during this function execution. It will be reverted to false after the erc20 - // fee denom contract is deployed. - // - // - // This function is not pointer receiver, so it will not affect the original value. - k.initializing = true _, _, _, err = k.EVMCreate2(ctx, types.StdAddress, code, nil, types.ERC20FactorySalt) if err != nil { return err @@ -37,13 +31,12 @@ func (k Keeper) Initialize(ctx context.Context) error { return err } - // deploy fee denom erc20 coins at genesis bootstrapping + // 2. deploy fee denom erc20 coins at genesis bootstrapping err = k.erc20Keeper.CreateERC20(ctx, params.FeeDenom) if err != nil { return err } - // k.initialize will be reverted to false. return nil } diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 3fe275b..83cdf3b 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -56,9 +56,6 @@ type Keeper struct { precompiles precompiles queryCosmosWhitelist types.QueryCosmosWhitelist - - // flag to check if the keeper is in the process of initialization - initializing bool } func NewKeeper(