|
| 1 | +package e2etests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + ethcommon "github.com/ethereum/go-ethereum/common" |
| 7 | + |
| 8 | + "github.com/zeta-chain/zetacore/e2e/contracts/testdapp" |
| 9 | + "github.com/zeta-chain/zetacore/e2e/runner" |
| 10 | + "github.com/zeta-chain/zetacore/e2e/utils" |
| 11 | +) |
| 12 | + |
| 13 | +// deployFunc is a function that deploys a contract |
| 14 | +type deployFunc func(r *runner.E2ERunner) (ethcommon.Address, error) |
| 15 | + |
| 16 | +// deployMap maps contract names to deploy functions |
| 17 | +var deployMap = map[string]deployFunc{ |
| 18 | + "testdapp_zevm": deployZEVMTestDApp, |
| 19 | + "testdapp_evm": deployEVMTestDApp, |
| 20 | +} |
| 21 | + |
| 22 | +// TestDeployContract deploys the specified contract |
| 23 | +func TestDeployContract(r *runner.E2ERunner, args []string) { |
| 24 | + availableContractNames := make([]string, 0, len(deployMap)) |
| 25 | + for contractName := range deployMap { |
| 26 | + availableContractNames = append(availableContractNames, contractName) |
| 27 | + } |
| 28 | + availableContractNamesMessage := fmt.Sprintf("Available contract names: %v", availableContractNames) |
| 29 | + |
| 30 | + if len(args) != 1 { |
| 31 | + panic( |
| 32 | + "TestDeployContract requires exactly one argument for the contract name. " + availableContractNamesMessage, |
| 33 | + ) |
| 34 | + } |
| 35 | + contractName := args[0] |
| 36 | + |
| 37 | + deployFunc, ok := deployMap[contractName] |
| 38 | + if !ok { |
| 39 | + panic(fmt.Sprintf("Unknown contract name: %s, %s", contractName, availableContractNamesMessage)) |
| 40 | + } |
| 41 | + |
| 42 | + addr, err := deployFunc(r) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | + |
| 47 | + r.Logger.Print("%s deployed at %s", contractName, addr.Hex()) |
| 48 | +} |
| 49 | + |
| 50 | +// deployZEVMTestDApp deploys the TestDApp contract on ZetaChain |
| 51 | +func deployZEVMTestDApp(r *runner.E2ERunner) (ethcommon.Address, error) { |
| 52 | + addr, tx, _, err := testdapp.DeployTestDApp( |
| 53 | + r.ZEVMAuth, |
| 54 | + r.ZEVMClient, |
| 55 | + r.ConnectorZEVMAddr, |
| 56 | + r.WZetaAddr, |
| 57 | + ) |
| 58 | + if err != nil { |
| 59 | + return addr, err |
| 60 | + } |
| 61 | + |
| 62 | + // Wait for the transaction to be mined |
| 63 | + receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout) |
| 64 | + if receipt.Status != 1 { |
| 65 | + return addr, fmt.Errorf("contract deployment failed") |
| 66 | + } |
| 67 | + |
| 68 | + return addr, nil |
| 69 | +} |
| 70 | + |
| 71 | +// deployEVMTestDApp deploys the TestDApp contract on Ethereum |
| 72 | +func deployEVMTestDApp(r *runner.E2ERunner) (ethcommon.Address, error) { |
| 73 | + addr, tx, _, err := testdapp.DeployTestDApp( |
| 74 | + r.EVMAuth, |
| 75 | + r.EVMClient, |
| 76 | + r.ConnectorEthAddr, |
| 77 | + r.ZetaEthAddr, |
| 78 | + ) |
| 79 | + if err != nil { |
| 80 | + return addr, err |
| 81 | + } |
| 82 | + |
| 83 | + // Wait for the transaction to be mined |
| 84 | + receipt := utils.MustWaitForTxReceipt(r.Ctx, r.EVMClient, tx, r.Logger, r.ReceiptTimeout) |
| 85 | + if receipt.Status != 1 { |
| 86 | + return addr, fmt.Errorf("contract deployment failed") |
| 87 | + } |
| 88 | + |
| 89 | + return addr, nil |
| 90 | +} |
0 commit comments