From 8be9f10f5045a4f74bb639ceb6053269314c06c5 Mon Sep 17 00:00:00 2001 From: lumtis Date: Mon, 9 Oct 2023 16:43:36 -0700 Subject: [PATCH 1/4] fix error message --- x/crosschain/types/errors.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/crosschain/types/errors.go b/x/crosschain/types/errors.go index ac6a1497f5..da0674b5c3 100644 --- a/x/crosschain/types/errors.go +++ b/x/crosschain/types/errors.go @@ -12,10 +12,10 @@ var ( ErrNotEnoughZetaBurnt = errorsmod.Register(ModuleName, 1109, "not enough zeta burnt") ErrCannotFindReceiverNonce = errorsmod.Register(ModuleName, 1110, "cannot find receiver chain nonce") - ErrGasCoinNotFound = errorsmod.Register(ModuleName, 1113, "gas coin not found for SenderChain") + ErrGasCoinNotFound = errorsmod.Register(ModuleName, 1113, "gas coin not found for sender chain") ErrUnableToParseAddress = errorsmod.Register(ModuleName, 1115, "cannot parse address and data") ErrCannotProcessWithdrawal = errorsmod.Register(ModuleName, 1116, "cannot process withdrawal event") - ErrForeignCoinNotFound = errorsmod.Register(ModuleName, 1118, "gas coin not found for SenderChain") + ErrForeignCoinNotFound = errorsmod.Register(ModuleName, 1118, "foreign coin not found for sender chain") ErrNotEnoughPermissions = errorsmod.Register(ModuleName, 1119, "not enough permissions for current actions") ErrCannotFindPendingNonces = errorsmod.Register(ModuleName, 1121, "cannot find pending nonces") From 7d0f46a8a8bd147b9044ecff27854cdeec371d7f Mon Sep 17 00:00:00 2001 From: lumtis Date: Mon, 9 Oct 2023 17:03:34 -0700 Subject: [PATCH 2/4] compare with ETH address type --- x/fungible/keeper/foreign_coins.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/x/fungible/keeper/foreign_coins.go b/x/fungible/keeper/foreign_coins.go index 7450b66cd8..bb8ad2abc7 100644 --- a/x/fungible/keeper/foreign_coins.go +++ b/x/fungible/keeper/foreign_coins.go @@ -2,6 +2,7 @@ package keeper import ( "fmt" + ethcommon "github.com/ethereum/go-ethereum/common" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" @@ -93,9 +94,15 @@ func (k Keeper) GetGasCoinForForeignCoin(ctx sdk.Context, chainID int64) (types. // GetForeignCoinFromAsset returns the foreign coin for a given asset for a given chain func (k Keeper) GetForeignCoinFromAsset(ctx sdk.Context, asset string, chainID int64) (types.ForeignCoins, bool) { + if !ethcommon.IsHexAddress(asset) { + return types.ForeignCoins{}, false + } + assetAddr := ethcommon.HexToAddress(asset) + foreignCoinList := k.GetAllForeignCoinsForChain(ctx, chainID) for _, coin := range foreignCoinList { - if coin.Asset == asset && coin.ForeignChainId == chainID { + coinAssetAddr := ethcommon.HexToAddress(coin.Asset) + if coinAssetAddr == assetAddr && coin.ForeignChainId == chainID { return coin, true } } From 4a8651eb067c2791bf33058f03338b289e8529c5 Mon Sep 17 00:00:00 2001 From: lumtis Date: Mon, 9 Oct 2023 17:03:44 -0700 Subject: [PATCH 3/4] tests --- x/fungible/keeper/foreign_coins_test.go | 120 ++++++++++++++---------- 1 file changed, 71 insertions(+), 49 deletions(-) diff --git a/x/fungible/keeper/foreign_coins_test.go b/x/fungible/keeper/foreign_coins_test.go index 347630002d..0a5a0f37f0 100644 --- a/x/fungible/keeper/foreign_coins_test.go +++ b/x/fungible/keeper/foreign_coins_test.go @@ -77,56 +77,78 @@ func TestKeeper_GetGasCoinForForeignCoin(t *testing.T) { } func TestKeeperGetForeignCoinFromAsset(t *testing.T) { - k, ctx, _, _ := keepertest.FungibleKeeper(t) + t.Run("can get foreign coin from asset", func(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeper(t) - gasAsset := sample.EthAddress().String() + gasAsset := sample.EthAddress().String() - // populate - setForeignCoins(ctx, k, - types.ForeignCoins{ - Zrc20ContractAddress: sample.EthAddress().String(), - Asset: sample.EthAddress().String(), - ForeignChainId: 1, - CoinType: common.CoinType_ERC20, - Name: "foo", - }, - types.ForeignCoins{ - Zrc20ContractAddress: sample.EthAddress().String(), - Asset: gasAsset, - ForeignChainId: 1, - CoinType: common.CoinType_ERC20, - Name: "bar", - }, - types.ForeignCoins{ - Zrc20ContractAddress: sample.EthAddress().String(), - Asset: sample.EthAddress().String(), - ForeignChainId: 1, - CoinType: common.CoinType_Gas, - Name: "foo", - }, - types.ForeignCoins{ - Zrc20ContractAddress: sample.EthAddress().String(), - Asset: sample.EthAddress().String(), - ForeignChainId: 2, - CoinType: common.CoinType_ERC20, - Name: "foo", - }, - types.ForeignCoins{ - Zrc20ContractAddress: sample.EthAddress().String(), - Asset: sample.EthAddress().String(), - ForeignChainId: 2, - CoinType: common.CoinType_ERC20, - Name: "foo", - }, - ) + // populate + setForeignCoins(ctx, k, + types.ForeignCoins{ + Zrc20ContractAddress: sample.EthAddress().String(), + Asset: sample.EthAddress().String(), + ForeignChainId: 1, + CoinType: common.CoinType_ERC20, + Name: "foo", + }, + types.ForeignCoins{ + Zrc20ContractAddress: sample.EthAddress().String(), + Asset: gasAsset, + ForeignChainId: 1, + CoinType: common.CoinType_ERC20, + Name: "bar", + }, + types.ForeignCoins{ + Zrc20ContractAddress: sample.EthAddress().String(), + Asset: sample.EthAddress().String(), + ForeignChainId: 1, + CoinType: common.CoinType_Gas, + Name: "foo", + }, + types.ForeignCoins{ + Zrc20ContractAddress: sample.EthAddress().String(), + Asset: sample.EthAddress().String(), + ForeignChainId: 2, + CoinType: common.CoinType_ERC20, + Name: "foo", + }, + types.ForeignCoins{ + Zrc20ContractAddress: sample.EthAddress().String(), + Asset: sample.EthAddress().String(), + ForeignChainId: 2, + CoinType: common.CoinType_ERC20, + Name: "foo", + }, + ) - fc, found := k.GetForeignCoinFromAsset(ctx, gasAsset, 1) - require.True(t, found) - require.Equal(t, "bar", fc.Name) - fc, found = k.GetForeignCoinFromAsset(ctx, sample.EthAddress().String(), 1) - require.False(t, found) - fc, found = k.GetForeignCoinFromAsset(ctx, gasAsset, 2) - require.False(t, found) - fc, found = k.GetForeignCoinFromAsset(ctx, gasAsset, 3) - require.False(t, found) + fc, found := k.GetForeignCoinFromAsset(ctx, gasAsset, 1) + require.True(t, found) + require.Equal(t, "bar", fc.Name) + fc, found = k.GetForeignCoinFromAsset(ctx, sample.EthAddress().String(), 1) + require.False(t, found) + fc, found = k.GetForeignCoinFromAsset(ctx, "invalid_address", 1) + require.False(t, found) + fc, found = k.GetForeignCoinFromAsset(ctx, gasAsset, 2) + require.False(t, found) + fc, found = k.GetForeignCoinFromAsset(ctx, gasAsset, 3) + require.False(t, found) + }) + + t.Run("can get foreign coin with non-checksum address", func(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeper(t) + + setForeignCoins(ctx, k, + types.ForeignCoins{ + Zrc20ContractAddress: sample.EthAddress().String(), + Asset: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", + ForeignChainId: 1, + CoinType: common.CoinType_ERC20, + Name: "foo", + }, + ) + + fc, found := k.GetForeignCoinFromAsset(ctx, "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", 1) + require.True(t, found) + require.Equal(t, "foo", fc.Name) + }) } From 1099923092efb75638609395c57a382402764f27 Mon Sep 17 00:00:00 2001 From: lumtis Date: Mon, 9 Oct 2023 17:04:59 -0700 Subject: [PATCH 4/4] goimport --- x/fungible/keeper/foreign_coins.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/fungible/keeper/foreign_coins.go b/x/fungible/keeper/foreign_coins.go index bb8ad2abc7..faa96d2876 100644 --- a/x/fungible/keeper/foreign_coins.go +++ b/x/fungible/keeper/foreign_coins.go @@ -2,10 +2,10 @@ package keeper import ( "fmt" - ethcommon "github.com/ethereum/go-ethereum/common" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" + ethcommon "github.com/ethereum/go-ethereum/common" "github.com/zeta-chain/zetacore/common" "github.com/zeta-chain/zetacore/x/fungible/types" )