1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//! `EthFlow` contract helpers — encode `createOrder` calldata for
//! native-currency orders.
//!
//! When a user wants to sell ETH (or another chain's native currency)
//! instead of an ERC-20, the order is submitted through the `EthFlow`
//! contract rather than the standard `GPv2Settlement` flow. This module
//! provides the types and encoding functions for that path.
//!
//! # Key items
//!
//! | Item | Purpose |
//! |---|---|
//! | [`EthFlowOrderData`] | Parameters for a native-currency sell order |
//! | [`EthFlowTransaction`] | Ready-to-send transaction (to, data, value) |
//! | [`encode_eth_flow_create_order`] | ABI-encode `createOrder(...)` calldata |
//! | [`build_eth_flow_transaction`] | Build the complete transaction |
//! | [`is_eth_flow_order_data`] | Check if on-chain data indicates an EthFlow order |
use ;
use crateOnchainOrderData;
/// Parameters for a native-currency sell order submitted through the
/// `EthFlow` contract.
///
/// Maps to the `EthFlowOrder` struct in the Solidity contract. The
/// sell token is implicitly the chain's native currency (ETH, xDAI, …).
///
/// # Example
///
/// ```
/// use alloy_primitives::{Address, B256, U256};
/// use cow_rs::ethflow::{EthFlowOrderData, encode_eth_flow_create_order};
///
/// let order = EthFlowOrderData {
/// buy_token: Address::ZERO,
/// receiver: Address::ZERO,
/// sell_amount: U256::from(1_000_000u64),
/// buy_amount: U256::from(500_000u64),
/// app_data: B256::ZERO,
/// fee_amount: U256::ZERO,
/// valid_to: 9_999_999,
/// partially_fillable: false,
/// quote_id: 42,
/// };
/// let cd = encode_eth_flow_create_order(&order);
/// assert_eq!(cd.len(), 292);
/// ```
/// Ready-to-send transaction for submitting a native-currency order.
///
/// Produced by [`build_eth_flow_transaction`]. Send this transaction with
/// the indicated [`value`](Self::value) of native currency attached.
/// Compute the 4-byte selector from a Solidity function signature.
/// Left-pad an [`Address`] to a 32-byte ABI word.
/// Encode a `u32` as a 32-byte big-endian ABI word.
/// Encode a `bool` as a 32-byte ABI word.
/// Encode an `i64` as a 32-byte two's-complement big-endian ABI word.
/// Encode the `EthFlow.createOrder(order, quoteId)` calldata.
///
/// Function signature:
/// `createOrder((address,address,uint256,uint256,bytes32,uint256,uint32,bool),int64)`
///
/// Total payload: selector (4) + 9 × 32-byte words = 292 bytes.
///
/// # Parameters
///
/// * `order` — the [`EthFlowOrderData`] to encode.
///
/// # Returns
///
/// A 292-byte `Vec<u8>` containing the ABI-encoded calldata.
///
/// # Example
///
/// ```
/// use alloy_primitives::{Address, B256, U256};
/// use cow_rs::ethflow::{EthFlowOrderData, encode_eth_flow_create_order};
///
/// let order = EthFlowOrderData {
/// buy_token: Address::ZERO,
/// receiver: Address::ZERO,
/// sell_amount: U256::from(1_000_000_u64),
/// buy_amount: U256::from(500_000_u64),
/// app_data: B256::ZERO,
/// fee_amount: U256::ZERO,
/// valid_to: 9_999_999_u32,
/// partially_fillable: false,
/// quote_id: 42,
/// };
/// let cd = encode_eth_flow_create_order(&order);
/// assert_eq!(cd.len(), 292);
/// ```
/// Build a complete [`EthFlowTransaction`] for `contract_address`.
///
/// The caller is responsible for sending the returned transaction with the
/// ETH value indicated by [`EthFlowTransaction::value`].
///
/// # Parameters
///
/// * `contract` — the [`Address`] of the `EthFlow` contract on the target chain (use
/// [`eth_flow_for_env`](crate::config::eth_flow_for_env) to look it up).
/// * `order` — the [`EthFlowOrderData`] to encode.
///
/// # Returns
///
/// An [`EthFlowTransaction`] with `to`, `data`, and `value` fields set.
///
/// # Example
///
/// ```
/// use alloy_primitives::{Address, B256, U256};
/// use cow_rs::ethflow::{EthFlowOrderData, build_eth_flow_transaction};
///
/// let order = EthFlowOrderData {
/// buy_token: Address::ZERO,
/// receiver: Address::ZERO,
/// sell_amount: U256::from(1_000_u64),
/// buy_amount: U256::from(500_u64),
/// app_data: B256::ZERO,
/// fee_amount: U256::ZERO,
/// valid_to: 0,
/// partially_fillable: false,
/// quote_id: 1,
/// };
/// let tx = build_eth_flow_transaction(Address::ZERO, &order);
/// assert_eq!(tx.value, order.sell_amount);
/// ```
/// Check whether on-chain data is present, indicating an `EthFlow` order.
///
/// Returns `true` if `onchain_data` is `Some`, which signals that the order
/// was submitted via the `EthFlow` contract rather than the standard
/// `GPv2Settlement` flow.
///
/// # Parameters
///
/// * `onchain_data` — optional reference to [`OnchainOrderData`].
///
/// # Returns
///
/// `true` if `onchain_data` is `Some`.
///
/// # Example
///
/// ```
/// use cow_rs::ethflow::is_eth_flow_order_data;
///
/// assert!(!is_eth_flow_order_data(None));
/// ```
pub const