Skip to content

Commit 8a7e865

Browse files
authored
feat(tx-cache): Pagination types and client impl (#131)
1 parent 43474d1 commit 8a7e865

File tree

3 files changed

+357
-26
lines changed

3 files changed

+357
-26
lines changed

crates/tx-cache/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ eyre.workspace = true
2121
reqwest.workspace = true
2222
serde = { workspace = true, features = ["derive"] }
2323
tracing.workspace = true
24-
uuid = { workspace = true, features = ["serde"] }
24+
uuid = { workspace = true, features = ["serde"] }
25+
26+
[dev-dependencies]
27+
serde_urlencoded = "0.7.1"
28+
uuid = { workspace = true, features = ["serde", "v4"] }
29+
serde_json.workspace = true

crates/tx-cache/src/client.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::types::{
2-
TxCacheOrdersResponse, TxCacheSendBundleResponse, TxCacheSendTransactionResponse,
3-
TxCacheTransactionsResponse,
2+
CacheObject, CacheResponse, OrderKey, TxCacheOrdersResponse, TxCacheSendBundleResponse,
3+
TxCacheSendTransactionResponse, TxCacheTransactionsResponse, TxKey,
44
};
55
use alloy::consensus::TxEnvelope;
66
use eyre::Error;
@@ -93,22 +93,21 @@ impl TxCache {
9393
self.client.post(url).json(&obj).send().await?.error_for_status().map_err(Into::into)
9494
}
9595

96-
async fn get_inner<T>(&self, join: &'static str) -> Result<T, Error>
96+
async fn get_inner<T>(&self, join: &'static str, query: Option<T::Key>) -> Result<T, Error>
9797
where
98-
T: DeserializeOwned,
98+
T: DeserializeOwned + CacheObject,
9999
{
100-
// Append the path to the URL.
101100
let url = self
102101
.url
103102
.join(join)
104103
.inspect_err(|e| warn!(%e, "Failed to join URL. Not querying transaction cache."))?;
105104

106-
// Get the result.
107105
self.client
108106
.get(url)
107+
.query(&query)
109108
.send()
110109
.await
111-
.inspect_err(|e| warn!(%e, "Failed to get object from transaction cache"))?
110+
.inspect_err(|e| warn!(%e, "Failed to get object from transaction cache."))?
112111
.json::<T>()
113112
.await
114113
.map_err(Into::into)
@@ -140,17 +139,19 @@ impl TxCache {
140139

141140
/// Get transactions from the URL.
142141
#[instrument(skip_all)]
143-
pub async fn get_transactions(&self) -> Result<Vec<TxEnvelope>, Error> {
144-
let response: TxCacheTransactionsResponse =
145-
self.get_inner::<TxCacheTransactionsResponse>(TRANSACTIONS).await?;
146-
Ok(response.transactions)
142+
pub async fn get_transactions(
143+
&self,
144+
query: Option<TxKey>,
145+
) -> Result<CacheResponse<TxCacheTransactionsResponse>, Error> {
146+
self.get_inner(TRANSACTIONS, query).await
147147
}
148148

149149
/// Get signed orders from the URL.
150150
#[instrument(skip_all)]
151-
pub async fn get_orders(&self) -> Result<Vec<SignedOrder>, Error> {
152-
let response: TxCacheOrdersResponse =
153-
self.get_inner::<TxCacheOrdersResponse>(ORDERS).await?;
154-
Ok(response.orders)
151+
pub async fn get_orders(
152+
&self,
153+
query: Option<OrderKey>,
154+
) -> Result<CacheResponse<TxCacheOrdersResponse>, Error> {
155+
self.get_inner(ORDERS, query).await
155156
}
156157
}

0 commit comments

Comments
 (0)