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
//! Trade simulation for estimating gas costs and detecting reverts.
//!
//! Provides [`TradeSimulator`] for simulating settlement execution against
//! an Ethereum node via JSON-RPC, and [`SimulationResult`] for inspecting
//! the outcome.

use std::fmt;

use alloy_primitives::Address;

use crate::{
    config::{chain::SupportedChainId, contracts::settlement_contract},
    error::CowError,
};

use super::encoder::SettlementEncoder;

/// Result of simulating a settlement transaction via `eth_call`.
///
/// Contains the success status, estimated gas usage, and raw return data
/// from the simulated call.
///
/// # Example
///
/// ```
/// use cow_rs::settlement::simulator::SimulationResult;
///
/// let result = SimulationResult::new(true, 150_000, vec![]);
/// assert!(result.is_success());
/// assert!(!result.is_revert());
/// assert_eq!(result.gas_used, 150_000);
/// ```
#[derive(Debug, Clone)]
pub struct SimulationResult {
    /// Whether the simulation completed without reverting.
    pub success: bool,
    /// Estimated gas consumed by the simulated transaction.
    pub gas_used: u64,
    /// Raw bytes returned by the simulated call.
    pub return_data: Vec<u8>,
}

impl SimulationResult {
    /// Create a new simulation result.
    ///
    /// # Arguments
    ///
    /// * `success` - Whether the simulation succeeded.
    /// * `gas_used` - Estimated gas consumed.
    /// * `return_data` - Raw return bytes from the call.
    ///
    /// # Returns
    ///
    /// A new [`SimulationResult`].
    #[must_use]
    pub const fn new(success: bool, gas_used: u64, return_data: Vec<u8>) -> Self {
        Self { success, gas_used, return_data }
    }

    /// Check whether the simulation succeeded (did not revert).
    ///
    /// # Returns
    ///
    /// `true` if the simulated transaction completed without reverting.
    #[must_use]
    pub const fn is_success(&self) -> bool {
        self.success
    }

    /// Check whether the simulation reverted.
    ///
    /// # Returns
    ///
    /// `true` if the simulated transaction reverted.
    #[must_use]
    pub const fn is_revert(&self) -> bool {
        !self.success
    }
}

impl fmt::Display for SimulationResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.success {
            write!(f, "Success (gas: {})", self.gas_used)
        } else {
            write!(f, "Revert (gas: {}, data: {} bytes)", self.gas_used, self.return_data.len())
        }
    }
}

/// Simulates settlement execution to estimate gas costs and detect reverts.
///
/// Wraps a `reqwest::Client` targeting a JSON-RPC endpoint and the canonical
/// `GPv2Settlement` contract on a specific chain.
///
/// # Example
///
/// ```rust
/// use cow_rs::{SupportedChainId, settlement::simulator::TradeSimulator};
///
/// let sim = TradeSimulator::new("https://rpc.sepolia.org", SupportedChainId::Sepolia);
/// ```
#[derive(Debug, Clone)]
pub struct TradeSimulator {
    /// The JSON-RPC endpoint URL.
    rpc_url: String,
    /// HTTP client for making RPC requests.
    client: reqwest::Client,
    /// The settlement contract address on the target chain.
    settlement: Address,
}

impl TradeSimulator {
    /// Build a `reqwest::Client` with platform-appropriate settings.
    ///
    /// # Returns
    ///
    /// A configured [`reqwest::Client`] with a 30-second timeout on native targets,
    /// or a default client on WASM targets.
    #[allow(clippy::shadow_reuse, reason = "builder pattern chains naturally shadow")]
    fn build_client() -> reqwest::Client {
        let builder = reqwest::Client::builder();
        #[cfg(not(target_arch = "wasm32"))]
        let builder = builder.timeout(std::time::Duration::from_secs(30));
        builder.build().unwrap_or_default()
    }

    /// Create a new trade simulator for the given chain.
    ///
    /// Uses the canonical `GPv2Settlement` contract address for `chain`.
    ///
    /// # Arguments
    ///
    /// * `rpc_url` - The JSON-RPC endpoint URL.
    /// * `chain` - The target [`SupportedChainId`].
    ///
    /// # Returns
    ///
    /// A new [`TradeSimulator`] configured for the specified chain.
    #[must_use]
    pub fn new(rpc_url: impl Into<String>, chain: SupportedChainId) -> Self {
        Self {
            rpc_url: rpc_url.into(),
            client: Self::build_client(),
            settlement: settlement_contract(chain),
        }
    }

    /// Return the settlement contract address this simulator targets.
    ///
    /// # Returns
    ///
    /// The settlement contract [`Address`].
    #[must_use]
    pub const fn settlement_address(&self) -> Address {
        self.settlement
    }

    /// Return the RPC URL this simulator is configured to use.
    ///
    /// # Returns
    ///
    /// A reference to the RPC URL string.
    #[must_use]
    pub fn rpc_url(&self) -> &str {
        &self.rpc_url
    }

    /// Estimate the gas cost of executing calldata against the settlement contract.
    ///
    /// Sends an `eth_estimateGas` JSON-RPC request with the provided calldata
    /// targeting the settlement contract.
    ///
    /// # Arguments
    ///
    /// * `calldata` - The ABI-encoded calldata to estimate gas for.
    ///
    /// # Returns
    ///
    /// The estimated gas as `u64`.
    ///
    /// # Errors
    ///
    /// Returns [`CowError::Rpc`] if the RPC request fails or the node returns
    /// an error (e.g., the transaction would revert).
    pub async fn estimate_gas(&self, calldata: &[u8]) -> Result<u64, CowError> {
        let to_hex = format!("{:#x}", self.settlement);
        let data_hex = format!("0x{}", alloy_primitives::hex::encode(calldata));

        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "method":  "eth_estimateGas",
            "params":  [{"to": to_hex, "data": data_hex}],
            "id":      1u32
        });

        let resp = self.client.post(&self.rpc_url).json(&body).send().await?;

        if !resp.status().is_success() {
            let code = i64::from(resp.status().as_u16());
            let msg = resp.text().await.unwrap_or_else(|_e| String::new());
            return Err(CowError::Rpc { code, message: msg });
        }

        let rpc: RpcResponse = resp.json().await?;

        if let Some(err) = rpc.error {
            return Err(CowError::Rpc { code: err.code, message: err.message });
        }

        let hex_str = rpc
            .result
            .ok_or_else(|| CowError::Rpc { code: -1, message: "missing result field".into() })?;

        parse_hex_u64(&hex_str)
    }

    /// Simulate executing calldata against the settlement contract via `eth_call`.
    ///
    /// Unlike [`estimate_gas`](Self::estimate_gas), this method does not fail on
    /// reverts — instead it returns a [`SimulationResult`] indicating whether the
    /// call succeeded or reverted.
    ///
    /// # Arguments
    ///
    /// * `calldata` - The ABI-encoded calldata to simulate.
    ///
    /// # Returns
    ///
    /// A [`SimulationResult`] with success status, gas estimate, and return data.
    ///
    /// # Errors
    ///
    /// Returns [`CowError::Rpc`] only on transport-level failures (HTTP errors).
    /// Execution reverts are captured in the returned [`SimulationResult`].
    pub async fn simulate(&self, calldata: &[u8]) -> Result<SimulationResult, CowError> {
        let to_hex = format!("{:#x}", self.settlement);
        let data_hex = format!("0x{}", alloy_primitives::hex::encode(calldata));

        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "method":  "eth_call",
            "params":  [{"to": to_hex, "data": data_hex}, "latest"],
            "id":      1u32
        });

        let resp = self.client.post(&self.rpc_url).json(&body).send().await?;

        if !resp.status().is_success() {
            let code = i64::from(resp.status().as_u16());
            let msg = resp.text().await.unwrap_or_else(|_e| String::new());
            return Err(CowError::Rpc { code, message: msg });
        }

        let rpc: RpcResponse = resp.json().await?;

        if let Some(err) = rpc.error {
            // Execution revert — capture as a failed simulation rather than
            // propagating as an error.
            return Ok(SimulationResult::new(false, 0, err.message.into_bytes()));
        }

        let hex_str = rpc
            .result
            .ok_or_else(|| CowError::Rpc { code: -1, message: "missing result field".into() })?;

        let return_data = decode_hex_result(&hex_str)?;

        // Attempt a gas estimate for successful simulations.
        let gas_used = self.estimate_gas(calldata).await.unwrap_or_default();

        Ok(SimulationResult::new(true, gas_used, return_data))
    }

    /// Convenience method: encode a settlement and estimate its gas cost.
    ///
    /// Combines [`SettlementEncoder::encode_settlement`] with
    /// [`estimate_gas`](Self::estimate_gas).
    ///
    /// # Arguments
    ///
    /// * `encoder` - The [`SettlementEncoder`] containing the settlement to estimate.
    ///
    /// # Returns
    ///
    /// The estimated gas as `u64`.
    ///
    /// # Errors
    ///
    /// Returns [`CowError::Rpc`] if the RPC request fails or the settlement
    /// would revert.
    pub async fn estimate_settlement(&self, encoder: &SettlementEncoder) -> Result<u64, CowError> {
        let calldata = encoder.encode_settlement();
        self.estimate_gas(&calldata).await
    }
}

// ── JSON-RPC response types (private) ────────────────────────────────────────

#[derive(serde::Deserialize)]
struct RpcResponse {
    result: Option<String>,
    error: Option<RpcError>,
}

#[derive(serde::Deserialize)]
struct RpcError {
    code: i64,
    message: String,
}

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

/// Parse a `0x`-prefixed hex string as a `u64`.
fn parse_hex_u64(hex_str: &str) -> Result<u64, CowError> {
    let clean = hex_str.trim_start_matches("0x");
    u64::from_str_radix(clean, 16)
        .map_err(|e| CowError::Parse { field: "gas_estimate", reason: format!("invalid hex: {e}") })
}

/// Decode a `0x`-prefixed hex result string into bytes.
fn decode_hex_result(hex_str: &str) -> Result<Vec<u8>, CowError> {
    let clean = hex_str.trim_start_matches("0x");
    alloy_primitives::hex::decode(clean)
        .map_err(|e| CowError::Rpc { code: -1, message: format!("hex decode: {e}") })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::contracts::SETTLEMENT_CONTRACT;

    // ── SimulationResult tests ───────────────────────────────────────────

    #[test]
    fn simulation_result_new() {
        let result = SimulationResult::new(true, 100_000, vec![0xab, 0xcd]);
        assert!(result.success);
        assert_eq!(result.gas_used, 100_000);
        assert_eq!(result.return_data, vec![0xab, 0xcd]);
    }

    #[test]
    fn simulation_result_is_success() {
        let success = SimulationResult::new(true, 50_000, vec![]);
        assert!(success.is_success());
        assert!(!success.is_revert());
    }

    #[test]
    fn simulation_result_is_revert() {
        let revert = SimulationResult::new(false, 0, vec![0xff]);
        assert!(!revert.is_success());
        assert!(revert.is_revert());
    }

    #[test]
    fn simulation_result_display_success() {
        let result = SimulationResult::new(true, 150_000, vec![]);
        assert_eq!(format!("{result}"), "Success (gas: 150000)");
    }

    #[test]
    fn simulation_result_display_revert() {
        let result = SimulationResult::new(false, 21_000, vec![0xde, 0xad]);
        assert_eq!(format!("{result}"), "Revert (gas: 21000, data: 2 bytes)");
    }

    #[test]
    fn simulation_result_clone() {
        let result = SimulationResult::new(true, 42, vec![1, 2, 3]);
        let cloned = result.clone();
        assert_eq!(cloned.success, result.success);
        assert_eq!(cloned.gas_used, result.gas_used);
        assert_eq!(cloned.return_data, result.return_data);
    }

    // ── TradeSimulator construction tests ────────────────────────────────

    #[test]
    fn trade_simulator_new_mainnet() {
        let sim = TradeSimulator::new("https://eth.example.com", SupportedChainId::Mainnet);
        assert_eq!(sim.settlement_address(), SETTLEMENT_CONTRACT);
        assert_eq!(sim.rpc_url(), "https://eth.example.com");
    }

    #[test]
    fn trade_simulator_new_sepolia() {
        let sim = TradeSimulator::new("https://sepolia.example.com", SupportedChainId::Sepolia);
        assert_eq!(sim.settlement_address(), settlement_contract(SupportedChainId::Sepolia));
        assert_eq!(sim.rpc_url(), "https://sepolia.example.com");
    }

    #[test]
    fn trade_simulator_new_gnosis() {
        let sim = TradeSimulator::new("https://gnosis.example.com", SupportedChainId::GnosisChain);
        assert_eq!(sim.settlement_address(), settlement_contract(SupportedChainId::GnosisChain));
    }

    #[test]
    fn trade_simulator_new_arbitrum() {
        let sim = TradeSimulator::new("https://arb.example.com", SupportedChainId::ArbitrumOne);
        assert_eq!(sim.settlement_address(), settlement_contract(SupportedChainId::ArbitrumOne));
    }

    #[test]
    fn trade_simulator_clone() {
        let sim = TradeSimulator::new("https://example.com", SupportedChainId::Mainnet);
        let cloned = sim.clone();
        assert_eq!(cloned.settlement_address(), sim.settlement_address());
        assert_eq!(cloned.rpc_url(), sim.rpc_url());
    }

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

    #[test]
    fn parse_hex_u64_valid() {
        assert_eq!(parse_hex_u64("0x5208").unwrap(), 21_000);
    }

    #[test]
    fn parse_hex_u64_no_prefix() {
        assert_eq!(parse_hex_u64("ff").unwrap(), 255);
    }

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

    #[test]
    fn decode_hex_result_valid() {
        let bytes = decode_hex_result("0xdeadbeef").unwrap();
        assert_eq!(bytes, vec![0xde, 0xad, 0xbe, 0xef]);
    }

    #[test]
    fn decode_hex_result_empty() {
        let bytes = decode_hex_result("0x").unwrap();
        assert!(bytes.is_empty());
    }
}