-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: SuperchainWETHWrapper contract
- Loading branch information
1 parent
dfdc930
commit 2177e9d
Showing
6 changed files
with
127 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.15; | ||
|
||
import {Unauthorized} from "@contracts-bedrock/libraries/errors/CommonErrors.sol"; | ||
import {Predeploys} from "@contracts-bedrock/libraries/Predeploys.sol"; | ||
import {SafeCall} from "@contracts-bedrock//libraries/SafeCall.sol"; | ||
import {IL2ToL2CrossDomainMessenger} from "@contracts-bedrock/L2/interfaces/IL2ToL2CrossDomainMessenger.sol"; | ||
import {ISuperchainERC20Extensions} from "@contracts-bedrock/L2/interfaces/ISuperchainERC20.sol"; | ||
import {IWETH} from "@contracts-bedrock/universal/interfaces/IWETH.sol"; | ||
|
||
interface IL2ToL2CrossDomainMessengerMissing { | ||
function successfulMessages(bytes32) external view returns (bool); | ||
function messageNonce() external view returns (uint256); | ||
} | ||
|
||
/** | ||
* @title SuperchainETHWrapper | ||
* @notice This contract implements a wrapping/unwrapping mechanism for | ||
* native ether using a combination of the SuperchainWETH contract | ||
* and the L2ToL2CrossDomainMessenger. | ||
* @dev This contract relies on the L2ToL2CrossDomainMessenger for cross-chain communication. | ||
*/ | ||
contract SuperchainETHWrapper { | ||
event LogReceived(address from, uint256 value); | ||
|
||
// Fallback function to receive ETH | ||
receive() external payable { | ||
emit LogReceived(msg.sender, msg.value); | ||
} | ||
|
||
function unwrapAndCall(bytes32 relayERC20MsgHash, address dst, uint256 wad, bytes memory _calldata) external { | ||
// Receive message from other chain. | ||
IL2ToL2CrossDomainMessenger messenger = IL2ToL2CrossDomainMessenger(Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER); | ||
if (msg.sender != address(messenger)) revert Unauthorized(); | ||
if (messenger.crossDomainMessageSender() != address(this)) revert Unauthorized(); | ||
|
||
if ( | ||
IL2ToL2CrossDomainMessengerMissing(Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER).successfulMessages( | ||
relayERC20MsgHash | ||
) == false | ||
) { | ||
revert(); | ||
} | ||
|
||
IWETH(Predeploys.SUPERCHAIN_WETH).withdraw(wad); | ||
SafeCall.call(dst, wad, _calldata); | ||
} | ||
|
||
function sendETH(address dst, uint256 chainId, bytes memory _calldata) public payable { | ||
IWETH(Predeploys.SUPERCHAIN_WETH).deposit{value: msg.value}(); | ||
|
||
bytes32 relayERC20MessageHash = keccak256( | ||
abi.encode( | ||
chainId, | ||
block.chainid, | ||
IL2ToL2CrossDomainMessengerMissing(Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER).messageNonce(), | ||
Predeploys.SUPERCHAIN_WETH, | ||
Predeploys.SUPERCHAIN_WETH, | ||
abi.encodeCall( | ||
ISuperchainERC20Extensions(Predeploys.SUPERCHAIN_WETH).relayERC20, | ||
(address(this), address(this), msg.value) | ||
) | ||
) | ||
); | ||
ISuperchainERC20Extensions(Predeploys.SUPERCHAIN_WETH).sendERC20(address(this), msg.value, chainId); | ||
IL2ToL2CrossDomainMessenger(Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER).sendMessage({ | ||
_destination: chainId, | ||
_target: address(this), | ||
_message: abi.encodeCall(this.unwrapAndCall, (relayERC20MessageHash, dst, msg.value, _calldata)) | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters