Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GetForeignCoinFromAsset): Ethereum comparaison checksum/non-checksum format #1261

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x/crosschain/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 8 additions & 1 deletion x/fungible/keeper/foreign_coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
}
Expand Down
120 changes: 71 additions & 49 deletions x/fungible/keeper/foreign_coins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}