L1ERC721Bridge.spec.ts 12.7 KB
Newer Older
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
/* Imports */
import { ethers } from 'hardhat'
import { Signer, ContractFactory, Contract, constants } from 'ethers'
import { Interface } from 'ethers/lib/utils'
import {
  smock,
  MockContractFactory,
  FakeContract,
  MockContract,
} from '@defi-wonderland/smock'
import ICrossDomainMessenger from '@eth-optimism/contracts/artifacts/contracts/libraries/bridge/ICrossDomainMessenger.sol/ICrossDomainMessenger.json'

import { expect } from '../../../setup'
import {
  NON_NULL_BYTES32,
  NON_ZERO_ADDRESS,
} from '../../../../../contracts/test/helpers'

const ERR_INVALID_MESSENGER = 'OVM_XCHAIN: messenger contract unauthenticated'
const ERR_INVALID_X_DOMAIN_MSG_SENDER =
  'OVM_XCHAIN: wrong sender of cross-domain message'
const DUMMY_L2_ERC721_ADDRESS = ethers.utils.getAddress(
  '0x' + 'abba'.repeat(10)
)
const DUMMY_L2_BRIDGE_ADDRESS = ethers.utils.getAddress(
  '0x' + 'acdc'.repeat(10)
)

const FINALIZATION_GAS = 1_200_000

describe('L1ERC721Bridge', () => {
  // init signers
  let l1MessengerImpersonator: Signer
  let alice: Signer
  let bob: Signer
  let bobsAddress
  let aliceAddress
  let tokenId
  let aliceInitialBalance

  // we can just make up this string since it's on the "other" Layer
  let Factory__L1ERC721: MockContractFactory<ContractFactory>
  let IL2ERC721Bridge: Interface
  before(async () => {
    ;[l1MessengerImpersonator, alice, bob] = await ethers.getSigners()

    // deploy an ERC721 contract on L1
    Factory__L1ERC721 = await smock.mock(
      '@openzeppelin/contracts/token/ERC721/ERC721.sol:ERC721'
    )

    // get an L2ERC721Bridge Interface
    IL2ERC721Bridge = (await ethers.getContractFactory('L2ERC721Bridge'))
      .interface

    aliceAddress = await alice.getAddress()
    bobsAddress = await bob.getAddress()
    aliceInitialBalance = 5
    tokenId = 10
  })

  let L1ERC721: MockContract<Contract>
  let L1ERC721Bridge: Contract
  let Fake__L1CrossDomainMessenger: FakeContract
  beforeEach(async () => {
    // Get a new mock L1 messenger
    Fake__L1CrossDomainMessenger = await smock.fake<Contract>(
      new ethers.utils.Interface(ICrossDomainMessenger.abi),
      { address: await l1MessengerImpersonator.getAddress() } // This allows us to use an ethers override {from: Fake__L1CrossDomainMessenger.address} to mock calls
    )

    // Deploy the contract under test
    L1ERC721Bridge = await (
      await ethers.getContractFactory('L1ERC721Bridge')
    ).deploy(Fake__L1CrossDomainMessenger.address, DUMMY_L2_BRIDGE_ADDRESS)

    L1ERC721 = await Factory__L1ERC721.deploy('L1ERC721', 'ERC')

    await L1ERC721.setVariable('_owners', {
      [tokenId]: aliceAddress,
    })
    await L1ERC721.setVariable('_balances', {
      [aliceAddress]: aliceInitialBalance,
    })
  })

  describe('ERC721 deposits', () => {
    beforeEach(async () => {
      await L1ERC721.connect(alice).approve(L1ERC721Bridge.address, tokenId)
    })

92
    it('bridgeERC721() escrows the deposit and sends the correct deposit message', async () => {
93
      // alice calls deposit on the bridge and the L1 bridge calls transferFrom on the token.
94
      // emits an ERC721BridgeInitiated event with the correct arguments.
95
      await expect(
96
        L1ERC721Bridge.connect(alice).bridgeERC721(
97 98 99 100 101 102 103
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          tokenId,
          FINALIZATION_GAS,
          NON_NULL_BYTES32
        )
      )
104
        .to.emit(L1ERC721Bridge, 'ERC721BridgeInitiated')
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        .withArgs(
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          aliceAddress,
          aliceAddress,
          tokenId,
          NON_NULL_BYTES32
        )

      const depositCallToMessenger =
        Fake__L1CrossDomainMessenger.sendMessage.getCall(0)

      // alice's balance decreases by 1
      const depositerBalance = await L1ERC721.balanceOf(aliceAddress)
      expect(depositerBalance).to.equal(aliceInitialBalance - 1)

      // bridge's balance increases by 1
      const bridgeBalance = await L1ERC721.balanceOf(L1ERC721Bridge.address)
      expect(bridgeBalance).to.equal(1)

      // Check the correct cross-chain call was sent:
      // Message should be sent to the L2 bridge
      expect(depositCallToMessenger.args[0]).to.equal(DUMMY_L2_BRIDGE_ADDRESS)
      // Message data should be a call telling the L2DepositedERC721 to finalize the deposit

      // the L1 bridge sends the correct message to the L1 messenger
      expect(depositCallToMessenger.args[1]).to.equal(
132
        IL2ERC721Bridge.encodeFunctionData('finalizeBridgeERC721', [
133
          DUMMY_L2_ERC721_ADDRESS,
134
          L1ERC721.address,
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
          aliceAddress,
          aliceAddress,
          tokenId,
          NON_NULL_BYTES32,
        ])
      )
      expect(depositCallToMessenger.args[2]).to.equal(FINALIZATION_GAS)

      // Updates the deposits mapping
      expect(
        await L1ERC721Bridge.deposits(
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          tokenId
        )
      ).to.equal(true)
    })

153
    it('bridgeERC721To() escrows the deposited NFT and sends the correct deposit message', async () => {
154
      // depositor calls deposit on the bridge and the L1 bridge calls transferFrom on the token.
155
      // emits an ERC721BridgeInitiated event with the correct arguments.
156
      await expect(
157
        L1ERC721Bridge.connect(alice).bridgeERC721To(
158 159 160 161 162 163 164 165
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          bobsAddress,
          tokenId,
          FINALIZATION_GAS,
          NON_NULL_BYTES32
        )
      )
166
        .to.emit(L1ERC721Bridge, 'ERC721BridgeInitiated')
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        .withArgs(
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          aliceAddress,
          bobsAddress,
          tokenId,
          NON_NULL_BYTES32
        )

      const depositCallToMessenger =
        Fake__L1CrossDomainMessenger.sendMessage.getCall(0)

      // alice's balance decreases by 1
      const depositerBalance = await L1ERC721.balanceOf(aliceAddress)
      expect(depositerBalance).to.equal(aliceInitialBalance - 1)

      // bridge's balance is increased
      const bridgeBalance = await L1ERC721.balanceOf(L1ERC721Bridge.address)
      expect(bridgeBalance).to.equal(1)

      // bridge is owner of tokenId
      const tokenIdOwner = await L1ERC721.ownerOf(tokenId)
      expect(tokenIdOwner).to.equal(L1ERC721Bridge.address)

      // Check the correct cross-chain call was sent:
      // Message should be sent to the L2DepositedERC721 on L2
      expect(depositCallToMessenger.args[0]).to.equal(DUMMY_L2_BRIDGE_ADDRESS)
      // Message data should be a call telling the L2DepositedERC721 to finalize the deposit

      // the L1 bridge sends the correct message to the L1 messenger
      expect(depositCallToMessenger.args[1]).to.equal(
198
        IL2ERC721Bridge.encodeFunctionData('finalizeBridgeERC721', [
199
          DUMMY_L2_ERC721_ADDRESS,
200
          L1ERC721.address,
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
          aliceAddress,
          bobsAddress,
          tokenId,
          NON_NULL_BYTES32,
        ])
      )
      expect(depositCallToMessenger.args[2]).to.equal(FINALIZATION_GAS)

      // Updates the deposits mapping
      expect(
        await L1ERC721Bridge.deposits(
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          tokenId
        )
      ).to.equal(true)
    })

219
    it('cannot bridgeERC721 from a contract account', async () => {
220
      await expect(
221
        L1ERC721Bridge.bridgeERC721(
222 223 224 225 226 227
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          tokenId,
          FINALIZATION_GAS,
          NON_NULL_BYTES32
        )
228
      ).to.be.revertedWith('L1ERC721Bridge: account is not externally owned')
229 230 231
    })

    describe('Handling ERC721.transferFrom() failures that revert', () => {
232
      it('bridgeERC721(): will revert if ERC721.transferFrom() reverts', async () => {
233
        await expect(
234
          L1ERC721Bridge.connect(bob).bridgeERC721To(
235 236 237 238 239 240 241 242 243 244
            L1ERC721.address,
            DUMMY_L2_ERC721_ADDRESS,
            bobsAddress,
            tokenId,
            FINALIZATION_GAS,
            NON_NULL_BYTES32
          )
        ).to.be.revertedWith('ERC721: transfer from incorrect owner')
      })

245
      it('bridgeERC721To(): will revert if ERC721.transferFrom() reverts', async () => {
246
        await expect(
247
          L1ERC721Bridge.connect(bob).bridgeERC721To(
248 249 250 251 252 253 254 255 256 257
            L1ERC721.address,
            DUMMY_L2_ERC721_ADDRESS,
            bobsAddress,
            tokenId,
            FINALIZATION_GAS,
            NON_NULL_BYTES32
          )
        ).to.be.revertedWith('ERC721: transfer from incorrect owner')
      })

258
      it('bridgeERC721To(): will revert if the L1 ERC721 is zero address', async () => {
259
        await expect(
260
          L1ERC721Bridge.connect(alice).bridgeERC721To(
261 262 263 264 265 266 267 268 269 270
            constants.AddressZero,
            DUMMY_L2_ERC721_ADDRESS,
            bobsAddress,
            tokenId,
            FINALIZATION_GAS,
            NON_NULL_BYTES32
          )
        ).to.be.revertedWith('function call to a non-contract account')
      })

271
      it('bridgeERC721To(): will revert if the L1 ERC721 has no code', async () => {
272
        await expect(
273
          L1ERC721Bridge.connect(alice).bridgeERC721To(
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
            bobsAddress,
            DUMMY_L2_ERC721_ADDRESS,
            bobsAddress,
            tokenId,
            FINALIZATION_GAS,
            NON_NULL_BYTES32
          )
        ).to.be.revertedWith('function call to a non-contract account')
      })
    })
  })

  describe('ERC721 withdrawals', () => {
    it('onlyFromCrossDomainAccount: should revert on calls from a non-crossDomainMessenger L1 account', async () => {
      await expect(
289
        L1ERC721Bridge.connect(alice).finalizeBridgeERC721(
290 291 292 293 294 295 296 297 298 299 300 301
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          constants.AddressZero,
          constants.AddressZero,
          tokenId,
          NON_NULL_BYTES32
        )
      ).to.be.revertedWith(ERR_INVALID_MESSENGER)
    })

    it('onlyFromCrossDomainAccount: should revert on calls from the right crossDomainMessenger, but wrong xDomainMessageSender (ie. not the L2DepositedERC721)', async () => {
      await expect(
302
        L1ERC721Bridge.finalizeBridgeERC721(
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          constants.AddressZero,
          constants.AddressZero,
          tokenId,
          NON_NULL_BYTES32,
          {
            from: Fake__L1CrossDomainMessenger.address,
          }
        )
      ).to.be.revertedWith(ERR_INVALID_X_DOMAIN_MSG_SENDER)
    })

    describe('withdrawal attempts that pass the onlyFromCrossDomainAccount check', () => {
      beforeEach(async () => {
        // First Alice will send an NFT so that there's a balance to be withdrawn
        await L1ERC721.connect(alice).approve(L1ERC721Bridge.address, tokenId)

321
        await L1ERC721Bridge.connect(alice).bridgeERC721(
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
          L1ERC721.address,
          DUMMY_L2_ERC721_ADDRESS,
          tokenId,
          FINALIZATION_GAS,
          NON_NULL_BYTES32
        )

        // make sure bridge owns NFT
        expect(await L1ERC721.ownerOf(tokenId)).to.equal(L1ERC721Bridge.address)

        Fake__L1CrossDomainMessenger.xDomainMessageSender.returns(
          DUMMY_L2_BRIDGE_ADDRESS
        )
      })

      it('should revert if the l1/l2 token pair has a token ID that has not been escrowed in the l1 bridge', async () => {
        await expect(
339
          L1ERC721Bridge.finalizeBridgeERC721(
340 341 342 343 344 345 346 347 348 349 350 351 352 353
            L1ERC721.address,
            DUMMY_L2_BRIDGE_ADDRESS, // incorrect l2 token address
            constants.AddressZero,
            constants.AddressZero,
            tokenId,
            NON_NULL_BYTES32,
            {
              from: Fake__L1CrossDomainMessenger.address,
            }
          )
        ).to.be.revertedWith('Token ID is not escrowed in the L1 Bridge')
      })

      it('should credit funds to the withdrawer and not use too much gas', async () => {
354
        // finalizing the withdrawal emits an ERC721BridgeFinalized event with the correct arguments.
355
        await expect(
356
          L1ERC721Bridge.finalizeBridgeERC721(
357 358 359 360 361 362 363 364 365
            L1ERC721.address,
            DUMMY_L2_ERC721_ADDRESS,
            NON_ZERO_ADDRESS,
            NON_ZERO_ADDRESS,
            tokenId,
            NON_NULL_BYTES32,
            { from: Fake__L1CrossDomainMessenger.address }
          )
        )
366
          .to.emit(L1ERC721Bridge, 'ERC721BridgeFinalized')
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
          .withArgs(
            L1ERC721.address,
            DUMMY_L2_ERC721_ADDRESS,
            NON_ZERO_ADDRESS,
            NON_ZERO_ADDRESS,
            tokenId,
            NON_NULL_BYTES32
          )

        // NFT is transferred to new owner
        expect(await L1ERC721.ownerOf(tokenId)).to.equal(NON_ZERO_ADDRESS)

        // deposits state variable is updated
        expect(
          await L1ERC721Bridge.deposits(
            L1ERC721.address,
            DUMMY_L2_ERC721_ADDRESS,
            tokenId
          )
        ).to.equal(false)
      })
    })
  })
})