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(e2e): fix liquidity cap advanced test #2047

Merged
merged 4 commits into from
Apr 23, 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 @@ -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

Expand Down
105 changes: 0 additions & 105 deletions e2e/e2etests/test_eth_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
130 changes: 130 additions & 0 deletions e2e/e2etests/test_eth_deposit_liquidity_cap.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading