Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering txs by status #7

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ impl Database {
pub async fn read_txs(
&self,
relayer_id: &str,
tx_status: Option<TxStatus>,
unsent: bool,
Dzejkop marked this conversation as resolved.
Show resolved Hide resolved
) -> eyre::Result<Vec<ReadTxData>> {
Ok(sqlx::query_as(
r#"
Expand All @@ -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)
Dzejkop marked this conversation as resolved.
Show resolved Hide resolved
"#,
)
.bind(relayer_id)
.bind(tx_status)
.bind(unsent)
.fetch_all(&self.pool)
.await?)
}
Expand Down
22 changes: 20 additions & 2 deletions src/server/routes/transaction.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<GetTxResponseStatus>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetTxResponse {
Expand Down Expand Up @@ -104,12 +111,23 @@ pub async fn send_tx(
pub async fn get_txs(
State(app): State<Arc<App>>,
Path(api_token): Path<ApiKey>,
Query(query): Query<GetTxQuery>,
) -> Result<Json<Vec<GetTxResponse>>, 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?,
Dzejkop marked this conversation as resolved.
Show resolved Hide resolved
};

let txs =
txs.into_iter()
Expand Down