|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.0; |
| 4 | + |
| 5 | +enum TxStatus { |
| 6 | + Failure, |
| 7 | + Success |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * @dev The log passed from L2 |
| 12 | + * @param l2ShardId The shard identifier, 0 - rollup, 1 - porter. All other values are not used but are reserved for |
| 13 | + * the future |
| 14 | + * @param isService A boolean flag that is part of the log along with `key`, `value`, and `sender` address. |
| 15 | + * This field is required formally but does not have any special meaning. |
| 16 | + * @param txNumberInBatch The L2 transaction number in the batch, in which the log was sent |
| 17 | + * @param sender The L2 address which sent the log |
| 18 | + * @param key The 32 bytes of information that was sent in the log |
| 19 | + * @param value The 32 bytes of information that was sent in the log |
| 20 | + * Both `key` and `value` are arbitrary 32-bytes selected by the log sender |
| 21 | + */ |
| 22 | +struct L2Log { |
| 23 | + uint8 l2ShardId; |
| 24 | + bool isService; |
| 25 | + uint16 txNumberInBatch; |
| 26 | + address sender; |
| 27 | + bytes32 key; |
| 28 | + bytes32 value; |
| 29 | +} |
| 30 | + |
| 31 | +uint160 constant ALIAS_OFFSET = uint160(0x1111000000000000000000000000000000001111); |
| 32 | + |
| 33 | +/** |
| 34 | + * @notice Utility function converts the address that submitted a tx |
| 35 | + * to the inbox on L1 to the msg.sender viewed on L2 |
| 36 | + * @param l1Address the address in the L1 that triggered the tx to L2 |
| 37 | + * @return l2Address L2 address as viewed in msg.sender |
| 38 | + */ |
| 39 | +function applyL1ToL2Alias(address l1Address) pure returns (address l2Address) { |
| 40 | + unchecked { |
| 41 | + l2Address = address(uint160(l1Address) + ALIAS_OFFSET); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * @notice Utility function that converts the msg.sender viewed on L2 to the |
| 47 | + * address that submitted a tx to the inbox on L1 |
| 48 | + * @param l2Address L2 address as viewed in msg.sender |
| 49 | + * @return l1Address the address in the L1 that triggered the tx to L2 |
| 50 | + */ |
| 51 | +function undoL1ToL2Alias(address l2Address) pure returns (address l1Address) { |
| 52 | + unchecked { |
| 53 | + l1Address = address(uint160(l2Address) - ALIAS_OFFSET); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +bytes32 constant CREATE_PREFIX = 0x63bae3a9951d38e8a3fbb7b70909afc1200610fc5bc55ade242f815974674f23; |
| 58 | + |
| 59 | +function computeCreateAddress(address deployer, uint256 nonce) pure returns (address) { |
| 60 | + bytes32 hash = keccak256(bytes.concat(CREATE_PREFIX, bytes32(uint256(uint160(deployer))), bytes32(nonce))); |
| 61 | + return address(uint160(uint256(hash))); |
| 62 | +} |
| 63 | + |
| 64 | +interface IZkSync { |
| 65 | + /** |
| 66 | + * @notice Request execution of L2 transaction from L1. |
| 67 | + * @param contractL2 The L2 receiver address |
| 68 | + * @param l2Value `msg.value` of L2 transaction |
| 69 | + * @param calldataBytes The input of the L2 transaction |
| 70 | + * @param l2GasLimit Maximum amount of L2 gas that transaction can consume during execution on L2 |
| 71 | + * @param l2GasPerPubdataByteLimit The maximum amount L2 gas that the operator may charge the user for single byte of pubdata. |
| 72 | + * @param factoryDeps An array of L2 bytecodes that will be marked as known on L2 |
| 73 | + * @param refundRecipient The address on L2 that will receive the refund for the transaction. |
| 74 | + * @dev If the L2 deposit finalization transaction fails, the `_refundRecipient` will receive the `_l2Value`. |
| 75 | + * Please note, the contract may change the refund recipient's address to eliminate sending funds to addresses out of control. |
| 76 | + * - If `_refundRecipient` is a contract on L1, the refund will be sent to the aliased `_refundRecipient`. |
| 77 | + * - If `_refundRecipient` is set to `address(0)` and the sender has NO deployed bytecode on L1, the refund will be sent to the `msg.sender` address. |
| 78 | + * - If `_refundRecipient` is set to `address(0)` and the sender has deployed bytecode on L1, the refund will be sent to the aliased `msg.sender` address. |
| 79 | + * @dev The address aliasing of L1 contracts as refund recipient on L2 is necessary to guarantee that the funds are controllable, |
| 80 | + * since address aliasing to the from address for the L2 tx will be applied if the L1 `msg.sender` is a contract. |
| 81 | + * Without address aliasing for L1 contracts as refund recipients they would not be able to make proper L2 tx requests |
| 82 | + * through the Mailbox to use or withdraw the funds from L2, and the funds would be lost. |
| 83 | + * @return canonicalTxHash The hash of the requested L2 transaction. This hash can be used to follow the transaction status |
| 84 | + */ |
| 85 | + function requestL2Transaction( |
| 86 | + address contractL2, |
| 87 | + uint256 l2Value, |
| 88 | + bytes calldata calldataBytes, |
| 89 | + uint256 l2GasLimit, |
| 90 | + uint256 l2GasPerPubdataByteLimit, |
| 91 | + bytes[] calldata factoryDeps, |
| 92 | + address refundRecipient |
| 93 | + ) external payable returns (bytes32 canonicalTxHash); |
| 94 | + |
| 95 | + /** |
| 96 | + * @notice Prove that the L1 -> L2 transaction was processed with the specified status. |
| 97 | + * @param l2TxHash The L2 canonical transaction hash |
| 98 | + * @param l2BatchNumber The L2 batch number where the transaction was processed |
| 99 | + * @param l2MessageIndex The position in the L2 logs Merkle tree of the l2Log that was sent with the message |
| 100 | + * @param l2TxNumberInBatch The L2 transaction number in the batch, in which the log was sent |
| 101 | + * @param merkleProof The Merkle proof of the processing L1 -> L2 transaction |
| 102 | + * @param status The execution status of the L1 -> L2 transaction (true - success & 0 - fail) |
| 103 | + * @return Whether the proof is correct and the transaction was actually executed with provided status |
| 104 | + */ |
| 105 | + function proveL1ToL2TransactionStatus( |
| 106 | + bytes32 l2TxHash, |
| 107 | + uint256 l2BatchNumber, |
| 108 | + uint256 l2MessageIndex, |
| 109 | + uint16 l2TxNumberInBatch, |
| 110 | + bytes32[] calldata merkleProof, |
| 111 | + TxStatus status |
| 112 | + ) external view returns (bool); |
| 113 | + |
| 114 | + /** |
| 115 | + * @notice Prove that a specific L2 log was sent in a specific L2 batch |
| 116 | + * @param l2BatchNumber The executed L2 batch number in which the log appeared |
| 117 | + * @param l2LogIndex The position of the l2log in the L2 logs Merkle tree |
| 118 | + * @param l2Log Information about the sent log |
| 119 | + * @param merkleProof Merkle proof for inclusion of the L2 log |
| 120 | + * @return Whether the proof is correct and L2 log is included in batch |
| 121 | + */ |
| 122 | + function proveL2LogInclusion( |
| 123 | + uint256 l2BatchNumber, |
| 124 | + uint256 l2LogIndex, |
| 125 | + L2Log memory l2Log, |
| 126 | + bytes32[] calldata merkleProof |
| 127 | + ) external view returns (bool); |
| 128 | +} |
| 129 | + |
| 130 | +IZkSync constant ZkSync = IZkSync(0xaBEA9132b05A70803a4E85094fD0e1800777fBEF); |
| 131 | + |
| 132 | +interface IL2Messenger { |
| 133 | + function sendL2ToL1Log(bool isService, bytes32 key, bytes32 value) external returns (uint256 logIdInMerkleTree); |
| 134 | +} |
0 commit comments