diff --git a/explorer/src/main.rs b/explorer/src/main.rs index 80f4188..1adb334 100644 --- a/explorer/src/main.rs +++ b/explorer/src/main.rs @@ -1,5 +1,6 @@ mod service; use crate::service::api::Api; +use crate::service::v2::asset::get_assets; use crate::service::v2::block::{get_block_by_hash, get_block_by_num, get_blocks}; use crate::service::v2::transaction::{get_tx_by_hash, get_txs}; use anyhow::Result; @@ -51,6 +52,7 @@ async fn main() -> Result<()> { .route("/api/v2/blocks", get(get_blocks)) .route("/api/v2/hash/tx", get(get_tx_by_hash)) .route("/api/v2/txs", get(get_txs)) + .route("/api/v2/assets", get(get_assets)) .layer(cors) .with_state(app_state); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); diff --git a/explorer/src/service/v2/asset.rs b/explorer/src/service/v2/asset.rs index ad509cd..c13622f 100644 --- a/explorer/src/service/v2/asset.rs +++ b/explorer/src/service/v2/asset.rs @@ -1,39 +1,24 @@ -use crate::service::api::Api; -use anyhow::Result; -use poem_openapi::param::Query; -use poem_openapi::{payload::Json, ApiResponse, Object}; -use reqwest::StatusCode; +use crate::service::v2::error::internal_error; +use crate::service::v2::error::Result; +use crate::service::v2::QueryResult; +use crate::AppState; +use axum::extract::{Query, State}; +use axum::Json; use serde::{Deserialize, Serialize}; use serde_json::Value; use sqlx::Row; +use std::ops::Add; +use std::sync::Arc; -#[derive(ApiResponse)] -pub enum V2AssetTxResponse { - #[oai(status = 200)] - Ok(Json), - #[oai(status = 404)] - NotFound(Json), - #[oai(status = 500)] - InternalError(Json), +#[derive(Serialize, Deserialize)] +pub struct GetAssetsParams { + pub address: Option, + pub page: Option, + pub page_size: Option, } -#[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2AssetTxResult { - pub code: u16, - pub message: String, - pub data: Option, -} - -#[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2AssetData { - pub page: i32, - pub page_size: i32, - pub total: i64, - pub assets: Vec, -} - -#[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2AssetOp { +#[derive(Serialize, Deserialize, Debug)] +pub struct AssetResponse { pub asset: String, pub tx: String, pub block: String, @@ -43,102 +28,61 @@ pub struct V2AssetOp { pub ty: i32, pub value: Value, } -#[allow(dead_code)] -pub async fn v2_get_asset( - api: &Api, - address: Query, - page: Query>, - page_size: Query>, -) -> Result { - let mut conn = api.storage.lock().await.acquire().await?; - let mut assets: Vec = vec![]; - let page = page.0.unwrap_or(1); - let page_size = page_size.0.unwrap_or(10); - let sql_total = format!( - "SELECT count(*) as cnt from assets WHERE asset='{}'", - address.0 - ); - let row_count = sqlx::query(sql_total.as_str()) - .fetch_one(&mut *conn) - .await?; - let total: i64 = row_count.try_get("cnt")?; - let sql_query = format!( - "SELECT asset,tx,block,issuer,height,timestamp,ty,content from assets WHERE asset='{}' order by height desc limit {} offset {}", - address.0, - page_size, - (page - 1) * page_size - ); - let rows = sqlx::query(sql_query.as_str()) - .fetch_all(&mut *conn) - .await?; - for row in rows { - let asset: String = row.try_get("asset")?; - let tx: String = row.try_get("tx")?; - let block: String = row.try_get("block")?; - let issuer: String = row.try_get("issuer")?; - let height: i64 = row.try_get("height")?; - let timestamp: i64 = row.try_get("timestamp")?; - let ty: i32 = row.try_get("ty")?; - let value: Value = row.try_get("content")?; - assets.push(V2AssetOp { - asset, - tx, - block, - issuer, - height, - timestamp, - ty, - value, - }); - } +pub async fn get_assets( + 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); - Ok(V2AssetTxResponse::Ok(Json(V2AssetTxResult { - code: StatusCode::OK.as_u16(), - message: "".to_string(), - data: Some(V2AssetData { - page, + let mut sql_total = "SELECT count(*) FROM assets ".to_string(); + let mut sql_query = + "SELECT asset,tx,block,issuer,height,timestamp,ty,content FROM assets ".to_string(); + let mut query_params: Vec = vec![]; + if let Some(addr) = params.address { + query_params.push(format!("asset='{}' ", addr)); + } + 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, - total, - assets, - }), - }))) -} -#[allow(dead_code)] -pub async fn v2_get_asset_list( - api: &Api, - page: Query>, - page_size: Query>, -) -> Result { - let mut conn = api.storage.lock().await.acquire().await?; - let mut assets: Vec = vec![]; - let page = page.0.unwrap_or(1); - let page_size = page_size.0.unwrap_or(10); + (page - 1) * page_size + ) + .as_str(), + ); - let sql_total = "SELECT count(*) AS cnt FROM assets".to_string(); - let row_count = sqlx::query(sql_total.as_str()) + let row = sqlx::query(&sql_total) .fetch_one(&mut *conn) - .await?; - let total: i64 = row_count.try_get("cnt")?; + .await + .map_err(internal_error)?; + let total: i64 = row.try_get("count").map_err(internal_error)?; - let sql_query = format!( - "SELECT asset,tx,block,issuer,height,timestamp,ty,content FROM assets ORDER BY height desc limit {} offset {}", - page_size, - (page - 1) * page_size - ); let rows = sqlx::query(sql_query.as_str()) .fetch_all(&mut *conn) - .await?; + .await + .map_err(internal_error)?; + + let mut assets: Vec = vec![]; for row in rows { - let asset: String = row.try_get("asset")?; - let tx: String = row.try_get("tx")?; - let block: String = row.try_get("block")?; - let issuer: String = row.try_get("issuer")?; - let height: i64 = row.try_get("height")?; - let timestamp: i64 = row.try_get("timestamp")?; - let ty: i32 = row.try_get("ty")?; - let value: Value = row.try_get("content")?; - assets.push(V2AssetOp { + let asset: String = row.try_get("asset").map_err(internal_error)?; + let tx: String = row.try_get("tx").map_err(internal_error)?; + let block: String = row.try_get("block").map_err(internal_error)?; + let issuer: String = row.try_get("issuer").map_err(internal_error)?; + let height: i64 = row.try_get("height").map_err(internal_error)?; + let timestamp: i64 = row.try_get("timestamp").map_err(internal_error)?; + let ty: i32 = row.try_get("ty").map_err(internal_error)?; + let value: Value = row.try_get("content").map_err(internal_error)?; + assets.push(AssetResponse { asset, tx, block, @@ -150,14 +94,10 @@ pub async fn v2_get_asset_list( }); } - Ok(V2AssetTxResponse::Ok(Json(V2AssetTxResult { - code: 200, - message: "".to_string(), - data: Some(V2AssetData { - page, - page_size, - total, - assets, - }), - }))) + Ok(Json(QueryResult { + total, + page, + page_size, + data: assets, + })) } diff --git a/explorer/src/service/v2/block.rs b/explorer/src/service/v2/block.rs index 3377cdd..6051865 100644 --- a/explorer/src/service/v2/block.rs +++ b/explorer/src/service/v2/block.rs @@ -1,15 +1,27 @@ use crate::service::v2::error::internal_error; use crate::service::v2::error::Result; -use crate::service::v2::{BlockResponse, QueryResult}; +use crate::service::v2::QueryResult; use crate::AppState; use axum::extract::{Query, State}; use axum::Json; -use module::rpc::block::BlockRPC; +use module::rpc::block::{BlockHeader, BlockId, BlockRPC}; use serde::{Deserialize, Serialize}; use serde_json::Value; use sqlx::Row; use std::sync::Arc; +#[derive(Serialize, Deserialize, Debug)] +pub struct BlockResponse { + pub block_hash: String, + pub block_num: i64, + pub app_hash: String, + pub proposer: String, + pub num_txs: i64, + pub block_size: i64, + pub block_id: BlockId, + pub block_header: BlockHeader, +} + #[derive(Serialize, Deserialize, Debug)] pub struct GetBlockByHeightParams { pub num: i64, diff --git a/explorer/src/service/v2/error.rs b/explorer/src/service/v2/error.rs index 71ff9c8..ee213c9 100644 --- a/explorer/src/service/v2/error.rs +++ b/explorer/src/service/v2/error.rs @@ -7,7 +7,7 @@ where E: std::error::Error, { let err_msg = err.to_string(); - if err_msg.contains("now rows") { + if err_msg.contains("no rows") { return (StatusCode::NOT_FOUND, "not found".to_string()); } diff --git a/explorer/src/service/v2/mod.rs b/explorer/src/service/v2/mod.rs index d020a93..ded7313 100644 --- a/explorer/src/service/v2/mod.rs +++ b/explorer/src/service/v2/mod.rs @@ -1,4 +1,3 @@ -use module::rpc::block::{BlockHeader, BlockId}; use serde::{Deserialize, Serialize}; pub mod asset; @@ -39,15 +38,3 @@ pub struct QueryResult { pub page_size: i32, pub data: T, } - -#[derive(Serialize, Deserialize, Debug)] -pub struct BlockResponse { - pub block_hash: String, - pub block_num: i64, - pub app_hash: String, - pub proposer: String, - pub num_txs: i64, - pub block_size: i64, - pub block_id: BlockId, - pub block_header: BlockHeader, -} diff --git a/explorer/src/service/v2/other.rs b/explorer/src/service/v2/other.rs index b666408..eccf205 100644 --- a/explorer/src/service/v2/other.rs +++ b/explorer/src/service/v2/other.rs @@ -44,31 +44,26 @@ pub async fn v2_statistics(api: &Api, ty: Query>) -> Result { - sql_addrs_count = - "select count(distinct address) as cnt from native_addrs".to_string(); + sql_addrs_count = "SELECT count(distinct address) FROM native_addrs".to_string(); sql_daily_txs = format!( - "select count(*) as cnt from transaction where ty=0 and timestamp>={}", + "SELECT count(*) FROM transaction WHERE ty=0 AND timestamp>={}", start_time.and_utc().timestamp() ); } _ => { - sql_addrs_count = - "select count(distinct address) as cnt from evm_addrs".to_string(); + sql_addrs_count = "SELECT count(distinct address) FROM evm_addrs".to_string(); sql_daily_txs = format!( - "select count(*) as cnt from transaction where ty=1 and timestamp>={}", + "SELECT count(*) FROM transaction WHERE ty=1 AND timestamp>={}", start_time.and_utc().timestamp() ); } @@ -77,45 +72,43 @@ pub async fn v2_statistics(api: &Api, ty: Query>) -> Result={}", + "SELECT count(*) FROM transaction WHERE timestamp>={}", start_time.and_utc().timestamp() ); let row_daily = sqlx::query(sql_daily_txs.as_str()) .fetch_one(&mut *conn) .await?; - let daily_txs = row_daily.try_get("cnt")?; + let daily_txs = row_daily.try_get("count")?; stat.active_addrs = native_addrs + evm_addrs; stat.total_txs = txs_count;