cow-rs 0.1.2

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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
//! Injectable trait abstractions for testing and composition.
//!
//! These traits decouple the SDK's high-level orchestration logic from
//! concrete HTTP clients, signers, and RPC providers. Production code
//! uses the real implementations ([`OrderBookApi`], [`PrivateKeySigner`],
//! [`OnchainReader`]), while test code can inject lightweight mocks.
//!
//! [`OrderBookApi`]: crate::order_book::OrderBookApi
//! [`PrivateKeySigner`]: alloy_signer_local::PrivateKeySigner
//! [`OnchainReader`]: crate::onchain::OnchainReader

use alloy_primitives::{Address, B256};

use crate::{
    error::CowError,
    order_book::types::{
        Order, OrderCancellations, OrderCreation, OrderQuoteRequest, OrderQuoteResponse, Trade,
    },
};

/// Abstraction over the `CoW` Protocol orderbook HTTP API.
///
/// [`OrderBookApi`](crate::order_book::OrderBookApi) implements this trait
/// by delegating to its existing async methods. Tests can inject mocks
/// that return canned responses without any network I/O.
///
/// Every method mirrors a core orderbook operation used by the
/// [`TradingSdk`](crate::trading::TradingSdk) internally.
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait OrderbookClient: Send + Sync {
    /// Obtain a price quote for an order.
    ///
    /// Mirrors [`OrderBookApi::get_quote`](crate::order_book::OrderBookApi::get_quote).
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the quote request fails or is rejected.
    async fn get_quote(&self, request: &OrderQuoteRequest) -> Result<OrderQuoteResponse, CowError>;

    /// Submit a signed order and return the assigned order UID.
    ///
    /// Mirrors [`OrderBookApi::send_order`](crate::order_book::OrderBookApi::send_order).
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the order is rejected or the request fails.
    async fn send_order(&self, creation: &OrderCreation) -> Result<String, CowError>;

    /// Fetch an order by its unique identifier.
    ///
    /// Mirrors [`OrderBookApi::get_order`](crate::order_book::OrderBookApi::get_order).
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the order is not found or the request fails.
    async fn get_order(&self, order_uid: &str) -> Result<Order, CowError>;

    /// List trades for a given order UID.
    ///
    /// Mirrors [`OrderBookApi::get_trades`](crate::order_book::OrderBookApi::get_trades)
    /// with a fixed `order_uid` filter and default limit.
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the request fails.
    async fn get_trades(&self, order_uid: &str) -> Result<Vec<Trade>, CowError>;

    /// Cancel one or more orders (best-effort off-chain cancellation).
    ///
    /// Mirrors [`OrderBookApi::cancel_orders`](crate::order_book::OrderBookApi::cancel_orders).
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the cancellation is rejected or the request fails.
    async fn cancel_orders(&self, cancellation: &OrderCancellations) -> Result<(), CowError>;
}

/// Abstraction over ECDSA signing used by the SDK.
///
/// [`PrivateKeySigner`](alloy_signer_local::PrivateKeySigner) implements
/// this trait. Tests can inject a mock signer that returns deterministic
/// signatures without a real private key.
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait CowSigner: Send + Sync {
    /// Return the signer's Ethereum address.
    fn address(&self) -> Address;

    /// Sign an EIP-712 typed-data digest.
    ///
    /// `domain_separator` and `struct_hash` are the two 32-byte components;
    /// the implementor must hash them with the `\x19\x01` prefix and sign
    /// the result.
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] on signing failure.
    async fn sign_typed_data(
        &self,
        domain_separator: B256,
        struct_hash: B256,
    ) -> Result<Vec<u8>, CowError>;

    /// Sign a raw message using EIP-191 personal-sign semantics.
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] on signing failure.
    async fn sign_message(&self, message: &[u8]) -> Result<Vec<u8>, CowError>;
}

/// Abstraction over JSON-RPC `eth_call` and `eth_getStorageAt`.
///
/// [`OnchainReader`](crate::onchain::OnchainReader) implements this trait.
/// Tests can inject a mock that returns pre-computed ABI-encoded results.
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait RpcProvider: Send + Sync {
    /// Execute a read-only `eth_call` against a contract.
    ///
    /// # Arguments
    ///
    /// * `to` - The contract address to call.
    /// * `data` - ABI-encoded calldata (selector + arguments).
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the RPC request fails.
    async fn eth_call(&self, to: Address, data: &[u8]) -> Result<Vec<u8>, CowError>;

    /// Read a single storage slot at block `"latest"`.
    ///
    /// # Arguments
    ///
    /// * `address` - The contract address whose storage to read.
    /// * `slot` - The storage slot position as a 32-byte value.
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the RPC request fails.
    async fn eth_get_storage_at(&self, address: Address, slot: B256) -> Result<B256, CowError>;
}

/// Abstraction over IPFS fetch and upload operations.
///
/// [`Ipfs`](crate::app_data::Ipfs) implements this trait by delegating to
/// the existing free functions in [`crate::app_data`]. Tests can inject a
/// mock that returns canned CID/content pairs without any network I/O.
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait IpfsClient: Send + Sync {
    /// Fetch a JSON document from IPFS by its CID.
    ///
    /// # Arguments
    ///
    /// * `cid` - The `CIDv1` base16 string identifying the document.
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the fetch or deserialisation fails.
    async fn fetch(&self, cid: &str) -> Result<String, CowError>;

    /// Upload a JSON string to IPFS and return the resulting CID.
    ///
    /// # Arguments
    ///
    /// * `content` - The JSON content to pin.
    ///
    /// # Errors
    ///
    /// Returns [`CowError`] if the upload fails (e.g. missing credentials).
    async fn upload(&self, content: &str) -> Result<String, CowError>;
}

// ── Blanket impl: OrderbookClient for OrderBookApi ──────────────────────────

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[allow(clippy::use_self, reason = "fully qualified calls needed to avoid infinite recursion")]
impl OrderbookClient for crate::order_book::OrderBookApi {
    async fn get_quote(&self, request: &OrderQuoteRequest) -> Result<OrderQuoteResponse, CowError> {
        crate::order_book::OrderBookApi::get_quote(self, request).await
    }

    async fn send_order(&self, creation: &OrderCreation) -> Result<String, CowError> {
        crate::order_book::OrderBookApi::send_order(self, creation).await
    }

    async fn get_order(&self, order_uid: &str) -> Result<Order, CowError> {
        crate::order_book::OrderBookApi::get_order(self, order_uid).await
    }

    async fn get_trades(&self, order_uid: &str) -> Result<Vec<Trade>, CowError> {
        crate::order_book::OrderBookApi::get_trades(self, Some(order_uid), None).await
    }

    async fn cancel_orders(&self, cancellation: &OrderCancellations) -> Result<(), CowError> {
        crate::order_book::OrderBookApi::cancel_orders(self, cancellation).await
    }
}

// ── Blanket impl: CowSigner for PrivateKeySigner ────────────────────────────

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl CowSigner for alloy_signer_local::PrivateKeySigner {
    fn address(&self) -> Address {
        alloy_signer::Signer::address(self)
    }

    async fn sign_typed_data(
        &self,
        domain_separator: B256,
        struct_hash: B256,
    ) -> Result<Vec<u8>, CowError> {
        // Reconstruct the EIP-712 digest: keccak256("\x19\x01" || domain_separator || struct_hash)
        use alloy_primitives::keccak256;
        let mut msg = [0u8; 66];
        msg[0] = 0x19;
        msg[1] = 0x01;
        msg[2..34].copy_from_slice(domain_separator.as_ref());
        msg[34..66].copy_from_slice(struct_hash.as_ref());
        let digest = keccak256(msg);
        let sig = alloy_signer::Signer::sign_hash(self, &digest)
            .await
            .map_err(|e| CowError::Signing(e.to_string()))?;
        Ok(sig.as_bytes().to_vec())
    }

    async fn sign_message(&self, message: &[u8]) -> Result<Vec<u8>, CowError> {
        let sig = alloy_signer::Signer::sign_message(self, message)
            .await
            .map_err(|e| CowError::Signing(e.to_string()))?;
        Ok(sig.as_bytes().to_vec())
    }
}

// ── Blanket impl: RpcProvider for OnchainReader ─────────────────────────────

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[allow(clippy::use_self, reason = "fully qualified calls needed to avoid infinite recursion")]
impl RpcProvider for crate::onchain::OnchainReader {
    async fn eth_call(&self, to: Address, data: &[u8]) -> Result<Vec<u8>, CowError> {
        crate::onchain::OnchainReader::eth_call(self, to, data).await
    }

    async fn eth_get_storage_at(&self, address: Address, slot: B256) -> Result<B256, CowError> {
        let slot_hex = format!("{slot:#x}");
        crate::onchain::OnchainReader::eth_get_storage_at(self, address, &slot_hex).await
    }
}

// ── Blanket impl: IpfsClient for Ipfs ──────────────────────────────────────

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl IpfsClient for crate::app_data::Ipfs {
    async fn fetch(&self, cid: &str) -> Result<String, CowError> {
        let base =
            self.read_uri.as_deref().unwrap_or_else(|| crate::app_data::DEFAULT_IPFS_READ_URI);
        let url = format!("{base}/{cid}");
        let text = reqwest::get(&url).await?.text().await?;
        Ok(text)
    }

    async fn upload(&self, content: &str) -> Result<String, CowError> {
        let api_key = self.pinata_api_key.as_deref().ok_or_else(|| {
            CowError::AppData("pinata_api_key is required for IPFS upload".into())
        })?;
        let api_secret = self.pinata_api_secret.as_deref().ok_or_else(|| {
            CowError::AppData("pinata_api_secret is required for IPFS upload".into())
        })?;

        let write_uri =
            self.write_uri.as_deref().unwrap_or_else(|| crate::app_data::DEFAULT_IPFS_WRITE_URI);
        let url = format!("{write_uri}/pinning/pinJSONToIPFS");

        let parsed: serde_json::Value =
            serde_json::from_str(content).map_err(|e| CowError::AppData(e.to_string()))?;

        let body = serde_json::json!({
            "pinataContent": parsed,
            "pinataOptions": { "cidVersion": 1 },
        });

        let resp = reqwest::Client::new()
            .post(&url)
            .header("pinata_api_key", api_key)
            .header("pinata_secret_api_key", api_secret)
            .json(&body)
            .send()
            .await?;

        let status = resp.status().as_u16();
        let text = resp.text().await?;
        if status != 200 {
            return Err(CowError::Api { status, body: text });
        }

        #[derive(serde::Deserialize)]
        #[serde(rename_all = "PascalCase")]
        struct PinataResponse {
            ipfs_hash: String,
        }
        let pinata: PinataResponse =
            serde_json::from_str(&text).map_err(|e| CowError::AppData(e.to_string()))?;
        Ok(pinata.ipfs_hash)
    }
}

// ── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use alloy_primitives::address;

    use super::*;

    // ── Mock: OrderbookClient ───────────────────────────────────────────

    /// A mock orderbook client that returns canned responses.
    struct MockOrderbook {
        /// The quote response returned by [`get_quote`].
        quote_response: OrderQuoteResponse,
        /// The order UID returned by [`send_order`].
        send_order_uid: String,
        /// The order returned by [`get_order`].
        order: Order,
        /// The trades returned by [`get_trades`].
        trades: Vec<Trade>,
    }

    impl MockOrderbook {
        /// Build a minimal mock with zeroed/empty canned responses.
        fn minimal() -> Self {
            Self {
                quote_response: serde_json::from_str(MINIMAL_QUOTE_RESPONSE_JSON)
                    .unwrap_or_else(|e| panic!("bad fixture: {e}")),
                send_order_uid: "0xmockuid".to_owned(),
                order: serde_json::from_str(MINIMAL_ORDER_JSON)
                    .unwrap_or_else(|e| panic!("bad fixture: {e}")),
                trades: Vec::new(),
            }
        }
    }

    #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
    #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    impl OrderbookClient for MockOrderbook {
        async fn get_quote(
            &self,
            _request: &OrderQuoteRequest,
        ) -> Result<OrderQuoteResponse, CowError> {
            Ok(self.quote_response.clone())
        }

        async fn send_order(&self, _creation: &OrderCreation) -> Result<String, CowError> {
            Ok(self.send_order_uid.clone())
        }

        async fn get_order(&self, _order_uid: &str) -> Result<Order, CowError> {
            Ok(self.order.clone())
        }

        async fn get_trades(&self, _order_uid: &str) -> Result<Vec<Trade>, CowError> {
            Ok(self.trades.clone())
        }

        async fn cancel_orders(&self, _cancellation: &OrderCancellations) -> Result<(), CowError> {
            Ok(())
        }
    }

    // ── Mock: CowSigner ────────────────────────────────────────────────

    /// A mock signer that returns a fixed address and dummy signatures.
    struct MockSigner {
        addr: Address,
    }

    #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
    #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    impl CowSigner for MockSigner {
        fn address(&self) -> Address {
            self.addr
        }

        async fn sign_typed_data(
            &self,
            _domain_separator: B256,
            _struct_hash: B256,
        ) -> Result<Vec<u8>, CowError> {
            Ok(vec![0u8; 65])
        }

        async fn sign_message(&self, _message: &[u8]) -> Result<Vec<u8>, CowError> {
            Ok(vec![0u8; 65])
        }
    }

    // ── Mock: RpcProvider ──────────────────────────────────────────────

    /// A mock RPC provider that returns zeroed responses.
    struct MockRpcProvider;

    #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
    #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    impl RpcProvider for MockRpcProvider {
        async fn eth_call(&self, _to: Address, _data: &[u8]) -> Result<Vec<u8>, CowError> {
            Ok(vec![0u8; 32])
        }

        async fn eth_get_storage_at(
            &self,
            _address: Address,
            _slot: B256,
        ) -> Result<B256, CowError> {
            Ok(B256::ZERO)
        }
    }

    // ── JSON fixtures ──────────────────────────────────────────────────

    const MINIMAL_QUOTE_RESPONSE_JSON: &str = r#"{
        "quote": {
            "sellToken": "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
            "buyToken": "0x1c7d4b196cb0c7b01d743fbc6116a902379c7238",
            "receiver": "0x0000000000000000000000000000000000000000",
            "sellAmount": "1000000000000000",
            "buyAmount": "500000",
            "validTo": 1700000000,
            "appData": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "feeAmount": "1000000000000",
            "kind": "sell",
            "partiallyFillable": false,
            "sellTokenBalance": "erc20",
            "buyTokenBalance": "erc20"
        },
        "from": "0x0000000000000000000000000000000000000000",
        "expiration": "2099-01-01T00:00:00Z",
        "id": 12345,
        "verified": false
    }"#;

    const MINIMAL_ORDER_JSON: &str = r#"{
        "uid": "0xmockuid",
        "sellToken": "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
        "buyToken": "0x1c7d4b196cb0c7b01d743fbc6116a902379c7238",
        "receiver": "0x0000000000000000000000000000000000000000",
        "sellAmount": "1000000000000000",
        "buyAmount": "500000",
        "validTo": 1700000000,
        "appData": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "feeAmount": "1000000000000",
        "kind": "sell",
        "partiallyFillable": false,
        "sellTokenBalance": "erc20",
        "buyTokenBalance": "erc20",
        "creationDate": "2024-01-01T00:00:00Z",
        "owner": "0x0000000000000000000000000000000000000000",
        "availableBalance": null,
        "executedSellAmount": "0",
        "executedSellAmountBeforeFees": "0",
        "executedBuyAmount": "0",
        "executedFeeAmount": "0",
        "invalidated": false,
        "status": "open",
        "signingScheme": "eip712",
        "signature": "0x",
        "fullAppData": null,
        "class": "market",
        "executedSurplusFee": "0"
    }"#;

    // ── Tests ──────────────────────────────────────────────────────────

    #[tokio::test]
    async fn mock_orderbook_get_quote() {
        let mock = MockOrderbook::minimal();
        let req: OrderQuoteRequest = serde_json::from_value(serde_json::json!({
            "sellToken": "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
            "buyToken": "0x1c7d4b196cb0c7b01d743fbc6116a902379c7238",
            "from": "0x0000000000000000000000000000000000000000",
            "appData": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "partiallyFillable": false,
            "sellTokenBalance": "erc20",
            "buyTokenBalance": "erc20",
            "priceQuality": "optimal",
            "signingScheme": "eip712",
            "kind": "sell",
            "sellAmountBeforeFee": "1000000000000000"
        }))
        .unwrap_or_else(|e| panic!("bad request fixture: {e}"));

        let resp = mock.get_quote(&req).await;
        assert!(resp.is_ok(), "mock get_quote should succeed");
        assert_eq!(resp.unwrap_or_else(|e| panic!("{e}")).id, Some(12345));
    }

    #[tokio::test]
    async fn mock_orderbook_send_order() {
        let mock = MockOrderbook::minimal();
        let creation: OrderCreation = serde_json::from_value(serde_json::json!({
            "sellToken": "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
            "buyToken": "0x1c7d4b196cb0c7b01d743fbc6116a902379c7238",
            "receiver": "0x0000000000000000000000000000000000000000",
            "sellAmount": "1000000000000000",
            "buyAmount": "500000",
            "validTo": 1700000000,
            "appData": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "feeAmount": "0",
            "kind": "sell",
            "partiallyFillable": false,
            "sellTokenBalance": "erc20",
            "buyTokenBalance": "erc20",
            "signingScheme": "eip712",
            "signature": "0x",
            "from": "0x0000000000000000000000000000000000000000"
        }))
        .unwrap_or_else(|e| panic!("bad creation fixture: {e}"));

        let uid = mock.send_order(&creation).await;
        assert!(uid.is_ok(), "mock send_order should succeed");
        assert_eq!(uid.unwrap_or_else(|e| panic!("{e}")), "0xmockuid");
    }

    #[tokio::test]
    async fn mock_orderbook_get_order() {
        let mock = MockOrderbook::minimal();
        let order = mock.get_order("0xmockuid").await;
        assert!(order.is_ok(), "mock get_order should succeed");
        assert_eq!(order.unwrap_or_else(|e| panic!("{e}")).uid, "0xmockuid");
    }

    #[tokio::test]
    async fn mock_orderbook_get_trades() {
        let mock = MockOrderbook::minimal();
        let trades = mock.get_trades("0xmockuid").await;
        assert!(trades.is_ok(), "mock get_trades should succeed");
        assert!(trades.unwrap_or_else(|e| panic!("{e}")).is_empty());
    }

    #[tokio::test]
    async fn mock_orderbook_cancel_orders() {
        let mock = MockOrderbook::minimal();
        let cancellation = OrderCancellations {
            order_uids: vec!["0xmockuid".to_owned()],
            signature: "0x".to_owned(),
            signing_scheme: crate::types::EcdsaSigningScheme::Eip712,
        };
        let result = mock.cancel_orders(&cancellation).await;
        assert!(result.is_ok(), "mock cancel_orders should succeed");
    }

    #[tokio::test]
    async fn mock_signer_address() {
        let signer = MockSigner { addr: address!("1111111111111111111111111111111111111111") };
        assert_eq!(signer.address(), address!("1111111111111111111111111111111111111111"));
    }

    #[tokio::test]
    async fn mock_signer_sign_typed_data() {
        let signer = MockSigner { addr: Address::ZERO };
        let sig = signer.sign_typed_data(B256::ZERO, B256::ZERO).await;
        assert!(sig.is_ok(), "mock sign_typed_data should succeed");
        assert_eq!(sig.unwrap_or_else(|e| panic!("{e}")).len(), 65);
    }

    #[tokio::test]
    async fn mock_signer_sign_message() {
        let signer = MockSigner { addr: Address::ZERO };
        let sig = signer.sign_message(b"test message").await;
        assert!(sig.is_ok(), "mock sign_message should succeed");
        assert_eq!(sig.unwrap_or_else(|e| panic!("{e}")).len(), 65);
    }

    #[tokio::test]
    async fn mock_rpc_provider_eth_call() {
        let provider = MockRpcProvider;
        let result = provider.eth_call(Address::ZERO, &[0u8; 4]).await;
        assert!(result.is_ok(), "mock eth_call should succeed");
        assert_eq!(result.unwrap_or_else(|e| panic!("{e}")).len(), 32);
    }

    #[tokio::test]
    async fn mock_rpc_provider_eth_get_storage_at() {
        let provider = MockRpcProvider;
        let result = provider.eth_get_storage_at(Address::ZERO, B256::ZERO).await;
        assert!(result.is_ok(), "mock eth_get_storage_at should succeed");
        assert_eq!(result.unwrap_or_else(|e| panic!("{e}")), B256::ZERO);
    }

    #[tokio::test]
    async fn trait_object_orderbook_client() {
        // Verify the trait is object-safe and works behind Arc<dyn>.
        let mock: std::sync::Arc<dyn OrderbookClient> =
            std::sync::Arc::new(MockOrderbook::minimal());
        let order = mock.get_order("0xmockuid").await;
        assert!(order.is_ok(), "trait object get_order should succeed");
    }

    #[tokio::test]
    async fn trait_object_cow_signer() {
        let mock: std::sync::Arc<dyn CowSigner> =
            std::sync::Arc::new(MockSigner { addr: Address::ZERO });
        assert_eq!(mock.address(), Address::ZERO);
    }

    #[tokio::test]
    async fn trait_object_rpc_provider() {
        let mock: std::sync::Arc<dyn RpcProvider> = std::sync::Arc::new(MockRpcProvider);
        let result = mock.eth_call(Address::ZERO, &[]).await;
        assert!(result.is_ok(), "trait object eth_call should succeed");
    }

    // ── Mock: IpfsClient ───────────────────────────────────────────────

    /// A mock IPFS client that returns canned responses.
    struct MockIpfsClient {
        /// The content returned by [`fetch`].
        fetch_content: String,
        /// The CID returned by [`upload`].
        upload_cid: String,
    }

    impl MockIpfsClient {
        /// Build a minimal mock with fixed canned responses.
        fn new() -> Self {
            Self {
                fetch_content: r#"{"version":"1.3.0","appCode":"test"}"#.to_owned(),
                upload_cid: "bafybeimockcid".to_owned(),
            }
        }
    }

    #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
    #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    impl IpfsClient for MockIpfsClient {
        async fn fetch(&self, _cid: &str) -> Result<String, CowError> {
            Ok(self.fetch_content.clone())
        }

        async fn upload(&self, _content: &str) -> Result<String, CowError> {
            Ok(self.upload_cid.clone())
        }
    }

    // ── Tests ──────────────────────────────────────────────────────────

    #[test]
    fn real_private_key_signer_implements_cow_signer() {
        // Compile-time check: PrivateKeySigner implements CowSigner.
        fn _assert_cow_signer<T: CowSigner>() {}
        _assert_cow_signer::<alloy_signer_local::PrivateKeySigner>();
    }

    #[test]
    fn real_orderbook_api_implements_orderbook_client() {
        // Compile-time check: OrderBookApi implements OrderbookClient.
        fn _assert_orderbook_client<T: OrderbookClient>() {}
        _assert_orderbook_client::<crate::order_book::OrderBookApi>();
    }

    #[test]
    fn real_onchain_reader_implements_rpc_provider() {
        // Compile-time check: OnchainReader implements RpcProvider.
        fn _assert_rpc_provider<T: RpcProvider>() {}
        _assert_rpc_provider::<crate::onchain::OnchainReader>();
    }

    #[test]
    fn real_ipfs_implements_ipfs_client() {
        // Compile-time check: Ipfs implements IpfsClient.
        fn _assert_ipfs_client<T: IpfsClient>() {}
        _assert_ipfs_client::<crate::app_data::Ipfs>();
    }

    #[tokio::test]
    async fn mock_ipfs_client_fetch() {
        let mock = MockIpfsClient::new();
        let result = mock.fetch("bafybeisomecid").await;
        assert!(result.is_ok(), "mock fetch should succeed");
        assert!(result.unwrap_or_else(|e| panic!("{e}")).contains("version"));
    }

    #[tokio::test]
    async fn mock_ipfs_client_upload() {
        let mock = MockIpfsClient::new();
        let result = mock.upload(r#"{"test": true}"#).await;
        assert!(result.is_ok(), "mock upload should succeed");
        assert_eq!(result.unwrap_or_else(|e| panic!("{e}")), "bafybeimockcid");
    }

    #[tokio::test]
    async fn trait_object_ipfs_client() {
        // Verify the trait is object-safe and works behind Arc<dyn>.
        let mock: std::sync::Arc<dyn IpfsClient> = std::sync::Arc::new(MockIpfsClient::new());
        let result = mock.fetch("bafybeisomecid").await;
        assert!(result.is_ok(), "trait object fetch should succeed");
    }

    // ── PrivateKeySigner blanket impl tests ────────────────────────────

    #[tokio::test]
    async fn private_key_signer_address() {
        let signer = alloy_signer_local::PrivateKeySigner::random();
        let expected = alloy_signer::Signer::address(&signer);
        let cow_addr = <alloy_signer_local::PrivateKeySigner as CowSigner>::address(&signer);
        assert_eq!(cow_addr, expected);
    }

    #[tokio::test]
    async fn private_key_signer_sign_typed_data() {
        let signer = alloy_signer_local::PrivateKeySigner::random();
        let result = CowSigner::sign_typed_data(&signer, B256::ZERO, B256::ZERO).await;
        assert!(result.is_ok(), "PrivateKeySigner sign_typed_data should succeed");
        assert_eq!(result.unwrap().len(), 65);
    }

    #[tokio::test]
    async fn private_key_signer_sign_message() {
        let signer = alloy_signer_local::PrivateKeySigner::random();
        let result = CowSigner::sign_message(&signer, b"hello world").await;
        assert!(result.is_ok(), "PrivateKeySigner sign_message should succeed");
        assert_eq!(result.unwrap().len(), 65);
    }

    // ── Ipfs struct construction tests ──────────────────────────────────

    #[test]
    fn ipfs_struct_default_fields() {
        let ipfs = crate::app_data::Ipfs {
            read_uri: None,
            write_uri: None,
            pinata_api_key: None,
            pinata_api_secret: None,
        };
        // Exercise the default read URI path in IpfsClient::fetch
        assert!(ipfs.read_uri.is_none());
        assert!(ipfs.pinata_api_key.is_none());
    }

    // ── Error-returning mock impls ────────────────────────────────────────

    /// A mock orderbook client that always returns errors.
    struct ErrorOrderbook;

    #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
    #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    impl OrderbookClient for ErrorOrderbook {
        async fn get_quote(
            &self,
            _request: &OrderQuoteRequest,
        ) -> Result<OrderQuoteResponse, CowError> {
            Err(CowError::Api { status: 500, body: "mock error".into() })
        }

        async fn send_order(&self, _creation: &OrderCreation) -> Result<String, CowError> {
            Err(CowError::Api { status: 500, body: "mock error".into() })
        }

        async fn get_order(&self, _order_uid: &str) -> Result<Order, CowError> {
            Err(CowError::Api { status: 404, body: "not found".into() })
        }

        async fn get_trades(&self, _order_uid: &str) -> Result<Vec<Trade>, CowError> {
            Err(CowError::Api { status: 500, body: "mock error".into() })
        }

        async fn cancel_orders(&self, _cancellation: &OrderCancellations) -> Result<(), CowError> {
            Err(CowError::Api { status: 403, body: "forbidden".into() })
        }
    }

    #[tokio::test]
    async fn error_orderbook_get_quote() {
        let mock = ErrorOrderbook;
        let req: OrderQuoteRequest = serde_json::from_value(serde_json::json!({
            "sellToken": "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
            "buyToken": "0x1c7d4b196cb0c7b01d743fbc6116a902379c7238",
            "from": "0x0000000000000000000000000000000000000000",
            "appData": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "partiallyFillable": false,
            "sellTokenBalance": "erc20",
            "buyTokenBalance": "erc20",
            "priceQuality": "optimal",
            "signingScheme": "eip712",
            "kind": "sell",
            "sellAmountBeforeFee": "1000000000000000"
        }))
        .unwrap();
        let result = mock.get_quote(&req).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn error_orderbook_send_order() {
        let mock = ErrorOrderbook;
        let creation: OrderCreation = serde_json::from_value(serde_json::json!({
            "sellToken": "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
            "buyToken": "0x1c7d4b196cb0c7b01d743fbc6116a902379c7238",
            "receiver": "0x0000000000000000000000000000000000000000",
            "sellAmount": "1000000000000000",
            "buyAmount": "500000",
            "validTo": 1700000000,
            "appData": "0x0000000000000000000000000000000000000000000000000000000000000000",
            "feeAmount": "0",
            "kind": "sell",
            "partiallyFillable": false,
            "sellTokenBalance": "erc20",
            "buyTokenBalance": "erc20",
            "signingScheme": "eip712",
            "signature": "0x",
            "from": "0x0000000000000000000000000000000000000000"
        }))
        .unwrap();
        let result = mock.send_order(&creation).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn error_orderbook_get_order() {
        let mock = ErrorOrderbook;
        let result = mock.get_order("0xmockuid").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn error_orderbook_get_trades() {
        let mock = ErrorOrderbook;
        let result = mock.get_trades("0xmockuid").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn error_orderbook_cancel_orders() {
        let mock = ErrorOrderbook;
        let cancellation = OrderCancellations {
            order_uids: vec!["0xmockuid".to_owned()],
            signature: "0x".to_owned(),
            signing_scheme: crate::types::EcdsaSigningScheme::Eip712,
        };
        let result = mock.cancel_orders(&cancellation).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn trait_object_error_orderbook() {
        let mock: std::sync::Arc<dyn OrderbookClient> = std::sync::Arc::new(ErrorOrderbook);
        let result = mock.get_order("0xmockuid").await;
        assert!(result.is_err());
    }

    // ── Error-returning mock signer ───────────────────────────────────────

    struct ErrorSigner;

    #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
    #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    impl CowSigner for ErrorSigner {
        fn address(&self) -> Address {
            Address::ZERO
        }

        async fn sign_typed_data(
            &self,
            _domain_separator: B256,
            _struct_hash: B256,
        ) -> Result<Vec<u8>, CowError> {
            Err(CowError::Signing("mock signer error".into()))
        }

        async fn sign_message(&self, _message: &[u8]) -> Result<Vec<u8>, CowError> {
            Err(CowError::Signing("mock signer error".into()))
        }
    }

    #[tokio::test]
    async fn error_signer_sign_typed_data() {
        let signer = ErrorSigner;
        let result = signer.sign_typed_data(B256::ZERO, B256::ZERO).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn error_signer_sign_message() {
        let signer = ErrorSigner;
        let result = signer.sign_message(b"test").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn trait_object_error_signer() {
        let mock: std::sync::Arc<dyn CowSigner> = std::sync::Arc::new(ErrorSigner);
        let result = mock.sign_typed_data(B256::ZERO, B256::ZERO).await;
        assert!(result.is_err());
    }

    // ── Error-returning mock RPC provider ─────────────────────────────────

    struct ErrorRpcProvider;

    #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
    #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    impl RpcProvider for ErrorRpcProvider {
        async fn eth_call(&self, _to: Address, _data: &[u8]) -> Result<Vec<u8>, CowError> {
            Err(CowError::Rpc { code: -32000, message: "mock rpc error".into() })
        }

        async fn eth_get_storage_at(
            &self,
            _address: Address,
            _slot: B256,
        ) -> Result<B256, CowError> {
            Err(CowError::Rpc { code: -32000, message: "mock rpc error".into() })
        }
    }

    #[tokio::test]
    async fn error_rpc_provider_eth_call() {
        let provider = ErrorRpcProvider;
        let result = provider.eth_call(Address::ZERO, &[0u8; 4]).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn error_rpc_provider_eth_get_storage_at() {
        let provider = ErrorRpcProvider;
        let result = provider.eth_get_storage_at(Address::ZERO, B256::ZERO).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn trait_object_error_rpc_provider() {
        let mock: std::sync::Arc<dyn RpcProvider> = std::sync::Arc::new(ErrorRpcProvider);
        let result = mock.eth_call(Address::ZERO, &[]).await;
        assert!(result.is_err());
    }

    // ── Error-returning mock IPFS client ──────────────────────────────────

    struct ErrorIpfsClient;

    #[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
    #[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
    impl IpfsClient for ErrorIpfsClient {
        async fn fetch(&self, _cid: &str) -> Result<String, CowError> {
            Err(CowError::AppData("mock ipfs fetch error".into()))
        }

        async fn upload(&self, _content: &str) -> Result<String, CowError> {
            Err(CowError::AppData("mock ipfs upload error".into()))
        }
    }

    #[tokio::test]
    async fn error_ipfs_client_fetch() {
        let mock = ErrorIpfsClient;
        let result = mock.fetch("bafybeisomecid").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn error_ipfs_client_upload() {
        let mock = ErrorIpfsClient;
        let result = mock.upload(r#"{"test": true}"#).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn trait_object_error_ipfs_client() {
        let mock: std::sync::Arc<dyn IpfsClient> = std::sync::Arc::new(ErrorIpfsClient);
        let result = mock.fetch("bafybeisomecid").await;
        assert!(result.is_err());
    }

    // ── PrivateKeySigner sign_typed_data with non-zero inputs ─────────────

    #[tokio::test]
    async fn private_key_signer_sign_typed_data_non_zero() {
        let signer = alloy_signer_local::PrivateKeySigner::random();
        let ds = B256::from([0xABu8; 32]);
        let sh = B256::from([0xCDu8; 32]);
        let result = CowSigner::sign_typed_data(&signer, ds, sh).await;
        assert!(result.is_ok());
        let sig = result.unwrap();
        assert_eq!(sig.len(), 65);
        // Different inputs should produce different signatures
        let result2 = CowSigner::sign_typed_data(&signer, B256::ZERO, B256::ZERO).await;
        assert!(result2.is_ok());
        assert_ne!(sig, result2.unwrap());
    }

    // ── Ipfs struct with custom read_uri ──────────────────────────────────

    #[test]
    fn ipfs_struct_custom_read_uri() {
        let ipfs = crate::app_data::Ipfs {
            read_uri: Some("https://custom.gateway.io/ipfs".to_owned()),
            write_uri: Some("https://custom.write.io".to_owned()),
            pinata_api_key: Some("key".to_owned()),
            pinata_api_secret: Some("secret".to_owned()),
        };
        assert_eq!(ipfs.read_uri.as_deref(), Some("https://custom.gateway.io/ipfs"));
        assert_eq!(ipfs.write_uri.as_deref(), Some("https://custom.write.io"));
    }
}