Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 686dc8c

Browse files
committedFeb 26, 2025·
refactor: consolidate proof validation logic for settlement and refund handlers
1 parent ff3b2a8 commit 686dc8c

File tree

1 file changed

+49
-69
lines changed

1 file changed

+49
-69
lines changed
 

‎solidity/src/Polymer7683.sol

+49-69
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ICrossL2ProverV2 } from "@polymerdao/prover-contracts/interfaces/ICross
55
import { OrderData, OrderEncoder } from "./libs/OrderEncoder.sol";
66
import { BasicSwap7683 } from "./BasicSwap7683.sol";
77
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
8+
import { TypeCasts } from "@hyperlane-xyz/libs/TypeCasts.sol";
89

910
/**
1011
* @title Polymer7683
@@ -67,31 +68,57 @@ contract Polymer7683 is BasicSwap7683, Ownable {
6768
* @param eventProof The proof of the Fill event from the destination chain
6869
* @param eventProof The proof of the Fill event from the destination chain
6970
*/
70-
function handleSettlementWithProof(
71-
bytes calldata eventProof
72-
) external {
73-
// 1. Verify event using Polymer prover
71+
function handleSettlementWithProof(bytes calldata eventProof) external {
72+
// Verify event using Polymer prover
7473
(
75-
bytes32 eventOrderId,
76-
, // originData
77-
bytes memory fillerData
78-
) = _validateSettlementProof(eventProof);
74+
uint32 provenChainId,
75+
address emitter,
76+
, // topics
77+
bytes memory data
78+
) = prover.validateEvent(eventProof);
79+
80+
// Verify destination contract is registered for the proven chain
81+
address expectedEmitter = destinationContracts[provenChainId];
82+
if (expectedEmitter == address(0)) revert UnregisteredDestinationChain();
83+
84+
// Validate emitter matches registered destination
85+
if (emitter != expectedEmitter) revert InvalidEmitter();
7986

80-
// 2. Process settlement - this function internally checks if the order is OPENED
81-
_handleSettleOrder(eventOrderId, abi.decode(fillerData, (bytes32)));
87+
// Decode data from the Filled event format
88+
(bytes32 eventOrderId, bytes memory originData, bytes memory fillerData) = abi.decode(data, (bytes32, bytes, bytes));
89+
90+
// Process settlement with the message origin, sender, order ID, and receiver
91+
_handleSettleOrder(
92+
provenChainId,
93+
TypeCasts.addressToBytes32(emitter),
94+
eventOrderId,
95+
abi.decode(fillerData, (bytes32))
96+
);
8297
}
8398

8499
/**
85100
* @notice Process a refund proof from a destination chain
86101
* @param orderId The order ID being refunded
87102
* @param eventProof The proof of the Refund event from the destination chain
88103
*/
89-
function handleRefundWithProof(
90-
bytes32 orderId,
91-
bytes calldata eventProof
92-
) external {
93-
// 1. Validate order ID is in the refunded set
94-
bytes32[] memory eventOrderIds = _validateRefundProof(eventProof);
104+
function handleRefundWithProof(bytes32 orderId, bytes calldata eventProof) external {
105+
// Verify event using Polymer prover
106+
(
107+
uint32 provenChainId,
108+
address emitter,
109+
, // topics
110+
bytes memory data
111+
) = prover.validateEvent(eventProof);
112+
113+
// Verify destination contract is registered for the proven chain
114+
address expectedEmitter = destinationContracts[provenChainId];
115+
if (expectedEmitter == address(0)) revert UnregisteredDestinationChain();
116+
117+
// Validate emitter matches registered destination
118+
if (emitter != expectedEmitter) revert InvalidEmitter();
119+
120+
// Decode refund-specific data and validate order ID
121+
bytes32[] memory eventOrderIds = abi.decode(data, (bytes32[]));
95122
bool found = false;
96123
for (uint256 i = 0; i < eventOrderIds.length; i++) {
97124
if (eventOrderIds[i] == orderId) {
@@ -101,8 +128,12 @@ contract Polymer7683 is BasicSwap7683, Ownable {
101128
}
102129
if (!found) revert InvalidEventData();
103130

104-
// 2. Process refund for the order
105-
_handleRefundOrder(orderId);
131+
// Process refund with message origin, sender, and order ID
132+
_handleRefundOrder(
133+
provenChainId,
134+
TypeCasts.addressToBytes32(emitter),
135+
orderId
136+
);
106137
}
107138

108139
// ============ Internal Functions ============
@@ -141,55 +172,4 @@ contract Polymer7683 is BasicSwap7683, Ownable {
141172
return uint32(localChainId);
142173
}
143174

144-
function _validateSettlementProof(
145-
bytes calldata eventProof
146-
) private view returns (
147-
bytes32 eventOrderId,
148-
bytes memory originData,
149-
bytes memory fillerData
150-
) {
151-
(
152-
uint32 provenChainId,
153-
address actualEmitter,
154-
, // topics
155-
bytes memory data
156-
) = prover.validateEvent(eventProof);
157-
158-
// Decode data from the Filled event format
159-
(eventOrderId, originData, fillerData) = abi.decode(data, (bytes32, bytes, bytes));
160-
161-
// Validate chain ID with origin data
162-
OrderData memory orderData = OrderEncoder.decode(originData);
163-
if (provenChainId != orderData.destinationDomain) {
164-
revert InvalidChainId();
165-
}
166-
167-
// Verify destination contract is registered for the proven chain
168-
address expectedEmitter = destinationContracts[provenChainId];
169-
if (expectedEmitter == address(0)) revert UnregisteredDestinationChain();
170-
171-
// Validate emitter matches registered destination
172-
if (actualEmitter != expectedEmitter) revert InvalidEmitter();
173-
}
174-
175-
function _validateRefundProof(
176-
bytes calldata eventProof
177-
) private view returns (bytes32[] memory eventOrderIds) {
178-
(
179-
uint32 provenChainId,
180-
address actualEmitter, // Now using this parameter
181-
, // topics
182-
bytes memory data
183-
) = prover.validateEvent(eventProof);
184-
185-
// Verify destination contract is registered for the proven chain
186-
address expectedEmitter = destinationContracts[provenChainId];
187-
if (expectedEmitter == address(0)) revert UnregisteredDestinationChain();
188-
189-
// Validate emitter matches registered destination
190-
if (actualEmitter != expectedEmitter) revert InvalidEmitter();
191-
192-
// Decode refund-specific data
193-
eventOrderIds = abi.decode(data, (bytes32[]));
194-
}
195175
}

0 commit comments

Comments
 (0)
Please sign in to comment.