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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! High-level executor configuration for `CoW` Protocol swap operations.
//!
//! Provides [`CowSwapConfig`] which bundles all the parameters needed to
//! submit orders (chain, environment, tokens, slippage, TTL) and
//! [`TokenRegistry`] which maps ticker symbols to on-chain addresses and
//! decimal counts.

use std::fmt;

use alloy_primitives::Address;
use foldhash::HashMap;

use super::chain::{Env, SupportedChainId};

/// Internal storage entry: `(token_address, decimals)`.
type TokenEntry = (Address, u8);

/// A registry mapping asset ticker symbols to their ERC-20 token metadata.
///
/// Stores both address and decimal precision so the executor can convert
/// human-readable quantities to token atoms without assuming 18 decimals.
///
/// # Example
///
/// ```
/// use alloy_primitives::Address;
/// use cow_rs::config::TokenRegistry;
///
/// let mut reg = TokenRegistry::new_with_decimals([
///     ("USDC", Address::ZERO, 6u8),
///     ("WETH", Address::ZERO, 18u8),
/// ]);
/// assert_eq!(reg.get_decimals("USDC"), Some(6));
/// assert!(reg.contains("WETH"));
/// assert!(!reg.contains("DAI"));
///
/// reg.insert("DAI", Address::ZERO);
/// assert!(reg.contains("DAI"));
/// assert_eq!(reg.len(), 3);
/// ```
#[derive(Debug)]
pub struct TokenRegistry {
    inner: HashMap<String, TokenEntry>,
}

impl TokenRegistry {
    /// Create a new registry from `(symbol, address)` pairs.
    ///
    /// All tokens registered this way are assumed to have **18 decimals**.
    /// Use [`new_with_decimals`](Self::new_with_decimals) when tokens have
    /// non-standard decimal counts (e.g. USDC = 6, WBTC = 8).
    ///
    /// # Parameters
    ///
    /// * `entries` — an iterator of `(symbol, address)` pairs.
    ///
    /// # Returns
    ///
    /// A new [`TokenRegistry`] with all entries set to 18 decimals.
    #[must_use]
    pub fn new(entries: impl IntoIterator<Item = (impl Into<String>, Address)>) -> Self {
        Self { inner: entries.into_iter().map(|(k, v)| (k.into(), (v, 18))).collect() }
    }

    /// Create a new registry from `(symbol, address, decimals)` tuples.
    ///
    /// Use this when tokens have non-standard decimal counts (e.g. USDC = 6,
    /// WBTC = 8).
    ///
    /// # Parameters
    ///
    /// * `entries` — an iterator of `(symbol, address, decimals)` tuples.
    ///
    /// # Returns
    ///
    /// A new [`TokenRegistry`] with explicit decimal counts per token.
    #[must_use]
    pub fn new_with_decimals(
        entries: impl IntoIterator<Item = (impl Into<String>, Address, u8)>,
    ) -> Self {
        Self { inner: entries.into_iter().map(|(k, v, d)| (k.into(), (v, d))).collect() }
    }

    /// Look up the [`Address`] for a given asset symbol, e.g. `"WETH"`.
    ///
    /// # Arguments
    ///
    /// * `asset` — the ticker symbol to look up.
    ///
    /// # Returns
    ///
    /// `Some(address)` if the symbol is registered, `None` otherwise.
    #[must_use]
    pub fn get(&self, asset: &str) -> Option<Address> {
        self.inner.get(asset).map(|&(addr, _)| addr)
    }

    /// Look up the decimal count for a given asset symbol.
    ///
    /// # Arguments
    ///
    /// * `asset` — the ticker symbol to look up.
    ///
    /// # Returns
    ///
    /// `Some(decimals)` when the symbol is registered, `None` otherwise.
    #[must_use]
    pub fn get_decimals(&self, asset: &str) -> Option<u8> {
        self.inner.get(asset).map(|&(_, decimals)| decimals)
    }

    /// Register a token with 18 decimals (or update an existing entry).
    ///
    /// # Arguments
    ///
    /// * `symbol` — the ticker symbol to register.
    /// * `address` — the ERC-20 contract [`Address`].
    pub fn insert(&mut self, symbol: impl Into<String>, address: Address) {
        self.inner.insert(symbol.into(), (address, 18));
    }

    /// Register a token with explicit decimals (or update an existing entry).
    ///
    /// # Arguments
    ///
    /// * `symbol` — the ticker symbol to register.
    /// * `address` — the ERC-20 contract [`Address`].
    /// * `decimals` — the token's decimal precision.
    pub fn insert_with_decimals(
        &mut self,
        symbol: impl Into<String>,
        address: Address,
        decimals: u8,
    ) {
        self.inner.insert(symbol.into(), (address, decimals));
    }

    /// Returns `true` if `asset` is registered in this registry.
    ///
    /// # Arguments
    ///
    /// * `asset` — the ticker symbol to check.
    ///
    /// # Returns
    ///
    /// `true` when the symbol exists in the registry.
    #[must_use]
    pub fn contains(&self, asset: &str) -> bool {
        self.inner.contains_key(asset)
    }

    /// Returns the number of registered tokens.
    ///
    /// # Returns
    ///
    /// The count of tokens in this registry.
    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns `true` if no tokens are registered.
    ///
    /// # Returns
    ///
    /// `true` when the registry contains zero tokens.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Look up both the address and decimal count for a given asset symbol.
    ///
    /// Returns `Some((address, decimals))` when registered, `None` otherwise.
    ///
    /// ```
    /// use alloy_primitives::Address;
    /// use cow_rs::config::TokenRegistry;
    ///
    /// let reg = TokenRegistry::new_with_decimals([("USDC", Address::ZERO, 6u8)]);
    /// assert_eq!(reg.get_entry("USDC"), Some((Address::ZERO, 6)));
    /// assert_eq!(reg.get_entry("WETH"), None);
    /// ```
    #[must_use]
    pub fn get_entry(&self, asset: &str) -> Option<(Address, u8)> {
        self.inner.get(asset).copied()
    }

    /// Remove a token from the registry.
    ///
    /// # Arguments
    ///
    /// * `asset` — the ticker symbol to remove.
    ///
    /// # Returns
    ///
    /// `Some((address, decimals))` if the symbol was registered, `None` otherwise.
    pub fn remove(&mut self, asset: &str) -> Option<(Address, u8)> {
        self.inner.remove(asset)
    }
}

impl fmt::Display for TokenRegistry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "registry({} tokens)", self.inner.len())
    }
}

/// Configuration for the `CoW` Protocol swap executor.
///
/// Bundles all parameters needed to submit orders: target chain,
/// environment, sell token, slippage, TTL, and a [`TokenRegistry`] mapping
/// strategy symbols to on-chain addresses.
///
/// Construct via [`prod`](Self::prod) or [`staging`](Self::staging), then
/// customise with the `with_*` builder methods.
///
/// # Example
///
/// ```
/// use alloy_primitives::Address;
/// use cow_rs::{
///     SupportedChainId,
///     config::{CowSwapConfig, TokenRegistry},
/// };
///
/// let empty: Vec<(&str, Address)> = vec![];
/// let config = CowSwapConfig::prod(
///     SupportedChainId::Mainnet,
///     Address::ZERO, // sell token
///     TokenRegistry::new(empty),
///     50,   // 0.5% slippage
///     1800, // 30 min TTL
/// );
/// assert!(config.env.is_prod());
/// assert_eq!(config.slippage_bps, 50);
/// ```
#[derive(Debug)]
pub struct CowSwapConfig {
    /// Target chain.
    pub chain_id: SupportedChainId,
    /// API environment (`Prod` or `Staging`).
    pub env: Env,
    /// The token used as the quote / sell currency (e.g. USDC on Sepolia).
    pub sell_token: Address,
    /// Decimal count for [`Self::sell_token`] (e.g. `6` for USDC, `18` for WETH).
    pub sell_token_decimals: u8,
    /// Registry mapping strategy asset symbols to their on-chain token addresses
    /// and decimal counts.
    pub tokens: TokenRegistry,
    /// Slippage tolerance in basis points (e.g. `50` = 0.5 %).
    pub slippage_bps: u32,
    /// Default order TTL in seconds (e.g. `1800` = 30 min).
    pub order_valid_secs: u32,
    /// Optional override for the buy-token receiver address.
    ///
    /// When `None` the order receiver defaults to the signing wallet address.
    pub receiver: Option<Address>,
}

impl CowSwapConfig {
    /// Convenience constructor defaulting to the production environment.
    ///
    /// [`Self::sell_token_decimals`] defaults to `18`; use
    /// [`with_sell_token_decimals`](Self::with_sell_token_decimals) for
    /// tokens such as USDC (`6`) or WBTC (`8`).
    ///
    /// # Parameters
    ///
    /// * `chain_id` — the target [`SupportedChainId`].
    /// * `sell_token` — the ERC-20 [`Address`] of the sell (quote) currency.
    /// * `tokens` — the [`TokenRegistry`] mapping strategy symbols to tokens.
    /// * `slippage_bps` — slippage tolerance in basis points (e.g. `50` = 0.5 %).
    /// * `order_valid_secs` — order TTL in seconds (e.g. `1800` = 30 min).
    ///
    /// # Returns
    ///
    /// A new [`CowSwapConfig`] targeting [`Env::Prod`] with no custom receiver.
    #[must_use]
    pub const fn prod(
        chain_id: SupportedChainId,
        sell_token: Address,
        tokens: TokenRegistry,
        slippage_bps: u32,
        order_valid_secs: u32,
    ) -> Self {
        Self {
            chain_id,
            env: Env::Prod,
            sell_token,
            sell_token_decimals: 18,
            tokens,
            slippage_bps,
            order_valid_secs,
            receiver: None,
        }
    }

    /// Convenience constructor defaulting to the staging (barn) environment.
    ///
    /// Same parameters as [`prod`](Self::prod) but targets [`Env::Staging`]
    /// (`barn.api.cow.fi`). [`Self::sell_token_decimals`] defaults to `18`.
    ///
    /// # Parameters
    ///
    /// See [`prod`](Self::prod) for parameter descriptions.
    ///
    /// # Returns
    ///
    /// A new [`CowSwapConfig`] targeting [`Env::Staging`].
    #[must_use]
    pub const fn staging(
        chain_id: SupportedChainId,
        sell_token: Address,
        tokens: TokenRegistry,
        slippage_bps: u32,
        order_valid_secs: u32,
    ) -> Self {
        Self {
            chain_id,
            env: Env::Staging,
            sell_token,
            sell_token_decimals: 18,
            tokens,
            slippage_bps,
            order_valid_secs,
            receiver: None,
        }
    }

    /// Override the sell token address.
    ///
    /// # Arguments
    ///
    /// * `token` — the new sell token [`Address`].
    ///
    /// # Returns
    ///
    /// `self` with the updated sell token.
    #[must_use]
    pub const fn with_sell_token(mut self, token: Address) -> Self {
        self.sell_token = token;
        self
    }

    /// Override the chain ID.
    ///
    /// # Arguments
    ///
    /// * `chain_id` — the new target [`SupportedChainId`].
    ///
    /// # Returns
    ///
    /// `self` with the updated chain ID.
    #[must_use]
    pub const fn with_chain_id(mut self, chain_id: SupportedChainId) -> Self {
        self.chain_id = chain_id;
        self
    }

    /// Override the API environment (`Prod` or `Staging`).
    ///
    /// # Arguments
    ///
    /// * `env` — the new [`Env`] value.
    ///
    /// # Returns
    ///
    /// `self` with the updated environment.
    #[must_use]
    pub const fn with_env(mut self, env: Env) -> Self {
        self.env = env;
        self
    }

    /// Override the slippage tolerance in basis points.
    ///
    /// # Arguments
    ///
    /// * `slippage_bps` — the new slippage in basis points (e.g. `50` = 0.5 %).
    ///
    /// # Returns
    ///
    /// `self` with the updated slippage.
    #[must_use]
    pub const fn with_slippage_bps(mut self, slippage_bps: u32) -> Self {
        self.slippage_bps = slippage_bps;
        self
    }

    /// Override the default order TTL in seconds.
    ///
    /// # Arguments
    ///
    /// * `secs` — the new TTL in seconds (e.g. `1800` = 30 min).
    ///
    /// # Returns
    ///
    /// `self` with the updated order TTL.
    #[must_use]
    pub const fn with_order_valid_secs(mut self, secs: u32) -> Self {
        self.order_valid_secs = secs;
        self
    }

    /// Override the decimal count for `sell_token` (defaults to `18`).
    ///
    /// # Arguments
    ///
    /// * `decimals` — the decimal precision of the sell token.
    ///
    /// # Returns
    ///
    /// `self` with the updated decimal count.
    #[must_use]
    pub const fn with_sell_token_decimals(mut self, decimals: u8) -> Self {
        self.sell_token_decimals = decimals;
        self
    }

    /// Override the order receiver address.
    ///
    /// # Arguments
    ///
    /// * `receiver` — the custom receiver [`Address`].
    ///
    /// # Returns
    ///
    /// `self` with the receiver override set.
    #[must_use]
    pub const fn with_receiver(mut self, receiver: Address) -> Self {
        self.receiver = Some(receiver);
        self
    }

    /// Returns `true` if a custom receiver address has been set.
    ///
    /// When `false`, the executor uses the signing wallet address as receiver.
    ///
    /// # Returns
    ///
    /// `true` when a receiver override is present.
    #[must_use]
    pub const fn has_custom_receiver(&self) -> bool {
        self.receiver.is_some()
    }

    /// Return the effective receiver: the override if set, otherwise `default`.
    ///
    /// # Example
    ///
    /// ```
    /// use alloy_primitives::{Address, address};
    /// use cow_rs::{
    ///     SupportedChainId,
    ///     config::{CowSwapConfig, TokenRegistry},
    /// };
    ///
    /// let wallet = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
    /// let empty: Vec<(&str, Address)> = vec![];
    /// let config = CowSwapConfig::prod(
    ///     SupportedChainId::Mainnet,
    ///     Address::ZERO,
    ///     TokenRegistry::new(empty),
    ///     50,
    ///     1800,
    /// );
    /// assert_eq!(config.effective_receiver(wallet), wallet);
    ///
    /// let override_addr = address!("0000000000000000000000000000000000000001");
    /// let with_recv = config.with_receiver(override_addr);
    /// assert_eq!(with_recv.effective_receiver(wallet), override_addr);
    /// ```
    #[must_use]
    pub fn effective_receiver(&self, default: Address) -> Address {
        self.receiver.map_or(default, |r| r)
    }
}

impl fmt::Display for CowSwapConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "config({}, {}, sell={:#x})", self.chain_id, self.env, self.sell_token)
    }
}

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

    // ── TokenRegistry ───────────────────────────────────────────────────

    #[test]
    fn token_registry_new_defaults_to_18_decimals() {
        let reg = TokenRegistry::new([("WETH", Address::ZERO)]);
        assert_eq!(reg.get_decimals("WETH"), Some(18));
    }

    #[test]
    fn token_registry_new_with_decimals() {
        let reg = TokenRegistry::new_with_decimals([("USDC", Address::ZERO, 6u8)]);
        assert_eq!(reg.get_decimals("USDC"), Some(6));
        assert_eq!(reg.get("USDC"), Some(Address::ZERO));
    }

    #[test]
    fn token_registry_insert_and_contains() {
        let mut reg = TokenRegistry::new(std::iter::empty::<(&str, Address)>());
        assert!(reg.is_empty());
        assert_eq!(reg.len(), 0);
        assert!(!reg.contains("DAI"));

        reg.insert("DAI", Address::ZERO);
        assert!(reg.contains("DAI"));
        assert_eq!(reg.len(), 1);
        assert!(!reg.is_empty());
    }

    #[test]
    fn token_registry_insert_with_decimals() {
        let mut reg = TokenRegistry::new(std::iter::empty::<(&str, Address)>());
        reg.insert_with_decimals("WBTC", Address::ZERO, 8);
        assert_eq!(reg.get_decimals("WBTC"), Some(8));
    }

    #[test]
    fn token_registry_get_entry() {
        let reg = TokenRegistry::new_with_decimals([("USDC", Address::ZERO, 6u8)]);
        assert_eq!(reg.get_entry("USDC"), Some((Address::ZERO, 6)));
        assert_eq!(reg.get_entry("NONEXISTENT"), None);
    }

    #[test]
    fn token_registry_remove() {
        let mut reg = TokenRegistry::new([("WETH", Address::ZERO)]);
        assert!(reg.contains("WETH"));
        let removed = reg.remove("WETH");
        assert!(removed.is_some());
        assert!(!reg.contains("WETH"));
        assert!(reg.is_empty());
    }

    #[test]
    fn token_registry_remove_nonexistent() {
        let mut reg = TokenRegistry::new(std::iter::empty::<(&str, Address)>());
        assert!(reg.remove("WETH").is_none());
    }

    #[test]
    fn token_registry_get_missing_returns_none() {
        let reg = TokenRegistry::new(std::iter::empty::<(&str, Address)>());
        assert_eq!(reg.get("WETH"), None);
        assert_eq!(reg.get_decimals("WETH"), None);
    }

    #[test]
    fn token_registry_display() {
        let reg = TokenRegistry::new([("A", Address::ZERO), ("B", Address::ZERO)]);
        let s = format!("{reg}");
        assert!(s.contains("2 tokens"));
    }

    // ── CowSwapConfig ───────────────────────────────────────────────────

    fn empty_registry() -> TokenRegistry {
        TokenRegistry::new(std::iter::empty::<(&str, Address)>())
    }

    #[test]
    fn config_prod_defaults() {
        let cfg = CowSwapConfig::prod(
            SupportedChainId::Mainnet,
            Address::ZERO,
            empty_registry(),
            50,
            1800,
        );
        assert!(cfg.env.is_prod());
        assert_eq!(cfg.slippage_bps, 50);
        assert_eq!(cfg.order_valid_secs, 1800);
        assert_eq!(cfg.sell_token_decimals, 18);
        assert!(!cfg.has_custom_receiver());
    }

    #[test]
    fn config_staging_defaults() {
        let cfg = CowSwapConfig::staging(
            SupportedChainId::Sepolia,
            Address::ZERO,
            empty_registry(),
            100,
            900,
        );
        assert!(cfg.env.is_staging());
        assert_eq!(cfg.slippage_bps, 100);
    }

    #[test]
    fn config_builder_methods() {
        let cfg = CowSwapConfig::prod(
            SupportedChainId::Mainnet,
            Address::ZERO,
            empty_registry(),
            50,
            1800,
        )
        .with_slippage_bps(100)
        .with_order_valid_secs(600)
        .with_sell_token_decimals(6)
        .with_chain_id(SupportedChainId::Sepolia)
        .with_env(Env::Staging);

        assert_eq!(cfg.slippage_bps, 100);
        assert_eq!(cfg.order_valid_secs, 600);
        assert_eq!(cfg.sell_token_decimals, 6);
        assert_eq!(cfg.chain_id, SupportedChainId::Sepolia);
        assert!(cfg.env.is_staging());
    }

    #[test]
    fn config_with_receiver() {
        let recv = Address::new([0x01; 20]);
        let wallet = Address::new([0x02; 20]);
        let cfg = CowSwapConfig::prod(
            SupportedChainId::Mainnet,
            Address::ZERO,
            empty_registry(),
            50,
            1800,
        )
        .with_receiver(recv);
        assert!(cfg.has_custom_receiver());
        assert_eq!(cfg.effective_receiver(wallet), recv);
    }

    #[test]
    fn config_effective_receiver_defaults_to_wallet() {
        let wallet = Address::new([0x02; 20]);
        let cfg = CowSwapConfig::prod(
            SupportedChainId::Mainnet,
            Address::ZERO,
            empty_registry(),
            50,
            1800,
        );
        assert_eq!(cfg.effective_receiver(wallet), wallet);
    }

    #[test]
    fn config_with_sell_token() {
        let token = Address::new([0xaa; 20]);
        let cfg = CowSwapConfig::prod(
            SupportedChainId::Mainnet,
            Address::ZERO,
            empty_registry(),
            50,
            1800,
        )
        .with_sell_token(token);
        assert_eq!(cfg.sell_token, token);
    }

    #[test]
    fn config_display() {
        let cfg = CowSwapConfig::prod(
            SupportedChainId::Mainnet,
            Address::ZERO,
            empty_registry(),
            50,
            1800,
        );
        let s = format!("{cfg}");
        assert!(s.contains("config("));
    }
}