diff --git a/changelog.md b/changelog.md index 61b85985bf..19c08aebc9 100644 --- a/changelog.md +++ b/changelog.md @@ -82,6 +82,7 @@ * [1985](https://github.com/zeta-chain/node/pull/1985) - improve fungible module coverage * [1992](https://github.com/zeta-chain/node/pull/1992) - remove setupKeeper from crosschain module * [2008](https://github.com/zeta-chain/node/pull/2008) - add test for connector bytecode update +* [2047](https://github.com/zeta-chain/node/pull/2047) - fix liquidity cap advanced test ### Fixes diff --git a/e2e/e2etests/test_eth_deposit.go b/e2e/e2etests/test_eth_deposit.go index 65a79f9c15..472a344423 100644 --- a/e2e/e2etests/test_eth_deposit.go +++ b/e2e/e2etests/test_eth_deposit.go @@ -6,8 +6,6 @@ import ( "math/big" "strings" - "cosmossdk.io/math" - "github.com/ethereum/go-ethereum/accounts/abi/bind" ethcommon "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -16,7 +14,6 @@ import ( "github.com/zeta-chain/zetacore/e2e/utils" testcontract "github.com/zeta-chain/zetacore/testutil/contracts" "github.com/zeta-chain/zetacore/x/crosschain/types" - fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types" ) // TestEtherDeposit tests deposit of ethers @@ -254,105 +251,3 @@ func TestDepositAndCallRefund(r *runner.E2ERunner, args []string) { r.Logger.Info(" block num: %d", receipt.BlockNumber) }() } - -// TestDepositEtherLiquidityCap tests depositing Ethers in a context where a liquidity cap is set -func TestDepositEtherLiquidityCap(r *runner.E2ERunner, args []string) { - if len(args) != 1 { - panic("TestDepositEtherLiquidityCap requires exactly one argument for the liquidity cap.") - } - - liquidityCapArg := math.NewUintFromString(args[0]) - supply, err := r.ETHZRC20.TotalSupply(&bind.CallOpts{}) - if err != nil { - panic(err) - } - - liquidityCap := math.NewUintFromBigInt(supply).Add(liquidityCapArg) - amountLessThanCap := liquidityCap.BigInt().Div(liquidityCap.BigInt(), big.NewInt(10)) // 1/10 of the cap - amountMoreThanCap := liquidityCap.BigInt().Mul(liquidityCap.BigInt(), big.NewInt(10)) // 10 times the cap - msg := fungibletypes.NewMsgUpdateZRC20LiquidityCap( - r.ZetaTxServer.GetAccountAddress(0), - r.ETHZRC20Addr.Hex(), - liquidityCap, - ) - res, err := r.ZetaTxServer.BroadcastTx(utils.FungibleAdminName, msg) - if err != nil { - panic(err) - } - r.Logger.Info("set liquidity cap tx hash: %s", res.TxHash) - - r.Logger.Info("Depositing more than liquidity cap should make cctx reverted") - signedTx, err := r.SendEther(r.TSSAddress, amountMoreThanCap, nil) - if err != nil { - panic(err) - } - receipt := utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, signedTx, r.Logger, r.ReceiptTimeout) - if receipt.Status == 0 { - panic("deposit eth tx failed") - } - cctx := utils.WaitCctxMinedByInTxHash(r.Ctx, signedTx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout) - if cctx.CctxStatus.Status != types.CctxStatus_Reverted { - panic(fmt.Sprintf("expected cctx status to be Reverted; got %s", cctx.CctxStatus.Status)) - } - r.Logger.Info("CCTX has been reverted") - - r.Logger.Info("Depositing less than liquidity cap should still succeed") - initialBal, err := r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress) - if err != nil { - panic(err) - } - signedTx, err = r.SendEther(r.TSSAddress, amountLessThanCap, nil) - if err != nil { - panic(err) - } - receipt = utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, signedTx, r.Logger, r.ReceiptTimeout) - if receipt.Status == 0 { - panic("deposit eth tx failed") - } - utils.WaitCctxMinedByInTxHash(r.Ctx, signedTx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout) - expectedBalance := big.NewInt(0).Add(initialBal, amountLessThanCap) - - bal, err := r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress) - if err != nil { - panic(err) - } - if bal.Cmp(expectedBalance) != 0 { - panic(fmt.Sprintf("expected balance to be %s; got %s", expectedBalance.String(), bal.String())) - } - r.Logger.Info("Deposit succeeded") - - r.Logger.Info("Removing the liquidity cap") - msg = fungibletypes.NewMsgUpdateZRC20LiquidityCap( - r.ZetaTxServer.GetAccountAddress(0), - r.ETHZRC20Addr.Hex(), - math.ZeroUint(), - ) - res, err = r.ZetaTxServer.BroadcastTx(utils.FungibleAdminName, msg) - if err != nil { - panic(err) - } - r.Logger.Info("remove liquidity cap tx hash: %s", res.TxHash) - initialBal, err = r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress) - if err != nil { - panic(err) - } - signedTx, err = r.SendEther(r.TSSAddress, amountMoreThanCap, nil) - if err != nil { - panic(err) - } - receipt = utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, signedTx, r.Logger, r.ReceiptTimeout) - if receipt.Status == 0 { - panic("deposit eth tx failed") - } - utils.WaitCctxMinedByInTxHash(r.Ctx, signedTx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout) - expectedBalance = big.NewInt(0).Add(initialBal, amountMoreThanCap) - - bal, err = r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress) - if err != nil { - panic(err) - } - if bal.Cmp(expectedBalance) != 0 { - panic(fmt.Sprintf("expected balance to be %s; got %s", expectedBalance.String(), bal.String())) - } - r.Logger.Info("New deposit succeeded") -} diff --git a/e2e/e2etests/test_eth_deposit_liquidity_cap.go b/e2e/e2etests/test_eth_deposit_liquidity_cap.go new file mode 100644 index 0000000000..47c1000c73 --- /dev/null +++ b/e2e/e2etests/test_eth_deposit_liquidity_cap.go @@ -0,0 +1,130 @@ +package e2etests + +import ( + "fmt" + "math/big" + + "cosmossdk.io/math" + + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/zeta-chain/zetacore/e2e/runner" + "github.com/zeta-chain/zetacore/e2e/utils" + "github.com/zeta-chain/zetacore/x/crosschain/types" + fungibletypes "github.com/zeta-chain/zetacore/x/fungible/types" +) + +// TestDepositEtherLiquidityCap tests depositing Ethers in a context where a liquidity cap is set +func TestDepositEtherLiquidityCap(r *runner.E2ERunner, args []string) { + if len(args) != 1 { + panic("TestDepositEtherLiquidityCap requires exactly one argument for the liquidity cap.") + } + + liquidityCapArg := math.NewUintFromString(args[0]) + supply, err := r.ETHZRC20.TotalSupply(&bind.CallOpts{}) + if err != nil { + panic(err) + } + + liquidityCap := math.NewUintFromBigInt(supply).Add(liquidityCapArg) + amountLessThanCap := liquidityCapArg.BigInt().Div(liquidityCapArg.BigInt(), big.NewInt(10)) // 1/10 of the cap + amountMoreThanCap := liquidityCapArg.BigInt().Mul(liquidityCapArg.BigInt(), big.NewInt(10)) // 10 times the cap + msg := fungibletypes.NewMsgUpdateZRC20LiquidityCap( + r.ZetaTxServer.GetAccountAddress(0), + r.ETHZRC20Addr.Hex(), + liquidityCap, + ) + res, err := r.ZetaTxServer.BroadcastTx(utils.FungibleAdminName, msg) + if err != nil { + panic(err) + } + r.Logger.Info("set liquidity cap tx hash: %s", res.TxHash) + + r.Logger.Info("Depositing more than liquidity cap should make cctx reverted") + signedTx, err := r.SendEther(r.TSSAddress, amountMoreThanCap, nil) + if err != nil { + panic(err) + } + receipt := utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, signedTx, r.Logger, r.ReceiptTimeout) + if receipt.Status == 0 { + panic("deposit eth tx failed") + } + cctx := utils.WaitCctxMinedByInTxHash(r.Ctx, signedTx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout) + if cctx.CctxStatus.Status != types.CctxStatus_Reverted { + panic(fmt.Sprintf("expected cctx status to be Reverted; got %s", cctx.CctxStatus.Status)) + } + r.Logger.Info("CCTX has been reverted") + + r.Logger.Info("Depositing less than liquidity cap should still succeed") + initialBal, err := r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress) + if err != nil { + panic(err) + } + signedTx, err = r.SendEther(r.TSSAddress, amountLessThanCap, nil) + if err != nil { + panic(err) + } + receipt = utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, signedTx, r.Logger, r.ReceiptTimeout) + if receipt.Status == 0 { + panic("deposit eth tx failed") + } + cctx = utils.WaitCctxMinedByInTxHash(r.Ctx, signedTx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout) + if cctx.CctxStatus.Status != types.CctxStatus_OutboundMined { + + panic(fmt.Sprintf( + "expected cctx status to be Success; got %s; message: %s; supply: %s; liquidity cap: %s, amountLessThanCap: %s, amountMoreThanCap: %s", + cctx.CctxStatus.Status, + cctx.CctxStatus.StatusMessage, + supply.String(), + liquidityCap.String(), + amountLessThanCap.String(), + amountMoreThanCap.String(), + )) + } + + expectedBalance := big.NewInt(0).Add(initialBal, amountLessThanCap) + + bal, err := r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress) + if err != nil { + panic(err) + } + if bal.Cmp(expectedBalance) != 0 { + + panic(fmt.Sprintf("expected balance to be %s; got %s", expectedBalance.String(), bal.String())) + } + r.Logger.Info("Deposit succeeded") + + r.Logger.Info("Removing the liquidity cap") + msg = fungibletypes.NewMsgUpdateZRC20LiquidityCap( + r.ZetaTxServer.GetAccountAddress(0), + r.ETHZRC20Addr.Hex(), + math.ZeroUint(), + ) + res, err = r.ZetaTxServer.BroadcastTx(utils.FungibleAdminName, msg) + if err != nil { + panic(err) + } + r.Logger.Info("remove liquidity cap tx hash: %s", res.TxHash) + initialBal, err = r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress) + if err != nil { + panic(err) + } + signedTx, err = r.SendEther(r.TSSAddress, amountMoreThanCap, nil) + if err != nil { + panic(err) + } + receipt = utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, signedTx, r.Logger, r.ReceiptTimeout) + if receipt.Status == 0 { + panic("deposit eth tx failed") + } + utils.WaitCctxMinedByInTxHash(r.Ctx, signedTx.Hash().Hex(), r.CctxClient, r.Logger, r.CctxTimeout) + expectedBalance = big.NewInt(0).Add(initialBal, amountMoreThanCap) + + bal, err = r.ETHZRC20.BalanceOf(&bind.CallOpts{}, r.DeployerAddress) + if err != nil { + panic(err) + } + if bal.Cmp(expectedBalance) != 0 { + panic(fmt.Sprintf("expected balance to be %s; got %s", expectedBalance.String(), bal.String())) + } + r.Logger.Info("New deposit succeeded") +}