From 3535fb2f79e54c4e1e4af7fa7ddefe78c385ccba Mon Sep 17 00:00:00 2001
From: Kelvin Fichter <kelvin@optimism.io>
Date: Sun, 6 Feb 2022 19:39:09 -0500
Subject: [PATCH] feat(itests): remove Watcher from fundUser

This commit removes the deprecated Watcher class from the fundUser
function in favor of using the CrossChainMessenger class from the new
Optimism SDK. This is part of a series of commits that are designed to
eventually remove the Watcher entirely from the integration tests (and
then from the entire repository).
---
 .../test/native-eth-ovm-calls.spec.ts         | 16 ++------
 integration-tests/test/rpc.spec.ts            |  2 +-
 integration-tests/test/shared/env.ts          | 16 ++++----
 integration-tests/test/shared/utils.ts        | 38 ++++++++++---------
 integration-tests/test/stress-tests.spec.ts   |  7 +---
 5 files changed, 33 insertions(+), 46 deletions(-)

diff --git a/integration-tests/test/native-eth-ovm-calls.spec.ts b/integration-tests/test/native-eth-ovm-calls.spec.ts
index e361a784f..ee7794f21 100644
--- a/integration-tests/test/native-eth-ovm-calls.spec.ts
+++ b/integration-tests/test/native-eth-ovm-calls.spec.ts
@@ -38,7 +38,7 @@ describe('Native ETH value integration tests', () => {
     }
 
     const value = ethers.utils.parseEther('0.01')
-    await fundUser(env.watcher, env.l1Bridge, value, wallet.address)
+    await fundUser(env.messenger, value, wallet.address)
 
     const initialBalances = await getBalances()
 
@@ -156,12 +156,7 @@ describe('Native ETH value integration tests', () => {
     beforeEach(async () => {
       ValueCalls0 = await Factory__ValueCalls.deploy()
       ValueCalls1 = await Factory__ValueCalls.deploy()
-      await fundUser(
-        env.watcher,
-        env.l1Bridge,
-        initialBalance0,
-        ValueCalls0.address
-      )
+      await fundUser(env.messenger, initialBalance0, ValueCalls0.address)
       // These tests ass assume ValueCalls0 starts with a balance, but ValueCalls1 does not.
       await checkBalances([initialBalance0, 0])
     })
@@ -203,12 +198,7 @@ describe('Native ETH value integration tests', () => {
     it('should have the correct ovmSELFBALANCE which includes the msg.value', async () => {
       // give an initial balance which the ovmCALLVALUE should be added to when calculating ovmSELFBALANCE
       const initialBalance = 10
-      await fundUser(
-        env.watcher,
-        env.l1Bridge,
-        initialBalance,
-        ValueCalls1.address
-      )
+      await fundUser(env.messenger, initialBalance, ValueCalls1.address)
 
       const sendAmount = 15
       const [success, returndata] = await ValueCalls0.callStatic.sendWithData(
diff --git a/integration-tests/test/rpc.spec.ts b/integration-tests/test/rpc.spec.ts
index 56d46cc83..5f7c4c7ae 100644
--- a/integration-tests/test/rpc.spec.ts
+++ b/integration-tests/test/rpc.spec.ts
@@ -219,7 +219,7 @@ describe('Basic RPC tests', () => {
       // Fund account to call from
       const from = wallet.address
       const value = 15
-      await fundUser(env.watcher, env.l1Bridge, value, from)
+      await fundUser(env.messenger, value, from)
 
       // Do the call and check msg.value
       const data = ValueContext.interface.encodeFunctionData('getCallValue')
diff --git a/integration-tests/test/shared/env.ts b/integration-tests/test/shared/env.ts
index d41b7d19f..e56ff8313 100644
--- a/integration-tests/test/shared/env.ts
+++ b/integration-tests/test/shared/env.ts
@@ -85,6 +85,13 @@ export class OptimismEnv {
   }
 
   static async new(): Promise<OptimismEnv> {
+    const network = await l1Provider.getNetwork()
+    const messenger = new CrossChainMessenger({
+      l1SignerOrProvider: l1Wallet,
+      l2SignerOrProvider: l2Wallet,
+      l1ChainId: network.chainId,
+    })
+
     const addressManager = getAddressManager(l1Wallet)
     const watcher = await initWatcher(l1Provider, l2Provider, addressManager)
     const l1Bridge = await getL1Bridge(l1Wallet, addressManager)
@@ -94,7 +101,7 @@ export class OptimismEnv {
     const min = envConfig.L2_WALLET_MIN_BALANCE_ETH.toString()
     const topUp = envConfig.L2_WALLET_TOP_UP_AMOUNT_ETH.toString()
     if (balance.lt(utils.parseEther(min))) {
-      await fundUser(watcher, l1Bridge, utils.parseEther(topUp))
+      await fundUser(messenger, utils.parseEther(topUp))
     }
     const l1Messenger = getContractFactory('L1CrossDomainMessenger')
       .connect(l1Wallet)
@@ -129,13 +136,6 @@ export class OptimismEnv {
       .connect(l2Wallet)
       .attach(predeploys.OVM_L1BlockNumber)
 
-    const network = await l1Provider.getNetwork()
-    const messenger = new CrossChainMessenger({
-      l1SignerOrProvider: l1Wallet,
-      l2SignerOrProvider: l2Wallet,
-      l1ChainId: network.chainId,
-    })
-
     return new OptimismEnv({
       addressManager,
       l1Bridge,
diff --git a/integration-tests/test/shared/utils.ts b/integration-tests/test/shared/utils.ts
index 086734101..a52d40ff5 100644
--- a/integration-tests/test/shared/utils.ts
+++ b/integration-tests/test/shared/utils.ts
@@ -4,7 +4,6 @@ import {
   Wallet,
   constants,
   providers,
-  BigNumberish,
   BigNumber,
   utils,
 } from 'ethers'
@@ -13,13 +12,13 @@ import {
   getContractInterface,
   predeploys,
 } from '@eth-optimism/contracts'
-import { injectL2Context, remove0x, Watcher } from '@eth-optimism/core-utils'
+import { injectL2Context, remove0x } from '@eth-optimism/core-utils'
+import { CrossChainMessenger, NumberLike } from '@eth-optimism/sdk'
 import { cleanEnv, str, num, bool, makeValidator } from 'envalid'
 import dotenv from 'dotenv'
 dotenv.config()
 
 /* Imports: Internal */
-import { Direction, waitForXDomainTransaction } from './watcher-utils'
 import { OptimismEnv } from './env'
 
 export const isLiveNetwork = () => {
@@ -180,23 +179,26 @@ export const getOvmEth = (wallet: Wallet) => {
 }
 
 export const fundUser = async (
-  watcher: Watcher,
-  bridge: Contract,
-  amount: BigNumberish,
+  messenger: CrossChainMessenger,
+  amount: NumberLike,
   recipient?: string
 ) => {
-  const value = BigNumber.from(amount)
-  const tx = recipient
-    ? bridge.depositETHTo(recipient, DEFAULT_TEST_GAS_L2, '0x', {
-        value,
-        gasLimit: DEFAULT_TEST_GAS_L1,
-      })
-    : bridge.depositETH(DEFAULT_TEST_GAS_L2, '0x', {
-        value,
-        gasLimit: DEFAULT_TEST_GAS_L1,
-      })
-
-  await waitForXDomainTransaction(watcher, tx, Direction.L1ToL2)
+  await messenger.waitForMessageReceipt(
+    await messenger.depositETH(amount, {
+      l2GasLimit: DEFAULT_TEST_GAS_L2,
+      overrides: {
+        gasPrice: DEFAULT_TEST_GAS_L1,
+      },
+    })
+  )
+
+  if (recipient !== undefined) {
+    const tx = await messenger.l2Signer.sendTransaction({
+      to: recipient,
+      value: amount,
+    })
+    await tx.wait()
+  }
 }
 
 export const conditionalTest = (
diff --git a/integration-tests/test/stress-tests.spec.ts b/integration-tests/test/stress-tests.spec.ts
index 948bbf25c..17443c39b 100644
--- a/integration-tests/test/stress-tests.spec.ts
+++ b/integration-tests/test/stress-tests.spec.ts
@@ -47,12 +47,7 @@ describe('stress tests', () => {
     }
 
     for (const wallet of wallets) {
-      await fundUser(
-        env.watcher,
-        env.l1Bridge,
-        utils.parseEther('0.1'),
-        wallet.address
-      )
+      await fundUser(env.messenger, utils.parseEther('0.1'), wallet.address)
     }
   })
 
-- 
2.23.0