Skip to content

Commit

Permalink
update of the forwardMessage function based on the Scroll team recomm…
Browse files Browse the repository at this point in the history
…endations
  • Loading branch information
kyzia551 committed Dec 12, 2023
1 parent 931b2d1 commit 4c819c1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/contracts/adapters/scroll/ScrollAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pragma solidity ^0.8.0;

import {ChainIds} from '../../libs/ChainIds.sol';
import {OpAdapter, IOpAdapter, IBaseAdapter} from '../optimism/OpAdapter.sol';
import {OpAdapter, IOpAdapter, IBaseAdapter, Errors} from '../optimism/OpAdapter.sol';
import {IScrollMessenger, IL1MessageQueue} from './interfaces/IScrollMessenger.sol';

/**
* @title ScrollAdapter
Expand All @@ -14,6 +15,10 @@ import {OpAdapter, IOpAdapter, IBaseAdapter} from '../optimism/OpAdapter.sol';
* @dev note that this adapter inherits from Optimism adapter and overrides supported chain and forwardMessage
*/
contract ScrollAdapter is OpAdapter {
// based on the recommendation of the Scroll team made immutable, because it can't change.
// Even if it's a variable on the Messenger side, will become immutable as well in the next update
IL1MessageQueue public immutable SCROLL_MESSAGE_QUEUE;

/**
* @param crossChainController address of the cross chain controller that will use this bridge adapter
* @param ovmCrossDomainMessenger optimism entry point address
Expand All @@ -23,7 +28,35 @@ contract ScrollAdapter is OpAdapter {
address crossChainController,
address ovmCrossDomainMessenger,
TrustedRemotesConfig[] memory trustedRemotes
) OpAdapter(crossChainController, ovmCrossDomainMessenger, trustedRemotes) {}
) OpAdapter(crossChainController, ovmCrossDomainMessenger, trustedRemotes) {
SCROLL_MESSAGE_QUEUE = IScrollMessenger(OVM_CROSS_DOMAIN_MESSENGER).messageQueue();
}

/// @inheritdoc IBaseAdapter
function forwardMessage(
address receiver,
uint256 destinationGasLimit,
uint256 destinationChainId,
bytes calldata message
) external virtual override returns (address, uint256) {
require(
isDestinationChainIdSupported(destinationChainId),
Errors.DESTINATION_CHAIN_ID_NOT_SUPPORTED
);
require(receiver != address(0), Errors.RECEIVER_NOT_SET);

// L2 message delivery fee
uint256 fee = SCROLL_MESSAGE_QUEUE.estimateCrossDomainMessageFee(destinationGasLimit);

IScrollMessenger(OVM_CROSS_DOMAIN_MESSENGER).sendMessage{value: fee}(
receiver,
fee,
abi.encodeWithSelector(IOpAdapter.ovmReceive.selector, message),
destinationGasLimit
);

return (OVM_CROSS_DOMAIN_MESSENGER, 0);
}

/// @inheritdoc IOpAdapter
function isDestinationChainIdSupported(
Expand Down
9 changes: 9 additions & 0 deletions src/contracts/adapters/scroll/interfaces/IL1MessageQueue.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.16;

interface IL1MessageQueue {
/// @notice Return the amount of ETH should pay for cross domain message.
/// @param gasLimit Gas limit required to complete the message relay on L2.
function estimateCrossDomainMessageFee(uint256 gasLimit) external view returns (uint256);
}
38 changes: 38 additions & 0 deletions src/contracts/adapters/scroll/interfaces/IScrollMessenger.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.16;
import {IL1MessageQueue} from './IL1MessageQueue.sol';

interface IScrollMessenger {
function messageQueue() external view returns (IL1MessageQueue);

/*****************************
* Public Mutating Functions *
*****************************/

/// @notice Send cross chain message from L1 to L2 or L2 to L1.
/// @param target The address of account who receive the message.
/// @param value The amount of ether passed when call target contract.
/// @param message The content of the message.
/// @param gasLimit Gas limit required to complete the message relay on corresponding chain.
function sendMessage(
address target,
uint256 value,
bytes calldata message,
uint256 gasLimit
) external payable;

/// @notice Send cross chain message from L1 to L2 or L2 to L1.
/// @param target The address of account who receive the message.
/// @param value The amount of ether passed when call target contract.
/// @param message The content of the message.
/// @param gasLimit Gas limit required to complete the message relay on corresponding chain.
/// @param refundAddress The address of account who will receive the refunded fee.
function sendMessage(
address target,
uint256 value,
bytes calldata message,
uint256 gasLimit,
address refundAddress
) external payable;
}

0 comments on commit 4c819c1

Please sign in to comment.