|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.15; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | + |
| 6 | +import {Unauthorized} from "@contracts-bedrock/libraries/errors/CommonErrors.sol"; |
| 7 | +import {Predeploys} from "@contracts-bedrock/libraries/Predeploys.sol"; |
| 8 | +import {IL2ToL2CrossDomainMessenger} from "@contracts-bedrock/L2/interfaces/IL2ToL2CrossDomainMessenger.sol"; |
| 9 | +import {ISuperchainTokenBridge} from "@contracts-bedrock/L2/interfaces/ISuperchainTokenBridge.sol"; |
| 10 | +import {ISuperchainWETH} from "@contracts-bedrock/L2/interfaces/ISuperchainWETH.sol"; |
| 11 | +import {IWETH} from "@contracts-bedrock/universal/interfaces/IWETH.sol"; |
| 12 | +import {SuperchainWETH} from "@contracts-bedrock/L2/SuperchainWETH.sol"; |
| 13 | + |
| 14 | +import {SuperchainETHWrapper, RelaySuperchainWETHNotSuccessful} from "../src/SuperchainETHWrapper.sol"; |
| 15 | + |
| 16 | +/// @title SuperchainETHWrapper Happy Path Tests |
| 17 | +/// @notice This contract contains the tests for successful paths in SuperchainETHWrapper. |
| 18 | +contract SuperchainETHWrapper_HappyPath_Test is Test { |
| 19 | + SuperchainETHWrapper public superchainETHWrapper; |
| 20 | + address bob; |
| 21 | + |
| 22 | + /// @notice Helper function to setup a mock and expect a call to it. |
| 23 | + function _mockAndExpect(address _receiver, bytes memory _calldata, bytes memory _returned) internal { |
| 24 | + vm.mockCall(_receiver, _calldata, _returned); |
| 25 | + vm.expectCall(_receiver, _calldata); |
| 26 | + } |
| 27 | + |
| 28 | + /// @notice Helper function to setup a mock and expect a call to it. |
| 29 | + function _mockAndExpect(address _receiver, uint256 _msgValue, bytes memory _calldata, bytes memory _returned) |
| 30 | + internal |
| 31 | + { |
| 32 | + vm.mockCall(_receiver, _msgValue, _calldata, _returned); |
| 33 | + vm.expectCall(_receiver, _msgValue, _calldata); |
| 34 | + } |
| 35 | + |
| 36 | + /// @notice Sets up the test suite. |
| 37 | + function setUp() public { |
| 38 | + superchainETHWrapper = new SuperchainETHWrapper(); |
| 39 | + SuperchainWETH superchainWETH = new SuperchainWETH(); |
| 40 | + vm.etch(Predeploys.SUPERCHAIN_WETH, address(superchainWETH).code); |
| 41 | + bob = makeAddr("bob"); |
| 42 | + } |
| 43 | + |
| 44 | + /// @notice Tests the `sendETH` function deposits the sender's tokens, calls |
| 45 | + /// SuperchainWETH.sendERC20, and sends an encoded call to |
| 46 | + /// SuperchainETHWrapper.unwrapAndCallAndCall through L2ToL2CrossDomainMessenger. |
| 47 | + function testFuzz_sendETH_succeeds( |
| 48 | + address _sender, |
| 49 | + address _to, |
| 50 | + uint256 _amount, |
| 51 | + uint256 _chainId, |
| 52 | + bytes32 messageHash, |
| 53 | + bytes memory _calldata |
| 54 | + ) public { |
| 55 | + vm.assume(_chainId != block.chainid); |
| 56 | + _amount = bound(_amount, 0, type(uint248).max - 1); |
| 57 | + vm.deal(_sender, _amount); |
| 58 | + _mockAndExpect( |
| 59 | + Predeploys.SUPERCHAIN_WETH, _amount, abi.encodeWithSelector(IWETH.deposit.selector), abi.encode("") |
| 60 | + ); |
| 61 | + _mockAndExpect( |
| 62 | + Predeploys.SUPERCHAIN_TOKEN_BRIDGE, |
| 63 | + abi.encodeCall( |
| 64 | + ISuperchainTokenBridge.sendERC20, |
| 65 | + (Predeploys.SUPERCHAIN_WETH, address(superchainETHWrapper), _amount, _chainId) |
| 66 | + ), |
| 67 | + abi.encode(messageHash) |
| 68 | + ); |
| 69 | + bytes memory _message = |
| 70 | + abi.encodeCall(superchainETHWrapper.unwrapAndCall, (messageHash, _to, _amount, _calldata)); |
| 71 | + _mockAndExpect( |
| 72 | + Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER, |
| 73 | + abi.encodeWithSelector( |
| 74 | + IL2ToL2CrossDomainMessenger.sendMessage.selector, _chainId, address(superchainETHWrapper), _message |
| 75 | + ), |
| 76 | + abi.encode("") |
| 77 | + ); |
| 78 | + |
| 79 | + vm.prank(_sender); |
| 80 | + superchainETHWrapper.sendETH{value: _amount}(_to, _chainId, _calldata); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @notice Tests the successful execution of the `unwrapAndCall` function. |
| 85 | + * @dev This test mocks the `crossDomainMessageSender` and `successfulMessages` function calls |
| 86 | + * to simulate the proper cross-domain message behavior. |
| 87 | + * @param _amount Amount of ETH to be unwrapped and sent. |
| 88 | + * @param _relayERC20MsgHash Hash of the relayed message. |
| 89 | + */ |
| 90 | + function testFuzz_unwrapAndCall_succeeds(uint256 _amount, bytes32 _relayERC20MsgHash, bytes memory _calldata) |
| 91 | + public |
| 92 | + { |
| 93 | + _amount = bound(_amount, 0, type(uint248).max - 1); |
| 94 | + |
| 95 | + _mockAndExpect( |
| 96 | + Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER, |
| 97 | + abi.encodeWithSelector(IL2ToL2CrossDomainMessenger.crossDomainMessageSender.selector), |
| 98 | + abi.encode(address(superchainETHWrapper)) |
| 99 | + ); |
| 100 | + _mockAndExpect( |
| 101 | + Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER, |
| 102 | + abi.encodeCall(IL2ToL2CrossDomainMessenger.successfulMessages, (_relayERC20MsgHash)), |
| 103 | + abi.encode(true) |
| 104 | + ); |
| 105 | + _mockAndExpect( |
| 106 | + Predeploys.SUPERCHAIN_WETH, |
| 107 | + abi.encodeCall(ISuperchainWETH(Predeploys.SUPERCHAIN_WETH).withdraw, (_amount)), |
| 108 | + abi.encode("") |
| 109 | + ); |
| 110 | + // Simulates the withdrawal being sent to the SuperchainETHWrapper contract. |
| 111 | + vm.deal(address(superchainETHWrapper), _amount); |
| 112 | + |
| 113 | + uint256 prevBalance = bob.balance; |
| 114 | + vm.expectCall(bob, _amount, _calldata); |
| 115 | + vm.prank(Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER); |
| 116 | + superchainETHWrapper.unwrapAndCall(_relayERC20MsgHash, bob, _amount, _calldata); |
| 117 | + assertEq(bob.balance - prevBalance, _amount); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/// @title SuperchainETHWrapper Revert Tests |
| 122 | +/// @notice This contract contains tests to check that certain conditions result in expected |
| 123 | +/// reverts. |
| 124 | +contract SuperchainETHWrapperRevertTests is Test { |
| 125 | + SuperchainETHWrapper public superchainETHWrapper; |
| 126 | + |
| 127 | + /// @notice Helper function to setup a mock and expect a call to it. |
| 128 | + function _mockAndExpect(address _receiver, bytes memory _calldata, bytes memory _returned) internal { |
| 129 | + vm.mockCall(_receiver, _calldata, _returned); |
| 130 | + vm.expectCall(_receiver, _calldata); |
| 131 | + } |
| 132 | + |
| 133 | + /// @notice Sets up the test suite. |
| 134 | + function setUp() public { |
| 135 | + superchainETHWrapper = new SuperchainETHWrapper(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @notice Tests that the `unwrap` function reverts when the message is unrelayed. |
| 140 | + * @dev Mocks the cross-domain message sender and sets `successfulMessages` to return `false`, |
| 141 | + * triggering a revert when trying to call `unwrap`. |
| 142 | + * @param _to Address receiving the unwrapped ETH. |
| 143 | + * @param _amount Amount of ETH to be unwrapped. |
| 144 | + * @param _relayERC20MsgHash Hash of the relayed message. |
| 145 | + */ |
| 146 | + function testFuzz_unwrap_fromUnrelayedMsgHash_reverts( |
| 147 | + address _to, |
| 148 | + uint256 _amount, |
| 149 | + bytes32 _relayERC20MsgHash, |
| 150 | + bytes memory _calldata |
| 151 | + ) public { |
| 152 | + _mockAndExpect( |
| 153 | + Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER, |
| 154 | + abi.encodeWithSelector(IL2ToL2CrossDomainMessenger.crossDomainMessageSender.selector), |
| 155 | + abi.encode(address(superchainETHWrapper)) |
| 156 | + ); |
| 157 | + _mockAndExpect( |
| 158 | + Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER, |
| 159 | + abi.encodeCall(IL2ToL2CrossDomainMessenger.successfulMessages, (_relayERC20MsgHash)), |
| 160 | + abi.encode(false) |
| 161 | + ); |
| 162 | + |
| 163 | + vm.prank(Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER); |
| 164 | + vm.expectRevert(RelaySuperchainWETHNotSuccessful.selector); |
| 165 | + superchainETHWrapper.unwrapAndCall(_relayERC20MsgHash, _to, _amount, _calldata); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @notice Tests that the `unwrap` function reverts when the sender is not the expected messenger. |
| 170 | + * @dev Mocks an invalid sender (not the messenger) to ensure the function reverts with the |
| 171 | + * `Unauthorized` error. |
| 172 | + * @param _sender Address that tries to call `unwrap` but is not the messenger. |
| 173 | + * @param _to Address receiving the unwrapped ETH. |
| 174 | + * @param _amount Amount of ETH to be unwrapped. |
| 175 | + * @param _relayERC20MsgHash Hash of the relayed message. |
| 176 | + */ |
| 177 | + function testFuzz_unwrap_nonMessengerSender_reverts( |
| 178 | + address _sender, |
| 179 | + address _to, |
| 180 | + uint256 _amount, |
| 181 | + bytes32 _relayERC20MsgHash, |
| 182 | + bytes memory _calldata |
| 183 | + ) public { |
| 184 | + vm.assume(_sender != Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER); |
| 185 | + |
| 186 | + vm.prank(_sender); |
| 187 | + vm.expectRevert(Unauthorized.selector); |
| 188 | + superchainETHWrapper.unwrapAndCall(_relayERC20MsgHash, _to, _amount, _calldata); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @notice Tests that the `unwrap` function reverts when the cross-domain message sender is |
| 193 | + * not the SuperchainETHWrapper contract. |
| 194 | + * @dev Mocks a wrong cross-domain message sender and ensures the function reverts with the |
| 195 | + * `Unauthorized` error. |
| 196 | + * @param _sender Address that tries to call `unwrap` but is not the correct message sender. |
| 197 | + * @param _to Address receiving the unwrapped ETH. |
| 198 | + * @param _amount Amount of ETH to be unwrapped. |
| 199 | + * @param _relayERC20MsgHash Hash of the relayed message. |
| 200 | + */ |
| 201 | + function testFuzz_unwrap_wrongCrossDomainMessageSender_reverts( |
| 202 | + address _sender, |
| 203 | + address _to, |
| 204 | + uint256 _amount, |
| 205 | + bytes32 _relayERC20MsgHash, |
| 206 | + bytes memory _calldata |
| 207 | + ) public { |
| 208 | + vm.assume(_sender != address(superchainETHWrapper)); |
| 209 | + |
| 210 | + _mockAndExpect( |
| 211 | + Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER, |
| 212 | + abi.encodeWithSelector(IL2ToL2CrossDomainMessenger.crossDomainMessageSender.selector), |
| 213 | + abi.encode(_sender) |
| 214 | + ); |
| 215 | + |
| 216 | + vm.prank(Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER); |
| 217 | + vm.expectRevert(Unauthorized.selector); |
| 218 | + superchainETHWrapper.unwrapAndCall(_relayERC20MsgHash, _to, _amount, _calldata); |
| 219 | + } |
| 220 | +} |
0 commit comments