Skip to content

Commit c8c5f5d

Browse files
committed
refactor: clean up the repo by creating l1 and l2 folders
1 parent 9e64cc7 commit c8c5f5d

File tree

7 files changed

+107
-82
lines changed

7 files changed

+107
-82
lines changed

foundry.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ libs = ["lib"]
66
via-ir = true
77

88
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
9+
10+
11+
[lint]
12+
exclude_lints = ["unwrapped-modifier-logic"]

src/apps/Morpho.sol

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ pragma solidity ^0.8.13;
33

44
import {IMorpho, MarketParams} from "../interfaces/IMorpho.sol";
55
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
6-
import {SignetStd} from "../SignetStd.sol";
6+
import {SafeERC20} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
7+
import {SignetL2} from "../l2/Signet.sol";
78
import {RollupOrders} from "zenith/src/orders/RollupOrders.sol";
8-
import {Passage} from "zenith/src/passage/Passage.sol";
99

1010
// How this works:
1111
// - The Signet Orders system calls `transferFrom` and check for the presence
@@ -38,68 +38,70 @@ import {Passage} from "zenith/src/passage/Passage.sol";
3838

3939
// The Rollup contract creates orders to interact with Morpho on host net via
4040
// the shortcut.
41-
contract UseMorpho is SignetStd {
41+
contract UseMorpho is SignetL2 {
42+
using SafeERC20 for IERC20;
43+
4244
/// @dev Address of the shortcut on the host chain.
43-
address immutable repayShortcut;
44-
address immutable supplyShortcut;
45-
address immutable borrowShortcut;
45+
address immutable REPAY_SHORTCUT;
46+
address immutable SUPPLY_SHORTCUT;
47+
address immutable BORROW_SHORTCUT;
4648

47-
address immutable hostLoanToken;
48-
address immutable hostCollateralToken;
49+
address immutable HOST_LOAN_TOKEN;
50+
address immutable HOST_COLLATERAL_TOKEN;
4951

50-
IERC20 immutable ruLoanToken;
51-
IERC20 immutable ruCollateralToken;
52+
IERC20 immutable RU_LOAN_TOKEN;
53+
IERC20 immutable RU_COLLATERAL_TOKEN;
5254

5355
constructor(address _repayShortcut, address _supplyShortcut, address _hostLoan, address _hostCollateral)
54-
SignetStd()
56+
SignetL2()
5557
{
56-
repayShortcut = _repayShortcut;
57-
supplyShortcut = _supplyShortcut;
58+
REPAY_SHORTCUT = _repayShortcut;
59+
SUPPLY_SHORTCUT = _supplyShortcut;
5860

5961
// Autodetect rollup tokens based on token addresses on host network.
60-
hostLoanToken = _hostLoan;
61-
hostCollateralToken = _hostCollateral;
62+
HOST_LOAN_TOKEN = _hostLoan;
63+
HOST_COLLATERAL_TOKEN = _hostCollateral;
6264
if (_hostLoan == HOST_WETH) {
63-
ruLoanToken = WETH;
65+
RU_LOAN_TOKEN = WETH;
6466
} else if (_hostLoan == HOST_WBTC) {
65-
ruLoanToken = WBTC;
67+
RU_LOAN_TOKEN = WBTC;
6668
} else if (_hostLoan == HOST_USDC || _hostLoan == HOST_USDT) {
67-
ruLoanToken = WUSD;
69+
RU_LOAN_TOKEN = WUSD;
6870
} else {
6971
revert("Unsupported loan token");
7072
}
7173
if (_hostCollateral == HOST_WETH) {
72-
ruCollateralToken = WETH;
74+
RU_COLLATERAL_TOKEN = WETH;
7375
} else if (_hostCollateral == HOST_WBTC) {
74-
ruCollateralToken = WBTC;
76+
RU_COLLATERAL_TOKEN = WBTC;
7577
} else if (_hostCollateral == HOST_USDC || _hostCollateral == HOST_USDT) {
76-
ruCollateralToken = WUSD;
78+
RU_COLLATERAL_TOKEN = WUSD;
7779
} else {
7880
revert("Unsupported collateral token");
7981
}
8082

8183
// Pre-emptively approve the Orders contract to spend our tokens.
82-
ruLoanToken.approve(address(ORDERS), type(uint256).max);
83-
ruCollateralToken.approve(address(ORDERS), type(uint256).max);
84+
RU_LOAN_TOKEN.approve(address(ORDERS), type(uint256).max);
85+
RU_COLLATERAL_TOKEN.approve(address(ORDERS), type(uint256).max);
8486
}
8587

8688
// Supply some amount of the collateral token on behalf of the user.
8789
function supplyCollateral(address onBehalf, uint256 amount) public {
8890
if (amount > 0) {
89-
ruCollateralToken.transferFrom(msg.sender, address(this), amount);
91+
RU_COLLATERAL_TOKEN.safeTransferFrom(msg.sender, address(this), amount);
9092
}
9193

9294
// the amount is whatever our current balance is
93-
amount = ruCollateralToken.balanceOf(address(this));
95+
amount = RU_COLLATERAL_TOKEN.balanceOf(address(this));
9496

9597
RollupOrders.Input[] memory inputs = new RollupOrders.Input[](1);
96-
inputs[0] = makeInput(address(ruCollateralToken), amount);
98+
inputs[0] = makeInput(address(RU_COLLATERAL_TOKEN), amount);
9799

98100
// The first output pays the collateral token to the shortcut.
99101
// The second output calls the shortcut to supply the collateral.
100102
RollupOrders.Output[] memory outputs = new RollupOrders.Output[](2);
101-
outputs[0] = makeHostOutput(address(hostCollateralToken), amount, supplyShortcut);
102-
outputs[1] = makeHostOutput(supplyShortcut, amount, onBehalf);
103+
outputs[0] = makeHostOutput(address(HOST_COLLATERAL_TOKEN), amount, SUPPLY_SHORTCUT);
104+
outputs[1] = makeHostOutput(SUPPLY_SHORTCUT, amount, onBehalf);
103105

104106
ORDERS.initiate(
105107
block.timestamp, // no deadline
@@ -111,20 +113,20 @@ contract UseMorpho is SignetStd {
111113
// Repay some amount of the loan token on behalf of the user.
112114
function repay(address onBehalf, uint256 amount) public {
113115
if (amount > 0) {
114-
ruLoanToken.transferFrom(msg.sender, address(this), amount);
116+
RU_LOAN_TOKEN.safeTransferFrom(msg.sender, address(this), amount);
115117
}
116118

117119
// Send all tokens.
118-
amount = ruLoanToken.balanceOf(address(this));
120+
amount = RU_LOAN_TOKEN.balanceOf(address(this));
119121

120122
RollupOrders.Input[] memory inputs = new RollupOrders.Input[](1);
121-
inputs[0] = makeInput(address(ruLoanToken), amount);
123+
inputs[0] = makeInput(address(RU_LOAN_TOKEN), amount);
122124

123125
// The first output pays the loan token to the shortcut.
124126
// The second output calls the shortcut to repay the loan.
125127
RollupOrders.Output[] memory outputs = new RollupOrders.Output[](2);
126-
outputs[0] = makeHostOutput(address(hostLoanToken), amount, repayShortcut);
127-
outputs[1] = makeHostOutput(repayShortcut, amount, onBehalf);
128+
outputs[0] = makeHostOutput(address(HOST_LOAN_TOKEN), amount, REPAY_SHORTCUT);
129+
outputs[1] = makeHostOutput(REPAY_SHORTCUT, amount, onBehalf);
128130

129131
ORDERS.initiate(
130132
block.timestamp, // no deadline
@@ -140,8 +142,8 @@ contract UseMorpho is SignetStd {
140142
// The first output calls the shortcut to borrow the loan.
141143
// The second output sends the loan token to the user on the rollup.
142144
RollupOrders.Output[] memory outputs = new RollupOrders.Output[](2);
143-
outputs[0] = makeHostOutput(borrowShortcut, amount, onBehalf);
144-
outputs[1] = makeRollupOutput(address(ruLoanToken), amount, msg.sender);
145+
outputs[0] = makeHostOutput(BORROW_SHORTCUT, amount, onBehalf);
146+
outputs[1] = makeRollupOutput(address(RU_LOAN_TOKEN), amount, msg.sender);
145147

146148
ORDERS.initiate(
147149
block.timestamp, // no deadline
@@ -160,29 +162,29 @@ contract UseMorpho is SignetStd {
160162
}
161163

162164
abstract contract HostMorphoUser {
163-
IMorpho immutable morpho;
165+
IMorpho immutable MORPHO;
164166

165167
// This is an unrolled MarketParams struct.
166-
IERC20 immutable loanToken;
167-
IERC20 immutable collateralToken;
168+
IERC20 immutable LOAN_TOKEN;
169+
IERC20 immutable COLLATERAL_TOKEN;
168170

169-
address immutable oracle;
170-
address immutable irm;
171-
uint256 immutable lltv;
171+
address immutable ORACLE;
172+
address immutable IRM;
173+
uint256 immutable LLTV;
172174

173175
error InsufficentTokensReceived(uint256 received, uint256 required);
174176

175177
constructor(IMorpho _morpho, MarketParams memory _params) {
176-
morpho = _morpho;
178+
MORPHO = _morpho;
177179

178-
loanToken = IERC20(_params.loanToken);
179-
collateralToken = IERC20(_params.collateralToken);
180-
oracle = _params.oracle;
181-
irm = _params.irm;
182-
lltv = _params.lltv;
180+
LOAN_TOKEN = IERC20(_params.loanToken);
181+
COLLATERAL_TOKEN = IERC20(_params.collateralToken);
182+
ORACLE = _params.oracle;
183+
IRM = _params.irm;
184+
LLTV = _params.lltv;
183185

184-
loanToken.approve(address(_morpho), type(uint256).max);
185-
collateralToken.approve(address(_morpho), type(uint256).max);
186+
LOAN_TOKEN.approve(address(_morpho), type(uint256).max);
187+
COLLATERAL_TOKEN.approve(address(_morpho), type(uint256).max);
186188
}
187189

188190
function checkReceived(uint256 received, uint256 required) internal pure {
@@ -192,11 +194,11 @@ abstract contract HostMorphoUser {
192194
}
193195

194196
function loadParams() internal view returns (MarketParams memory params) {
195-
params.loanToken = address(loanToken);
196-
params.collateralToken = address(collateralToken);
197-
params.oracle = oracle;
198-
params.irm = irm;
199-
params.lltv = lltv;
197+
params.loanToken = address(LOAN_TOKEN);
198+
params.collateralToken = address(COLLATERAL_TOKEN);
199+
params.oracle = ORACLE;
200+
params.irm = IRM;
201+
params.lltv = LLTV;
200202
}
201203
}
202204

@@ -208,9 +210,9 @@ contract HostMorphoRepay is HostMorphoUser {
208210
/// Uses the ERC20 transferFrom interface to invoke contract logic. This
209211
/// allows us to invoke logic from the Orders contract
210212
function transferFrom(address, address recipient, uint256 amount) external returns (bool) {
211-
uint256 loanTokenBalance = loanToken.balanceOf(address(this));
213+
uint256 loanTokenBalance = LOAN_TOKEN.balanceOf(address(this));
212214
checkReceived(loanTokenBalance, amount);
213-
morpho.repay(loadParams(), loanTokenBalance, 0, recipient, "");
215+
MORPHO.repay(loadParams(), loanTokenBalance, 0, recipient, "");
214216
return true;
215217
}
216218
}
@@ -221,11 +223,11 @@ contract HostMorphoSupply is HostMorphoUser {
221223
/// Uses the ERC20 transferFrom interface to invoke contract logic. This
222224
/// allows us to invoke logic from the Orders contract
223225
function transferFrom(address, address recipient, uint256 amount) external returns (bool) {
224-
uint256 collateralTokenBalance = collateralToken.balanceOf(address(this));
226+
uint256 collateralTokenBalance = COLLATERAL_TOKEN.balanceOf(address(this));
225227

226228
checkReceived(collateralTokenBalance, amount);
227229

228-
morpho.supplyCollateral(loadParams(), collateralTokenBalance, recipient, "");
230+
MORPHO.supplyCollateral(loadParams(), collateralTokenBalance, recipient, "");
229231

230232
// Future extension:
231233
// borrow some amount of loanToken
@@ -245,7 +247,7 @@ contract HosyMorphoBorrow is HostMorphoUser {
245247

246248
function transferFrom(address filler, address onBehalf, uint256 amount) external returns (bool) {
247249
// borrow some amount of loanToken
248-
morpho.borrow(loadParams(), amount, 0, onBehalf, address(this));
250+
MORPHO.borrow(loadParams(), amount, 0, onBehalf, address(this));
249251

250252
// User logic to use the tokens goes here.
251253
// Could send the tokens to the rollup via Passage, or do something

src/SignetStd.sol renamed to src/l2/Signet.sol

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import {RollupOrders} from "zenith/src/orders/RollupOrders.sol";
55
import {RollupPassage} from "zenith/src/passage/RollupPassage.sol";
66
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
77

8-
import {PecorinoConstants} from "./chains/Pecorino.sol";
8+
import {PecorinoConstants} from "../chains/Pecorino.sol";
99

10-
contract SignetStd {
11-
/// @notice The native asset address, used as a sentinel for native USD on
12-
/// the rollup, or native ETH on the host.
13-
address constant NATIVE_ASSET = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
10+
contract SignetL2 {
11+
/// @notice Sentinal value for the native asset in order inputs/outputs
12+
address constant NATIVE_ASSET = address(0);
1413

1514
/// @notice The chain ID of the host network.
1615
uint32 internal immutable HOST_CHAIN_ID;
@@ -36,6 +35,9 @@ contract SignetStd {
3635
/// @notice The WETH token address on the host network.
3736
address internal immutable HOST_WETH;
3837

38+
/// @notice Error for unsupported chain IDs.
39+
error UnsupportedChain(uint256);
40+
3941
constructor() {
4042
// Auto-configure based on the chain ID.
4143
if (block.chainid == PecorinoConstants.ROLLUP_CHAIN_ID) {
@@ -66,6 +68,14 @@ contract SignetStd {
6668
input.amount = amount;
6769
}
6870

71+
/// @notice Creates an Input struct for the native asset (ETH).
72+
/// @param amount The amount of the native asset (in wei).
73+
/// @return input The created Input struct for the native asset.
74+
function makeEthInput(uint256 amount) internal pure returns (RollupOrders.Input memory input) {
75+
input.token = address(0);
76+
input.amount = amount;
77+
}
78+
6979
/// @notice Creates an Output struct for the RollupOrders.
7080
/// @param token The address of the token.
7181
/// @param amount The amount of the token.
@@ -166,6 +176,6 @@ contract SignetStd {
166176
view
167177
returns (RollupOrders.Output memory output)
168178
{
169-
return makeHostOutput(NATIVE_ASSET, amount, recipient);
179+
return makeHostOutput(address(0), amount, recipient);
170180
}
171181
}

src/examples/Flash.sol renamed to src/l2/examples/Flash.sol

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
pragma solidity ^0.8.13;
33

44
import {RollupOrders} from "zenith/src/orders/RollupOrders.sol";
5-
import {SignetStd} from "../SignetStd.sol";
5+
import {SignetL2} from "../Signet.sol";
66

77
/// @notice This contract provides a modifier that allows functions to
88
/// utilize liquidity only during the duration of a function.
9-
abstract contract Flash is SignetStd {
9+
abstract contract Flash is SignetL2 {
1010
/// @notice This modifier enables a contract to access some amount only
1111
/// during function execution - the amount is received by the
1212
/// contract before the function executes, then is sent directly
@@ -18,7 +18,10 @@ abstract contract Flash is SignetStd {
1818
/// @param amount The amount of the asset to be flash held.
1919
modifier flash(address asset, uint256 amount) {
2020
_;
21+
_flash(asset, amount);
22+
}
2123

24+
function _flash(address asset, uint256 amount) internal {
2225
// Output is received *before* the modified function is called
2326
RollupOrders.Output[] memory outputs = new RollupOrders.Output[](1);
2427
outputs[0] = makeRollupOutput(asset, amount, address(this));

src/examples/GetOut.sol renamed to src/l2/examples/GetOut.sol

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
pragma solidity ^0.8.13;
33

44
import {RollupOrders} from "zenith/src/orders/RollupOrders.sol";
5-
import {SignetStd} from "../SignetStd.sol";
5+
import {SignetL2} from "../Signet.sol";
66

77
/// @title GetOut
88
/// @author init4
99
/// @notice A contract that gets out of the Rollup by converting native USD to
1010
/// to USDC on the host network.
1111
/// @dev This contract inherits the SignetStd contract and automatically
1212
/// configures rollup constants on construction.
13-
contract GetOut is SignetStd {
13+
contract GetOut is SignetL2 {
1414
/// @notice Thrown when no value is sent to the contract.
1515
error MissingValue();
1616

@@ -30,14 +30,12 @@ contract GetOut is SignetStd {
3030
uint256 desired = msg.value * 995 / 1000; // 0.5% fee
3131

3232
RollupOrders.Input[] memory inputs = new RollupOrders.Input[](1);
33-
inputs[0] = makeInput(NATIVE_ASSET, msg.value);
33+
inputs[0] = makeEthInput(msg.value);
3434

3535
RollupOrders.Output[] memory outputs = new RollupOrders.Output[](1);
3636
outputs[0] = hostUsdcOutput(desired, msg.sender);
3737

38-
ORDERS.initiate{
39-
value: msg.value
40-
}(
38+
ORDERS.initiate{value: msg.value}(
4139
block.timestamp, // this is equivalent to no deadline
4240
inputs,
4341
outputs

src/examples/PayMe.sol renamed to src/l2/examples/PayMe.sol

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
pragma solidity ^0.8.13;
33

44
import {RollupOrders} from "zenith/src/orders/RollupOrders.sol";
5-
import {SignetStd} from "../SignetStd.sol";
5+
import {SignetL2} from "../Signet.sol";
66

77
/// @notice This contract provides a modifier that allows functions to be gated
88
/// by requiring a payment of a specified amount of native asset.
9-
abstract contract PayMe is SignetStd {
9+
abstract contract PayMe is SignetL2 {
1010
/// @notice This modifier crates an order with no input, that pays the
1111
/// specified amount of native asset to the contract. It can be used
1212
/// to gate access to payment-gate functions.
@@ -19,9 +19,14 @@ abstract contract PayMe is SignetStd {
1919
/// transaction by deducting it from the payment amount.
2020
modifier payMeSubsidizedGas(uint256 amount) {
2121
uint256 pre = gasleft();
22-
uint256 gp = tx.gasprice;
2322
_;
23+
_payMeSubsidizedGasAfter(pre, amount);
24+
}
25+
26+
/// @notice This silences spurious foundry warnings.
27+
function _payMeSubsidizedGasAfter(uint256 pre, uint256 amount) internal {
2428
uint256 post = gasleft();
29+
uint256 gp = tx.gasprice;
2530
uint256 loot = amount - (gp * (pre - post));
2631
demandPayment(NATIVE_ASSET, loot);
2732
}

0 commit comments

Comments
 (0)