From 7c16c9b08a272e0181fdc3f5cc02167ec2cf7a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E8=80=81=E5=B8=88?= Date: Sun, 7 Apr 2024 14:46:30 +0800 Subject: [PATCH] Feat: get txs. (#249) --- explorer/src/main.rs | 3 +- explorer/src/service/v2/error.rs | 7 +- explorer/src/service/v2/transaction.rs | 100 ++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/explorer/src/main.rs b/explorer/src/main.rs index 12d635d..80f4188 100644 --- a/explorer/src/main.rs +++ b/explorer/src/main.rs @@ -1,7 +1,7 @@ mod service; use crate::service::api::Api; use crate::service::v2::block::{get_block_by_hash, get_block_by_num, get_blocks}; -use crate::service::v2::transaction::get_tx_by_hash; +use crate::service::v2::transaction::{get_tx_by_hash, get_txs}; use anyhow::Result; use axum::http::Method; use axum::routing::get; @@ -50,6 +50,7 @@ async fn main() -> Result<()> { .route("/api/v2/hash/block", get(get_block_by_hash)) .route("/api/v2/blocks", get(get_blocks)) .route("/api/v2/hash/tx", get(get_tx_by_hash)) + .route("/api/v2/txs", get(get_txs)) .layer(cors) .with_state(app_state); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); diff --git a/explorer/src/service/v2/error.rs b/explorer/src/service/v2/error.rs index fb888eb..71ff9c8 100644 --- a/explorer/src/service/v2/error.rs +++ b/explorer/src/service/v2/error.rs @@ -6,5 +6,10 @@ pub fn internal_error(err: E) -> (StatusCode, String) where E: std::error::Error, { - (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()) + let err_msg = err.to_string(); + if err_msg.contains("now rows") { + return (StatusCode::NOT_FOUND, "not found".to_string()); + } + + (StatusCode::INTERNAL_SERVER_ERROR, err_msg) } diff --git a/explorer/src/service/v2/transaction.rs b/explorer/src/service/v2/transaction.rs index 6f0bba8..da58a95 100644 --- a/explorer/src/service/v2/transaction.rs +++ b/explorer/src/service/v2/transaction.rs @@ -1,5 +1,5 @@ -use crate::service::v2::error::internal_error; -use crate::service::v2::error::Result; +use crate::service::v2::error::{internal_error, Result}; +use crate::service::v2::QueryResult; use crate::AppState; use axum::extract::{Query, State}; use axum::Json; @@ -7,8 +7,102 @@ use module::schema::TransactionResponse; use serde::{Deserialize, Serialize}; use serde_json::Value; use sqlx::Row; +use std::ops::Add; use std::sync::Arc; +#[derive(Serialize, Deserialize)] +pub struct GetTxsParams { + pub from: Option, + pub to: Option, + pub ty: Option, + pub page: Option, + pub page_size: Option, +} + +pub async fn get_txs( + State(state): State>, + Query(params): Query, +) -> Result>>> { + let mut conn = state.pool.acquire().await.map_err(internal_error)?; + let page = params.page.unwrap_or(1); + let page_size = params.page_size.unwrap_or(10); + + let mut sql_query = String::from("SELECT tx_hash,block_hash,ty,timestamp,height,code,log,origin,result,value FROM transaction "); + let mut sql_total = String::from("SELECT count(*) FROM transaction "); + let mut query_params: Vec = vec![]; + if let Some(from) = params.from { + query_params.push(format!("sender='{}' ", from)) + } + if let Some(to) = params.to { + query_params.push(format!("(receiver @? '$.addrs[*] ? (@==\"{}\")') ", to)); + } + if let Some(ty) = params.ty { + query_params.push(format!("ty={} ", ty)); + } + if !query_params.is_empty() { + sql_query = sql_query + .add("WHERE ") + .add(query_params.join("AND ").as_str()); + sql_total = sql_total + .add("WHERE ") + .add(query_params.join("AND ").as_str()); + } + + sql_query = sql_query.add( + format!( + "ORDER BY timestamp DESC LIMIT {} OFFSET {} ", + page_size, + (page - 1) * page_size + ) + .as_str(), + ); + + let row = sqlx::query(&sql_total) + .fetch_one(&mut *conn) + .await + .map_err(internal_error)?; + let total: i64 = row.try_get("count").map_err(internal_error)?; + + let rows = sqlx::query(&sql_query) + .fetch_all(&mut *conn) + .await + .map_err(internal_error)?; + let mut txs: Vec = vec![]; + for row in rows { + let tx_hash: String = row.try_get("tx_hash").map_err(internal_error)?; + let block_hash: String = row.try_get("block_hash").map_err(internal_error)?; + let ty: i32 = row.try_get("ty").map_err(internal_error)?; + let timestamp: i64 = row.try_get("timestamp").map_err(internal_error)?; + let height: i64 = row.try_get("height").map_err(internal_error)?; + let code: i64 = row.try_get("code").map_err(internal_error)?; + let log: String = row.try_get("log").map_err(internal_error)?; + let origin: String = row.try_get("origin").map_err(internal_error)?; + let result: Value = row.try_get("result").map_err(internal_error)?; + let value: Value = row.try_get("value").map_err(internal_error)?; + + txs.push(TransactionResponse { + tx_hash, + evm_tx_hash: "".to_string(), + block_hash, + height, + timestamp, + ty, + code, + log, + origin, + result, + value, + }); + } + + Ok(Json(QueryResult { + total, + page, + page_size, + data: txs, + })) +} + #[derive(Serialize, Deserialize)] pub struct GetTxByHashParams { pub hash: String, @@ -31,7 +125,7 @@ pub async fn get_tx_by_hash( let tx_hash: String = row.try_get("tx_hash").map_err(internal_error)?; let block_hash: String = row.try_get("block_hash").map_err(internal_error)?; - let ty: i32 = row.try_get("ty_sub").map_err(internal_error)?; + let ty: i32 = row.try_get("ty").map_err(internal_error)?; let timestamp: i64 = row.try_get("timestamp").map_err(internal_error)?; let height: i64 = row.try_get("height").map_err(internal_error)?; let code: i64 = row.try_get("code").map_err(internal_error)?;