cow-rs 0.1.1

Rust SDK for the CoW Protocol: quoting, signing, posting and tracking orders, plus composable orders, on-chain reads and subgraph queries.
Documentation
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Order refund helpers for reclaiming unfilled portions of `CoW` Protocol orders.
//!
//! Provides [`OrderRefund`] for representing refund claims, ABI-encoded calldata
//! builders for settlement and `EthFlow` refunds, and helpers for computing
//! refundable amounts.

use std::fmt;

use alloy_primitives::{U256, keccak256};

use crate::error::CowError;

/// Refund claim for a `CoW` Protocol order.
///
/// Represents an order whose unfilled portion can be reclaimed by the owner.
/// The [`refund_type`](Self::refund_type) determines which contract to target.
///
/// # Example
///
/// ```
/// use cow_rs::settlement::refunds::{OrderRefund, RefundType};
///
/// let refund = OrderRefund::new("0xabcd".to_owned(), RefundType::Settlement);
/// assert_eq!(refund.refund_type, RefundType::Settlement);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrderRefund {
    /// The hex-encoded order UID (with or without `0x` prefix).
    pub order_uid: String,
    /// Which contract path to use for the refund.
    pub refund_type: RefundType,
}

impl OrderRefund {
    /// Create a new order refund claim.
    ///
    /// # Arguments
    ///
    /// * `order_uid` - The hex-encoded order UID.
    /// * `refund_type` - The refund path ([`RefundType::Settlement`] or [`RefundType::EthFlow`]).
    ///
    /// # Returns
    ///
    /// A new [`OrderRefund`].
    #[must_use]
    pub const fn new(order_uid: String, refund_type: RefundType) -> Self {
        Self { order_uid, refund_type }
    }
}

impl fmt::Display for OrderRefund {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Refund({}, {:?})", self.order_uid, self.refund_type)
    }
}

/// The contract path to use for an order refund.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RefundType {
    /// Standard order refund via the settlement contract.
    Settlement,
    /// `EthFlow` order refund via the `EthFlow` contract.
    EthFlow,
}

impl RefundType {
    /// Check whether this is a settlement refund.
    ///
    /// # Returns
    ///
    /// `true` if this is [`RefundType::Settlement`].
    #[must_use]
    pub const fn is_settlement(&self) -> bool {
        matches!(self, Self::Settlement)
    }

    /// Check whether this is an `EthFlow` refund.
    ///
    /// # Returns
    ///
    /// `true` if this is [`RefundType::EthFlow`].
    #[must_use]
    pub const fn is_eth_flow(&self) -> bool {
        matches!(self, Self::EthFlow)
    }
}

impl fmt::Display for RefundType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Settlement => write!(f, "Settlement"),
            Self::EthFlow => write!(f, "EthFlow"),
        }
    }
}

/// Build calldata for `GPv2Settlement.freeFilledAmountStorage(bytes orderUid)`.
///
/// This reclaims the storage slot used by the settlement contract to track
/// the filled amount for a fully cancelled or expired order, triggering a
/// gas refund.
///
/// # Arguments
///
/// * `order_uid` - The hex-encoded order UID (with or without `0x` prefix).
///
/// # Returns
///
/// The ABI-encoded calldata as a `Vec<u8>`.
///
/// # Errors
///
/// Returns [`CowError::Api`] if `order_uid` is not valid hex.
///
/// # Example
///
/// ```
/// use cow_rs::settlement::refunds::settlement_refund_calldata;
///
/// let uid = "0x".to_owned() + &"ab".repeat(56);
/// let calldata = settlement_refund_calldata(&uid).unwrap();
/// assert!(!calldata.is_empty());
/// ```
pub fn settlement_refund_calldata(order_uid: &str) -> Result<Vec<u8>, CowError> {
    let uid_bytes = decode_uid(order_uid)?;
    let sel = selector("freeFilledAmountStorage(bytes)");

    let padded_len = padded32(uid_bytes.len());
    let mut buf = Vec::with_capacity(4 + 32 + 32 + padded_len);
    buf.extend_from_slice(&sel);
    // Offset to dynamic bytes data (1 head slot = 32).
    buf.extend_from_slice(&u256_be(32));
    // Length of bytes data.
    buf.extend_from_slice(&u256_be(uid_bytes.len() as u64));
    // Actual data.
    buf.extend_from_slice(&uid_bytes);
    // Pad to 32-byte boundary.
    pad_to(&mut buf, uid_bytes.len());
    Ok(buf)
}

/// Build calldata for `CoWSwapEthFlow.invalidateOrder(bytes orderUid)`.
///
/// Invalidates an `EthFlow` order on the `EthFlow` contract, allowing the
/// user to reclaim their deposited ETH.
///
/// # Arguments
///
/// * `order_uid` - The hex-encoded order UID (with or without `0x` prefix).
///
/// # Returns
///
/// The ABI-encoded calldata as a `Vec<u8>`.
///
/// # Errors
///
/// Returns [`CowError::Api`] if `order_uid` is not valid hex.
///
/// # Example
///
/// ```
/// use cow_rs::settlement::refunds::ethflow_refund_calldata;
///
/// let uid = "0x".to_owned() + &"ab".repeat(56);
/// let calldata = ethflow_refund_calldata(&uid).unwrap();
/// assert!(!calldata.is_empty());
/// ```
pub fn ethflow_refund_calldata(order_uid: &str) -> Result<Vec<u8>, CowError> {
    let uid_bytes = decode_uid(order_uid)?;
    let sel = selector("invalidateOrder(bytes)");

    let padded_len = padded32(uid_bytes.len());
    let mut buf = Vec::with_capacity(4 + 32 + 32 + padded_len);
    buf.extend_from_slice(&sel);
    // Offset to dynamic bytes data (1 head slot = 32).
    buf.extend_from_slice(&u256_be(32));
    // Length of bytes data.
    buf.extend_from_slice(&u256_be(uid_bytes.len() as u64));
    // Actual data.
    buf.extend_from_slice(&uid_bytes);
    // Pad to 32-byte boundary.
    pad_to(&mut buf, uid_bytes.len());
    Ok(buf)
}

/// Check whether an order has an unfilled portion that can be refunded.
///
/// An order is refundable if it has not been fully filled (i.e.,
/// `filled_amount < total_amount`).
///
/// # Arguments
///
/// * `filled_amount` - The amount already filled.
/// * `total_amount` - The total order amount.
///
/// # Returns
///
/// `true` if the order has an unfilled portion (`filled_amount < total_amount`).
///
/// # Example
///
/// ```
/// use alloy_primitives::U256;
/// use cow_rs::settlement::refunds::is_refundable;
///
/// assert!(is_refundable(U256::ZERO, U256::from(1000)));
/// assert!(is_refundable(U256::from(500), U256::from(1000)));
/// assert!(!is_refundable(U256::from(1000), U256::from(1000)));
/// ```
#[must_use]
pub fn is_refundable(filled_amount: U256, total_amount: U256) -> bool {
    filled_amount < total_amount
}

/// Compute the refundable (unfilled) amount for an order.
///
/// Returns `total_amount - filled_amount`, saturating at zero if the
/// filled amount exceeds the total (which should not happen in practice).
///
/// # Arguments
///
/// * `filled_amount` - The amount already filled.
/// * `total_amount` - The total order amount.
///
/// # Returns
///
/// The refundable amount as [`U256`].
///
/// # Example
///
/// ```
/// use alloy_primitives::U256;
/// use cow_rs::settlement::refunds::refund_amount;
///
/// assert_eq!(refund_amount(U256::from(300), U256::from(1000)), U256::from(700));
/// assert_eq!(refund_amount(U256::from(1000), U256::from(1000)), U256::ZERO);
/// ```
#[must_use]
pub const fn refund_amount(filled_amount: U256, total_amount: U256) -> U256 {
    total_amount.saturating_sub(filled_amount)
}

// ── Private helpers ──────────────────────────────────────────────────────────

/// Compute the 4-byte selector from a Solidity function signature.
fn selector(sig: &str) -> [u8; 4] {
    let h = keccak256(sig.as_bytes());
    [h[0], h[1], h[2], h[3]]
}

/// Encode a `u64` as a 32-byte big-endian ABI word.
fn u256_be(v: u64) -> [u8; 32] {
    let mut out = [0u8; 32];
    out[24..].copy_from_slice(&v.to_be_bytes());
    out
}

/// Zero-pad `buf` to the next 32-byte boundary after `written` bytes.
fn pad_to(buf: &mut Vec<u8>, written: usize) {
    let rem = written % 32;
    if rem != 0 {
        buf.resize(buf.len() + (32 - rem), 0);
    }
}

/// Round `n` up to the next multiple of 32.
const fn padded32(n: usize) -> usize {
    if n.is_multiple_of(32) { n } else { n + (32 - n % 32) }
}

/// Decode a hex order UID (with or without `0x` prefix) into raw bytes.
fn decode_uid(uid: &str) -> Result<Vec<u8>, CowError> {
    let stripped = uid.trim_start_matches("0x");
    alloy_primitives::hex::decode(stripped)
        .map_err(|_e| CowError::Api { status: 0, body: format!("invalid orderUid: {uid}") })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn dummy_uid_56() -> String {
        "0x".to_owned() + &"ab".repeat(56)
    }

    // ── OrderRefund tests ────────────────────────────────────────────────

    #[test]
    fn order_refund_new() {
        let refund = OrderRefund::new("0xdead".to_owned(), RefundType::Settlement);
        assert_eq!(refund.order_uid, "0xdead");
        assert_eq!(refund.refund_type, RefundType::Settlement);
    }

    #[test]
    fn order_refund_display() {
        let refund = OrderRefund::new("0xbeef".to_owned(), RefundType::EthFlow);
        let s = format!("{refund}");
        assert!(s.contains("0xbeef"));
        assert!(s.contains("EthFlow"));
    }

    #[test]
    fn order_refund_clone_eq() {
        let a = OrderRefund::new("0xaa".to_owned(), RefundType::Settlement);
        let b = a.clone();
        assert_eq!(a, b);
    }

    // ── RefundType tests ─────────────────────────────────────────────────

    #[test]
    fn refund_type_is_settlement() {
        assert!(RefundType::Settlement.is_settlement());
        assert!(!RefundType::Settlement.is_eth_flow());
    }

    #[test]
    fn refund_type_is_eth_flow() {
        assert!(RefundType::EthFlow.is_eth_flow());
        assert!(!RefundType::EthFlow.is_settlement());
    }

    #[test]
    fn refund_type_display() {
        assert_eq!(format!("{}", RefundType::Settlement), "Settlement");
        assert_eq!(format!("{}", RefundType::EthFlow), "EthFlow");
    }

    #[test]
    fn refund_type_copy() {
        let a = RefundType::Settlement;
        let b = a;
        assert_eq!(a, b);
    }

    // ── settlement_refund_calldata tests ─────────────────────────────────

    #[test]
    fn settlement_refund_calldata_valid() {
        let uid = dummy_uid_56();
        let data = settlement_refund_calldata(&uid).unwrap();
        // 4 (selector) + 32 (offset) + 32 (length) + 64 (56 bytes padded to 64) = 132
        assert_eq!(data.len(), 132);
        assert_eq!(&data[..4], &selector("freeFilledAmountStorage(bytes)"));
    }

    #[test]
    fn settlement_refund_calldata_invalid_hex() {
        assert!(settlement_refund_calldata("0xZZZZ").is_err());
    }

    #[test]
    fn settlement_refund_calldata_without_prefix() {
        let uid = "ab".repeat(56);
        let data = settlement_refund_calldata(&uid).unwrap();
        assert_eq!(data.len(), 132);
    }

    #[test]
    fn settlement_refund_calldata_empty_uid() {
        let data = settlement_refund_calldata("0x").unwrap();
        // 4 (selector) + 32 (offset) + 32 (length=0) = 68
        assert_eq!(data.len(), 68);
    }

    // ── ethflow_refund_calldata tests ────────────────────────────────────

    #[test]
    fn ethflow_refund_calldata_valid() {
        let uid = dummy_uid_56();
        let data = ethflow_refund_calldata(&uid).unwrap();
        assert_eq!(data.len(), 132);
        assert_eq!(&data[..4], &selector("invalidateOrder(bytes)"));
    }

    #[test]
    fn ethflow_refund_calldata_invalid_hex() {
        assert!(ethflow_refund_calldata("not_hex_gg").is_err());
    }

    #[test]
    fn ethflow_refund_calldata_without_prefix() {
        let uid = "cd".repeat(56);
        let data = ethflow_refund_calldata(&uid).unwrap();
        assert_eq!(data.len(), 132);
    }

    // ── is_refundable tests ──────────────────────────────────────────────

    #[test]
    fn is_refundable_zero_filled() {
        assert!(is_refundable(U256::ZERO, U256::from(1000)));
    }

    #[test]
    fn is_refundable_partial_filled() {
        assert!(is_refundable(U256::from(500), U256::from(1000)));
    }

    #[test]
    fn is_refundable_fully_filled() {
        assert!(!is_refundable(U256::from(1000), U256::from(1000)));
    }

    #[test]
    fn is_refundable_zero_total() {
        assert!(!is_refundable(U256::ZERO, U256::ZERO));
    }

    // ── refund_amount tests ──────────────────────────────────────────────

    #[test]
    fn refund_amount_partial() {
        assert_eq!(refund_amount(U256::from(300), U256::from(1000)), U256::from(700));
    }

    #[test]
    fn refund_amount_fully_filled() {
        assert_eq!(refund_amount(U256::from(1000), U256::from(1000)), U256::ZERO);
    }

    #[test]
    fn refund_amount_zero_filled() {
        assert_eq!(refund_amount(U256::ZERO, U256::from(500)), U256::from(500));
    }

    #[test]
    fn refund_amount_overfilled_saturates() {
        // Saturating subtraction prevents underflow.
        assert_eq!(refund_amount(U256::from(2000), U256::from(1000)), U256::ZERO);
    }

    // ── Helper tests ────────────────────────────────────────────────────

    #[test]
    fn padded32_rounds_up() {
        assert_eq!(padded32(0), 0);
        assert_eq!(padded32(1), 32);
        assert_eq!(padded32(31), 32);
        assert_eq!(padded32(32), 32);
        assert_eq!(padded32(33), 64);
    }
}