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") diff --git a/x/fungible/keeper/foreign_coins.go b/x/fungible/keeper/foreign_coins.go index 7450b66cd8..faa96d2876 100644 --- a/x/fungible/keeper/foreign_coins.go +++ b/x/fungible/keeper/foreign_coins.go @@ -5,6 +5,7 @@ import ( "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" ) @@ -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 } } 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) + }) }