|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import {HostOrders} from "zenith/src/orders/HostOrders.sol"; |
| 5 | +import {Passage} from "zenith/src/passage/Passage.sol"; |
| 6 | +import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; |
| 7 | + |
| 8 | +import {AddressAliasHelper} from "../vendor/AddressAliasHelper.sol"; |
| 9 | +import {PecorinoConstants} from "../chains/Pecorino.sol"; |
| 10 | + |
| 11 | +abstract contract SignetL1 { |
| 12 | + /// @notice Sentinal value for the native asset in order inputs/outputs |
| 13 | + address constant NATIVE_ASSET = address(0); |
| 14 | + |
| 15 | + /// @notice The Passage address |
| 16 | + Passage internal immutable PASSAGE; |
| 17 | + /// @notice The Host Orders address |
| 18 | + HostOrders internal immutable ORDERS; |
| 19 | + |
| 20 | + /// @notice The WETH token address. |
| 21 | + IERC20 internal immutable WETH; |
| 22 | + /// @notice The WBTC token address. |
| 23 | + IERC20 internal immutable WBTC; |
| 24 | + /// @notice The USDC token address. |
| 25 | + IERC20 internal immutable USDC; |
| 26 | + /// @notice The USDT token address. |
| 27 | + IERC20 internal immutable USDT; |
| 28 | + |
| 29 | + /// @notice The Rollup WUSD token address. |
| 30 | + address internal immutable RU_WUSD; |
| 31 | + /// @notice The Rollup WBTC token address. |
| 32 | + address internal immutable RU_WBTC; |
| 33 | + /// @notice The Rollup WETH token address. |
| 34 | + address internal immutable RU_WETH; |
| 35 | + |
| 36 | + /// @notice Error for unsupported chain IDs. |
| 37 | + error UnsupportedChain(uint256); |
| 38 | + |
| 39 | + constructor() { |
| 40 | + if (block.chainid == PecorinoConstants.HOST_CHAIN_ID) { |
| 41 | + PASSAGE = PecorinoConstants.HOST_PASSAGE; |
| 42 | + ORDERS = PecorinoConstants.HOST_ORDERS; |
| 43 | + |
| 44 | + WETH = IERC20(PecorinoConstants.HOST_WETH); |
| 45 | + WBTC = IERC20(PecorinoConstants.HOST_WBTC); |
| 46 | + USDC = IERC20(PecorinoConstants.HOST_USDC); |
| 47 | + USDT = IERC20(PecorinoConstants.HOST_USDT); |
| 48 | + |
| 49 | + RU_WUSD = address(PecorinoConstants.WUSD); |
| 50 | + RU_WBTC = address(PecorinoConstants.WBTC); |
| 51 | + RU_WETH = address(PecorinoConstants.WETH); |
| 52 | + } else { |
| 53 | + revert UnsupportedChain(block.chainid); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// @notice This is used to know whether to alias. It should generally |
| 58 | + /// return false. If writing an ephemeral contract, override to return true. |
| 59 | + function isEphemeral() internal pure virtual returns (bool) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + /// @notice Returns |
| 64 | + function selfOnL2() internal view returns (address) { |
| 65 | + if (isEphemeral()) { |
| 66 | + return address(this); |
| 67 | + } |
| 68 | + if (address(this).code.length == 23) { |
| 69 | + bool is7702; |
| 70 | + assembly { |
| 71 | + let ptr := mload(0x40) |
| 72 | + extcodecopy(caller(), ptr, 0, 0x20) |
| 73 | + is7702 := eq(shr(232, mload(ptr)), 0xEF0100) |
| 74 | + // clean the memory we used. Unnecessary, but good hygiene |
| 75 | + mstore(ptr, 0x0) |
| 76 | + } |
| 77 | + if (is7702) { |
| 78 | + return address(this); |
| 79 | + } |
| 80 | + } |
| 81 | + return AddressAliasHelper.applyL1ToL2Alias(address(this)); |
| 82 | + } |
| 83 | + |
| 84 | + function makeOutput(address token, uint256 amount, address recipient) |
| 85 | + internal |
| 86 | + pure |
| 87 | + returns (HostOrders.Output memory output) |
| 88 | + { |
| 89 | + output.token = token; |
| 90 | + output.amount = amount; |
| 91 | + output.recipient = recipient; |
| 92 | + output.chainId = PecorinoConstants.HOST_CHAIN_ID; |
| 93 | + } |
| 94 | + |
| 95 | + function usdcOutput(uint256 amount, address recipient) internal view returns (HostOrders.Output memory output) { |
| 96 | + return makeOutput(address(USDC), amount, recipient); |
| 97 | + } |
| 98 | + |
| 99 | + function usdtOutput(uint256 amount, address recipient) internal view returns (HostOrders.Output memory output) { |
| 100 | + return makeOutput(address(USDT), amount, recipient); |
| 101 | + } |
| 102 | + |
| 103 | + function wbtcOutput(uint256 amount, address recipient) internal view returns (HostOrders.Output memory output) { |
| 104 | + return makeOutput(address(WBTC), amount, recipient); |
| 105 | + } |
| 106 | + |
| 107 | + function wethOutput(uint256 amount, address recipient) internal view returns (HostOrders.Output memory output) { |
| 108 | + return makeOutput(address(WETH), amount, recipient); |
| 109 | + } |
| 110 | + |
| 111 | + function ethOutput(uint256 amount, address recipient) internal pure returns (HostOrders.Output memory output) { |
| 112 | + return makeOutput(NATIVE_ASSET, amount, recipient); |
| 113 | + } |
| 114 | + |
| 115 | + function enterSignetToken(address token, uint256 amount) internal { |
| 116 | + if (token == NATIVE_ASSET) { |
| 117 | + enterSignetEth(amount); |
| 118 | + return; |
| 119 | + } |
| 120 | + IERC20(token).approve(address(PASSAGE), amount); |
| 121 | + PASSAGE.enterToken(selfOnL2(), token, amount); |
| 122 | + } |
| 123 | + |
| 124 | + function enterSignetEth(uint256 amount) internal { |
| 125 | + PASSAGE.enter{value: amount}(selfOnL2()); |
| 126 | + } |
| 127 | +} |
0 commit comments