Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Feb 20, 2024
1 parent 2375a71 commit 7ded446
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 18 deletions.
2 changes: 1 addition & 1 deletion x/crosschain/keeper/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion x/fungible/keeper/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion x/fungible/keeper/gas_coin_and_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion x/fungible/keeper/system_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
179 changes: 165 additions & 14 deletions x/fungible/keeper/system_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7ded446

Please sign in to comment.