|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +pragma solidity ^0.8.0; |
| 4 | + |
| 5 | +import "@openzeppelin-upgradeable/contracts/proxy/utils/Initializable.sol"; |
| 6 | +import "./IL1Sender.sol"; |
| 7 | +import "./IL2Receiver.sol"; |
| 8 | + |
| 9 | +interface ICrossDomainMessenger { |
| 10 | + function sendMessage(address target, bytes calldata message, uint32 gasLimit) external; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * @title OpL1Sender |
| 15 | + * @notice Optimism-based contract responsible for sending data |
| 16 | + * to the L2 receiver contract via the bridge messenger. |
| 17 | + */ |
| 18 | +contract OpL1Sender is IL1Sender, Initializable { |
| 19 | + ICrossDomainMessenger public immutable messenger; |
| 20 | + address public immutable dispatcher; |
| 21 | + uint32 public immutable gasLimit; |
| 22 | + IL2Receiver public l2Receiver; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param _dispatcher The address of the dispatcher contract. |
| 26 | + * @param _messenger The address of the CrossDomainMessenger contract. |
| 27 | + * @param _gasLimit The gas limit for the message. |
| 28 | + */ |
| 29 | + constructor(address _dispatcher, ICrossDomainMessenger _messenger, uint32 _gasLimit) { |
| 30 | + dispatcher = _dispatcher; |
| 31 | + messenger = _messenger; |
| 32 | + gasLimit = _gasLimit; |
| 33 | + } |
| 34 | + |
| 35 | + function initialize(IL2Receiver _l2Receiver) external initializer { |
| 36 | + l2Receiver = _l2Receiver; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @notice Sends data to the L2 receiver contract via the messenger. |
| 41 | + * @param target The address of the target contract on L2. |
| 42 | + * @param data The data to be sent to the target contract. |
| 43 | + */ |
| 44 | + function sendData(address target, bytes calldata data) external { |
| 45 | + require(dispatcher == msg.sender, "Unauthorized caller"); |
| 46 | + bytes memory payload = abi.encode(target, data); |
| 47 | + bytes memory callData = abi.encodeWithSelector(l2Receiver.recvData.selector, payload); |
| 48 | + messenger.sendMessage(address(l2Receiver), callData, gasLimit); |
| 49 | + } |
| 50 | +} |
0 commit comments