forked from zeta-chain/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_message_passing_zevm_to_evm.go
116 lines (93 loc) · 4.51 KB
/
test_message_passing_zevm_to_evm.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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/contracts/testdapp"
"github.com/zeta-chain/zetacore/e2e/runner"
"github.com/zeta-chain/zetacore/e2e/utils"
cctxtypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)
func TestMessagePassingZEVMtoEVM(r *runner.E2ERunner, args []string) {
require.Len(r, args, 1)
amount, ok := big.NewInt(0).SetString(args[0], 10)
require.True(r, ok, "Invalid amount specified for TestMessagePassingZEVMtoEVM.")
// Set destination details
EVMChainID, err := r.EVMClient.ChainID(r.Ctx)
require.NoError(r, err)
destinationAddress := r.EvmTestDAppAddr
// Contract call originates from ZEVM chain
r.ZEVMAuth.Value = amount
tx, err := r.WZeta.Deposit(r.ZEVMAuth)
require.NoError(r, err)
r.ZEVMAuth.Value = big.NewInt(0)
r.Logger.Info("wzeta deposit tx hash: %s", tx.Hash().Hex())
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
r.Logger.EVMReceipt(*receipt, "wzeta deposit")
utils.RequireTxSuccessful(r, receipt)
tx, err = r.WZeta.Approve(r.ZEVMAuth, r.ZevmTestDAppAddr, amount)
require.NoError(r, err)
r.Logger.Info("wzeta approve tx hash: %s", tx.Hash().Hex())
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
r.Logger.EVMReceipt(*receipt, "wzeta approve")
utils.RequireTxSuccessful(r, receipt)
testDAppZEVM, err := testdapp.NewTestDApp(r.ZevmTestDAppAddr, r.ZEVMClient)
require.NoError(r, err)
// Get previous balances
previousBalanceEVM, err := r.ZetaEth.BalanceOf(&bind.CallOpts{}, r.EvmTestDAppAddr)
require.NoError(r, err)
previousBalanceZEVM, err := r.WZeta.BalanceOf(&bind.CallOpts{}, r.ZEVMAuth.From)
require.NoError(r, err)
// Call the SendHelloWorld function on the ZEVM dapp Contract which would in turn create a new send, to be picked up by the zetanode evm hooks
// set Do revert to false which adds a message to signal the EVM zetaReceiver to not revert the transaction
tx, err = testDAppZEVM.SendHelloWorld(r.ZEVMAuth, destinationAddress, EVMChainID, amount, false)
require.NoError(r, err)
r.Logger.Info("TestDApp.SendHello tx hash: %s", tx.Hash().Hex())
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
// Transaction is picked up by the zetanode evm hooks and a new contract call is initiated on the EVM chain
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, receipt.TxHash.String(), r.CctxClient, r.Logger, r.CctxTimeout)
utils.RequireCCTXStatus(r, cctx, cctxtypes.CctxStatus_OutboundMined)
// On finalization the Tss calls the onReceive function which in turn calls the onZetaMessage function on the destination contract.
receipt, err = r.EVMClient.TransactionReceipt(r.Ctx, ethcommon.HexToHash(cctx.GetCurrentOutboundParam().Hash))
require.NoError(r, err)
utils.RequireTxSuccessful(r, receipt)
testDAppEVM, err := testdapp.NewTestDApp(r.EvmTestDAppAddr, r.EVMClient)
require.NoError(r, err)
receivedHelloWorldEvent := false
for _, log := range receipt.Logs {
_, err := testDAppEVM.ParseHelloWorldEvent(*log)
if err == nil {
r.Logger.Info("Received HelloWorld event:")
receivedHelloWorldEvent = true
}
}
require.True(r, receivedHelloWorldEvent, "expected HelloWorld event")
// Check ZETA balance on EVM TestDApp and check new balance between previous balance and previous balance + amount
// Contract receive less than the amount because of the gas fee to pay
newBalanceEVM, err := r.ZetaEth.BalanceOf(&bind.CallOpts{}, r.EvmTestDAppAddr)
require.NoError(r, err)
previousBalanceAndAmountEVM := big.NewInt(0).Add(previousBalanceEVM, amount)
// check higher than previous balance and lower than previous balance + amount
invariant := newBalanceEVM.Cmp(previousBalanceEVM) <= 0 || newBalanceEVM.Cmp(previousBalanceAndAmountEVM) > 0
require.False(
r,
invariant,
"expected new balance to be between %s and %s, got %s",
previousBalanceEVM.String(),
previousBalanceAndAmountEVM.String(),
newBalanceEVM.String(),
)
// Check ZETA balance on ZEVM TestDApp and check new balance is previous balance - amount
newBalanceZEVM, err := r.WZeta.BalanceOf(&bind.CallOpts{}, r.ZEVMAuth.From)
require.NoError(r, err)
require.Equal(
r,
0,
newBalanceZEVM.Cmp(big.NewInt(0).Sub(previousBalanceZEVM, amount)),
"expected new balance to be %s, got %s",
big.NewInt(0).Sub(previousBalanceZEVM, amount).String(),
newBalanceZEVM.String(),
)
}