From e69a9f72b8ca24d4c1a442714c5e0bac6f0c1373 Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Wed, 13 Dec 2023 14:03:21 +0100 Subject: [PATCH] Allow filtering txs by status --- src/db.rs | 6 ++++++ src/server/routes/transaction.rs | 22 ++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/db.rs b/src/db.rs index 9676582..84beb0e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -734,6 +734,8 @@ impl Database { pub async fn read_txs( &self, relayer_id: &str, + tx_status: Option, + unsent: bool, ) -> eyre::Result> { Ok(sqlx::query_as( r#" @@ -743,9 +745,13 @@ impl Database { LEFT JOIN sent_transactions s ON t.id = s.tx_id LEFT JOIN tx_hashes h ON s.valid_tx_hash = h.tx_hash WHERE t.relayer_id = $1 + AND ($2 IS NULL OR s.status = $2) + AND ($3 = false OR s.tx_id IS NULL) "#, ) .bind(relayer_id) + .bind(tx_status) + .bind(unsent) .fetch_all(&self.pool) .await?) } diff --git a/src/server/routes/transaction.rs b/src/server/routes/transaction.rs index a1dbca7..b84eaec 100644 --- a/src/server/routes/transaction.rs +++ b/src/server/routes/transaction.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use axum::extract::{Json, Path, State}; +use axum::extract::{Json, Path, Query, State}; use ethers::types::{Address, Bytes, H256, U256}; use eyre::Result; use serde::{Deserialize, Serialize}; @@ -33,6 +33,13 @@ pub struct SendTxResponse { pub tx_id: String, } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct GetTxQuery { + #[serde(default)] + pub status: Option, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct GetTxResponse { @@ -104,12 +111,23 @@ pub async fn send_tx( pub async fn get_txs( State(app): State>, Path(api_token): Path, + Query(query): Query, ) -> Result>, ApiError> { if !app.is_authorized(&api_token).await? { return Err(ApiError::Unauthorized); } - let txs = app.db.read_txs(&api_token.relayer_id).await?; + let txs = match query.status { + Some(GetTxResponseStatus::TxStatus(status)) => { + app.db + .read_txs(&api_token.relayer_id, Some(status), false) + .await? + } + Some(GetTxResponseStatus::Unsent(_)) => { + app.db.read_txs(&api_token.relayer_id, None, true).await? + } + _ => app.db.read_txs(&api_token.relayer_id, None, false).await?, + }; let txs = txs.into_iter()