From b843da6a81805ac01b876931042ade4c783b3af5 Mon Sep 17 00:00:00 2001 From: lumtis Date: Wed, 13 Sep 2023 15:19:30 +0200 Subject: [PATCH] deploy fungible coin --- ..._server_deploy_fungible_coin_zrc20_test.go | 147 ++++++++++++++++++ .../msg_server_remove_foreign_coin_test.go | 3 +- .../msg_server_update_system_contract_test.go | 5 +- x/fungible/keeper/system_contract_test.go | 5 +- 4 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20_test.go diff --git a/x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20_test.go b/x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20_test.go new file mode 100644 index 0000000000..5d7d84cc66 --- /dev/null +++ b/x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20_test.go @@ -0,0 +1,147 @@ +package keeper_test + +import ( + ethcommon "github.com/ethereum/go-ethereum/common" + "math/big" + "testing" + + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "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) + }) +} diff --git a/x/fungible/keeper/msg_server_remove_foreign_coin_test.go b/x/fungible/keeper/msg_server_remove_foreign_coin_test.go index 2fd8ae0551..c866d81f21 100644 --- a/x/fungible/keeper/msg_server_remove_foreign_coin_test.go +++ b/x/fungible/keeper/msg_server_remove_foreign_coin_test.go @@ -1,13 +1,14 @@ package keeper_test import ( + "testing" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/stretchr/testify/require" 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" - "testing" ) func TestMsgServer_RemoveForeignCoin(t *testing.T) { diff --git a/x/fungible/keeper/msg_server_update_system_contract_test.go b/x/fungible/keeper/msg_server_update_system_contract_test.go index 612428edc4..1d38265022 100644 --- a/x/fungible/keeper/msg_server_update_system_contract_test.go +++ b/x/fungible/keeper/msg_server_update_system_contract_test.go @@ -1,6 +1,9 @@ package keeper_test import ( + "math/big" + "testing" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" @@ -11,8 +14,6 @@ import ( "github.com/zeta-chain/zetacore/testutil/sample" "github.com/zeta-chain/zetacore/x/fungible/keeper" "github.com/zeta-chain/zetacore/x/fungible/types" - "math/big" - "testing" ) func TestKeeper_UpdateSystemContract(t *testing.T) { diff --git a/x/fungible/keeper/system_contract_test.go b/x/fungible/keeper/system_contract_test.go index f7307b099d..753f339770 100644 --- a/x/fungible/keeper/system_contract_test.go +++ b/x/fungible/keeper/system_contract_test.go @@ -1,11 +1,12 @@ package keeper_test import ( + "math/big" + "testing" + "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" "github.com/zeta-chain/zetacore/testutil/sample" - "math/big" - "testing" keepertest "github.com/zeta-chain/zetacore/testutil/keeper" "github.com/zeta-chain/zetacore/x/fungible/types"