forked from zeta-chain/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_erc20_multiple_deposits.go
82 lines (64 loc) · 2.49 KB
/
test_erc20_multiple_deposits.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
package e2etests
import (
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
"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 TestMultipleERC20Deposit(r *runner.E2ERunner, args []string) {
require.Len(r, args, 2)
depositAmount, ok := big.NewInt(0).SetString(args[0], 10)
require.True(r, ok)
numberOfDeposits, ok := big.NewInt(0).SetString(args[1], 10)
require.True(r, ok)
require.NotZero(r, numberOfDeposits.Int64())
initialBal, err := r.ERC20ZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
require.NoError(r, err)
txhash := multipleDeposits(r, depositAmount, numberOfDeposits)
cctxs := utils.WaitCctxsMinedByInboundHash(
r.Ctx,
txhash.Hex(),
r.CctxClient,
int(numberOfDeposits.Int64()),
r.Logger,
r.CctxTimeout,
)
require.Len(r, cctxs, 3)
// check new balance is increased by amount * count
bal, err := r.ERC20ZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
require.NoError(r, err)
diff := big.NewInt(0).Sub(bal, initialBal)
total := depositAmount.Mul(depositAmount, numberOfDeposits)
require.Equal(r, 0, diff.Cmp(total), "balance difference is not correct")
}
func multipleDeposits(r *runner.E2ERunner, amount, count *big.Int) ethcommon.Hash {
// deploy depositor
depositorAddr, _, depositor, err := testcontract.DeployDepositor(r.EVMAuth, r.EVMClient, r.ERC20CustodyAddr)
require.NoError(r, err)
fullAmount := big.NewInt(0).Mul(amount, count)
// approve
tx, err := r.ERC20.Approve(r.EVMAuth, depositorAddr, fullAmount)
require.NoError(r, err)
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
r.Logger.Info("ERC20 Approve receipt tx hash: %s", tx.Hash().Hex())
// deposit
tx, err = depositor.RunDeposits(r.EVMAuth, r.EVMAddress().Bytes(), r.ERC20Addr, amount, []byte{}, count)
require.NoError(r, err)
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
r.Logger.Info("Deposits receipt tx hash: %s", tx.Hash().Hex())
for _, log := range receipt.Logs {
event, err := r.ERC20Custody.ParseDeposited(*log)
if err != nil {
continue
}
r.Logger.Info("Multiple deposit event: ")
r.Logger.Info(" Amount: %d, ", event.Amount)
}
r.Logger.Info("gas limit %d", r.ZEVMAuth.GasLimit)
return tx.Hash()
}