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
//! Transaction Resolve Protocol (TRP) Client
//!
//! This module provides a client for interacting with the Transaction Resolve Protocol (TRP),
//! a JSON-RPC based protocol for resolving, submitting, and tracking UTxO transactions.
//!
//! ## Key Features
//!
//! - **Transaction Resolution**: Convert TX3 transaction templates into concrete UTxO transactions
//! - **Transaction Submission**: Submit signed transactions to the network
//! - **Status Monitoring**: Track transaction lifecycle from pending to finalization
//! - **Queue Inspection**: Peek at pending and in-flight transactions
//! - **Log Access**: Query historical transaction logs
//!
//! ## Usage Example
//!
//! ```ignore
//! use tx3_sdk::trp::{Client, ClientOptions, ResolveParams, SubmitParams};
//! use tx3_sdk::core::TirEnvelope;
//!
//! // Create TRP client
//! let client = Client::new(ClientOptions {
//!     endpoint: "https://trp.example.com".to_string(),
//!     headers: None,
//! });
//!
//! // Resolve a transaction
//! let params = ResolveParams {
//!     tir: TirEnvelope { /* ... */ },
//!     args: serde_json::Map::new(),
//!     env: None,
//! };
//!
//! let tx_envelope = client.resolve(params).await?;
//! println!("Resolved transaction hash: {}", tx_envelope.hash);
//!
//! // Check status
//! let status = client.check_status(vec![tx_envelope.hash]).await?;
//! ```

use reqwest::header;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use thiserror::Error;
use uuid::Uuid;

pub use crate::trp::spec::{
    ChainPoint, CheckStatusResponse, DumpLogsResponse, InflightTx, InputNotResolvedDiagnostic,
    MissingTxArgDiagnostic, PeekInflightResponse, PeekPendingResponse, PendingTx, ResolveParams,
    SubmitParams, SubmitResponse, TxEnvelope, TxLog, TxScriptFailureDiagnostic, TxStage, TxStatus,
    TxStatusMap, TxWitness, UnsupportedTirDiagnostic, WitnessType,
};

mod spec;

/// Error type for TRP client operations.
///
/// This enum represents all possible errors that can occur when interacting
/// with the TRP protocol, including network errors, HTTP errors, deserialization
/// errors, and specific TRP protocol errors.
#[derive(Debug, Error)]
pub enum Error {
    /// Network error from the underlying HTTP client.
    #[error("network error: {0}")]
    NetworkError(#[from] reqwest::Error),

    /// HTTP error with status code and message.
    #[error("HTTP error {0}: {1}")]
    HttpError(u16, String),

    /// Failed to deserialize the response from the server.
    #[error("Failed to deserialize response: {0}")]
    DeserializationError(String),

    /// Generic JSON-RPC error with code, message, and optional data.
    #[error("({0}) {1}")]
    GenericRpcError(i32, String, Option<Value>),

    /// Unknown error with a message.
    #[error("Unknown error: {0}")]
    UnknownError(String),

    /// The TIR version provided is not supported by the server.
    ///
    /// Contains the expected and provided version information.
    #[error("TIR version {provided} is not supported, expected {expected}", provided = .0.provided, expected = .0.expected)]
    UnsupportedTir(UnsupportedTirDiagnostic),

    /// The TIR envelope format is invalid.
    #[error("invalid TIR envelope")]
    InvalidTirEnvelope,

    /// Failed to decode the intermediate representation bytes.
    #[error("failed to decode IR bytes")]
    InvalidTirBytes,

    /// Only transactions from the Conway era are supported.
    #[error("only txs from Conway era are supported")]
    UnsupportedTxEra,

    /// The node cannot resolve transactions while running at the specified era.
    #[error("node can't resolve txs while running at era {era}")]
    UnsupportedEra {
        /// The era that doesn't support transaction resolution.
        era: String,
    },

    /// A required transaction argument is missing.
    ///
    /// Contains the name and expected type of the missing argument.
    #[error("missing argument `{key}` of type {ty}", key = .0.key, ty = .0.arg_type)]
    MissingTxArg(MissingTxArgDiagnostic),

    /// An input could not be resolved during transaction construction.
    ///
    /// Contains diagnostic information about the failed query.
    #[error("input `{name}` not resolved", name = .0.name)]
    InputNotResolved(Box<InputNotResolvedDiagnostic>),

    /// The transaction script execution failed.
    ///
    /// Contains log output from the failed script.
    #[error("tx script returned failure")]
    TxScriptFailure(TxScriptFailureDiagnostic),
}

impl Error {
    fn generic(payload: JsonRpcError) -> Self {
        Self::GenericRpcError(payload.code, payload.message, payload.data)
    }
}

fn expect_json_rpc_error_data<T: DeserializeOwned>(payload: JsonRpcError) -> Result<T, Error> {
    let Some(data) = payload.data.clone() else {
        return Err(Error::generic(payload));
    };

    let Ok(data) = serde_json::from_value(data.clone()) else {
        return Err(Error::generic(payload));
    };

    Ok(data)
}

impl From<JsonRpcError> for Error {
    fn from(error: JsonRpcError) -> Self {
        match error.code {
            -32000 => match expect_json_rpc_error_data(error) {
                Ok(data) => Error::UnsupportedTir(data),
                Err(e) => e,
            },
            -32001 => match expect_json_rpc_error_data(error) {
                Ok(data) => Error::MissingTxArg(data),
                Err(e) => e,
            },
            -32002 => match expect_json_rpc_error_data(error) {
                Ok(data) => Error::InputNotResolved(Box::new(data)),
                Err(e) => e,
            },
            -32003 => match expect_json_rpc_error_data(error) {
                Ok(data) => Error::TxScriptFailure(data),
                Err(e) => e,
            },
            _ => Error::generic(error),
        }
    }
}

/// Configuration options for the TRP client.
///
/// This structure holds the configuration needed to create a TRP client,
/// including the endpoint URL and optional custom headers.
///
/// # Example
///
/// ```ignore
/// use tx3_sdk::trp::ClientOptions;
/// use std::collections::HashMap;
///
/// let mut headers = HashMap::new();
/// headers.insert("Authorization".to_string(), "Bearer token123".to_string());
///
/// let options = ClientOptions {
///     endpoint: "https://trp.example.com".to_string(),
///     headers: Some(headers),
/// };
/// ```
#[derive(Debug, Clone)]
pub struct ClientOptions {
    /// The TRP server endpoint URL.
    pub endpoint: String,

    /// Optional custom HTTP headers to include in requests.
    pub headers: Option<HashMap<String, String>>,
}

/// JSON-RPC request structure.
///
/// Internal structure used to serialize JSON-RPC requests to the TRP server.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
    /// JSON-RPC version (always "2.0").
    pub jsonrpc: String,

    /// The method name to call.
    pub method: String,

    /// The method parameters.
    pub params: serde_json::Value,

    /// Request ID (UUID).
    pub id: String,
}

#[derive(Debug, Deserialize)]
struct JsonRpcResponse {
    result: Option<serde_json::Value>,
    error: Option<JsonRpcError>,
}

#[derive(Debug, Deserialize)]
struct JsonRpcError {
    code: i32,
    message: String,
    data: Option<Value>,
}

/// Client for the Transaction Resolve Protocol (TRP).
///
/// This client provides methods for interacting with a TRP server to resolve
/// transaction templates, submit signed transactions, and monitor transaction
/// status.
///
/// The client is cloneable and can be reused across multiple requests.
///
/// # Example
///
/// ```ignore
/// use tx3_sdk::trp::{Client, ClientOptions};
///
/// let client = Client::new(ClientOptions {
///     endpoint: "https://trp.example.com".to_string(),
///     headers: None,
/// });
///
/// // Use the client for multiple operations
/// let tx = client.resolve(params).await?;
/// let status = client.check_status(vec![tx.hash]).await?;
/// ```
#[derive(Clone)]
pub struct Client {
    options: ClientOptions,
    client: reqwest::Client,
}

impl Client {
    /// Creates a new TRP client with the given options.
    ///
    /// # Arguments
    ///
    /// * `options` - Configuration options including endpoint URL and optional headers
    ///
    /// # Example
    ///
    /// ```ignore
    /// use tx3_sdk::trp::{Client, ClientOptions};
    ///
    /// let client = Client::new(ClientOptions {
    ///     endpoint: "https://trp.example.com".to_string(),
    ///     headers: None,
    /// });
    /// ```
    pub fn new(options: ClientOptions) -> Self {
        Self {
            options,
            client: reqwest::Client::new(),
        }
    }

    /// Makes a raw JSON-RPC call to the TRP server.
    ///
    /// This is a low-level method for making JSON-RPC calls. Generally, you should
    /// use the higher-level methods like `resolve`, `submit`, etc.
    ///
    /// # Arguments
    ///
    /// * `method` - The JSON-RPC method name
    /// * `params` - The method parameters as a JSON value
    ///
    /// # Returns
    ///
    /// Returns the result as a JSON value on success, or an error on failure.
    pub async fn call(
        &self,
        method: &str,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, Error> {
        // Prepare headers
        let mut headers = header::HeaderMap::new();
        headers.insert(
            header::CONTENT_TYPE,
            header::HeaderValue::from_static("application/json"),
        );

        if let Some(user_headers) = &self.options.headers {
            for (key, value) in user_headers {
                if let Ok(header_name) = header::HeaderName::from_bytes(key.as_bytes()) {
                    if let Ok(header_value) = header::HeaderValue::from_str(value) {
                        headers.insert(header_name, header_value);
                    }
                }
            }
        }

        // Prepare request body with FlattenedArgs for proper serialization
        let body = JsonRpcRequest {
            jsonrpc: "2.0".to_string(),
            method: method.to_string(),
            params,
            id: Uuid::new_v4().to_string(),
        };

        // Send request
        let response = self
            .client
            .post(&self.options.endpoint)
            .headers(headers)
            .json(&serde_json::to_value(body).unwrap())
            .send()
            .await
            .map_err(Error::from)?;

        // If the response at the HTTP level is not successful, return an error
        if !response.status().is_success() {
            return Err(Error::HttpError(
                response.status().as_u16(),
                response.status().to_string(),
            ));
        }

        // Parse response
        let result: JsonRpcResponse = response
            .json()
            .await
            .map_err(|e| Error::DeserializationError(e.to_string()))?;

        // Handle possible error
        if let Some(error) = result.error {
            return Err(Error::from(error));
        }

        result
            .result
            .ok_or_else(|| Error::UnknownError("No result in response".to_string()))
    }

    /// Resolves a transaction template into a concrete transaction.
    ///
    /// This method takes a Transaction Intermediate Representation (TIR) envelope
    /// and arguments, and resolves it into a concrete UTxO transaction ready
    /// for signing.
    ///
    /// # Arguments
    ///
    /// * `request` - The resolve parameters including TIR and arguments
    ///
    /// # Returns
    ///
    /// Returns a `TxEnvelope` containing the resolved transaction hash and CBOR bytes.
    ///
    /// # Errors
    ///
    /// Can return various errors including:
    /// - `Error::UnsupportedTir` if the TIR version is not supported
    /// - `Error::MissingTxArg` if required arguments are missing
    /// - `Error::InputNotResolved` if an input cannot be found
    /// - `Error::TxScriptFailure` if script execution fails
    ///
    /// # Example
    ///
    /// ```ignore
    /// use tx3_sdk::trp::{Client, ResolveParams};
    /// use tx3_sdk::core::TirEnvelope;
    ///
    /// let client = Client::new(/* ... */);
    ///
    /// let params = ResolveParams {
    ///     tir: TirEnvelope { /* ... */ },
    ///     args: serde_json::Map::new(),
    ///     env: None,
    /// };
    ///
    /// let tx = client.resolve(params).await?;
    /// println!("Resolved hash: {}", tx.hash);
    /// ```
    pub async fn resolve(&self, request: ResolveParams) -> Result<TxEnvelope, Error> {
        let params = serde_json::to_value(request).unwrap();

        let response = self.call("trp.resolve", params).await?;

        // Return result
        let out = serde_json::from_value(response)
            .map_err(|e| Error::DeserializationError(e.to_string()))?;

        Ok(out)
    }

    /// Submits a signed transaction to the network.
    ///
    /// This method submits a signed transaction with its witnesses to the
    /// blockchain network via the TRP server.
    ///
    /// # Arguments
    ///
    /// * `request` - The submit parameters including transaction bytes and witnesses
    ///
    /// # Returns
    ///
    /// Returns a `SubmitResponse` containing the submitted transaction hash.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use tx3_sdk::trp::{Client, SubmitParams, TxWitness, WitnessType};
    /// use tx3_sdk::core::BytesEnvelope;
    ///
    /// let client = Client::new(/* ... */);
    ///
    /// let params = SubmitParams {
    ///     tx: BytesEnvelope { /* signed tx */ },
    ///     witnesses: vec![TxWitness { /* ... */ }],
    /// };
    ///
    /// let response = client.submit(params).await?;
    /// println!("Submitted: {}", response.hash);
    /// ```
    pub async fn submit(&self, request: SubmitParams) -> Result<SubmitResponse, Error> {
        let params = serde_json::to_value(request).unwrap();

        let response = self.call("trp.submit", params).await?;

        let out = serde_json::from_value(response)
            .map_err(|e| Error::DeserializationError(e.to_string()))?;

        Ok(out)
    }

    /// Checks the status of one or more transactions.
    ///
    /// This method queries the TRP server for the current status of the
    /// specified transactions.
    ///
    /// # Arguments
    ///
    /// * `hashes` - Vector of transaction hashes to check
    ///
    /// # Returns
    ///
    /// Returns a `CheckStatusResponse` containing a map of transaction hashes
    /// to their current status.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use tx3_sdk::trp::Client;
    ///
    /// let client = Client::new(/* ... */);
    ///
    /// let hashes = vec!["abc123...".to_string()];
    /// let status = client.check_status(hashes).await?;
    ///
    /// for (hash, tx_status) in status.statuses {
    ///     println!("{}: {:?}", hash, tx_status.stage);
    /// }
    /// ```
    pub async fn check_status(&self, hashes: Vec<String>) -> Result<CheckStatusResponse, Error> {
        let params = serde_json::json!({ "hashes": hashes });

        let response = self.call("trp.checkStatus", params).await?;

        let out = serde_json::from_value(response)
            .map_err(|e| Error::DeserializationError(e.to_string()))?;

        Ok(out)
    }

    /// Dumps transaction logs with optional pagination.
    ///
    /// This method retrieves a paginated list of transaction log entries,
    /// useful for monitoring and auditing transaction history.
    ///
    /// # Arguments
    ///
    /// * `cursor` - Optional pagination cursor for fetching specific pages
    /// * `limit` - Optional limit on the number of entries to return
    /// * `include_payload` - Whether to include transaction payloads in the response
    ///
    /// # Returns
    ///
    /// Returns a `DumpLogsResponse` containing log entries and an optional
    /// next cursor for pagination.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use tx3_sdk::trp::Client;
    ///
    /// let client = Client::new(/* ... */);
    ///
    /// // Get first page with 100 entries
    /// let logs = client.dump_logs(None, Some(100), Some(false)).await?;
    ///
    /// for entry in logs.entries {
    ///     println!("{}: {:?}", entry.hash, entry.stage);
    /// }
    ///
    /// // Get next page if available
    /// if let Some(next) = logs.next_cursor {
    ///     let more_logs = client.dump_logs(Some(next), Some(100), Some(false)).await?;
    /// }
    /// ```
    pub async fn dump_logs(
        &self,
        cursor: Option<u64>,
        limit: Option<u64>,
        include_payload: Option<bool>,
    ) -> Result<DumpLogsResponse, Error> {
        let mut params = serde_json::Map::new();
        if let Some(cursor) = cursor {
            params.insert("cursor".to_string(), serde_json::json!(cursor));
        }
        if let Some(limit) = limit {
            params.insert("limit".to_string(), serde_json::json!(limit));
        }
        if let Some(include_payload) = include_payload {
            params.insert(
                "includePayload".to_string(),
                serde_json::json!(include_payload),
            );
        }

        let response = self
            .call("trp.dumpLogs", serde_json::Value::Object(params))
            .await?;

        let out = serde_json::from_value(response)
            .map_err(|e| Error::DeserializationError(e.to_string()))?;

        Ok(out)
    }

    /// Peeks at pending transactions in the mempool.
    ///
    /// This method retrieves pending transactions that are waiting to be
    /// included in a block, useful for monitoring mempool state.
    ///
    /// # Arguments
    ///
    /// * `limit` - Optional limit on the number of pending transactions to return
    /// * `include_payload` - Whether to include transaction payloads in the response
    ///
    /// # Returns
    ///
    /// Returns a `PeekPendingResponse` containing pending transactions.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use tx3_sdk::trp::Client;
    ///
    /// let client = Client::new(/* ... */);
    ///
    /// let pending = client.peek_pending(Some(50), Some(false)).await?;
    ///
    /// println!("Found {} pending transactions", pending.entries.len());
    /// if pending.has_more {
    ///     println!("More transactions available");
    /// }
    /// ```
    pub async fn peek_pending(
        &self,
        limit: Option<u64>,
        include_payload: Option<bool>,
    ) -> Result<PeekPendingResponse, Error> {
        let mut params = serde_json::Map::new();
        if let Some(limit) = limit {
            params.insert("limit".to_string(), serde_json::json!(limit));
        }
        if let Some(include_payload) = include_payload {
            params.insert(
                "includePayload".to_string(),
                serde_json::json!(include_payload),
            );
        }

        let response = self
            .call("trp.peekPending", serde_json::Value::Object(params))
            .await?;

        let out = serde_json::from_value(response)
            .map_err(|e| Error::DeserializationError(e.to_string()))?;

        Ok(out)
    }

    /// Peeks at in-flight transactions being tracked by the server.
    ///
    /// This method retrieves transactions that have been submitted and are
    /// being tracked through their lifecycle stages.
    ///
    /// # Arguments
    ///
    /// * `limit` - Optional limit on the number of in-flight transactions to return
    /// * `include_payload` - Whether to include transaction payloads in the response
    ///
    /// # Returns
    ///
    /// Returns a `PeekInflightResponse` containing in-flight transactions.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use tx3_sdk::trp::Client;
    ///
    /// let client = Client::new(/* ... */);
    ///
    /// let inflight = client.peek_inflight(Some(50), Some(false)).await?;
    ///
    /// for tx in inflight.entries {
    ///     println!("{}: {:?} ({} confirmations)",
    ///         tx.hash, tx.stage, tx.confirmations);
    /// }
    /// ```
    pub async fn peek_inflight(
        &self,
        limit: Option<u64>,
        include_payload: Option<bool>,
    ) -> Result<PeekInflightResponse, Error> {
        let mut params = serde_json::Map::new();
        if let Some(limit) = limit {
            params.insert("limit".to_string(), serde_json::json!(limit));
        }
        if let Some(include_payload) = include_payload {
            params.insert(
                "includePayload".to_string(),
                serde_json::json!(include_payload),
            );
        }

        let response = self
            .call("trp.peekInflight", serde_json::Value::Object(params))
            .await?;

        let out = serde_json::from_value(response)
            .map_err(|e| Error::DeserializationError(e.to_string()))?;

        Ok(out)
    }
}