-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathtest_erc20_multiple_withdraws.go
89 lines (70 loc) · 2.65 KB
/
test_erc20_multiple_withdraws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package e2etests
import (
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
testcontract "github.com/zeta-chain/zetacore/testutil/contracts"
)
func TestMultipleERC20Withdraws(r *runner.E2ERunner, args []string) {
require.Len(r, args, 2)
approvedAmount := big.NewInt(1e18)
// parse the withdrawal amount and number of withdrawals
withdrawalAmount := parseBigInt(r, args[0])
require.Equal(
r,
-1,
withdrawalAmount.Cmp(approvedAmount),
"Invalid withdrawal amount specified for TestMultipleWithdraws.",
)
numberOfWithdrawals := parseBigInt(r, args[1])
require.NotEmpty(r, numberOfWithdrawals.Int64())
// calculate total withdrawal to ensure it doesn't exceed approved amount.
totalWithdrawal := big.NewInt(0).Mul(withdrawalAmount, numberOfWithdrawals)
require.Equal(r, -1, totalWithdrawal.Cmp(approvedAmount), "Total withdrawal amount exceeds approved limit.")
// deploy withdrawer
withdrawerAddr, _, withdrawer, err := testcontract.DeployWithdrawer(r.ZEVMAuth, r.ZEVMClient)
require.NoError(r, err)
// approve
tx, err := r.ERC20ZRC20.Approve(r.ZEVMAuth, withdrawerAddr, approvedAmount)
require.NoError(r, err)
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
r.Logger.Info("ERC20 ZRC20 approve receipt: status %d", receipt.Status)
// approve gas token
tx, err = r.ETHZRC20.Approve(r.ZEVMAuth, withdrawerAddr, approvedAmount)
require.NoError(r, err)
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
r.Logger.Info("eth zrc20 approve receipt: status %d", receipt.Status)
// check the balance
bal, err := r.ERC20ZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
require.NoError(r, err)
r.Logger.Info("balance of deployer on ERC20 ZRC20: %d", bal)
require.Less(r, totalWithdrawal.Int64(), bal.Int64(), "not enough ERC20 ZRC20 balance!")
// withdraw
tx, err = withdrawer.RunWithdraws(
r.ZEVMAuth,
r.EVMAddress().Bytes(),
r.ERC20ZRC20Addr,
withdrawalAmount,
numberOfWithdrawals,
)
require.NoError(r, err)
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
cctxs := utils.WaitCctxsMinedByInboundHash(
r.Ctx,
tx.Hash().Hex(),
r.CctxClient,
int(numberOfWithdrawals.Int64()),
r.Logger,
r.CctxTimeout,
)
require.Len(r, cctxs, 3)
// verify the withdraw value
for _, cctx := range cctxs {
verifyTransferAmountFromCCTX(r, cctx, withdrawalAmount.Int64())
}
}