diff --git a/src/database.rs b/src/database.rs index 8d96bfd..82db13c 100644 --- a/src/database.rs +++ b/src/database.rs @@ -960,6 +960,22 @@ impl Database { .map_err(Error::from) } + #[instrument(skip(self))] + /// Returns Transaction identified by hash + pub async fn get_txs_by_address(&self, address: &String) -> Result, Error> { + // query for transaction with hash + let str = format!( + "SELECT * FROM {}.{TX_TABLE_NAME} WHERE data->>'source' = $1 OR data->>'target' = $1;", + self.network + ); + + query(&str) + .bind(address) + .fetch_all(&*self.pool) + .await + .map_err(Error::from) + } + #[instrument(skip(self))] /// Returns all the tx hashes for a block pub async fn get_tx_hashes_block(&self, hash: &[u8]) -> Result, Error> { diff --git a/src/server/endpoints.rs b/src/server/endpoints.rs index 8d20f8c..0cbe8ad 100644 --- a/src/server/endpoints.rs +++ b/src/server/endpoints.rs @@ -1,4 +1,5 @@ pub mod account; +pub mod address; pub mod block; pub mod transaction; pub mod validator; diff --git a/src/server/endpoints/address.rs b/src/server/endpoints/address.rs new file mode 100644 index 0000000..963a5a2 --- /dev/null +++ b/src/server/endpoints/address.rs @@ -0,0 +1,31 @@ +use axum::{ + extract::{Path, State}, + Json, +}; +use tracing::info; + +use crate::{ + server::{ServerState, TxInfo}, + Error, +}; + +pub async fn get_txs_by_address( + State(state): State, + Path(address): Path, +) -> Result>>, Error> { + info!("calling /address/:{}", address); + + let rows = state.db.get_txs_by_address(&address).await?; + + if rows.is_empty() { + return Ok(Json(None)); + } + + let mut response: Vec = vec![]; + for row in rows { + let tx = TxInfo::try_from(row)?; + response.push(tx); + } + + Ok(Json(Some(response))) +} diff --git a/src/server/mod.rs b/src/server/mod.rs index e54a136..93cd1e7 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -26,6 +26,7 @@ pub(crate) use utils::{from_hex, serialize_hex}; use self::endpoints::{ account::get_account_updates, + address::get_txs_by_address, block::{get_block_by_hash, get_block_by_height, get_last_block}, transaction::{get_shielded_tx, get_tx_by_hash, get_vote_proposal}, validator::get_validator_uptime, @@ -45,6 +46,7 @@ fn server_routes(state: ServerState) -> Router<()> { .allow_methods([Method::GET, Method::POST]) .allow_origin(Any); Router::new() + .route("/address/:address", get(get_txs_by_address)) .route("/block/height/:block_height", get(get_block_by_height)) .route("/block/hash/:block_hash", get(get_block_by_hash)) .route("/block/last", get(get_last_block)) diff --git a/src/server/tx.rs b/src/server/tx.rs index b3b9d11..190b086 100644 --- a/src/server/tx.rs +++ b/src/server/tx.rs @@ -1,7 +1,6 @@ use crate::error::Error; use namada_sdk::ibc::primitives::proto::Any; use serde::{Deserialize, Serialize}; -use tracing::info; use super::utils::serialize_optional_hex; @@ -83,8 +82,6 @@ impl TryFrom for TxInfo { type Error = Error; fn try_from(row: Row) -> Result { - info!("TxInfo::try_from"); - let hash: Vec = row.try_get("hash")?; let block_id: Vec = row.try_get("block_id")?; let tx_type: String = row.try_get("tx_type")?;