// SPDX-License-Identifier: Apache-2.0 /* * Copyright 2020, Offchain Labs, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ pragma solidity ^0.8.0; import "../../libraries/Whitelist.sol"; import { ArbitrumEnabledToken } from "../ICustomToken.sol"; import "../L1ArbitrumMessenger.sol"; import "../../libraries/gateway/GatewayRouter.sol"; import "../../arbitrum/gateway/L2GatewayRouter.sol"; import "../../libraries/ERC165.sol"; import "./IL1GatewayRouter.sol"; import "./IL1ArbitrumGateway.sol"; /** * @title Handles deposits from Erhereum into Arbitrum. Tokens are routered to their appropriate L1 gateway (Router itself also conforms to the Gateway itnerface). * @notice Router also serves as an L1-L2 token address oracle. */ contract L1GatewayRouter is WhitelistConsumer, L1ArbitrumMessenger, GatewayRouter, ERC165, IL1GatewayRouter { using Address for address; address public override owner; address public override inbox; modifier onlyOwner() { require(msg.sender == owner, "ONLY_OWNER"); _; } function initialize( address _owner, address _defaultGateway, address, // was _whitelist, now unused address _counterpartGateway, address _inbox ) public { GatewayRouter._initialize(_counterpartGateway, address(0), _defaultGateway); owner = _owner; WhitelistConsumer.whitelist = address(0); inbox = _inbox; } function setDefaultGateway( address newL1DefaultGateway, uint256 _maxGas, uint256 _gasPriceBid, uint256 _maxSubmissionCost ) external payable virtual onlyOwner returns (uint256) { return _setDefaultGateway( newL1DefaultGateway, _maxGas, _gasPriceBid, _maxSubmissionCost, msg.value ); } function _setDefaultGateway( address newL1DefaultGateway, uint256 _maxGas, uint256 _gasPriceBid, uint256 _maxSubmissionCost, uint256 feeAmount ) internal returns (uint256) { defaultGateway = newL1DefaultGateway; emit DefaultGatewayUpdated(newL1DefaultGateway); address l2NewDefaultGateway; if (newL1DefaultGateway != address(0)) { l2NewDefaultGateway = TokenGateway(newL1DefaultGateway).counterpartGateway(); } bytes memory data = abi.encodeWithSelector( L2GatewayRouter.setDefaultGateway.selector, l2NewDefaultGateway ); return sendTxToL2( inbox, counterpartGateway, msg.sender, feeAmount, 0, L2GasParams({ _maxSubmissionCost: _maxSubmissionCost, _maxGas: _maxGas, _gasPriceBid: _gasPriceBid }), data ); } function setOwner(address newOwner) external onlyOwner { require(newOwner != address(0), "INVALID_OWNER"); // set newOwner to address(1) to disable owner and keep `initialize` safe owner = newOwner; } /** * @notice Allows L1 Token contract to trustlessly register its gateway. (other setGateway method allows excess eth recovery from _maxSubmissionCost and is recommended) * @param _gateway l1 gateway address * @param _maxGas max gas for L2 retryable exrecution * @param _gasPriceBid gas price for L2 retryable ticket * @param _maxSubmissionCost base submission cost L2 retryable tick3et * @return Retryable ticket ID */ function setGateway( address _gateway, uint256 _maxGas, uint256 _gasPriceBid, uint256 _maxSubmissionCost ) external payable virtual override returns (uint256) { return setGateway(_gateway, _maxGas, _gasPriceBid, _maxSubmissionCost, msg.sender); } /** * @notice Allows L1 Token contract to trustlessly register its gateway. * @param _gateway l1 gateway address * @param _maxGas max gas for L2 retryable exrecution * @param _gasPriceBid gas price for L2 retryable ticket * @param _maxSubmissionCost base submission cost L2 retryable tick3et * @param _creditBackAddress address for crediting back overpayment of _maxSubmissionCost * @return Retryable ticket ID */ function setGateway( address _gateway, uint256 _maxGas, uint256 _gasPriceBid, uint256 _maxSubmissionCost, address _creditBackAddress ) public payable virtual override returns (uint256) { return _setGatewayWithCreditBack( _gateway, _maxGas, _gasPriceBid, _maxSubmissionCost, _creditBackAddress, msg.value ); } function _setGatewayWithCreditBack( address _gateway, uint256 _maxGas, uint256 _gasPriceBid, uint256 _maxSubmissionCost, address _creditBackAddress, uint256 feeAmount ) internal returns (uint256) { require( ArbitrumEnabledToken(msg.sender).isArbitrumEnabled() == uint8(0xb1), "NOT_ARB_ENABLED" ); require(_gateway.isContract(), "NOT_TO_CONTRACT"); address currGateway = getGateway(msg.sender); if (currGateway != address(0) && currGateway != defaultGateway) { // if gateway is already set to a non-default gateway, don't allow it to set a different gateway require(currGateway == _gateway, "NO_UPDATE_TO_DIFFERENT_ADDR"); } address[] memory _tokenArr = new address[](1); _tokenArr[0] = address(msg.sender); address[] memory _gatewayArr = new address[](1); _gatewayArr[0] = _gateway; return _setGateways( _tokenArr, _gatewayArr, _maxGas, _gasPriceBid, _maxSubmissionCost, _creditBackAddress, feeAmount ); } function setGateways( address[] memory _token, address[] memory _gateway, uint256 _maxGas, uint256 _gasPriceBid, uint256 _maxSubmissionCost ) external payable virtual onlyOwner returns (uint256) { // it is assumed that token and gateway are both contracts // require(_token[i].isContract() && _gateway[i].isContract(), "NOT_CONTRACT"); return _setGateways( _token, _gateway, _maxGas, _gasPriceBid, _maxSubmissionCost, msg.sender, msg.value ); } function _setGateways( address[] memory _token, address[] memory _gateway, uint256 _maxGas, uint256 _gasPriceBid, uint256 _maxSubmissionCost, address _creditBackAddress, uint256 feeAmount ) internal returns (uint256) { require(_token.length == _gateway.length, "WRONG_LENGTH"); for (uint256 i = 0; i < _token.length; i++) { l1TokenToGateway[_token[i]] = _gateway[i]; emit GatewaySet(_token[i], _gateway[i]); // overwrite memory so the L2 router receives the L2 address of each gateway if (_gateway[i] != address(0) && _gateway[i] != DISABLED) { // if we are assigning a gateway to the token, the address oracle of the gateway // must return something other than the 0 address // this check helps avoid misconfiguring gateways require( TokenGateway(_gateway[i]).calculateL2TokenAddress(_token[i]) != address(0), "TOKEN_NOT_HANDLED_BY_GATEWAY" ); _gateway[i] = TokenGateway(_gateway[i]).counterpartGateway(); } } bytes memory data = abi.encodeWithSelector( L2GatewayRouter.setGateway.selector, _token, _gateway ); return sendTxToL2( inbox, counterpartGateway, _creditBackAddress, feeAmount, 0, L2GasParams({ _maxSubmissionCost: _maxSubmissionCost, _maxGas: _maxGas, _gasPriceBid: _gasPriceBid }), data ); } function outboundTransfer( address _token, address _to, uint256 _amount, uint256 _maxGas, uint256 _gasPriceBid, bytes calldata _data ) public payable override(GatewayRouter, ITokenGateway) returns (bytes memory) { return super.outboundTransfer(_token, _to, _amount, _maxGas, _gasPriceBid, _data); } /** * @notice Deposit ERC20 token from Ethereum into Arbitrum using the registered or otherwise default gateway * @dev Some legacy gateway might not have the outboundTransferCustomRefund method and will revert, in such case use outboundTransfer instead * L2 address alias will not be applied to the following types of addresses on L1: * - an externally-owned account * - a contract in construction * - an address where a contract will be created * - an address where a contract lived, but was destroyed * The msg.sender that calls this method, or its L2 alias if it has code in L1, will be set to the callValueRefundAddress; * this means it can cancel the retryable ticket if its auto-redeem fails, and also that it receives the l2 callvalue refund. * @param _token L1 address of ERC20 * @param _refundTo Account, or its L2 alias if it have code in L1, to be credited with excess gas refund in L2 * @param _to Account to be credited with the tokens in the L2 (can be an EOA or a contract), not subject to L2 aliasing. * @param _amount Token Amount * @param _maxGas Max gas deducted from user's L2 balance to cover L2 execution * @param _gasPriceBid Gas price for L2 execution * @param _data encoded data from router and user * @return res abi encoded inbox sequence number */ function outboundTransferCustomRefund( address _token, address _refundTo, address _to, uint256 _amount, uint256 _maxGas, uint256 _gasPriceBid, bytes calldata _data ) public payable override returns (bytes memory) { address gateway = getGateway(_token); bytes memory gatewayData = GatewayMessageHandler.encodeFromRouterToGateway( msg.sender, _data ); emit TransferRouted(_token, msg.sender, _to, gateway); // here we use `IL1ArbitrumGateway` since we don't assume all ITokenGateway implements `outboundTransferCustomRefund` return IL1ArbitrumGateway(gateway).outboundTransferCustomRefund{ value: msg.value }( _token, _refundTo, _to, _amount, _maxGas, _gasPriceBid, gatewayData ); } modifier onlyCounterpartGateway() override { // don't expect messages from L2 router revert("ONLY_COUNTERPART_GATEWAY"); _; } function supportsInterface(bytes4 interfaceId) public view override(ERC165, IERC165) returns (bool) { // registering interfaces that is added after arb-bridge-peripherals >1.0.11 // using function selector instead of single function interfaces to reduce bloat return interfaceId == this.outboundTransferCustomRefund.selector || super.supportsInterface(interfaceId); } }