diff --git a/explorer/src/service/api.rs b/explorer/src/service/api.rs index e304cd9..51fbe46 100644 --- a/explorer/src/service/api.rs +++ b/explorer/src/service/api.rs @@ -842,8 +842,11 @@ impl Api { method = "get", tag = "ApiTags::BlockChain" )] - async fn v2_statistics(&self) -> poem::Result { - v2_statistics(self).await.map_err(handle_fetch_one_err) + async fn v2_statistics( + &self, + ty: Query>, + ) -> poem::Result { + v2_statistics(self, ty).await.map_err(handle_fetch_one_err) } /////////////////////////////////////////////////////////////////////////////////////////////// // Distribution diff --git a/explorer/src/service/v2/other.rs b/explorer/src/service/v2/other.rs index 100ef3f..51783b5 100644 --- a/explorer/src/service/v2/other.rs +++ b/explorer/src/service/v2/other.rs @@ -32,51 +32,101 @@ pub struct V2StatisticsData { pub daily_txs: i64, } -pub async fn v2_statistics(api: &Api) -> Result { +pub async fn v2_statistics(api: &Api, ty: Query>) -> Result { let mut conn = api.storage.lock().await.acquire().await?; - // total txs - let sql_txs_count = "select count(*) as cnt from transaction".to_string(); - let row = sqlx::query(sql_txs_count.as_str()) - .fetch_one(&mut conn) - .await?; - let total_txs = row.try_get("cnt")?; - - // total addrs - let sql_native_addr_count = - "select count(distinct address) as cnt from native_addrs".to_string(); - let row = sqlx::query(sql_native_addr_count.as_str()) - .fetch_one(&mut conn) - .await?; - let native_active_addrs: i64 = row.try_get("cnt")?; - - let sql_evm_addr_count = "select count(distinct address) as cnt from evm_addrs".to_string(); - let row = sqlx::query(sql_evm_addr_count.as_str()) - .fetch_one(&mut conn) - .await?; - let evm_active_addrs: i64 = row.try_get("cnt")?; - let active_addrs = native_active_addrs + evm_active_addrs; + let mut stat = V2StatisticsData { + active_addrs: 0, + total_txs: 0, + daily_txs: 0, + }; - // daily txs let start_time = Local::now().date_naive().and_hms_opt(0, 0, 0).unwrap(); - let sql_daily_txs = format!( - "select count(*) as cnt from transaction where timestamp>={}", - start_time.timestamp() - ); - let row = sqlx::query(sql_daily_txs.as_str()) - .fetch_one(&mut conn) - .await?; - let daily_txs = row.try_get("cnt")?; + + if let Some(tx_type) = ty.0 { + let sql_txs_count = format!( + "select count(*) as cnt from transaction where ty={}", + tx_type + ); + let row_txs_count = sqlx::query(sql_txs_count.as_str()) + .fetch_one(&mut conn) + .await?; + let txs_count = row_txs_count.try_get("cnt")?; + + let sql_addrs_count: String; + let sql_daily_txs: String; + match tx_type { + 0 => { + sql_addrs_count = + "select count(distinct address) as cnt from native_addrs".to_string(); + sql_daily_txs = format!( + "select count(*) as cnt from transaction where ty=0 and timestamp>={}", + start_time.timestamp() + ); + } + _ => { + sql_addrs_count = + "select count(distinct address) as cnt from evm_addrs".to_string(); + sql_daily_txs = format!( + "select count(*) as cnt from transaction where ty=1 and timestamp>={}", + start_time.timestamp() + ); + } + } + + let row_addr_count = sqlx::query(sql_addrs_count.as_str()) + .fetch_one(&mut conn) + .await?; + let addr_count: i64 = row_addr_count.try_get("cnt")?; + + let row_daily = sqlx::query(sql_daily_txs.as_str()) + .fetch_one(&mut conn) + .await?; + let daily_txs = row_daily.try_get("cnt")?; + + stat.active_addrs = addr_count; + stat.total_txs = txs_count; + stat.daily_txs = daily_txs + } else { + let sql_txs_count = "select count(*) as cnt from transaction".to_string(); + let row_txs_count = sqlx::query(sql_txs_count.as_str()) + .fetch_one(&mut conn) + .await?; + let txs_count = row_txs_count.try_get("cnt")?; + + let sql_evm_addrs_count = + "select count(distinct address) as cnt from evm_addrs".to_string(); + let row_evm_addr = sqlx::query(sql_evm_addrs_count.as_str()) + .fetch_one(&mut conn) + .await?; + let evm_addrs: i64 = row_evm_addr.try_get("cnt")?; + + let sql_native_addrs_count = + "select count(distinct address) as cnt from native_addrs".to_string(); + let row_native_addr = sqlx::query(sql_native_addrs_count.as_str()) + .fetch_one(&mut conn) + .await?; + let native_addrs: i64 = row_native_addr.try_get("cnt")?; + + let sql_daily_txs = format!( + "select count(*) as cnt from transaction where timestamp>={}", + start_time.timestamp() + ); + let row_daily = sqlx::query(sql_daily_txs.as_str()) + .fetch_one(&mut conn) + .await?; + let daily_txs = row_daily.try_get("cnt")?; + + stat.active_addrs = native_addrs + evm_addrs; + stat.total_txs = txs_count; + stat.daily_txs = daily_txs + } Ok(V2ChainStatisticsResponse::Ok(Json( V2ChainStatisticsResult { code: 200, message: "".to_string(), - data: Some(V2StatisticsData { - active_addrs, - total_txs, - daily_txs, - }), + data: Some(stat), }, ))) } diff --git a/scanner/src/rpc.rs b/scanner/src/rpc.rs index 26d636e..06b79cc 100644 --- a/scanner/src/rpc.rs +++ b/scanner/src/rpc.rs @@ -215,7 +215,11 @@ impl RPCCaller { let xhub_opt: XHubOpt = serde_json::from_value(value).unwrap(); for xo in &xhub_opt.function.xhub.nonconfidential_transfer.outputs { let to = pubkey_to_fra_address(&xo.target).unwrap(); - addrs.push(to); + native_addrs.push(Address { + tx: tx_hash.clone(), + address: to, + timestamp: timestamp.timestamp(), + }); } sender = "".to_string(); ty_sub = FindoraTxType::EVMToNative as i32;