-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathtest_message_passing_evm_to_zevm.go
89 lines (69 loc) · 3.58 KB
/
test_message_passing_evm_to_zevm.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 (
"fmt"
"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 TestMessagePassingEVMtoZEVM(r *runner.E2ERunner, args []string) {
require.Len(r, args, 1)
// parse the amount
amount := parseBigInt(r, args[0])
// Set destination details
zEVMChainID, err := r.ZEVMClient.ChainID(r.Ctx)
require.NoError(r, err)
destinationAddress := r.ZevmTestDAppAddr
// Contract call originates from EVM chain
tx, err := r.ZetaEth.Approve(r.EVMAuth, r.EvmTestDAppAddr, amount)
require.NoError(r, err)
r.Logger.Info("Approve tx hash: %s", tx.Hash().Hex())
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
r.Logger.Info("Approve tx receipt: %d", receipt.Status)
testDAppEVM, err := testdapp.NewTestDApp(r.EvmTestDAppAddr, r.EVMClient)
require.NoError(r, err)
// Get ZETA balance on ZEVM TestDApp
previousBalanceZEVM, err := r.WZeta.BalanceOf(&bind.CallOpts{}, r.ZevmTestDAppAddr)
require.NoError(r, err)
previousBalanceEVM, err := r.ZetaEth.BalanceOf(&bind.CallOpts{}, r.EVMAuth.From)
require.NoError(r, err)
// Call the SendHelloWorld function on the EVM dapp Contract which would in turn create a new send, to be picked up by the zeta-clients
// set Do revert to false which adds a message to signal the ZEVM zetaReceiver to not revert the transaction
tx, err = testDAppEVM.SendHelloWorld(r.EVMAuth, destinationAddress, zEVMChainID, amount, false)
require.NoError(r, err)
r.Logger.Info("TestDApp.SendHello tx hash: %s", tx.Hash().Hex())
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout)
// New inbound message picked up by zeta-clients and voted on by observers to initiate a contract call on zEVM
cctx := utils.WaitCctxMinedByInboundHash(r.Ctx, receipt.TxHash.String(), r.CctxClient, r.Logger, r.CctxTimeout)
utils.RequireCCTXStatus(r, cctx, cctxtypes.CctxStatus_OutboundMined)
r.Logger.Info(fmt.Sprintf("🔄 Cctx mined for contract call chain zevm %s", cctx.Index))
// On finalization the Fungible module calls the onReceive function which in turn calls the onZetaMessage function on the destination contract
receipt, err = r.ZEVMClient.TransactionReceipt(r.Ctx, ethcommon.HexToHash(cctx.GetCurrentOutboundParam().Hash))
require.NoError(r, err)
utils.RequireTxSuccessful(r, receipt)
testDAppZEVM, err := testdapp.NewTestDApp(r.ZevmTestDAppAddr, r.ZEVMClient)
require.NoError(r, err)
// Check event emitted
receivedHelloWorldEvent := false
for _, log := range receipt.Logs {
_, err := testDAppZEVM.ParseHelloWorldEvent(*log)
if err == nil {
r.Logger.Info("Received HelloWorld event")
receivedHelloWorldEvent = true
}
}
require.True(r, receivedHelloWorldEvent, "expected HelloWorld event")
// Check ZETA balance on ZEVM TestDApp and check new balance is previous balance + amount
newBalanceZEVM, err := r.WZeta.BalanceOf(&bind.CallOpts{}, r.ZevmTestDAppAddr)
require.NoError(r, err)
require.Equal(r, 0, newBalanceZEVM.Cmp(big.NewInt(0).Add(previousBalanceZEVM, amount)))
// Check ZETA balance on EVM TestDApp and check new balance is previous balance - amount
newBalanceEVM, err := r.ZetaEth.BalanceOf(&bind.CallOpts{}, r.EVMAuth.From)
require.NoError(r, err)
require.Equal(r, 0, newBalanceEVM.Cmp(big.NewInt(0).Sub(previousBalanceEVM, amount)))
}