Skip to content

Commit

Permalink
deploy fungible coin
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Sep 13, 2023
1 parent 6982e15 commit b843da6
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 5 deletions.
147 changes: 147 additions & 0 deletions x/fungible/keeper/msg_server_deploy_fungible_coin_zrc20_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
3 changes: 2 additions & 1 deletion x/fungible/keeper/msg_server_remove_foreign_coin_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions x/fungible/keeper/msg_server_update_system_contract_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions x/fungible/keeper/system_contract_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit b843da6

Please sign in to comment.