Skip to content

Commit

Permalink
allow fee denom countract not found (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored Jul 16, 2024
1 parent 219db7d commit bcab82b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
24 changes: 14 additions & 10 deletions x/evm/keeper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions x/evm/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"errors"
"math/big"
"strings"

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
11 changes: 2 additions & 9 deletions x/evm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
3 changes: 0 additions & 3 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bcab82b

Please sign in to comment.