diff --git a/x/crosschain/keeper/utils_test.go b/x/crosschain/keeper/utils_test.go index 253e20dbb6..aed660b70b 100644 --- a/x/crosschain/keeper/utils_test.go +++ b/x/crosschain/keeper/utils_test.go @@ -175,7 +175,7 @@ func setupZRC20Pool( ) require.NoError(t, err) - // approve the router to spend the zeta + // approve the router to spend the zrc20 err = k.CallZRC20Approve( ctx, types.ModuleAddressEVM, diff --git a/x/fungible/keeper/evm_test.go b/x/fungible/keeper/evm_test.go index e698f2a01c..e1af5c041c 100644 --- a/x/fungible/keeper/evm_test.go +++ b/x/fungible/keeper/evm_test.go @@ -92,7 +92,8 @@ type SystemContractDeployConfig struct { DeployUniswapV2Router bool } -// deploySystemContracts deploys the system contracts and returns their addresses. +// deploySystemContractsConfigurable deploys the system contracts and returns their addresses +// while having a possibility to skip some deployments to test different scenarios func deploySystemContractsConfigurable( t *testing.T, ctx sdk.Context, diff --git a/x/fungible/keeper/gas_coin_and_pool_test.go b/x/fungible/keeper/gas_coin_and_pool_test.go index a422183a31..9935f11ee6 100644 --- a/x/fungible/keeper/gas_coin_and_pool_test.go +++ b/x/fungible/keeper/gas_coin_and_pool_test.go @@ -91,7 +91,7 @@ func setupZRC20Pool( ) require.NoError(t, err) - // approve the router to spend the zeta + // approve the router to spend the zrc20 err = k.CallZRC20Approve( ctx, types.ModuleAddressEVM, diff --git a/x/fungible/keeper/system_contract.go b/x/fungible/keeper/system_contract.go index f93138a973..65f4a4c169 100644 --- a/x/fungible/keeper/system_contract.go +++ b/x/fungible/keeper/system_contract.go @@ -79,7 +79,6 @@ func (k *Keeper) GetWZetaContractAddress(ctx sdk.Context) (ethcommon.Address, er ) if err != nil { return ethcommon.Address{}, cosmoserrors.Wrapf(types.ErrContractCall, "failed to call wZetaContractAddress (%s)", err.Error()) - } type AddressResponse struct { Value ethcommon.Address @@ -278,6 +277,9 @@ func (k *Keeper) QuerySystemContractGasCoinZRC20(ctx sdk.Context, chainid *big.I if err := sysABI.UnpackIntoInterface(&zrc20Res, "gasCoinZRC20ByChainId", res.Ret); err != nil { return ethcommon.Address{}, cosmoserrors.Wrapf(types.ErrABIUnpack, "failed to unpack gasCoinZRC20ByChainId: %s", err.Error()) } + if zrc20Res.Value == ethcommon.HexToAddress("0x0") { + return ethcommon.Address{}, sdkerrors.Wrapf(types.ErrContractNotFound, "gas coin contract invalid address") + } return zrc20Res.Value, nil } diff --git a/x/fungible/keeper/system_contract_test.go b/x/fungible/keeper/system_contract_test.go index 90550a2051..f3b538cb92 100644 --- a/x/fungible/keeper/system_contract_test.go +++ b/x/fungible/keeper/system_contract_test.go @@ -53,20 +53,6 @@ func TestKeeper_GetWZetaContractAddress(t *testing.T) { require.Equal(t, wzeta, found) } -func TestKeeper_GetWZetaContractAddressFails(t *testing.T) { - k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) - k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) - - _, err := k.GetWZetaContractAddress(ctx) - require.Error(t, err) - require.ErrorIs(t, err, types.ErrStateVariableNotFound) - - wzeta, _, _, _, _ := deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) - found, err := k.GetWZetaContractAddress(ctx) - require.NoError(t, err) - require.Equal(t, wzeta, found) -} - func TestKeeper_GetUniswapV2FactoryAddress(t *testing.T) { k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) @@ -146,6 +132,36 @@ func TestKeeper_GetWZetaFailsIfNotSet(t *testing.T) { require.ErrorIs(t, err, types.ErrContractNotFound) } +func TestKeeper_GetWZetaFails(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{ + UseEVMMock: true, + }) + mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContractsWithMockEvmKeeper(t, ctx, k, mockEVMKeeper) + + mockEVMKeeper.MockEVMFailCallOnce() + + _, err := k.GetWZetaContractAddress(ctx) + require.ErrorIs(t, err, types.ErrContractCall) +} + +func TestKeeper_GetWZetaFailsToUnpack(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{ + UseEVMMock: true, + }) + mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContractsWithMockEvmKeeper(t, ctx, k, mockEVMKeeper) + + mockEVMKeeper.MockEVMSuccessCallOnce() + + _, err := k.GetWZetaContractAddress(ctx) + require.ErrorIs(t, err, types.ErrABIUnpack) +} + func TestKeeper_GetUniswapFactoryFailsIfNotSet(t *testing.T) { k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) @@ -160,6 +176,36 @@ func TestKeeper_GetUniswapFactoryFailsIfNotSet(t *testing.T) { require.ErrorIs(t, err, types.ErrContractNotFound) } +func TestKeeper_GetUniswapFactoryFails(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{ + UseEVMMock: true, + }) + mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContractsWithMockEvmKeeper(t, ctx, k, mockEVMKeeper) + + mockEVMKeeper.MockEVMFailCallOnce() + + _, err := k.GetUniswapV2FactoryAddress(ctx) + require.ErrorIs(t, err, types.ErrContractCall) +} + +func TestKeeper_TestKeeper_GetUniswapFactoryFailsToUnpack(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{ + UseEVMMock: true, + }) + mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContractsWithMockEvmKeeper(t, ctx, k, mockEVMKeeper) + + mockEVMKeeper.MockEVMSuccessCallOnce() + + _, err := k.GetUniswapV2FactoryAddress(ctx) + require.ErrorIs(t, err, types.ErrABIUnpack) +} + func TestKeeper_GetUniswapRouterFailsIfNotSet(t *testing.T) { k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) @@ -174,6 +220,36 @@ func TestKeeper_GetUniswapRouterFailsIfNotSet(t *testing.T) { require.ErrorIs(t, err, types.ErrContractNotFound) } +func TestKeeper_GetUniswapRouterFails(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{ + UseEVMMock: true, + }) + mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContractsWithMockEvmKeeper(t, ctx, k, mockEVMKeeper) + + mockEVMKeeper.MockEVMFailCallOnce() + + _, err := k.GetUniswapV2Router02Address(ctx) + require.ErrorIs(t, err, types.ErrContractCall) +} + +func TestKeeper_TestKeeper_TestKeeper_GetUniswapRouterFailsToUnpack(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{ + UseEVMMock: true, + }) + mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContractsWithMockEvmKeeper(t, ctx, k, mockEVMKeeper) + + mockEVMKeeper.MockEVMSuccessCallOnce() + + _, err := k.GetUniswapV2Router02Address(ctx) + require.ErrorIs(t, err, types.ErrABIUnpack) +} + func TestKeeper_QuerySystemContractGasCoinZRC20(t *testing.T) { k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) @@ -191,6 +267,51 @@ func TestKeeper_QuerySystemContractGasCoinZRC20(t *testing.T) { require.Equal(t, zrc20, found) } +func TestKeeper_QuerySystemContractGasCoinZRC20FailsIfContractNotSet(t *testing.T) { + k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + chainID := getValidChainID(t) + + _, err := k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(chainID)) + require.Error(t, err) + require.ErrorIs(t, err, types.ErrStateVariableNotFound) + + deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) + + _, err = k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(chainID)) + require.ErrorIs(t, err, types.ErrContractNotFound) +} + +func TestKeeper_QuerySystemContractGasCoinZRC20Fails(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{ + UseEVMMock: true, + }) + mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContractsWithMockEvmKeeper(t, ctx, k, mockEVMKeeper) + + mockEVMKeeper.MockEVMFailCallOnce() + + _, err := k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(1)) + require.ErrorIs(t, err, types.ErrContractCall) +} + +func TestKeeper_QuerySystemContractGasCoinZRC20FailsToUnpack(t *testing.T) { + k, ctx, _, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{ + UseEVMMock: true, + }) + mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContractsWithMockEvmKeeper(t, ctx, k, mockEVMKeeper) + + mockEVMKeeper.MockEVMSuccessCallOnce() + + _, err := k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(1)) + require.ErrorIs(t, err, types.ErrABIUnpack) +} + func TestKeeper_CallUniswapV2RouterSwapExactETHForToken(t *testing.T) { k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) @@ -534,6 +655,16 @@ func TestKeeper_CallUniswapV2RouterSwapExactTokensForTokensFails(t *testing.T) { require.ErrorIs(t, err, types.ErrContractCall) } +func TestKeeper_QueryUniswapV2RouterGetZRC4AmountsInFails(t *testing.T) { + k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) + + _, err := k.QueryUniswapV2RouterGetZRC4AmountsIn(ctx, big.NewInt(1), sample.EthAddress()) + require.ErrorIs(t, err, types.ErrContractCall) +} + func TestKeeper_QueryUniswapV2RouterGetZRC4AmountsInFailsIfWZetaContractNotSet(t *testing.T) { k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) @@ -562,6 +693,16 @@ func TestKeeper_QueryUniswapV2RouterGetZRC4AmountsInFailsIfRouterContractNotSet( require.ErrorIs(t, err, types.ErrContractNotFound) } +func TestKeeper_QueryUniswapV2RouterGetZetaAmountsInFails(t *testing.T) { + k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) + + _, err := k.QueryUniswapV2RouterGetZetaAmountsIn(ctx, big.NewInt(1), sample.EthAddress()) + require.ErrorIs(t, err, types.ErrContractCall) +} + func TestKeeper_QueryUniswapV2RouterGetZetaAmountsInFailsIfWZetaContractNotSet(t *testing.T) { k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) @@ -590,6 +731,16 @@ func TestKeeper_QueryUniswapV2RouterGetZetaAmountsInFailsIfRouterContractNotSet( require.ErrorIs(t, err, types.ErrContractNotFound) } +func TestKeeper_QueryUniswapV2RouterGetZRC4ToZRC4AmountsInFails(t *testing.T) { + k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) + k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) + + _, err := k.QueryUniswapV2RouterGetZRC4ToZRC4AmountsIn(ctx, big.NewInt(1), sample.EthAddress(), sample.EthAddress()) + require.ErrorIs(t, err, types.ErrContractCall) +} + func TestKeeper_QueryUniswapV2RouterGetZRC4ToZRC4AmountsInFailsIfWZetaContractNotSet(t *testing.T) { k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)