@@ -98,7 +98,7 @@ abstract contract BasicSwap7683 is Base7683 {
9898 override
9999 {
100100 // at this point we are sure all orders are filled, use the first order to get the originDomain
101- // if some order differs on the originDomain ir can be re-settle later
101+ // if some order differs on the originDomain it can be re-settle later
102102 _dispatchSettle (OrderEncoder.decode (_ordersOriginData[0 ]).originDomain, _orderIds, _ordersFillerData);
103103 }
104104
@@ -108,7 +108,7 @@ abstract contract BasicSwap7683 is Base7683 {
108108 * @param _orderIds The IDs of the orders to refund.
109109 */
110110 function _refundOrders (OnchainCrossChainOrder[] memory _orders , bytes32 [] memory _orderIds ) internal override {
111- // at this point we are sure all orders are filled, use the first order to get the originDomain
111+ // at this point we are sure all orders are NOT filled, use the first order to get the originDomain
112112 // if some order differs on the originDomain ir can be re-refunded later
113113 _dispatchRefund (OrderEncoder.decode (_orders[0 ].orderData).originDomain, _orderIds);
114114 }
@@ -119,64 +119,110 @@ abstract contract BasicSwap7683 is Base7683 {
119119 * @param _orderIds The IDs of the orders to refund.
120120 */
121121 function _refundOrders (GaslessCrossChainOrder[] memory _orders , bytes32 [] memory _orderIds ) internal override {
122- // at this point we are sure all orders are filled, use the first order to get the originDomain
123- // if some order differs on the originDomain ir can be re-refunded later
122+ // at this point we are sure all orders are NOT filled, use the first order to get the originDomain
123+ // if some order differs on the originDomain it can be re-refunded later
124124 _dispatchRefund (OrderEncoder.decode (_orders[0 ].orderData).originDomain, _orderIds);
125125 }
126126
127127 /**
128128 * @dev Handles settling an individual order, should be called by the inheriting contract when receiving a setting
129129 * instruction from a remote chain.
130+ * @param _messageOrigin The domain from which the message originates (unused in this implementation).
131+ * @param _messageSender The address of the sender on the origin domain (unused in this implementation).
130132 * @param _orderId The ID of the order to settle.
131133 * @param _receiver The receiver address (encoded as bytes32).
132134 */
133- function _handleSettleOrder (bytes32 _orderId , bytes32 _receiver ) internal virtual {
134- // check if the order is opened to ensure it belongs to this domain, skip otherwise
135- if (orderStatus[_orderId] != OPENED) return ;
136-
137- (,bytes memory _orderData ) = abi.decode (openOrders[_orderId], (bytes32 , bytes ));
138- OrderData memory orderData = OrderEncoder.decode (_orderData);
135+ function _handleSettleOrder (
136+ uint32 _messageOrigin ,
137+ bytes32 _messageSender ,
138+ bytes32 _orderId ,
139+ bytes32 _receiver
140+ ) internal virtual {
141+ (
142+ bool validOpenOrder ,
143+ OrderData memory orderData
144+ ) = _isValidOpenOrder (_messageOrigin, _messageSender, _orderId);
145+
146+ if (! validOpenOrder) return ;
139147
140148 orderStatus[_orderId] = SETTLED;
141149
142150 address receiver = TypeCasts.bytes32ToAddress (_receiver);
143151 address inputToken = TypeCasts.bytes32ToAddress (orderData.inputToken);
144152
145- if (inputToken == address (0 )) {
146- Address.sendValue (payable (receiver), orderData.amountIn);
147- } else {
148- IERC20 (inputToken).safeTransfer (receiver, orderData.amountIn);
149- }
153+ _transferTokenOut (inputToken, receiver, orderData.amountIn);
150154
151155 emit Settled (_orderId, receiver);
152156 }
153157
154158 /**
155159 * @dev Handles refunding an individual order, should be called by the inheriting contract when receiving a
156160 * refunding instruction from a remote chain.
161+ * @param _messageOrigin The domain from which the message originates (unused in this implementation).
162+ * @param _messageSender The address of the sender on the origin domain (unused in this implementation).
157163 * @param _orderId The ID of the order to refund.
158164 */
159- function _handleRefundOrder (bytes32 _orderId ) internal virtual {
160- // check if the order is opened to ensure it belongs to this domain, skip otherwise
161- if (orderStatus[_orderId] != OPENED) return ;
165+ function _handleRefundOrder (uint32 _messageOrigin , bytes32 _messageSender , bytes32 _orderId ) internal virtual {
166+ (
167+ bool validOpenOrder ,
168+ OrderData memory orderData
169+ ) = _isValidOpenOrder (_messageOrigin, _messageSender, _orderId);
162170
163- (,bytes memory _orderData ) = abi.decode (openOrders[_orderId], (bytes32 , bytes ));
164- OrderData memory orderData = OrderEncoder.decode (_orderData);
171+ if (! validOpenOrder) return ;
165172
166173 orderStatus[_orderId] = REFUNDED;
167174
168175 address orderSender = TypeCasts.bytes32ToAddress (orderData.sender);
169176 address inputToken = TypeCasts.bytes32ToAddress (orderData.inputToken);
170177
171- if (inputToken == address (0 )) {
172- Address.sendValue (payable (orderSender), orderData.amountIn);
173- } else {
174- IERC20 (inputToken).safeTransfer (orderSender, orderData.amountIn);
175- }
178+ _transferTokenOut (inputToken, orderSender, orderData.amountIn);
176179
177180 emit Refunded (_orderId, orderSender);
178181 }
179182
183+ /**
184+ * @notice Validates an open order.
185+ * @dev Checks that the order is open and that its destination domain and settler match the provided parameters.
186+ * @param _messageOrigin The origin domain of the message.
187+ * @param _messageSender The sender identifier of the message.
188+ * @param _orderId The unique identifier of the order.
189+ * @return A boolean indicating if the order is valid, and the decoded OrderData structure.
190+ */
191+ function _isValidOpenOrder (
192+ uint32 _messageOrigin ,
193+ bytes32 _messageSender ,
194+ bytes32 _orderId
195+ ) internal virtual returns (bool , OrderData memory ) {
196+ OrderData memory orderData;
197+
198+ // check if the order is opened to ensure it belongs to this domain, skip otherwise
199+ if (orderStatus[_orderId] != OPENED) return (false , orderData);
200+
201+ (,bytes memory _orderData ) = abi.decode (openOrders[_orderId], (bytes32 , bytes ));
202+ orderData = OrderEncoder.decode (_orderData);
203+
204+ if (orderData.destinationDomain != _messageOrigin || orderData.destinationSettler != _messageSender)
205+ return (false , orderData);
206+
207+ return (true , orderData);
208+ }
209+
210+ /**
211+ * @notice Transfers tokens or ETH out of the contract.
212+ * @dev If _token is the zero address, transfers ETH using a safe method; otherwise, performs an ERC20 token
213+ * transfer.
214+ * @param _token The address of the token to transfer (use address(0) for ETH).
215+ * @param _to The recipient address.
216+ * @param _amount The amount of tokens or ETH to transfer.
217+ */
218+ function _transferTokenOut (address _token , address _to , uint256 _amount ) internal {
219+ if (_token == address (0 )) {
220+ Address.sendValue (payable (_to), _amount);
221+ } else {
222+ IERC20 (_token).safeTransfer (_to, _amount);
223+ }
224+ }
225+
180226 /**
181227 * @dev Gets the ID of a GaslessCrossChainOrder.
182228 * @param _order The GaslessCrossChainOrder to compute the ID for.
0 commit comments