Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/tx-cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
serde_json.workspace = true
20 changes: 10 additions & 10 deletions crates/tx-cache/src/client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,7 +39,7 @@ impl TxCache {
}

/// Create a new cache given a string URL.
pub fn new_from_string(url: &str) -> Result<Self, Error> {
pub fn new_from_string(url: &str) -> Result<Self> {
let url = reqwest::Url::parse(url)?;
Ok(Self::new(url))
}
Expand Down Expand Up @@ -88,7 +88,7 @@ impl TxCache {
&self,
join: &'static str,
obj: T,
) -> Result<R, Error> {
) -> Result<R> {
self.forward_inner_raw(join, obj)
.await?
.json::<R>()
Expand All @@ -101,7 +101,7 @@ impl TxCache {
&self,
join: &'static str,
obj: T,
) -> Result<reqwest::Response, Error> {
) -> Result<reqwest::Response> {
// Append the path to the URL.
let url = self
.url
Expand All @@ -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<T>(&self, join: &'static str, query: Option<T::Key>) -> Result<T, Error>
async fn get_inner<T>(&self, join: &'static str, query: Option<T::Key>) -> Result<T>
where
T: DeserializeOwned + CacheObject,
{
Expand All @@ -137,7 +137,7 @@ impl TxCache {
pub async fn forward_raw_transaction(
&self,
tx: TxEnvelope,
) -> Result<TxCacheSendTransactionResponse, Error> {
) -> Result<TxCacheSendTransactionResponse> {
self.forward_inner(TRANSACTIONS, tx).await
}

Expand All @@ -146,13 +146,13 @@ impl TxCache {
pub async fn forward_bundle(
&self,
bundle: SignetEthBundle,
) -> Result<TxCacheSendBundleResponse, Error> {
) -> Result<TxCacheSendBundleResponse> {
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)
}

Expand All @@ -161,7 +161,7 @@ impl TxCache {
pub async fn get_transactions(
&self,
query: Option<TxKey>,
) -> Result<CacheResponse<TxCacheTransactionsResponse>, Error> {
) -> Result<CacheResponse<TxCacheTransactionsResponse>> {
self.get_inner(TRANSACTIONS, query).await
}

Expand All @@ -170,7 +170,7 @@ impl TxCache {
pub async fn get_orders(
&self,
query: Option<OrderKey>,
) -> Result<CacheResponse<TxCacheOrdersResponse>, Error> {
) -> Result<CacheResponse<TxCacheOrdersResponse>> {
self.get_inner(ORDERS, query).await
}
}
35 changes: 35 additions & 0 deletions crates/tx-cache/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Result type for [`TxCache`] operations.
///
/// [`TxCache`]: crate::client::TxCache
pub type Result<T> = std::result::Result<T, TxCacheError>;

/// 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<reqwest::Error> 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),
}
}
}
7 changes: 6 additions & 1 deletion crates/tx-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;