|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {Passage} from "./Passage.sol"; |
| 5 | + |
| 6 | +/// @notice A contract deployed to Host chain that enables transactions from L1 to be sent on an L2. |
| 7 | +contract Transactor { |
| 8 | + /// @notice The chainId of rollup that Ether will be sent to by default when entering the rollup via fallback() or receive(). |
| 9 | + uint256 public immutable defaultRollupChainId; |
| 10 | + |
| 11 | + /// @notice The address that is allowed to configure `transact` gas limits. |
| 12 | + address public immutable gasAdmin; |
| 13 | + |
| 14 | + /// @notice The address of the Passage contract, to enable transact + enter. |
| 15 | + Passage public immutable passage; |
| 16 | + |
| 17 | + /// @notice The sum of `transact` calls in a block cannot use more than this limit. |
| 18 | + uint256 public perBlockGasLimit; |
| 19 | + |
| 20 | + /// @notice Each `transact` call cannot use more than this limit. |
| 21 | + uint256 public perTransactGasLimit; |
| 22 | + |
| 23 | + /// @notice The total gas used by `transact` so far in this block. |
| 24 | + /// rollupChainId => block number => `transasct` gasLimit used so far. |
| 25 | + mapping(uint256 => mapping(uint256 => uint256)) public transactGasUsed; |
| 26 | + |
| 27 | + /// @notice Emitted to send a special transaction to the rollup. |
| 28 | + event Transact( |
| 29 | + uint256 indexed rollupChainId, |
| 30 | + address indexed sender, |
| 31 | + address indexed to, |
| 32 | + bytes data, |
| 33 | + uint256 value, |
| 34 | + uint256 gas, |
| 35 | + uint256 maxFeePerGas |
| 36 | + ); |
| 37 | + |
| 38 | + /// @notice Emitted when the admin configures gas limits. |
| 39 | + event GasConfigured(uint256 perBlock, uint256 perTransact); |
| 40 | + |
| 41 | + /// @notice Thrown when attempting to use more then the current global `transact` gasLimit for the block. |
| 42 | + error PerBlockTransactGasLimit(); |
| 43 | + |
| 44 | + /// @notice Thrown when attempting to use too much gas per single `transact` call. |
| 45 | + error PerTransactGasLimit(); |
| 46 | + |
| 47 | + /// @notice Thrown when attempting to configure gas if not the admin. |
| 48 | + error OnlyGasAdmin(); |
| 49 | + |
| 50 | + /// @param _defaultRollupChainId - the chainId of the rollup that Ether will be sent to by default |
| 51 | + /// when entering the rollup via fallback() or receive() fns. |
| 52 | + constructor( |
| 53 | + uint256 _defaultRollupChainId, |
| 54 | + address _gasAdmin, |
| 55 | + Passage _passage, |
| 56 | + uint256 _perBlockGasLimit, |
| 57 | + uint256 _perTransactGasLimit |
| 58 | + ) { |
| 59 | + defaultRollupChainId = _defaultRollupChainId; |
| 60 | + gasAdmin = _gasAdmin; |
| 61 | + passage = _passage; |
| 62 | + _configureGas(_perBlockGasLimit, _perTransactGasLimit); |
| 63 | + } |
| 64 | + |
| 65 | + /// @notice Configure the `transact` gas limits. |
| 66 | + function configureGas(uint256 perBlock, uint256 perTransact) external { |
| 67 | + if (msg.sender != gasAdmin) revert OnlyGasAdmin(); |
| 68 | + _configureGas(perBlock, perTransact); |
| 69 | + } |
| 70 | + |
| 71 | + /// @notice Allows a special transaction to be sent to the rollup with sender == L1 msg.sender. |
| 72 | + /// @dev Transaction is processed after normal rollup block execution. |
| 73 | + /// @dev See `enterTransact` for docs. |
| 74 | + function transact( |
| 75 | + uint256 rollupChainId, |
| 76 | + address to, |
| 77 | + bytes calldata data, |
| 78 | + uint256 value, |
| 79 | + uint256 gas, |
| 80 | + uint256 maxFeePerGas |
| 81 | + ) public payable { |
| 82 | + enterTransact(rollupChainId, msg.sender, to, data, value, gas, maxFeePerGas); |
| 83 | + } |
| 84 | + |
| 85 | + /// @dev See `transact` for docs. |
| 86 | + function transact(address to, bytes calldata data, uint256 value, uint256 gas, uint256 maxFeePerGas) |
| 87 | + external |
| 88 | + payable |
| 89 | + { |
| 90 | + enterTransact(defaultRollupChainId, msg.sender, to, data, value, gas, maxFeePerGas); |
| 91 | + } |
| 92 | + |
| 93 | + /// @notice Send Ether on the rollup, send a special transaction to be sent to the rollup with sender == L1 msg.sender. |
| 94 | + /// @dev Enter and Transact are processed after normal rollup block execution. |
| 95 | + /// @dev See `enter` for Enter docs. |
| 96 | + /// @param rollupChainId - The rollup chain to send the transaction to. |
| 97 | + /// @param etherRecipient - The recipient of the ether. |
| 98 | + /// @param to - The address to call on the rollup. |
| 99 | + /// @param data - The data to send to the rollup. |
| 100 | + /// @param value - The amount of Ether to send on the rollup. |
| 101 | + /// @param gas - The gas limit for the transaction. |
| 102 | + /// @param maxFeePerGas - The maximum fee per gas for the transaction (per EIP-1559). |
| 103 | + /// @custom:emits Transact indicating the transaction to mine on the rollup. |
| 104 | + function enterTransact( |
| 105 | + uint256 rollupChainId, |
| 106 | + address etherRecipient, |
| 107 | + address to, |
| 108 | + bytes calldata data, |
| 109 | + uint256 value, |
| 110 | + uint256 gas, |
| 111 | + uint256 maxFeePerGas |
| 112 | + ) public payable { |
| 113 | + // if msg.value is attached, Enter |
| 114 | + if (msg.value > 0) { |
| 115 | + passage.enter{value: msg.value}(rollupChainId, etherRecipient); |
| 116 | + } |
| 117 | + |
| 118 | + // ensure per-transact gas limit is respected |
| 119 | + if (gas > perTransactGasLimit) revert PerTransactGasLimit(); |
| 120 | + |
| 121 | + // ensure global transact gas limit is respected |
| 122 | + uint256 gasUsed = transactGasUsed[rollupChainId][block.number]; |
| 123 | + if (gasUsed + gas > perBlockGasLimit) revert PerBlockTransactGasLimit(); |
| 124 | + transactGasUsed[rollupChainId][block.number] = gasUsed + gas; |
| 125 | + |
| 126 | + // emit Transact event |
| 127 | + emit Transact(rollupChainId, msg.sender, to, data, value, gas, maxFeePerGas); |
| 128 | + } |
| 129 | + |
| 130 | + /// @notice Helper to configure gas limits on deploy & via admin function |
| 131 | + function _configureGas(uint256 perBlock, uint256 perTransact) internal { |
| 132 | + perBlockGasLimit = perBlock; |
| 133 | + perTransactGasLimit = perTransact; |
| 134 | + emit GasConfigured(perBlock, perTransact); |
| 135 | + } |
| 136 | +} |
0 commit comments