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 (system_contract tests) #1782

Merged
merged 24 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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 @@ -41,6 +41,7 @@
* [1584](https://github.com/zeta-chain/node/pull/1584) - allow to run E2E tests on any networks
* [1753](https://github.com/zeta-chain/node/pull/1753) - fix gosec errors on usage of rand package
* [1762](https://github.com/zeta-chain/node/pull/1762) - improve coverage for fungibile module
* [1782](https://github.com/zeta-chain/node/pull/1782) - improve coverage for fungibile module system contract

### CI

Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func setupZRC20Pool(
)
require.NoError(t, err)

// approve the router to spend the zeta
// approve the router to spend the zrc20
err = k.CallZRC20Approve(
ctx,
types.ModuleAddressEVM,
Expand Down
51 changes: 51 additions & 0 deletions x/fungible/keeper/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,57 @@ func deploySystemContracts(
return
}

type SystemContractDeployConfig struct {
DeployWZeta bool
DeployUniswapV2Factory bool
DeployUniswapV2Router bool
}

// deploySystemContractsConfigurable deploys the system contracts and returns their addresses
// while having a possibility to skip some deployments to test different scenarios
func deploySystemContractsConfigurable(
t *testing.T,
ctx sdk.Context,
k *fungiblekeeper.Keeper,
evmk types.EVMKeeper,
config *SystemContractDeployConfig,
) (wzeta, uniswapV2Factory, uniswapV2Router, connector, systemContract common.Address) {
var err error

if config.DeployWZeta {
wzeta, err = k.DeployWZETA(ctx)
require.NoError(t, err)
require.NotEmpty(t, wzeta)
assertContractDeployment(t, evmk, ctx, wzeta)
}

if config.DeployUniswapV2Factory {
uniswapV2Factory, err = k.DeployUniswapV2Factory(ctx)
require.NoError(t, err)
require.NotEmpty(t, uniswapV2Factory)
assertContractDeployment(t, evmk, ctx, uniswapV2Factory)
}

if config.DeployUniswapV2Router {
uniswapV2Router, err = k.DeployUniswapV2Router02(ctx, uniswapV2Factory, wzeta)
require.NoError(t, err)
require.NotEmpty(t, uniswapV2Router)
assertContractDeployment(t, evmk, ctx, uniswapV2Router)
}

connector, err = k.DeployConnectorZEVM(ctx, wzeta)
require.NoError(t, err)
require.NotEmpty(t, connector)
assertContractDeployment(t, evmk, ctx, connector)

systemContract, err = k.DeploySystemContract(ctx, wzeta, uniswapV2Factory, uniswapV2Router)
require.NoError(t, err)
require.NotEmpty(t, systemContract)
assertContractDeployment(t, evmk, ctx, systemContract)

return
}

// assertExampleBarValue asserts value Bar of the contract Example, used to test onCrossChainCall
func assertExampleBarValue(
t *testing.T,
Expand Down
70 changes: 70 additions & 0 deletions x/fungible/keeper/gas_coin_and_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
"github.com/stretchr/testify/require"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
uniswapv2router02 "github.com/zeta-chain/protocol-contracts/pkg/uniswap/v2-periphery/contracts/uniswapv2router02.sol"
"github.com/zeta-chain/zetacore/cmd/zetacored/config"
testkeeper "github.com/zeta-chain/zetacore/testutil/keeper"
fungiblekeeper "github.com/zeta-chain/zetacore/x/fungible/keeper"
"github.com/zeta-chain/zetacore/x/fungible/types"
Expand Down Expand Up @@ -62,6 +65,73 @@ func deployZRC20(
return addr
}

// setupZRC20Pool setup a Uniswap pool with liquidity for the pair zeta/asset
func setupZRC20Pool(
t *testing.T,
ctx sdk.Context,
k *fungiblekeeper.Keeper,
bankKeeper bankkeeper.Keeper,
zrc20Addr common.Address,
) {
routerAddress, err := k.GetUniswapV2Router02Address(ctx)
require.NoError(t, err)
routerABI, err := uniswapv2router02.UniswapV2Router02MetaData.GetAbi()
require.NoError(t, err)

// enough for the small numbers used in test
liquidityAmount := big.NewInt(1e17)

// mint some zrc20 and zeta
_, err = k.DepositZRC20(ctx, zrc20Addr, types.ModuleAddressEVM, liquidityAmount)
require.NoError(t, err)
err = bankKeeper.MintCoins(
ctx,
types.ModuleName,
sdk.NewCoins(sdk.NewCoin(config.BaseDenom, sdk.NewIntFromBigInt(liquidityAmount))),
)
require.NoError(t, err)

// approve the router to spend the zrc20
err = k.CallZRC20Approve(
ctx,
types.ModuleAddressEVM,
zrc20Addr,
routerAddress,
liquidityAmount,
false,
)
require.NoError(t, err)

// k2 := liquidityAmount.Sub(liquidityAmount, big.NewInt(1000))
// add the liquidity
//function addLiquidityETH(
// address token,
// uint amountTokenDesired,
// uint amountTokenMin,
// uint amountETHMin,
// address to,
// uint deadline
//)
_, err = k.CallEVM(
ctx,
*routerABI,
types.ModuleAddressEVM,
routerAddress,
liquidityAmount,
big.NewInt(5_000_000),
true,
false,
"addLiquidityETH",
zrc20Addr,
liquidityAmount,
fungiblekeeper.BigIntZero,
fungiblekeeper.BigIntZero,
types.ModuleAddressEVM,
liquidityAmount,
)
require.NoError(t, err)
}

func TestKeeper_SetupChainGasCoinAndPool(t *testing.T) {
t.Run("can setup a new chain gas coin", func(t *testing.T) {
k, ctx, sdkk, _ := testkeeper.FungibleKeeper(t)
Expand Down
43 changes: 29 additions & 14 deletions x/fungible/keeper/system_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
cosmoserrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/zevm/systemcontract.sol"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/zevm/wzeta.sol"
Expand Down Expand Up @@ -78,7 +79,6 @@ func (k *Keeper) GetWZetaContractAddress(ctx sdk.Context) (ethcommon.Address, er
)
if err != nil {
return ethcommon.Address{}, cosmoserrors.Wrapf(types.ErrContractCall, "failed to call wZetaContractAddress (%s)", err.Error())

}
type AddressResponse struct {
Value ethcommon.Address
Expand All @@ -87,6 +87,10 @@ func (k *Keeper) GetWZetaContractAddress(ctx sdk.Context) (ethcommon.Address, er
if err := sysABI.UnpackIntoInterface(&wzetaResponse, "wZetaContractAddress", res.Ret); err != nil {
return ethcommon.Address{}, cosmoserrors.Wrapf(types.ErrABIUnpack, "failed to unpack wZetaContractAddress: %s", err.Error())
}

if wzetaResponse.Value == ethcommon.HexToAddress("0x0") {
skosito marked this conversation as resolved.
Show resolved Hide resolved
return ethcommon.Address{}, sdkerrors.Wrapf(types.ErrContractNotFound, "wzeta contract invalid address")
skosito marked this conversation as resolved.
Show resolved Hide resolved
}
return wzetaResponse.Value, nil
}

Expand Down Expand Up @@ -119,11 +123,15 @@ func (k *Keeper) GetUniswapV2FactoryAddress(ctx sdk.Context) (ethcommon.Address,
type AddressResponse struct {
Value ethcommon.Address
}
var wzetaResponse AddressResponse
if err := sysABI.UnpackIntoInterface(&wzetaResponse, "uniswapv2FactoryAddress", res.Ret); err != nil {
var uniswapFactoryResponse AddressResponse
if err := sysABI.UnpackIntoInterface(&uniswapFactoryResponse, "uniswapv2FactoryAddress", res.Ret); err != nil {
return ethcommon.Address{}, cosmoserrors.Wrapf(types.ErrABIUnpack, "failed to unpack uniswapv2FactoryAddress: %s", err.Error())
}
return wzetaResponse.Value, nil

if uniswapFactoryResponse.Value == ethcommon.HexToAddress("0x0") {
skosito marked this conversation as resolved.
Show resolved Hide resolved
skosito marked this conversation as resolved.
Show resolved Hide resolved
return ethcommon.Address{}, sdkerrors.Wrapf(types.ErrContractNotFound, "uniswap factory contract invalid address")
}
return uniswapFactoryResponse.Value, nil
}

// GetUniswapV2Router02Address returns the uniswapv2 router02 address on ZetaChain
Expand Down Expand Up @@ -159,6 +167,10 @@ func (k *Keeper) GetUniswapV2Router02Address(ctx sdk.Context) (ethcommon.Address
if err := sysABI.UnpackIntoInterface(&routerResponse, "uniswapv2Router02Address", res.Ret); err != nil {
return ethcommon.Address{}, cosmoserrors.Wrapf(types.ErrABIUnpack, "failed to unpack uniswapv2Router02Address: %s", err.Error())
}

if routerResponse.Value == ethcommon.HexToAddress("0x0") {
return ethcommon.Address{}, sdkerrors.Wrapf(types.ErrContractNotFound, "uniswap router contract invalid address")
skosito marked this conversation as resolved.
Show resolved Hide resolved
}
return routerResponse.Value, nil
}

Expand Down Expand Up @@ -265,6 +277,9 @@ func (k *Keeper) QuerySystemContractGasCoinZRC20(ctx sdk.Context, chainid *big.I
if err := sysABI.UnpackIntoInterface(&zrc20Res, "gasCoinZRC20ByChainId", res.Ret); err != nil {
return ethcommon.Address{}, cosmoserrors.Wrapf(types.ErrABIUnpack, "failed to unpack gasCoinZRC20ByChainId: %s", err.Error())
}
if zrc20Res.Value == ethcommon.HexToAddress("0x0") {
skosito marked this conversation as resolved.
Show resolved Hide resolved
return ethcommon.Address{}, sdkerrors.Wrapf(types.ErrContractNotFound, "gas coin contract invalid address")
}
return zrc20Res.Value, nil
}

Expand Down Expand Up @@ -316,7 +331,7 @@ func (k *Keeper) CallUniswapV2RouterSwapExactTokensForTokens(
big.NewInt(1e17),
)
if err != nil {
return nil, cosmoserrors.Wrapf(err, "failed to CallEVM method swapExactTokensForTokens")
return nil, cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method swapExactTokensForTokens (%s)", err.Error())
}

amounts := new([3]*big.Int)
Expand Down Expand Up @@ -374,7 +389,7 @@ func (k *Keeper) CallUniswapV2RouterSwapExactTokensForETH(
big.NewInt(1e17),
)
if err != nil {
return nil, cosmoserrors.Wrapf(err, "failed to CallEVM method swapExactTokensForETH")
return nil, cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method swapExactTokensForETH (%s)", err.Error())
}

amounts := new([2]*big.Int)
Expand Down Expand Up @@ -426,7 +441,7 @@ func (k *Keeper) CallUniswapV2RouterSwapExactETHForToken(
big.NewInt(1e17),
)
if err != nil {
return nil, cosmoserrors.Wrapf(err, "failed to CallEVM method swapExactETHForTokens")
return nil, cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method swapExactETHForTokens (%s)", err.Error())
}

amounts := new([2]*big.Int)
Expand Down Expand Up @@ -477,7 +492,7 @@ func (k *Keeper) CallUniswapV2RouterSwapEthForExactToken(
big.NewInt(1e17),
)
if err != nil {
return nil, cosmoserrors.Wrapf(err, "failed to CallEVM method swapETHForExactTokens")
return nil, cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method swapETHForExactTokens (%s)", err.Error())
}

amounts := new([2]*big.Int)
Expand Down Expand Up @@ -519,7 +534,7 @@ func (k *Keeper) QueryUniswapV2RouterGetZetaAmountsIn(ctx sdk.Context, amountOut
[]ethcommon.Address{wzetaAddr, outZRC4},
)
if err != nil {
return nil, cosmoserrors.Wrapf(err, "failed to CallEVM method getAmountsIn")
return nil, cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method getAmountsIn (%s)", err.Error())
}

amounts := new([2]*big.Int)
Expand Down Expand Up @@ -560,7 +575,7 @@ func (k *Keeper) QueryUniswapV2RouterGetZRC4AmountsIn(ctx sdk.Context, amountOut
[]ethcommon.Address{inZRC4, wzetaAddr},
)
if err != nil {
return nil, cosmoserrors.Wrapf(err, "failed to CallEVM method getAmountsIn")
return nil, cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method getAmountsIn (%s)", err.Error())
}

amounts := new([2]*big.Int)
Expand Down Expand Up @@ -601,7 +616,7 @@ func (k *Keeper) QueryUniswapV2RouterGetZRC4ToZRC4AmountsIn(ctx sdk.Context, amo
[]ethcommon.Address{inZRC4, wzetaAddr, outZRC4},
)
if err != nil {
return nil, cosmoserrors.Wrapf(err, "failed to CallEVM method getAmountsIn")
return nil, cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method getAmountsIn (%s)", err.Error())
}

amounts := new([3]*big.Int)
Expand Down Expand Up @@ -638,7 +653,7 @@ func (k *Keeper) CallZRC20Burn(
amount,
)
if err != nil {
return cosmoserrors.Wrapf(err, "failed to CallEVM method burn")
return cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method burn (%s)", err.Error())
}

return nil
Expand Down Expand Up @@ -671,7 +686,7 @@ func (k *Keeper) CallZRC20Deposit(
amount,
)
if err != nil {
return cosmoserrors.Wrapf(err, "failed to CallEVM method burn")
return cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method burn (%s)", err.Error())
}
return nil
}
Expand Down Expand Up @@ -704,7 +719,7 @@ func (k *Keeper) CallZRC20Approve(
amount,
)
if err != nil {
return cosmoserrors.Wrapf(err, "failed to CallEVM method approve")
return cosmoserrors.Wrapf(types.ErrContractCall, "failed to CallEVM method approve (%s)", err.Error())
}

return nil
Expand Down
Loading
Loading