@hyperbridge/core 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134,14 +134,15 @@ abstract contract HyperApp is IApp {
134
134
  * If the payer is not this contract, transfers fee tokens from the payer to this contract first.
135
135
  * @param request The POST request to dispatch containing destination, body, timeout, and fee parameters
136
136
  * @param payer The address that will pay the fee token. If different from this contract, must have approved this contract to spend the fee amount
137
+ * @return commitment The unique identifier for the dispatched request
137
138
  */
138
- function dispatchWithFeeToken(DispatchPost memory request, address payer) internal {
139
+ function dispatchWithFeeToken(DispatchPost memory request, address payer) internal returns (bytes32) {
139
140
  address hostAddr = host();
140
141
  address feeToken = IDispatcher(hostAddr).feeToken();
141
142
  uint256 fee = quote(request);
142
143
  if (payer != address(this)) IERC20(feeToken).safeTransferFrom(payer, address(this), fee);
143
144
  IERC20(feeToken).forceApprove(hostAddr, fee);
144
- IDispatcher(hostAddr).dispatch(request);
145
+ return IDispatcher(hostAddr).dispatch(request);
145
146
  }
146
147
 
147
148
  /**
@@ -150,14 +151,15 @@ abstract contract HyperApp is IApp {
150
151
  * If the payer is not this contract, transfers fee tokens from the payer to this contract first.
151
152
  * @param response The POST response to dispatch containing the original request, response data, timeout, and fee parameters
152
153
  * @param payer The address that will pay the fee token. If different from this contract, must have approved this contract to spend the fee amount
154
+ * @return commitment The unique identifier for the dispatched response
153
155
  */
154
- function dispatchWithFeeToken(DispatchPostResponse memory response, address payer) internal {
156
+ function dispatchWithFeeToken(DispatchPostResponse memory response, address payer) internal returns (bytes32) {
155
157
  address hostAddr = host();
156
158
  address feeToken = IDispatcher(hostAddr).feeToken();
157
159
  uint256 fee = quote(response);
158
160
  if (payer != address(this)) IERC20(feeToken).safeTransferFrom(payer, address(this), fee);
159
161
  IERC20(feeToken).forceApprove(hostAddr, fee);
160
- IDispatcher(hostAddr).dispatch(response);
162
+ return IDispatcher(hostAddr).dispatch(response);
161
163
  }
162
164
 
163
165
  /**
@@ -166,14 +168,15 @@ abstract contract HyperApp is IApp {
166
168
  * If the payer is not this contract, transfers fee tokens from the payer to this contract first.
167
169
  * @param request The GET request to dispatch containing destination, keys, height, timeout, and fee parameters
168
170
  * @param payer The address that will pay the fee token. If different from this contract, must have approved this contract to spend the fee amount
171
+ * @return commitment The unique identifier for the dispatched request
169
172
  */
170
- function dispatchWithFeeToken(DispatchGet memory request, address payer) internal {
173
+ function dispatchWithFeeToken(DispatchGet memory request, address payer) internal returns (bytes32) {
171
174
  address hostAddr = host();
172
175
  address feeToken = IDispatcher(hostAddr).feeToken();
173
176
  uint256 fee = quote(request);
174
177
  if (payer != address(this)) IERC20(feeToken).safeTransferFrom(payer, address(this), fee);
175
178
  IERC20(feeToken).forceApprove(hostAddr, fee);
176
- IDispatcher(hostAddr).dispatch(request);
179
+ return IDispatcher(hostAddr).dispatch(request);
177
180
  }
178
181
 
179
182
  /**
@@ -0,0 +1,362 @@
1
+ // Copyright (C) Polytope Labs Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ pragma solidity ^0.8.24;
16
+
17
+ /**
18
+ * @notice Tokens that must be received for a valid order fulfillment
19
+ */
20
+ struct PaymentInfo {
21
+ /// @dev The address to receive the output tokens
22
+ bytes32 beneficiary;
23
+ /// @dev The assets to be provided by the filler
24
+ TokenInfo[] assets;
25
+ /// @dev Optional calldata to be executed on the destination chain
26
+ bytes call;
27
+ }
28
+
29
+ /**
30
+ * @notice Tokens that must be escrowed for an order
31
+ */
32
+ struct TokenInfo {
33
+ /// @dev The address of the ERC20 token on the destination chain
34
+ /// @dev address(0) used as a sentinel for the native token
35
+ bytes32 token;
36
+ /// @dev The amount of the token to be sent
37
+ uint256 amount;
38
+ }
39
+
40
+ struct DispatchInfo {
41
+ /// @dev Assets to execute a predispatch call with
42
+ TokenInfo[] assets;
43
+ /// @dev The actual call data to be executed
44
+ bytes call;
45
+ }
46
+
47
+ /**
48
+ * @dev Represents an order in the IntentGateway module.
49
+ * @param Order The structure defining an order.
50
+ */
51
+ struct Order {
52
+ /// @dev The address of the user who is initiating the transfer
53
+ bytes32 user;
54
+ /// @dev The state machine identifier of the origin chain
55
+ bytes source;
56
+ /// @dev The state machine identifier of the destination chain
57
+ bytes destination;
58
+ /// @dev The block number by which the order must be filled on the destination chain
59
+ uint256 deadline;
60
+ /// @dev The nonce of the order
61
+ uint256 nonce;
62
+ /// @dev Represents the dispatch fees associated with the IntentGateway.
63
+ uint256 fees;
64
+ /// @dev Optional session key used to select winning solver.
65
+ address session;
66
+ /// @dev The predispatch information for the order
67
+ /// This is used to encode any calls before the order is placed
68
+ DispatchInfo predispatch;
69
+ /// @dev The tokens that are escrowed for the filler.
70
+ TokenInfo[] inputs;
71
+ /// @dev The filler output, ie the tokens that the filler will provide
72
+ PaymentInfo output;
73
+ }
74
+
75
+ /**
76
+ * @dev Request from hyperbridge for sweeping accumulated dust
77
+ */
78
+ struct SweepDust {
79
+ /// @dev The address of the beneficiary of the protocol fee
80
+ address beneficiary;
81
+ /// @dev The tokens to be withdrawn
82
+ TokenInfo[] outputs;
83
+ }
84
+
85
+ /**
86
+ * @dev Struct to define the parameters for the IntentGateway module.
87
+ */
88
+ struct Params {
89
+ /// @dev The address of the host contract
90
+ address host;
91
+ /// @dev Address of the dispatcher contract responsible for handling intents.
92
+ address dispatcher;
93
+ /// @dev Flag indicating whether solver selection is enabled.
94
+ bool solverSelection;
95
+ /// @dev The percentage of surplus (in basis points) that goes to the protocol. The rest goes to beneficiary.
96
+ /// 10000 = 100%, 5000 = 50%, etc.
97
+ uint256 surplusShareBps;
98
+ /// @dev The protocol fee in basis points charged on order inputs.
99
+ /// 10000 = 100%, 100 = 1%, etc.
100
+ uint256 protocolFeeBps;
101
+ }
102
+
103
+ /**
104
+ * @dev Struct representing the body of a request.
105
+ */
106
+ struct RequestBody {
107
+ /// @dev Represents the commitment of an order. This is typically a hash that uniquely identifies the order.
108
+ bytes32 commitment;
109
+ /// @dev Stores the identifier for the beneficiary.
110
+ bytes32 beneficiary;
111
+ /// @dev An array of token identifiers. Each element in the array represents a unique token involved in the order.
112
+ TokenInfo[] tokens;
113
+ }
114
+
115
+ /**
116
+ * @notice A struct representing the options for filling an intent.
117
+ * @dev This struct is used to specify various parameters and options
118
+ * when filling an intent in the IntentGateway contract.
119
+ */
120
+ struct FillOptions {
121
+ /// @dev The fee paid in feeTokens to the relayer for processing transactions.
122
+ uint256 relayerFee;
123
+ /// @dev The fee paid in native tokens for cross-chain dispatch.
124
+ uint256 nativeDispatchFee;
125
+ /// @dev The output tokens with amounts the solver is willing to give
126
+ /// @dev Must be strictly >= the amounts requested in order.output.assets
127
+ TokenInfo[] outputs;
128
+ }
129
+
130
+ /**
131
+ * @notice A struct representing the options for selecting a solver
132
+ * @dev This struct is used to specify various parameters and options
133
+ * when selecting a solver.
134
+ */
135
+ struct SelectOptions {
136
+ /// @dev The commitment hash of the order.
137
+ bytes32 commitment;
138
+ /// @dev The solver address to select.
139
+ address solver;
140
+ /// @dev The EIP-712 signature from the session key that signed SelectSolver(commitment, solver)
141
+ bytes signature;
142
+ }
143
+
144
+ /**
145
+ * @dev Struct representing the options for canceling an intent.
146
+ */
147
+ struct CancelOptions {
148
+ /// @dev The fee paid to the relayer for processing transactions.
149
+ uint256 relayerFee;
150
+ /// @dev Stores the height value.
151
+ uint256 height;
152
+ }
153
+
154
+ /**
155
+ * @dev Request from hyperbridge for adding a new deployment of IntentGateway
156
+ */
157
+ struct NewDeployment {
158
+ /// @dev Identifier for the state machine.
159
+ bytes stateMachineId;
160
+ /// @dev An address variable to store the gateway identifier.
161
+ address gateway;
162
+ }
163
+
164
+ /**
165
+ * @title IIntentGatewayV2
166
+ * @author Polytope Labs (hello@polytope.technology)
167
+ * @notice Interface for the IntentGatewayV2 contract
168
+ * @dev Defines all external functions, events, and errors for cross-chain intent fulfillment
169
+ */
170
+ interface IIntentGatewayV2 {
171
+ // ============================================
172
+ // Errors
173
+ // ============================================
174
+
175
+ /// @notice Thrown when an unauthorized action is attempted.
176
+ error Unauthorized();
177
+
178
+ /// @notice Thrown when an invalid input is provided.
179
+ error InvalidInput();
180
+
181
+ /// @notice Thrown when an action is attempted on an expired order.
182
+ error Expired();
183
+
184
+ /// @notice Thrown when there are insufficient native tokens to complete an action.
185
+ error InsufficientNativeToken();
186
+
187
+ /// @notice Thrown when an action is attempted on an order that has not yet expired.
188
+ error NotExpired();
189
+
190
+ /// @notice Thrown when an action is attempted on an order that has already been filled.
191
+ error Filled();
192
+
193
+ /// @notice Thrown when an action is attempted on an order that has been cancelled.
194
+ error Cancelled();
195
+
196
+ /// @notice Thrown when an action is attempted on the wrong chain.
197
+ error WrongChain();
198
+
199
+ /// @notice Thrown when an action is attempted on an unknown order.
200
+ error UnknownOrder();
201
+
202
+ // ============================================
203
+ // Events
204
+ // ============================================
205
+
206
+ /**
207
+ * @notice Emitted when an order is placed.
208
+ * @param user The address of the user who is initiating the transfer
209
+ * @param source The state machine identifier of the origin chain
210
+ * @param destination The state machine identifier of the destination chain
211
+ * @param deadline The block number by which the order must be filled
212
+ * @param nonce The nonce of the order
213
+ * @param fees The dispatch fees associated with the order
214
+ * @param session Optional session key used to select winning solver
215
+ * @param beneficiary The address to receive the output tokens
216
+ * @param predispatch The predispatch assets for the order
217
+ * @param inputs The tokens that are escrowed for the filler (amounts reflect values after protocol fee deduction)
218
+ * @param outputs The tokens that the filler will provide
219
+ */
220
+ event OrderPlaced(
221
+ bytes32 user,
222
+ bytes source,
223
+ bytes destination,
224
+ uint256 deadline,
225
+ uint256 nonce,
226
+ uint256 fees,
227
+ address session,
228
+ bytes32 beneficiary,
229
+ TokenInfo[] predispatch,
230
+ TokenInfo[] inputs,
231
+ TokenInfo[] outputs
232
+ );
233
+
234
+ /**
235
+ * @notice Emitted when an order is filled.
236
+ * @param commitment The unique identifier of the order
237
+ * @param filler The address of the entity that filled the order
238
+ */
239
+ event OrderFilled(bytes32 indexed commitment, address filler);
240
+
241
+ /**
242
+ * @notice Emitted when an escrow is released.
243
+ * @param commitment The unique identifier of the order
244
+ */
245
+ event EscrowReleased(bytes32 indexed commitment);
246
+
247
+ /**
248
+ * @notice Emitted when an escrow is refunded.
249
+ * @param commitment The unique identifier of the order
250
+ */
251
+ event EscrowRefunded(bytes32 indexed commitment);
252
+
253
+ /**
254
+ * @notice Emitted when parameters are updated.
255
+ * @param previous The previous parameters
256
+ * @param current The current parameters
257
+ */
258
+ event ParamsUpdated(Params previous, Params current);
259
+
260
+ /**
261
+ * @notice Emitted when a new deployment is added.
262
+ * @param stateMachineId The state machine identifier
263
+ * @param gateway The gateway identifier
264
+ */
265
+ event NewDeploymentAdded(bytes stateMachineId, address gateway);
266
+
267
+ /**
268
+ * @notice Emitted when dust is collected.
269
+ * @param token The token address
270
+ * @param amount The amount of dust collected
271
+ */
272
+ event DustCollected(address token, uint256 amount);
273
+
274
+ /**
275
+ * @notice Emitted when dust is swept to a beneficiary.
276
+ * @param token The token address
277
+ * @param amount The amount swept
278
+ * @param beneficiary The beneficiary of the funds
279
+ */
280
+ event DustSwept(address token, uint256 amount, address beneficiary);
281
+
282
+ // ============================================
283
+ // Constants
284
+ // ============================================
285
+
286
+ /**
287
+ * @notice EIP-712 type hash for SelectSolver message
288
+ */
289
+ function SELECT_SOLVER_TYPEHASH() external view returns (bytes32);
290
+
291
+ /**
292
+ * @notice EIP-712 domain separator
293
+ */
294
+ function DOMAIN_SEPARATOR() external view returns (bytes32);
295
+
296
+ // ============================================
297
+ // Functions
298
+ // ============================================
299
+
300
+ /**
301
+ * @notice Returns the host contract address.
302
+ * @return address The address of the IsmpHost contract
303
+ */
304
+ function host() external view returns (address);
305
+
306
+ /**
307
+ * @notice Fetch the IntentGateway contract instance for a chain.
308
+ * @param stateMachineId The state machine identifier
309
+ * @return address The gateway address for the given state machine
310
+ */
311
+ function instance(bytes calldata stateMachineId) external view returns (address);
312
+
313
+ /**
314
+ * @notice Sets the parameters for the IntentGateway module.
315
+ * @param p The parameters to be set, encapsulated in a Params struct
316
+ */
317
+ function setParams(Params memory p) external;
318
+
319
+ /**
320
+ * @notice Returns the current parameters of the module.
321
+ * @return Params A struct containing the module's current parameters
322
+ */
323
+ function params() external view returns (Params memory);
324
+
325
+ /**
326
+ * @notice Calculates the commitment slot hash for storage proof verification.
327
+ * @param commitment The commitment hash
328
+ * @return bytes The calculated commitment slot hash
329
+ */
330
+ function calculateCommitmentSlotHash(bytes32 commitment) external pure returns (bytes memory);
331
+
332
+ /**
333
+ * @notice Places an order for cross-chain intent fulfillment.
334
+ * @dev If protocolFeeBps is configured, a protocol fee is deducted from each input token amount.
335
+ * The full input amounts are escrowed, but the OrderPlaced event emits reduced amounts (after fee).
336
+ * Protocol fees are retained as dust and can be swept via SweepDust requests.
337
+ * @param order The order to be placed
338
+ * @param graffiti The arbitrary data used for identification purposes
339
+ */
340
+ function placeOrder(Order memory order, bytes32 graffiti) external payable;
341
+
342
+ /**
343
+ * @notice Selects a solver for an order (when solver selection is enabled).
344
+ * @param options The options for selecting a solver
345
+ * @return sessionKey The recovered session key address
346
+ */
347
+ function select(SelectOptions calldata options) external returns (address sessionKey);
348
+
349
+ /**
350
+ * @notice Fills an order with the specified options.
351
+ * @param order The order to be filled
352
+ * @param options The options to be used when filling the order
353
+ */
354
+ function fillOrder(Order calldata order, FillOptions calldata options) external payable;
355
+
356
+ /**
357
+ * @notice Cancels an order after it has expired.
358
+ * @param order The order to be cancelled
359
+ * @param options The cancellation options
360
+ */
361
+ function cancelOrder(Order calldata order, CancelOptions calldata options) external payable;
362
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperbridge/core",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Hyperbridge Solidity SDK for dispatching & receiving cross-chain messages",
5
5
  "author": "Polytope Labs <hello@polytope.technology>",
6
6
  "license": "Apache-2.0",