Skip to content

Commit 4a5e192

Browse files
authored
Merge pull request #4 from init4tech/anna/z2
update hash construction, add tests
2 parents 8b27dc8 + aec39db commit 4a5e192

File tree

3 files changed

+298
-200
lines changed

3 files changed

+298
-200
lines changed

src/Passage.sol

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,79 @@ pragma solidity ^0.8.24;
44
// import IERC20 from OpenZeppelin
55
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
66

7-
// @notice A contract deployed to Host chain that allows tokens to enter the rollup,
8-
// and enables Builders to fulfill requests to exchange tokens on the Rollup for tokens on the Host.
7+
/// @notice A contract deployed to Host chain that allows tokens to enter the rollup,
8+
/// and enables Builders to fulfill requests to exchange tokens on the Rollup for tokens on the Host.
99
contract HostPassage {
10-
// @notice Thrown when attempting to fulfill an exit order with a deadline that has passed.
11-
error Expired();
10+
/// @notice Thrown when attempting to fulfill an exit order with a deadline that has passed.
11+
error OrderExpired();
1212

13-
// @notice Emitted when tokens enter the rollup.
14-
// @param token - The address of the token entering the rollup.
15-
// @param rollupRecipient - The recipient of the token on the rollup.
16-
// @param amount - The amount of the token entering the rollup.
13+
/// @notice Emitted when tokens enter the rollup.
14+
/// @param token - The address of the token entering the rollup.
15+
/// @param rollupRecipient - The recipient of the token on the rollup.
16+
/// @param amount - The amount of the token entering the rollup.
1717
event Enter(address indexed token, address indexed rollupRecipient, uint256 amount);
1818

19-
// @notice Emitted when an exit order is fulfilled by the Builder.
20-
// @param token - The address of the token transferred to the recipient.
21-
// @param hostRecipient - The recipient of the token on host.
22-
// @param amount - The amount of the token transferred to the recipient.
19+
/// @notice Emitted when an exit order is fulfilled by the Builder.
20+
/// @param token - The address of the token transferred to the recipient.
21+
/// @param hostRecipient - The recipient of the token on host.
22+
/// @param amount - The amount of the token transferred to the recipient.
2323
event ExitFilled(address indexed token, address indexed hostRecipient, uint256 amount);
2424

25-
// @notice Details of an exit order to be fulfilled by the Builder.
26-
// @param token - The address of the token to be transferred to the recipient.
27-
// If token is the zero address, the amount is native Ether.
28-
// Corresponds to tokenOut_H in the RollupPassage contract.
29-
// @param recipient - The recipient of the token on host.
30-
// Corresponds to recipient_H in the RollupPassage contract.
31-
// @param amount - The amount of the token to be transferred to the recipient.
32-
// Corresponds to one or more amountOutMinimum_H in the RollupPassage contract.
33-
// @param deadline - The deadline by which the exit order must be fulfilled.
34-
// Corresponds to deadline in the RollupPassage contract.
35-
// If the ExitOrder is a combination of multiple orders, the deadline SHOULD be the latest of all orders.
25+
/// @notice Details of an exit order to be fulfilled by the Builder.
26+
/// @param token - The address of the token to be transferred to the recipient.
27+
/// If token is the zero address, the amount is native Ether.
28+
/// Corresponds to tokenOut_H in the RollupPassage contract.
29+
/// @param recipient - The recipient of the token on host.
30+
/// Corresponds to recipient_H in the RollupPassage contract.
31+
/// @param amount - The amount of the token to be transferred to the recipient.
32+
/// Corresponds to one or more amountOutMinimum_H in the RollupPassage contract.
33+
/// @param deadline - The deadline by which the exit order must be fulfilled.
34+
/// Corresponds to deadline in the RollupPassage contract.
35+
/// If the ExitOrder is a combination of multiple orders, the deadline SHOULD be the latest of all orders.
3636
struct ExitOrder {
3737
address token;
3838
address recipient;
3939
uint256 amount;
4040
uint256 deadline;
4141
}
4242

43-
// @notice Allows native Ether to enter the rollup by being sent directly to the contract.
43+
/// @notice Allows native Ether to enter the rollup by being sent directly to the contract.
4444
fallback() external payable {
4545
enter(msg.sender);
4646
}
4747

48-
// @notice Allows native Ether to enter the rollup by being sent directly to the contract.
48+
/// @notice Allows native Ether to enter the rollup by being sent directly to the contract.
4949
receive() external payable {
5050
enter(msg.sender);
5151
}
5252

53-
// @notice Allows native Ether to enter the rollup.
54-
// @dev Permanently burns the entire msg.value by locking it in this contract.
55-
// @param rollupRecipient - The recipient of the Ether on the rollup.
56-
// @custom:emits Enter indicatig the amount of Ether to mint on the rollup & its recipient.
53+
/// @notice Allows native Ether to enter the rollup.
54+
/// @dev Permanently burns the entire msg.value by locking it in this contract.
55+
/// @param rollupRecipient - The recipient of the Ether on the rollup.
56+
/// @custom:emits Enter indicatig the amount of Ether to mint on the rollup & its recipient.
5757
function enter(address rollupRecipient) public payable {
5858
emit Enter(address(0), rollupRecipient, msg.value);
5959
}
6060

61-
// @notice Fulfills exit orders by transferring tokenOut to the recipient
62-
// @param orders The exit orders to fulfill
63-
// @custom:emits ExitFilled for each exit order fulfilled.
64-
// @dev Called by the Builder atomically with a transaction calling `submitBlock`.
65-
// The user-submitted transactions initiating the ExitOrders on the rollup
66-
// must be included by the Builder in the rollup block submitted via `submitBlock`.
67-
// @dev The user transfers tokenIn on the rollup, and receives tokenOut on host.
68-
// @dev The Builder receives tokenIn on the rollup, and transfers tokenOut to the user on host.
69-
// @dev The rollup STF MUST NOT apply `submitExit` transactions to the rollup state
70-
// UNLESS a corresponding ExitFilled event is emitted on host in the same block.
71-
// @dev If the user submits multiple exit transactions for the same token in the same rollup block,
72-
// the Builder may transfer the cumulative tokenOut to the user in a single ExitFilled event.
73-
// The rollup STF will apply the user's exit transactions on the rollup up to the point that sum(tokenOut) is lte the ExitFilled amount.
74-
// TODO: add option to fulfill ExitOrders with native ETH? or is it sufficient to only allow users to exit via WETH?
61+
/// @notice Fulfills exit orders by transferring tokenOut to the recipient
62+
/// @param orders The exit orders to fulfill
63+
/// @custom:emits ExitFilled for each exit order fulfilled.
64+
/// @dev Called by the Builder atomically with a transaction calling `submitBlock`.
65+
/// The user-submitted transactions initiating the ExitOrders on the rollup
66+
/// must be included by the Builder in the rollup block submitted via `submitBlock`.
67+
/// @dev The user transfers tokenIn on the rollup, and receives tokenOut on host.
68+
/// @dev The Builder receives tokenIn on the rollup, and transfers tokenOut to the user on host.
69+
/// @dev The rollup STF MUST NOT apply `submitExit` transactions to the rollup state
70+
/// UNLESS a corresponding ExitFilled event is emitted on host in the same block.
71+
/// @dev If the user submits multiple exit transactions for the same token in the same rollup block,
72+
/// the Builder may transfer the cumulative tokenOut to the user in a single ExitFilled event.
73+
/// The rollup STF will apply the user's exit transactions on the rollup up to the point that sum(tokenOut) is lte the ExitFilled amount.
74+
/// TODO: add option to fulfill ExitOrders with native ETH? or is it sufficient to only allow users to exit via WETH?
7575
function fulfillExits(ExitOrder[] calldata orders) external {
7676
for (uint256 i = 0; i < orders.length; i++) {
7777
ExitOrder memory order = orders[i];
7878
// check that the deadline hasn't passed
79-
if (block.timestamp >= order.deadline) revert Expired();
79+
if (block.timestamp > order.deadline) revert OrderExpired();
8080
// transfer tokens to the recipient
8181
IERC20(order.token).transferFrom(msg.sender, order.recipient, order.amount);
8282
// emit
@@ -85,13 +85,13 @@ contract HostPassage {
8585
}
8686
}
8787

88-
// A contract deployed to the Rollup that allows users to atomically exchange tokens on the Rollup for tokens on the Host.
88+
/// @notice A contract deployed to the Rollup that allows users to atomically exchange tokens on the Rollup for tokens on the Host.
8989
contract RollupPassage {
90-
// @notice Thrown when an exit tranaction is submitted with a deadline that has passed.
91-
error Expired();
90+
/// @notice Thrown when an exit transaction is submitted with a deadline that has passed.
91+
error OrderExpired();
9292

93-
// @notice Emitted when an exit order is submitted & successfully processed, indicating it was also fulfilled on host.
94-
// @dev See `submitExit` for parameter docs.
93+
/// @notice Emitted when an exit order is submitted & successfully processed, indicating it was also fulfilled on host.
94+
/// @dev See `submitExit` for parameter docs.
9595
event Exit(
9696
address indexed tokenIn_RU,
9797
address indexed tokenOut_H,
@@ -101,31 +101,31 @@ contract RollupPassage {
101101
uint256 amountOutMinimum_H
102102
);
103103

104-
// @notice Emitted when tokens or native Ether is swept from the contract.
105-
// @dev Intended to improve visibility for Builders to ensure Sweep isn't called unexpectedly.
106-
// Intentionally does not bother to emit which token(s) were swept, nor their amounts.
104+
/// @notice Emitted when tokens or native Ether is swept from the contract.
105+
/// @dev Intended to improve visibility for Builders to ensure Sweep isn't called unexpectedly.
106+
/// Intentionally does not bother to emit which token(s) were swept, nor their amounts.
107107
event Sweep(address indexed recipient);
108108

109-
// @notice Expresses an intent to exit the rollup with ERC20s.
110-
// @dev Exits are modeled as a swap between two tokens.
111-
// tokenIn_RU is provided on the rollup; in exchange,
112-
// tokenOut_H is expected to be received on host.
113-
// Exits may "swap" native rollup Ether for host WETH -
114-
// two assets that represent the same underlying token and should have roughly the same value -
115-
// or they may be a more "true" swap of rollup USDC for host WETH.
116-
// Fees paid to the Builders for fulfilling the exit orders
117-
// can be included within the "exchange rate" between tokenIn and tokenOut.
118-
// @dev The Builder claims the tokenIn_RU from the contract by submitting a transaction to `sweep` the tokens within the same block.
119-
// @dev The Rollup STF MUST NOT apply `submitExit` transactions to the rollup state
120-
// UNLESS a sufficient ExitFilled event is emitted on host within the same block.
121-
// @param tokenIn_RU - The address of the token the user supplies as the input on the rollup for the trade.
122-
// @param tokenOut_H - The address of the token the user expects to receive on host.
123-
// @param recipient_H - The address of the recipient of tokenOut_H on host.
124-
// @param deadline - The deadline by which the exit order must be fulfilled.
125-
// @param amountIn_RU - The amount of tokenIn_RU the user supplies as the input on the rollup for the trade.
126-
// @param amountOutMinimum_H - The minimum amount of tokenOut_H the user expects to receive on host.
127-
// @custom:reverts Expired if the deadline has passed.
128-
// @custom:emits Exit if the exit transaction succeeds.
109+
/// @notice Expresses an intent to exit the rollup with ERC20s.
110+
/// @dev Exits are modeled as a swap between two tokens.
111+
/// tokenIn_RU is provided on the rollup; in exchange,
112+
/// tokenOut_H is expected to be received on host.
113+
/// Exits may "swap" native rollup Ether for host WETH -
114+
/// two assets that represent the same underlying token and should have roughly the same value -
115+
/// or they may be a more "true" swap of rollup USDC for host WETH.
116+
/// Fees paid to the Builders for fulfilling the exit orders
117+
/// can be included within the "exchange rate" between tokenIn and tokenOut.
118+
/// @dev The Builder claims the tokenIn_RU from the contract by submitting a transaction to `sweep` the tokens within the same block.
119+
/// @dev The Rollup STF MUST NOT apply `submitExit` transactions to the rollup state
120+
/// UNLESS a sufficient ExitFilled event is emitted on host within the same block.
121+
/// @param tokenIn_RU - The address of the token the user supplies as the input on the rollup for the trade.
122+
/// @param tokenOut_H - The address of the token the user expects to receive on host.
123+
/// @param recipient_H - The address of the recipient of tokenOut_H on host.
124+
/// @param deadline - The deadline by which the exit order must be fulfilled.
125+
/// @param amountIn_RU - The amount of tokenIn_RU the user supplies as the input on the rollup for the trade.
126+
/// @param amountOutMinimum_H - The minimum amount of tokenOut_H the user expects to receive on host.
127+
/// @custom:reverts Expired if the deadline has passed.
128+
/// @custom:emits Exit if the exit transaction succeeds.
129129
function submitExit(
130130
address tokenIn_RU,
131131
address tokenOut_H,
@@ -135,7 +135,7 @@ contract RollupPassage {
135135
uint256 amountOutMinimum_H
136136
) external {
137137
// check that the deadline hasn't passed
138-
if (block.timestamp >= deadline) revert Expired();
138+
if (block.timestamp >= deadline) revert OrderExpired();
139139

140140
// transfer the tokens from the user to the contract
141141
IERC20(tokenIn_RU).transferFrom(msg.sender, address(this), amountIn_RU);
@@ -144,33 +144,33 @@ contract RollupPassage {
144144
emit Exit(tokenIn_RU, tokenOut_H, recipient_H, deadline, amountIn_RU, amountOutMinimum_H);
145145
}
146146

147-
// @notice Expresses an intent to exit the rollup with native Ether.
148-
// @dev See `submitExit` above for dev details on how exits work.
149-
// @dev tokenIn_RU is set to address(0), native rollup Ether.
150-
// amountIn_RU is set to msg.value.
151-
// @param tokenOut_H - The address of the token the user expects to receive on host.
152-
// @param recipient_H - The address of the recipient of tokenOut_H on host.
153-
// @param deadline - The deadline by which the exit order must be fulfilled.
154-
// @param amountOutMinimum_H - The minimum amount of tokenOut_H the user expects to receive on host.
155-
// @custom:reverts Expired if the deadline has passed.
156-
// @custom:emits Exit if the exit transaction succeeds.
147+
/// @notice Expresses an intent to exit the rollup with native Ether.
148+
/// @dev See `submitExit` above for dev details on how exits work.
149+
/// @dev tokenIn_RU is set to address(0), native rollup Ether.
150+
/// amountIn_RU is set to msg.value.
151+
/// @param tokenOut_H - The address of the token the user expects to receive on host.
152+
/// @param recipient_H - The address of the recipient of tokenOut_H on host.
153+
/// @param deadline - The deadline by which the exit order must be fulfilled.
154+
/// @param amountOutMinimum_H - The minimum amount of tokenOut_H the user expects to receive on host.
155+
/// @custom:reverts Expired if the deadline has passed.
156+
/// @custom:emits Exit if the exit transaction succeeds.
157157
function submitExit(address tokenOut_H, address recipient_H, uint256 deadline, uint256 amountOutMinimum_H)
158158
external
159159
payable
160160
{
161161
// check that the deadline hasn't passed
162-
if (block.timestamp >= deadline) revert Expired();
162+
if (block.timestamp >= deadline) revert OrderExpired();
163163

164164
// emit the exit event
165165
emit Exit(address(0), tokenOut_H, recipient_H, deadline, msg.value, amountOutMinimum_H);
166166
}
167167

168-
// @notice Transfer the entire balance of tokens to the recipient.
169-
// @dev Called by the Builder within the same block as `submitExit` transactions to claim the amounts of `tokenIn`.
170-
// @dev Builder MUST ensure that no other account calls `sweep` before them.
171-
// @param recipient - The address to receive the tokens.
172-
// @param tokens - The addresses of the tokens to transfer.
173-
// TODO: should there be more granular control for the builder to specify a different recipient for each token?
168+
/// @notice Transfer the entire balance of tokens to the recipient.
169+
/// @dev Called by the Builder within the same block as `submitExit` transactions to claim the amounts of `tokenIn`.
170+
/// @dev Builder MUST ensure that no other account calls `sweep` before them.
171+
/// @param recipient - The address to receive the tokens.
172+
/// @param tokens - The addresses of the tokens to transfer.
173+
/// TODO: should there be more granular control for the builder to specify a different recipient for each token?
174174
function sweep(address recipient, address[] calldata tokens) public {
175175
for (uint256 i = 0; i < tokens.length; i++) {
176176
IERC20 token = IERC20(tokens[i]);
@@ -179,10 +179,10 @@ contract RollupPassage {
179179
emit Sweep(recipient);
180180
}
181181

182-
// @notice Transfer the entire balance of native Ether to the recipient.
183-
// @dev Called by the Builder within the same block as `submitExit` transactions to claim the amounts of native Ether.
184-
// @dev Builder MUST ensure that no other account calls `sweepETH` before them.
185-
// @param recipient - The address to receive the native Ether.
182+
/// @notice Transfer the entire balance of native Ether to the recipient.
183+
/// @dev Called by the Builder within the same block as `submitExit` transactions to claim the amounts of native Ether.
184+
/// @dev Builder MUST ensure that no other account calls `sweepETH` before them.
185+
/// @param recipient - The address to receive the native Ether.
186186
function sweepETH(address payable recipient) public {
187187
recipient.transfer(address(this).balance);
188188
emit Sweep(recipient);

0 commit comments

Comments
 (0)