diff --git a/crates/tx-cache/Cargo.toml b/crates/tx-cache/Cargo.toml index 3be91014..fdfc817c 100644 --- a/crates/tx-cache/Cargo.toml +++ b/crates/tx-cache/Cargo.toml @@ -17,13 +17,14 @@ signet-types.workspace = true alloy.workspace = true -eyre.workspace = true reqwest.workspace = true serde = { workspace = true, features = ["derive"] } tracing.workspace = true uuid = { workspace = true, features = ["serde"] } +thiserror.workspace = true +url = "2.5.7" [dev-dependencies] serde_urlencoded = "0.7.1" uuid = { workspace = true, features = ["serde", "v4"] } -serde_json.workspace = true \ No newline at end of file +serde_json.workspace = true diff --git a/crates/tx-cache/src/client.rs b/crates/tx-cache/src/client.rs index b4f033d0..9e9ca17c 100644 --- a/crates/tx-cache/src/client.rs +++ b/crates/tx-cache/src/client.rs @@ -1,9 +1,9 @@ +use crate::error::Result; use crate::types::{ CacheObject, CacheResponse, OrderKey, TxCacheOrdersResponse, TxCacheSendBundleResponse, TxCacheSendTransactionResponse, TxCacheTransactionsResponse, TxKey, }; use alloy::consensus::TxEnvelope; -use eyre::Error; use serde::{de::DeserializeOwned, Serialize}; use signet_bundle::SignetEthBundle; use signet_constants::parmigiana; @@ -39,7 +39,7 @@ impl TxCache { } /// Create a new cache given a string URL. - pub fn new_from_string(url: &str) -> Result { + pub fn new_from_string(url: &str) -> Result { let url = reqwest::Url::parse(url)?; Ok(Self::new(url)) } @@ -88,7 +88,7 @@ impl TxCache { &self, join: &'static str, obj: T, - ) -> Result { + ) -> Result { self.forward_inner_raw(join, obj) .await? .json::() @@ -101,7 +101,7 @@ impl TxCache { &self, join: &'static str, obj: T, - ) -> Result { + ) -> Result { // Append the path to the URL. let url = self .url @@ -112,7 +112,7 @@ impl TxCache { self.client.post(url).json(&obj).send().await?.error_for_status().map_err(Into::into) } - async fn get_inner(&self, join: &'static str, query: Option) -> Result + async fn get_inner(&self, join: &'static str, query: Option) -> Result where T: DeserializeOwned + CacheObject, { @@ -137,7 +137,7 @@ impl TxCache { pub async fn forward_raw_transaction( &self, tx: TxEnvelope, - ) -> Result { + ) -> Result { self.forward_inner(TRANSACTIONS, tx).await } @@ -146,13 +146,13 @@ impl TxCache { pub async fn forward_bundle( &self, bundle: SignetEthBundle, - ) -> Result { + ) -> Result { self.forward_inner(BUNDLES, bundle).await } /// Forward an order to the URL. #[instrument(skip_all)] - pub async fn forward_order(&self, order: SignedOrder) -> Result<(), Error> { + pub async fn forward_order(&self, order: SignedOrder) -> Result<()> { self.forward_inner_raw(ORDERS, order).await.map(drop) } @@ -161,7 +161,7 @@ impl TxCache { pub async fn get_transactions( &self, query: Option, - ) -> Result, Error> { + ) -> Result> { self.get_inner(TRANSACTIONS, query).await } @@ -170,7 +170,7 @@ impl TxCache { pub async fn get_orders( &self, query: Option, - ) -> Result, Error> { + ) -> Result> { self.get_inner(ORDERS, query).await } } diff --git a/crates/tx-cache/src/error.rs b/crates/tx-cache/src/error.rs new file mode 100644 index 00000000..2df04e24 --- /dev/null +++ b/crates/tx-cache/src/error.rs @@ -0,0 +1,35 @@ +/// Result type for [`TxCache`] operations. +/// +/// [`TxCache`]: crate::client::TxCache +pub type Result = std::result::Result; + +/// Errors returned by the [`TxCache`] client. +/// +/// [`TxCache`]: crate::client::TxCache +#[derive(thiserror::Error, Debug)] +pub enum TxCacheError { + /// The requested transaction or bundle was not found in the cache. + #[error("Transaction not found in cache")] + NotFound, + /// The request was made during a slot that is not assigned to this builder. + #[error("Request occurred during a slot that is not assigned to this builder")] + NotOurSlot, + + /// An error occurred while parsing the URL. + #[error(transparent)] + Url(#[from] url::ParseError), + + /// An error occurred while contacting the TxCache API. + #[error("Error contacting TxCache API: {0}")] + Reqwest(reqwest::Error), +} + +impl From for TxCacheError { + fn from(err: reqwest::Error) -> Self { + match err.status() { + Some(reqwest::StatusCode::NOT_FOUND) => TxCacheError::NotFound, + Some(reqwest::StatusCode::FORBIDDEN) => TxCacheError::NotOurSlot, + _ => TxCacheError::Reqwest(err), + } + } +} diff --git a/crates/tx-cache/src/lib.rs b/crates/tx-cache/src/lib.rs index 2b1fad12..815c260a 100644 --- a/crates/tx-cache/src/lib.rs +++ b/crates/tx-cache/src/lib.rs @@ -13,9 +13,14 @@ #![cfg_attr(docsrs, feature(doc_cfg))] /// The [`TxCache`] client. -pub mod client; +mod client; +pub use client::TxCache; /// Response types for the [`TxCache`]. /// /// [`TxCache`]: crate::client::TxCache pub mod types; + +/// Errors returned by the [`TxCache`] client. +pub mod error; +pub use error::TxCacheError;