Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: passthrough L1->L3 adapter to send messages to L3 via an L2 forwarder #607

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
64 changes: 64 additions & 0 deletions contracts/chain-adapters/Arbitrum_L3_Adapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import { AdapterInterface } from "./interfaces/AdapterInterface.sol";

/**
* @notice Contract containing logic to send messages from L1 to Arbitrum-like L3s using an intermediate L2 message forwarder.
* @notice This contract requires an L2 forwarder contract to be deployed, since we overwrite the target field to this new target.
* @dev Public functions calling external contracts do not guard against reentrancy because they are expected to be
* called via delegatecall, which will execute this contract's logic within the context of the originating contract.
* For example, the HubPool will delegatecall these functions, therefore its only necessary that the HubPool's methods
* that call this contract's logic guard against reentrancy.
*/

// solhint-disable-next-line contract-name-camelcase
contract Arbitrum_L3_Adapter is AdapterInterface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this contract is specific to arbitrum, right? It's really just an adapter for any forwarder, no?

Copy link
Contributor Author

@bmzig bmzig Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely due to my poor naming, but I did this to emphasize that the L3 is Arbitrum-like. There is no assumption about the structure of the L2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming is still a sticking point for me. I think it makes sense to spend some more time thinking about this. This contract actually functions more like a (re-)router I guess, but maybe that's just my networking bias.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this naming is not useful right now. Because it has "L3" in it its non-intuitive that its actually meant to be deployed on L1. Let's also remove the Arbitrum mention from the name because its clearly not specific to Arbitrum. Please change all the comments also to make it clear this can be used generically.

I think a rerouter in the name could work, and clearly to be consistent with other L1 contracts it should end with Adapter. What about RerouterAdapter?

@pxrl

Copy link
Contributor Author

@bmzig bmzig Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimistically changing it to RerouterAdapter and updating the comments to emphasize that it can be used in other contexts 1e984cc

address public immutable l2Adapter;
address public immutable l2Forwarder;

error RelayMessageFailed();
error RelayTokensFailed(address l1Token);

/**
* @notice Constructs new Adapter for sending tokens/messages to Arbitrum-like L3s.
* @param _l2Adapter Address of the adapter contract on mainnet which implements message transfers
* and token relays.
*/
constructor(address _l2Adapter, address _l2Forwarder) {
l2Adapter = _l2Adapter;
l2Forwarder = _l2Forwarder;
bmzig marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @notice Send cross-chain message to target on L2, which is forwarded to the Arbitrum-like L3.
* @dev there is a bijective mapping of L3 adapters (on L1) to L2 forwarders to L3 spoke pools. The
* spoke pool address is stored by the L2 forwarder and the L2 forwarder address is stored in this contract.
* @param message Data to send to target.
*/
function relayMessage(address, bytes memory message) external payable override {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOC, why did you decide to do this via delegatecall rather than inheritance? To avoid having to have a different contract for an L2 with custom gas vs non custom gas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a delegatecall so this can work with any L2 by piggybacking off of the adapter logic. Naming is admittedly poor here, but the idea was for this (L3) adapter to send messages and tokens to L2 using the exact same logic as the corresponding L2 adapter (e.g. an Arbitrum_*_Adapter, Optimism_Adapter, ZkSync_Adapter, etc) and overwrite the target (since the hub pool will specify the target as the L3 spoke pool due to setCrossChainContracts, which doesn't exist on L2). If I did inheritance, then we would need to make an adapter which inherits from the L2 adapters based on the architecture of the L2 -- something like Arbitrum_L3_Adapter is Arbitrum_CustomGasToken_Adapter or Arbitrum_L3_Adapter is Optimism_Adapter, whereas when we delegatecall, we can keep the code exactly the same and just change constructor arguments.

(bool success, ) = l2Adapter.delegatecall(

Check warning on line 40 in contracts/chain-adapters/Arbitrum_L3_Adapter.sol

View workflow job for this annotation

GitHub Actions / Solhint (16)

Avoid to use low level calls

Check warning on line 40 in contracts/chain-adapters/Arbitrum_L3_Adapter.sol

View workflow job for this annotation

GitHub Actions / Solhint (16)

Avoid to use low level calls
abi.encodeCall(AdapterInterface.relayMessage, (l2Forwarder, message))
);
if (!success) revert RelayMessageFailed();
}

/**
* @notice Bridge tokens to an Arbitrum-like L3, using an L2 forwarder.
* @param l1Token L1 token to deposit.
* @param l2Token L2 token to receive.
* @param amount Amount of L1 tokens to deposit and L2 tokens to receive.
* @dev we discard the "to" field since tokens are always sent to the l2Forwarder.
*/
function relayTokens(
address l1Token,
address l2Token, // l2Token is unused for Arbitrum.
bmzig marked this conversation as resolved.
Show resolved Hide resolved
uint256 amount,
address
) external payable override {
(bool success, ) = l2Adapter.delegatecall(
abi.encodeCall(AdapterInterface.relayTokens, (l1Token, l2Token, amount, l2Forwarder))
);
if (!success) revert RelayTokensFailed(l1Token);
}
}
Loading