|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import {RollupOrders} from "zenith/src/orders/RollupOrders.sol"; |
| 5 | +import {BurnMintERC20} from "../vendor/BurnMintERC20.sol"; |
| 6 | + |
| 7 | +import {SignetL2} from "../l2/Signet.sol"; |
| 8 | + |
| 9 | +abstract contract BridgeL2 is SignetL2, BurnMintERC20 { |
| 10 | + /// @notice The address of the asset on the host chain. |
| 11 | + address immutable HOST_ASSET; |
| 12 | + /// @notice The address of the bank on the host chain. The bank holds the |
| 13 | + /// asset while tokens are bridged into the rollup. |
| 14 | + address immutable HOST_BANK; |
| 15 | + |
| 16 | + constructor(address _hostAsset, address _hostBank, string memory _name, string memory _symbol, uint8 _decimals) |
| 17 | + BurnMintERC20(_name, _symbol, _decimals, 0, 0) |
| 18 | + { |
| 19 | + HOST_ASSET = _hostAsset; |
| 20 | + HOST_BANK = _hostBank; |
| 21 | + } |
| 22 | + |
| 23 | + /// @notice Bridges assets into the rollup for a given recipient. |
| 24 | + function _bridgeIn(address recipient, uint256 amount, RollupOrders.Input[] memory inputs) internal virtual { |
| 25 | + _mint(recipient, amount); |
| 26 | + |
| 27 | + RollupOrders.Output[] memory outputs = new RollupOrders.Output[](1); |
| 28 | + outputs[0] = makeHostOutput(HOST_ASSET, amount, HOST_BANK); |
| 29 | + |
| 30 | + ORDERS.initiate(block.timestamp, inputs, outputs); |
| 31 | + } |
| 32 | + |
| 33 | + /// @notice Bridges assets into the rollup for a given recipient. |
| 34 | + function bridgeIn(address recipient, uint256 amount) public virtual { |
| 35 | + _bridgeIn(recipient, amount, new RollupOrders.Input[](0)); |
| 36 | + } |
| 37 | + |
| 38 | + /// @notice Burn asset on L2, and create an order to bridge out asset to |
| 39 | + /// L1. If the order is not filled, the asset will not be burned. |
| 40 | + /// |
| 41 | + /// This transaction should be paired with some off-chain logic that fills |
| 42 | + /// orders from the L1 bank. |
| 43 | + function _bridgeOut(address sender, address recipient, uint256 amount, RollupOrders.Input[] memory inputs) |
| 44 | + internal |
| 45 | + virtual |
| 46 | + { |
| 47 | + if (_msgSender() != sender) { |
| 48 | + _spendAllowance(sender, _msgSender(), amount); |
| 49 | + } |
| 50 | + |
| 51 | + _burn(msg.sender, amount); |
| 52 | + |
| 53 | + RollupOrders.Output[] memory outputs = new RollupOrders.Output[](1); |
| 54 | + outputs[0] = makeHostOutput(HOST_ASSET, amount, recipient); |
| 55 | + |
| 56 | + ORDERS.initiate(block.timestamp, inputs, outputs); |
| 57 | + } |
| 58 | + |
| 59 | + /// @notice Burn asset on L2, and create an order to bridge out assets to |
| 60 | + /// L1. If the order is not filled, the asset will not be burned. |
| 61 | + /// |
| 62 | + /// This transaction should be paired with some off-chain logic that fills |
| 63 | + /// orders from the L1 bank. |
| 64 | + function bridgeOut(address recipient, uint256 amount) public virtual { |
| 65 | + _bridgeOut(msg.sender, recipient, amount, new RollupOrders.Input[](0)); |
| 66 | + } |
| 67 | + |
| 68 | + /// @notice Burn asset on L2 from `sender`, and create an order to bridge |
| 69 | + /// out assets to L1. If the order is not filled, the asset will not be |
| 70 | + /// burned. Used when the caller is not the sender. |
| 71 | + /// |
| 72 | + /// This transaction should be paired with some off-chain logic that fills |
| 73 | + /// orders from the L1 bank. |
| 74 | + function bridgeOutFrom(address sender, address recipient, uint256 amount) public virtual { |
| 75 | + _bridgeOut(sender, recipient, amount, new RollupOrders.Input[](0)); |
| 76 | + } |
| 77 | +} |
0 commit comments