From 734da9df7b728fbb49fef653fb03c95f9526567d Mon Sep 17 00:00:00 2001 From: dreamer Date: Fri, 19 Apr 2024 11:20:51 +0800 Subject: [PATCH] remove unused code --- modules/token/keeper/grpc_query.go | 2 +- modules/token/keeper/msg_server.go | 8 +------- modules/token/types/v1/msgs.go | 5 +++-- modules/token/types/validation.go | 12 ++---------- 4 files changed, 7 insertions(+), 20 deletions(-) diff --git a/modules/token/keeper/grpc_query.go b/modules/token/keeper/grpc_query.go index 5160bfff..b651af7a 100644 --- a/modules/token/keeper/grpc_query.go +++ b/modules/token/keeper/grpc_query.go @@ -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 } diff --git a/modules/token/keeper/msg_server.go b/modules/token/keeper/msg_server.go index 59db1ca1..f3598a56 100644 --- a/modules/token/keeper/msg_server.go +++ b/modules/token/keeper/msg_server.go @@ -2,7 +2,6 @@ package keeper import ( "context" - "encoding/hex" errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" @@ -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 } diff --git a/modules/token/types/v1/msgs.go b/modules/token/types/v1/msgs.go index 46471994..cff0b48e 100644 --- a/modules/token/types/v1/msgs.go +++ b/modules/token/types/v1/msgs.go @@ -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" ) @@ -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() { diff --git a/modules/token/types/validation.go b/modules/token/types/validation.go index 3a58eb10..24d477b9 100644 --- a/modules/token/types/validation.go +++ b/modules/token/types/validation.go @@ -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 @@ -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) -}