-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(
fungible
): increase keeper code coverage (#1128)
* initialize system contract * system contract tests * update_system_contract_test * remove foreign coin * add res and fix event emit * deploy fungible coin * gas price * format imports * make generate --------- Co-authored-by: brewmaster012 <[email protected]>
- Loading branch information
1 parent
bc0e26d
commit 2765dc4
Showing
16 changed files
with
666 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
ethcommon "github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
"github.com/zeta-chain/protocol-contracts/pkg/contracts/zevm/systemcontract.sol" | ||
keepertest "github.com/zeta-chain/zetacore/testutil/keeper" | ||
"github.com/zeta-chain/zetacore/testutil/sample" | ||
"github.com/zeta-chain/zetacore/x/fungible/keeper" | ||
"github.com/zeta-chain/zetacore/x/fungible/types" | ||
) | ||
|
||
func TestKeeper_SetGasPrice(t *testing.T) { | ||
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) | ||
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) | ||
|
||
_, _, _, _, system := deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) | ||
|
||
queryGasPrice := func(chainID *big.Int) *big.Int { | ||
abi, err := systemcontract.SystemContractMetaData.GetAbi() | ||
require.NoError(t, err) | ||
res, err := k.CallEVM(ctx, *abi, types.ModuleAddressEVM, system, keeper.BigIntZero, nil, false, false, "gasPriceByChainId", chainID) | ||
require.NoError(t, err) | ||
unpacked, err := abi.Unpack("gasPriceByChainId", res.Ret) | ||
require.NoError(t, err) | ||
gasPrice, ok := unpacked[0].(*big.Int) | ||
require.True(t, ok) | ||
return gasPrice | ||
} | ||
|
||
_, err := k.SetGasPrice(ctx, big.NewInt(1), big.NewInt(42)) | ||
require.NoError(t, err) | ||
require.Equal(t, big.NewInt(42), queryGasPrice(big.NewInt(1))) | ||
} | ||
|
||
func TestKeeper_SetGasCoin(t *testing.T) { | ||
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) | ||
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) | ||
gas := sample.EthAddress() | ||
|
||
deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) | ||
err := k.SetGasCoin(ctx, big.NewInt(1), gas) | ||
require.NoError(t, err) | ||
|
||
found, err := k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(1)) | ||
require.NoError(t, err) | ||
require.Equal(t, gas.Hex(), found.Hex()) | ||
} | ||
|
||
func TestKeeper_SetGasZetaPool(t *testing.T) { | ||
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) | ||
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) | ||
zrc20 := sample.EthAddress() | ||
|
||
_, _, _, _, system := deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) | ||
|
||
queryZetaPool := func(chainID *big.Int) ethcommon.Address { | ||
abi, err := systemcontract.SystemContractMetaData.GetAbi() | ||
require.NoError(t, err) | ||
res, err := k.CallEVM(ctx, *abi, types.ModuleAddressEVM, system, keeper.BigIntZero, nil, false, false, "gasZetaPoolByChainId", chainID) | ||
require.NoError(t, err) | ||
unpacked, err := abi.Unpack("gasZetaPoolByChainId", res.Ret) | ||
require.NoError(t, err) | ||
pool, ok := unpacked[0].(ethcommon.Address) | ||
require.True(t, ok) | ||
return pool | ||
} | ||
|
||
err := k.SetGasZetaPool(ctx, big.NewInt(1), zrc20) | ||
require.NoError(t, err) | ||
require.NotEqual(t, ethcommon.Address{}, queryZetaPool(big.NewInt(1))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
ethcommon "github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
"github.com/zeta-chain/zetacore/common" | ||
keepertest "github.com/zeta-chain/zetacore/testutil/keeper" | ||
"github.com/zeta-chain/zetacore/testutil/sample" | ||
"github.com/zeta-chain/zetacore/x/fungible/keeper" | ||
"github.com/zeta-chain/zetacore/x/fungible/types" | ||
observertypes "github.com/zeta-chain/zetacore/x/observer/types" | ||
) | ||
|
||
func TestMsgServer_DeployFungibleCoinZRC20(t *testing.T) { | ||
t.Run("can deploy a new zrc20", func(t *testing.T) { | ||
k, ctx, sdkk, zk := keepertest.FungibleKeeper(t) | ||
msgServer := keeper.NewMsgServerImpl(*k) | ||
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) | ||
admin := sample.AccAddress() | ||
setAdminDeployFungibleCoin(ctx, zk, admin) | ||
chainID := getValidChainID(t) | ||
|
||
deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) | ||
|
||
res, err := msgServer.DeployFungibleCoinZRC20(ctx, types.NewMsgDeployFungibleCoinZRC20( | ||
admin, | ||
sample.EthAddress().Hex(), | ||
chainID, | ||
8, | ||
"foo", | ||
"foo", | ||
common.CoinType_Gas, | ||
1000000, | ||
)) | ||
require.NoError(t, err) | ||
gasAddress := res.Address | ||
assertContractDeployment(t, sdkk.EvmKeeper, ctx, ethcommon.HexToAddress(gasAddress)) | ||
|
||
// can retrieve the gas coin | ||
foreignCoin, found := k.GetForeignCoins(ctx, gasAddress) | ||
require.True(t, found) | ||
require.Equal(t, foreignCoin.CoinType, common.CoinType_Gas) | ||
require.Contains(t, foreignCoin.Name, "foo") | ||
|
||
gas, err := k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(chainID)) | ||
require.NoError(t, err) | ||
require.Equal(t, gasAddress, gas.Hex()) | ||
|
||
// can deploy non-gas zrc20 | ||
res, err = msgServer.DeployFungibleCoinZRC20(ctx, types.NewMsgDeployFungibleCoinZRC20( | ||
admin, | ||
sample.EthAddress().Hex(), | ||
chainID, | ||
8, | ||
"bar", | ||
"bar", | ||
common.CoinType_ERC20, | ||
1000000, | ||
)) | ||
require.NoError(t, err) | ||
assertContractDeployment(t, sdkk.EvmKeeper, ctx, ethcommon.HexToAddress(res.Address)) | ||
|
||
foreignCoin, found = k.GetForeignCoins(ctx, res.Address) | ||
require.True(t, found) | ||
require.Equal(t, foreignCoin.CoinType, common.CoinType_ERC20) | ||
require.Contains(t, foreignCoin.Name, "bar") | ||
|
||
// gas should remain the same | ||
gas, err = k.QuerySystemContractGasCoinZRC20(ctx, big.NewInt(chainID)) | ||
require.NoError(t, err) | ||
require.NotEqual(t, res.Address, gas.Hex()) | ||
require.Equal(t, gasAddress, gas.Hex()) | ||
}) | ||
|
||
t.Run("should not deploy a new zrc20 if not admin", func(t *testing.T) { | ||
k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) | ||
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) | ||
chainID := getValidChainID(t) | ||
|
||
deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) | ||
|
||
// should not deploy a new zrc20 if not admin | ||
_, err := keeper.NewMsgServerImpl(*k).DeployFungibleCoinZRC20(ctx, types.NewMsgDeployFungibleCoinZRC20( | ||
sample.AccAddress(), | ||
sample.EthAddress().Hex(), | ||
chainID, | ||
8, | ||
"foo", | ||
"foo", | ||
common.CoinType_Gas, | ||
1000000, | ||
)) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, sdkerrors.ErrUnauthorized) | ||
}) | ||
|
||
t.Run("should not deploy a new zrc20 with wrong decimal", func(t *testing.T) { | ||
k, ctx, sdkk, zk := keepertest.FungibleKeeper(t) | ||
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) | ||
admin := sample.AccAddress() | ||
setAdminDeployFungibleCoin(ctx, zk, admin) | ||
chainID := getValidChainID(t) | ||
|
||
deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) | ||
|
||
// should not deploy a new zrc20 if not admin | ||
_, err := keeper.NewMsgServerImpl(*k).DeployFungibleCoinZRC20(ctx, types.NewMsgDeployFungibleCoinZRC20( | ||
admin, | ||
sample.EthAddress().Hex(), | ||
chainID, | ||
256, | ||
"foo", | ||
"foo", | ||
common.CoinType_Gas, | ||
1000000, | ||
)) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, sdkerrors.ErrInvalidRequest) | ||
}) | ||
|
||
t.Run("should not deploy a new zrc20 with invalid chain ID", func(t *testing.T) { | ||
k, ctx, sdkk, zk := keepertest.FungibleKeeper(t) | ||
k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) | ||
admin := sample.AccAddress() | ||
setAdminDeployFungibleCoin(ctx, zk, admin) | ||
|
||
deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) | ||
|
||
// should not deploy a new zrc20 if not admin | ||
_, err := keeper.NewMsgServerImpl(*k).DeployFungibleCoinZRC20(ctx, types.NewMsgDeployFungibleCoinZRC20( | ||
admin, | ||
sample.EthAddress().Hex(), | ||
9999999, | ||
8, | ||
"foo", | ||
"foo", | ||
common.CoinType_Gas, | ||
1000000, | ||
)) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, observertypes.ErrSupportedChains) | ||
}) | ||
} |
Oops, something went wrong.