Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
feat: get transactions per addresses (#161)
Browse files Browse the repository at this point in the history
This PR add an endpoint to collect all of the transactions related to an
address.
  • Loading branch information
rllola authored Mar 12, 2024
1 parent aecf92d commit 0721665
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Row>, 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<Vec<Row>, Error> {
Expand Down
1 change: 1 addition & 0 deletions src/server/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod account;
pub mod address;
pub mod block;
pub mod transaction;
pub mod validator;
31 changes: 31 additions & 0 deletions src/server/endpoints/address.rs
Original file line number Diff line number Diff line change
@@ -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<ServerState>,
Path(address): Path<String>,
) -> Result<Json<Option<Vec<TxInfo>>>, 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<TxInfo> = vec![];
for row in rows {
let tx = TxInfo::try_from(row)?;
response.push(tx);
}

Ok(Json(Some(response)))
}
2 changes: 2 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand Down
3 changes: 0 additions & 3 deletions src/server/tx.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -83,8 +82,6 @@ impl TryFrom<Row> for TxInfo {
type Error = Error;

fn try_from(row: Row) -> Result<Self, Self::Error> {
info!("TxInfo::try_from");

let hash: Vec<u8> = row.try_get("hash")?;
let block_id: Vec<u8> = row.try_get("block_id")?;
let tx_type: String = row.try_get("tx_type")?;
Expand Down

0 comments on commit 0721665

Please sign in to comment.