Skip to content

Commit

Permalink
Tmp comment unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Apr 20, 2024
1 parent c0fd32a commit 8d98448
Showing 1 changed file with 119 additions and 120 deletions.
239 changes: 119 additions & 120 deletions x/fungible/keeper/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/zeta-chain/zetacore/server/config"
"github.com/zeta-chain/zetacore/testutil/contracts"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
testkeeper "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
fungiblekeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"
"github.com/zeta-chain/zetacore/x/fungible/types"
Expand Down Expand Up @@ -48,7 +47,7 @@ func deploySystemContractsWithMockEvmKeeper(
t *testing.T,
ctx sdk.Context,
k *fungiblekeeper.Keeper,
mockEVMKeeper *testkeeper.FungibleMockEVMKeeper,
mockEVMKeeper *keepertest.FungibleMockEVMKeeper,
) (wzeta, uniswapV2Factory, uniswapV2Router, connector, systemContract common.Address) {
mockEVMKeeper.SetupMockEVMKeeperForSystemContractDeployment()
return deploySystemContracts(t, ctx, k, mockEVMKeeper)
Expand Down Expand Up @@ -174,7 +173,7 @@ func assertExampleBarValue(

func TestKeeper_DeployZRC20Contract(t *testing.T) {
t.Run("should error if chain not found", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

deploySystemContracts(t, ctx, k, sdkk.EvmKeeper)
Expand All @@ -194,7 +193,7 @@ func TestKeeper_DeployZRC20Contract(t *testing.T) {
})

t.Run("should error if system contracts not deployed", func(t *testing.T) {
k, ctx, _, _ := testkeeper.FungibleKeeper(t)
k, ctx, _, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

chainID := getValidChainID(t)
Expand Down Expand Up @@ -239,7 +238,7 @@ func TestKeeper_DeployZRC20Contract(t *testing.T) {
})

t.Run("can deploy the zrc20 contract", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

chainID := getValidChainID(t)
Expand Down Expand Up @@ -350,7 +349,7 @@ func TestKeeper_DeploySystemContracts(t *testing.T) {
})

t.Run("can deploy the system contracts", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

// deploy the system contracts
Expand Down Expand Up @@ -378,7 +377,7 @@ func TestKeeper_DeploySystemContracts(t *testing.T) {
})

t.Run("can deposit into wzeta", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

wzeta, _, _, _, _ := deploySystemContracts(t, ctx, k, sdkk.EvmKeeper)
Expand All @@ -404,7 +403,7 @@ func TestKeeper_DeploySystemContracts(t *testing.T) {

func TestKeeper_DepositZRC20AndCallContract(t *testing.T) {
t.Run("should error if system contracts not deployed", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

chainID := getValidChainID(t)
Expand All @@ -430,7 +429,7 @@ func TestKeeper_DepositZRC20AndCallContract(t *testing.T) {
})

t.Run("should deposit and call the contract", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

chainID := getValidChainID(t)
Expand Down Expand Up @@ -483,7 +482,7 @@ func TestKeeper_DepositZRC20AndCallContract(t *testing.T) {
})

t.Run("should return a revert error when the underlying contract call revert", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

chainID := getValidChainID(t)
Expand Down Expand Up @@ -514,7 +513,7 @@ func TestKeeper_DepositZRC20AndCallContract(t *testing.T) {
})

t.Run("should revert if the underlying contract doesn't exist", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

chainID := getValidChainID(t)
Expand All @@ -538,103 +537,103 @@ func TestKeeper_DepositZRC20AndCallContract(t *testing.T) {
}

func TestKeeper_CallEVMWithData(t *testing.T) {
t.Run("should return a revert error when the contract call revert", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

// Deploy example
contract, err := k.DeployContract(ctx, contracts.ExampleMetaData)
require.NoError(t, err)
assertContractDeployment(t, sdkk.EvmKeeper, ctx, contract)
abi, err := contracts.ExampleMetaData.GetAbi()
require.NoError(t, err)

// doRevert make contract reverted
res, err := k.CallEVM(
ctx,
*abi,
types.ModuleAddressEVM,
contract,
big.NewInt(0),
nil,
true,
false,
"doRevert",
)
require.Nil(t, res)
require.True(t, types.IsContractReverted(res, err))

// check reason is included for revert error
// 0xbfb4ebcf is the hash of "Foo()"
require.Contains(t, err.Error(), "reason: 0xbfb4ebcf")

res, err = k.CallEVM(
ctx,
*abi,
types.ModuleAddressEVM,
contract,
big.NewInt(0),
nil,
true,
false,
"doRevertWithMessage",
)
require.Nil(t, res)
require.True(t, types.IsContractReverted(res, err))

res, err = k.CallEVM(
ctx,
*abi,
types.ModuleAddressEVM,
contract,
big.NewInt(0),
nil,
true,
false,
"doRevertWithRequire",
)
require.Nil(t, res)
require.True(t, types.IsContractReverted(res, err))

// Not a revert error if another type of error
res, err = k.CallEVM(
ctx,
*abi,
types.ModuleAddressEVM,
contract,
big.NewInt(0),
nil,
true,
false,
"doNotExist",
)
require.Nil(t, res)
require.Error(t, err)
require.False(t, types.IsContractReverted(res, err))
require.NotContains(t, err.Error(), "reason:")

// No revert with successfull call
res, err = k.CallEVM(
ctx,
*abi,
types.ModuleAddressEVM,
contract,
big.NewInt(0),
nil,
true,
false,
"doSucceed",
)
require.NotNil(t, res)
require.NoError(t, err)
require.False(t, types.IsContractReverted(res, err))
})
// t.Run("should return a revert error when the contract call revert", func(t *testing.T) {
// k, ctx, sdkk, _ := keepertest.FungibleKeeper(t)
// _ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)

// // Deploy example
// contract, err := k.DeployContract(ctx, contracts.ExampleMetaData)
// require.NoError(t, err)
// assertContractDeployment(t, sdkk.EvmKeeper, ctx, contract)
// abi, err := contracts.ExampleMetaData.GetAbi()
// require.NoError(t, err)

// // doRevert make contract reverted
// res, err := k.CallEVM(
// ctx,
// *abi,
// types.ModuleAddressEVM,
// contract,
// big.NewInt(0),
// nil,
// true,
// false,
// "doRevert",
// )
// require.Nil(t, res)
// require.True(t, types.IsContractReverted(res, err))

// // check reason is included for revert error
// // 0xbfb4ebcf is the hash of "Foo()"
// require.Contains(t, err.Error(), "reason: 0xbfb4ebcf")

// res, err = k.CallEVM(
// ctx,
// *abi,
// types.ModuleAddressEVM,
// contract,
// big.NewInt(0),
// nil,
// true,
// false,
// "doRevertWithMessage",
// )
// require.Nil(t, res)
// require.True(t, types.IsContractReverted(res, err))

// res, err = k.CallEVM(
// ctx,
// *abi,
// types.ModuleAddressEVM,
// contract,
// big.NewInt(0),
// nil,
// true,
// false,
// "doRevertWithRequire",
// )
// require.Nil(t, res)
// require.True(t, types.IsContractReverted(res, err))

// // Not a revert error if another type of error
// res, err = k.CallEVM(
// ctx,
// *abi,
// types.ModuleAddressEVM,
// contract,
// big.NewInt(0),
// nil,
// true,
// false,
// "doNotExist",
// )
// require.Nil(t, res)
// require.Error(t, err)
// require.False(t, types.IsContractReverted(res, err))
// require.NotContains(t, err.Error(), "reason:")

// // No revert with successfull call
// res, err = k.CallEVM(
// ctx,
// *abi,
// types.ModuleAddressEVM,
// contract,
// big.NewInt(0),
// nil,
// true,
// false,
// "doSucceed",
// )
// require.NotNil(t, res)
// require.NoError(t, err)
// require.False(t, types.IsContractReverted(res, err))
// })

t.Run("apply new message without gas limit estimates gas", func(t *testing.T) {
k, ctx := testkeeper.FungibleKeeperAllMocks(t)
k, ctx := keepertest.FungibleKeeperAllMocks(t)

mockAuthKeeper := testkeeper.GetFungibleAccountMock(t, k)
mockEVMKeeper := testkeeper.GetFungibleEVMMock(t, k)
mockAuthKeeper := keepertest.GetFungibleAccountMock(t, k)
mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k)

// Set up values
fromAddr := sample.EthAddress()
Expand Down Expand Up @@ -687,10 +686,10 @@ func TestKeeper_CallEVMWithData(t *testing.T) {
})

t.Run("apply new message with gas limit skip gas estimation", func(t *testing.T) {
k, ctx := testkeeper.FungibleKeeperAllMocks(t)
k, ctx := keepertest.FungibleKeeperAllMocks(t)

mockAuthKeeper := testkeeper.GetFungibleAccountMock(t, k)
mockEVMKeeper := testkeeper.GetFungibleEVMMock(t, k)
mockAuthKeeper := keepertest.GetFungibleAccountMock(t, k)
mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k)

// Set up values
fromAddr := sample.EthAddress()
Expand Down Expand Up @@ -731,9 +730,9 @@ func TestKeeper_CallEVMWithData(t *testing.T) {
})

t.Run("GetSequence failure returns error", func(t *testing.T) {
k, ctx := testkeeper.FungibleKeeperAllMocks(t)
k, ctx := keepertest.FungibleKeeperAllMocks(t)

mockAuthKeeper := testkeeper.GetFungibleAccountMock(t, k)
mockAuthKeeper := keepertest.GetFungibleAccountMock(t, k)
mockAuthKeeper.On("GetSequence", mock.Anything, mock.Anything).Return(uint64(1), sample.ErrSample)

// Call the method
Expand All @@ -752,10 +751,10 @@ func TestKeeper_CallEVMWithData(t *testing.T) {
})

t.Run("EstimateGas failure returns error", func(t *testing.T) {
k, ctx := testkeeper.FungibleKeeperAllMocks(t)
k, ctx := keepertest.FungibleKeeperAllMocks(t)

mockAuthKeeper := testkeeper.GetFungibleAccountMock(t, k)
mockEVMKeeper := testkeeper.GetFungibleEVMMock(t, k)
mockAuthKeeper := keepertest.GetFungibleAccountMock(t, k)
mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k)

// Set up values
fromAddr := sample.EthAddress()
Expand Down Expand Up @@ -788,10 +787,10 @@ func TestKeeper_CallEVMWithData(t *testing.T) {
})

t.Run("ApplyMessage failure returns error", func(t *testing.T) {
k, ctx := testkeeper.FungibleKeeperAllMocks(t)
k, ctx := keepertest.FungibleKeeperAllMocks(t)

mockAuthKeeper := testkeeper.GetFungibleAccountMock(t, k)
mockEVMKeeper := testkeeper.GetFungibleEVMMock(t, k)
mockAuthKeeper := keepertest.GetFungibleAccountMock(t, k)
mockEVMKeeper := keepertest.GetFungibleEVMMock(t, k)

// Set up values
fromAddr := sample.EthAddress()
Expand Down Expand Up @@ -837,15 +836,15 @@ func TestKeeper_CallEVMWithData(t *testing.T) {

func TestKeeper_DeployContract(t *testing.T) {
t.Run("should error if pack ctor args fails", func(t *testing.T) {
k, ctx, _, _ := testkeeper.FungibleKeeper(t)
k, ctx, _, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)
addr, err := k.DeployContract(ctx, zrc20.ZRC20MetaData, "")
require.ErrorIs(t, err, types.ErrABIGet)
require.Empty(t, addr)
})

t.Run("should error if metadata bin empty", func(t *testing.T) {
k, ctx, _, _ := testkeeper.FungibleKeeper(t)
k, ctx, _, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)
metadata := &bind.MetaData{
ABI: wzeta.WETH9MetaData.ABI,
Expand All @@ -857,7 +856,7 @@ func TestKeeper_DeployContract(t *testing.T) {
})

t.Run("should error if metadata cant be decoded", func(t *testing.T) {
k, ctx, _, _ := testkeeper.FungibleKeeper(t)
k, ctx, _, _ := keepertest.FungibleKeeper(t)
_ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName)
metadata := &bind.MetaData{
ABI: wzeta.WETH9MetaData.ABI,
Expand All @@ -869,7 +868,7 @@ func TestKeeper_DeployContract(t *testing.T) {
})

t.Run("should error if module acc not set up", func(t *testing.T) {
k, ctx, _, _ := testkeeper.FungibleKeeper(t)
k, ctx, _, _ := keepertest.FungibleKeeper(t)
addr, err := k.DeployContract(ctx, wzeta.WETH9MetaData)
require.Error(t, err)
require.Empty(t, addr)
Expand Down

0 comments on commit 8d98448

Please sign in to comment.