Skip to content

Commit

Permalink
fix: fix some bugs about erc20 in token module (#407)
Browse files Browse the repository at this point in the history
* fix the problem that module account does not exist

* remove unused code
  • Loading branch information
dreamer-zq authored Apr 19, 2024
1 parent af0ac66 commit 94a7b83
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 27 deletions.
14 changes: 7 additions & 7 deletions modules/token/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (k Keeper) DeployERC20(
if err != nil {
return common.Address{}, errorsmod.Wrapf(types.ErrABIPack, "erc20 metadata is invalid %s: %s", name, err.Error())
}
deployer := k.moduleAddress()
deployer := k.getModuleEthAddress(ctx)

data := make([]byte, len(contracts.ERC20TokenContract.Bin)+len(contractArgs))
copy(data[:len(contracts.ERC20TokenContract.Bin)], contracts.ERC20TokenContract.Bin)
Expand Down Expand Up @@ -205,7 +205,7 @@ func (k Keeper) MintERC20(
}

abi := contracts.ERC20TokenContract.ABI
res, err := k.CallEVM(ctx, abi, k.moduleAddress(), contract, true, contracts.MethodMint, to, amount)
res, err := k.CallEVM(ctx, abi, k.getModuleEthAddress(ctx), contract, true, contracts.MethodMint, to, amount)
if err != nil {
return err
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func (k Keeper) BurnERC20(
}

abi := contracts.ERC20TokenContract.ABI
res, err := k.CallEVM(ctx, abi, k.moduleAddress(), contract, true, contracts.MethodBurn, from, amount)
res, err := k.CallEVM(ctx, abi, k.getModuleEthAddress(ctx), contract, true, contracts.MethodBurn, from, amount)
if err != nil {
return err
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func (k Keeper) BalanceOf(
contract, account common.Address,
) (*big.Int, error) {
abi := contracts.ERC20TokenContract.ABI
res, err := k.CallEVM(ctx, abi, k.moduleAddress(), contract, false, contracts.MethodBalanceOf, account)
res, err := k.CallEVM(ctx, abi, k.getModuleEthAddress(ctx), contract, false, contracts.MethodBalanceOf, account)
if err != nil {
return nil, err
}
Expand All @@ -320,9 +320,9 @@ func (k Keeper) BalanceOf(
return balance, nil
}

func (k Keeper) moduleAddress() common.Address {
moduleAddr := k.accountKeeper.GetModuleAddress(types.ModuleName)
return common.BytesToAddress(moduleAddr.Bytes())
func (k Keeper) getModuleEthAddress(ctx sdk.Context) common.Address {
moduleAccount := k.accountKeeper.GetModuleAccount(ctx,types.ModuleName)
return common.BytesToAddress(moduleAccount.GetAddress().Bytes())
}

func (k Keeper) buildERC20Token(
Expand Down
2 changes: 1 addition & 1 deletion modules/token/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (k Keeper) Balances(c context.Context, req *v1.QueryBalancesRequest) (*v1.Q
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
balances = balances.Add(sdk.NewCoin(token.GetContract(), sdkmath.NewIntFromBigInt(erc20Balance)))
balances = balances.Add(sdk.NewCoin("erc20/"+token.GetContract(), sdkmath.NewIntFromBigInt(erc20Balance)))
}
return &v1.QueryBalancesResponse{Balances: balances}, nil
}
8 changes: 1 addition & 7 deletions modules/token/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
"encoding/hex"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -318,12 +317,7 @@ func (m msgServer) SwapToERC20(goCtx context.Context, msg *v1.MsgSwapToERC20) (*
return nil, err
}

bz, err := hex.DecodeString(msg.Receiver)
if err != nil {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "expecting a hex address of 0x, got %s", msg.Receiver)
}
receiver := common.BytesToAddress(bz)

receiver := common.HexToAddress(msg.Receiver)
if err := m.k.SwapToERC20(ctx, sender, receiver, msg.Amount); err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions modules/token/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type AccountKeeper interface {
GetModuleAddress(moduleName string) sdk.AccAddress
GetSequence(sdk.Context, sdk.AccAddress) (uint64, error)
GetAccount(sdk.Context, sdk.AccAddress) authtypes.AccountI
GetModuleAccount(ctx sdk.Context, moduleName string) authtypes.ModuleAccountI
}

// EVMKeeper defines the expected keeper of the evm module
Expand Down
5 changes: 3 additions & 2 deletions modules/token/types/v1/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"

tokentypes "github.com/irisnet/irismod/modules/token/types"
)
Expand Down Expand Up @@ -420,8 +421,8 @@ func (m *MsgSwapToERC20) ValidateBasic() error {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}

if tokentypes.IsValidEthAddress(m.Receiver) {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "expecting a hex address of 0x, got %s", m.Receiver)
if !common.IsHexAddress(m.Receiver) {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "expecting a hex address, got %s", m.Receiver)
}

if !m.Amount.IsValid() {
Expand Down
12 changes: 2 additions & 10 deletions modules/token/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ var (
regexpMinUintFmt = fmt.Sprintf("^[a-z][a-z0-9]{%d,%d}$", MinimumMinUnitLen-1, MaximumMinUnitLen-1)
regexpMinUint = regexp.MustCompile(regexpMinUintFmt).MatchString

regexpEthAddressLowerStr = "^0x[0-9a-f]{40}$"
regexpEthAddressUpperStr = "^0x[0-9A-F]{40}$"
regexpEthAddressLower = regexp.MustCompile(regexpEthAddressLowerStr).MatchString
regexpEthAddressUpper = regexp.MustCompile(regexpEthAddressUpperStr).MatchString
regexpEthAddressStr = "^0x[0-9a-f]{40}$"
regexpEthAddress = regexp.MustCompile(regexpEthAddressStr).MatchString
)

// ValidateInitialSupply verifies whether the initial supply is legal
Expand Down Expand Up @@ -120,9 +118,3 @@ func ValidateCoin(coin sdk.Coin) error {
}
return ValidateMinUnit(coin.Denom)
}

// IsValidEthAddress checks if the given address is valid ethereum address
func IsValidEthAddress(address string) bool {
address = strings.ToLower(address)
return regexpEthAddressLower(address) || regexpEthAddressUpper(address)
}

0 comments on commit 94a7b83

Please sign in to comment.