Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve fungible module coverage #1985

Merged
merged 7 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* [1961](https://github.com/zeta-chain/node/pull/1961) - improve observer module coverage
* [1967](https://github.com/zeta-chain/node/pull/1967) - improve crosschain module coverage
* [1955](https://github.com/zeta-chain/node/pull/1955) - improve emissions module coverage
* [1985](https://github.com/zeta-chain/node/pull/1985) - improve fungible module coverage

### Fixes

Expand Down
5 changes: 0 additions & 5 deletions testutil/keeper/mocks/observer/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions x/fungible/keeper/deposits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/cmd/zetacored/config"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/coin"
"github.com/zeta-chain/zetacore/testutil/contracts"
Expand Down Expand Up @@ -352,3 +354,18 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) {
require.EqualValues(t, int64(0), balance.Int64())
})
}

func TestKeeper_DepositCoinZeta(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
to := sample.EthAddress()
amount := big.NewInt(1)
zetaToAddress := sdk.AccAddress(to.Bytes())

b := sdkk.BankKeeper.GetBalance(ctx, zetaToAddress, config.BaseDenom)
require.Equal(t, int64(0), b.Amount.Int64())

err := k.DepositCoinZeta(ctx, to, amount)
require.NoError(t, err)
b = sdkk.BankKeeper.GetBalance(ctx, zetaToAddress, config.BaseDenom)
require.Equal(t, amount.Int64(), b.Amount.Int64())
}
43 changes: 1 addition & 42 deletions x/fungible/keeper/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (k Keeper) DeployContract(ctx sdk.Context, metadata *bind.MetaData, ctorArg
}

if len(metadata.Bin) <= 2 {
return common.Address{}, cosmoserrors.Wrapf(types.ErrABIGet, "metadata Bin field too short: %s", err.Error())
return common.Address{}, cosmoserrors.Wrapf(types.ErrABIGet, "metadata Bin field too short")
}

bin, err := hex.DecodeString(metadata.Bin[2:])
Expand Down Expand Up @@ -98,10 +98,6 @@ func (k Keeper) DeployZRC20Contract(
if chain == nil {
return common.Address{}, cosmoserrors.Wrapf(zetaObserverTypes.ErrSupportedChains, "chain %d not found", chainID)
}
chainStr := chain.ChainName.String()
if chain == nil {
return common.Address{}, cosmoserrors.Wrapf(zetaObserverTypes.ErrSupportedChains, "chain %s not found", chainStr)
}
// Check if Contract has already been deployed for Asset
_, found := k.GetForeignCoinFromAsset(ctx, erc20Contract, chainID)
if found {
Expand Down Expand Up @@ -309,43 +305,6 @@ func (k Keeper) DepositZRC20AndCallContract(ctx sdk.Context,
)
}

// QueryWithdrawGasFee returns the gas fee for a withdrawal transaction associated with a given zrc20
func (k Keeper) QueryWithdrawGasFee(ctx sdk.Context, contract common.Address) (*big.Int, error) {
zrc20ABI, err := zrc20.ZRC20MetaData.GetAbi()
if err != nil {
return nil, err
}
res, err := k.CallEVM(
ctx,
*zrc20ABI,
types.ModuleAddressEVM,
contract,
BigIntZero,
nil,
false,
false,
"withdrawGasFee",
)
if err != nil {
return nil, err
}

unpacked, err := zrc20ABI.Unpack("withdrawGasFee", res.Ret)
if err != nil {
return nil, err
}
if len(unpacked) < 2 {
return nil, fmt.Errorf("expect 2 returned values, got %d", len(unpacked))
}

GasFee, ok := unpacked[1].(*big.Int)
if !ok {
return nil, errors.New("can't read returned value as big.Int")
}

return GasFee, nil
}

// QueryProtocolFlatFee returns the protocol flat fee associated with a given zrc20
func (k Keeper) QueryProtocolFlatFee(ctx sdk.Context, contract common.Address) (*big.Int, error) {
zrc20ABI, err := zrc20.ZRC20MetaData.GetAbi()
Expand Down
6 changes: 3 additions & 3 deletions x/fungible/keeper/evm_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func (k Keeper) EVMHooks() EVMHooks {

// PostTxProcessing is a wrapper for calling the EVM PostTxProcessing hook on the module keeper
func (h EVMHooks) PostTxProcessing(ctx sdk.Context, _ core.Message, receipt *ethtypes.Receipt) error {
return h.k.CheckPausedZRC20(ctx, receipt)
return h.k.checkPausedZRC20(ctx, receipt)
}

// CheckPausedZRC20 checks the events of the receipt
// checkPausedZRC20 checks the events of the receipt
// if an event is emitted from a paused ZRC20 contract it will revert the transaction
func (k Keeper) CheckPausedZRC20(ctx sdk.Context, receipt *ethtypes.Receipt) error {
func (k Keeper) checkPausedZRC20(ctx sdk.Context, receipt *ethtypes.Receipt) error {
if receipt == nil {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion x/fungible/keeper/evm_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func TestKeeper_CheckPausedZRC20(t *testing.T) {
assertPaused(addrPausedZRC20.Hex())

// process test
err := k.CheckPausedZRC20(ctx, tc.receipt)
h := k.EVMHooks()
err := h.PostTxProcessing(ctx, nil, tc.receipt)
if tc.wantErr {
require.ErrorIs(t, err, types.ErrPausedZRC20)
} else {
Expand Down
Loading
Loading