From bce009d5eb7435c6288800205458eed9d16873e2 Mon Sep 17 00:00:00 2001 From: blowcowbeer Date: Tue, 2 Apr 2024 16:26:29 +0800 Subject: [PATCH] Feat: v2 prism evm to native. --- explorer/src/service/api.rs | 18 +++++ explorer/src/service/v2/evm_to_native.rs | 99 ++++++++++++++++++++++++ explorer/src/service/v2/mod.rs | 1 + 3 files changed, 118 insertions(+) create mode 100644 explorer/src/service/v2/evm_to_native.rs diff --git a/explorer/src/service/api.rs b/explorer/src/service/api.rs index b15179a..8e5274c 100644 --- a/explorer/src/service/api.rs +++ b/explorer/src/service/api.rs @@ -22,6 +22,8 @@ use crate::service::v2::claim::{v2_get_claim, v2_get_claims, V2ClaimResponse, V2 use crate::service::v2::delegation::{ v2_get_delegation, v2_get_delegations, V2DelegationResponse, V2DelegationsResponse, }; + +use crate::service::v2::evm_to_native::V2EvmToNativeTxsResponse; use crate::service::v2::native_to_evm::V2NativeToEvmTxsResponse; use crate::service::v2::native_to_evm::{ v2_get_n2e_tx, v2_get_prism_records_send, V2NativeToEvmTxResponse, @@ -539,6 +541,22 @@ impl Api { // .await // .map_err(handle_fetch_one_err) // } + + #[oai( + path = "/v2/txs/prism/e2n", + method = "get", + tag = "ApiTags::Transaction" + )] + async fn get_prism_e2n_txs( + &self, + page: Query>, + page_size: Query>, + ) -> poem::Result { + service::v2::evm_to_native::v2_get_e2n_txs(self, page, page_size) + .await + .map_err(handle_fetch_one_err) + } + #[oai( path = "/v2/txs/prism/n2e", method = "get", diff --git a/explorer/src/service/v2/evm_to_native.rs b/explorer/src/service/v2/evm_to_native.rs new file mode 100644 index 0000000..b7e66b9 --- /dev/null +++ b/explorer/src/service/v2/evm_to_native.rs @@ -0,0 +1,99 @@ +use crate::service::api::Api; +use poem_openapi::param::Query; +use poem_openapi::payload::Json; +use poem_openapi::{ApiResponse, Object}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use sqlx::Row; + +#[derive(ApiResponse)] +pub enum V2EvmToNativeTxsResponse { + #[oai(status = 200)] + Ok(Json), + #[oai(status = 404)] + NotFound, + #[oai(status = 500)] + InternalError, +} + +#[derive(Serialize, Deserialize, Debug, Object)] +pub struct V2EvmToNativeTxsResult { + pub code: u16, + pub message: String, + pub data: V2EvmToNativeTxsData, +} + +#[derive(Serialize, Deserialize, Debug, Object)] +pub struct V2EvmToNativeTxsData { + pub total: i64, + pub page: i32, + pub page_size: i32, + pub txs: Vec, +} + +#[derive(Serialize, Deserialize, Debug, Object)] +pub struct V2EvmToNativeTx { + pub tx_hash: String, + pub block_hash: String, + pub from: String, + pub to: String, + pub asset: String, + pub amount: String, + pub decimal: i32, + pub height: i64, + pub timestamp: i64, + pub value: Value, +} + +pub async fn v2_get_e2n_txs( + api: &Api, + page: Query>, + page_size: Query>, +) -> anyhow::Result { + let mut conn = api.storage.lock().await.acquire().await?; + let page = page.0.unwrap_or(1); + let page_size = page_size.0.unwrap_or(10); + + let sql_total = "SELECT count(*) FROM e2n"; + let row = sqlx::query(sql_total).fetch_one(&mut conn).await?; + let total: i64 = row.try_get("count")?; + + let sql_query = format!("SELECT tx_hash,block_hash,sender,receiver,asset,amount,decimal,height,timestamp,value FROM e2n ORDER BY timestamp DESC LIMIT {} OFFSET {}", page_size, (page-1)*page_size); + let mut res: Vec = vec![]; + let rows = sqlx::query(sql_query.as_str()).fetch_all(&mut conn).await?; + for row in rows { + let tx_hash: String = row.try_get("tx_hash")?; + let block_hash: String = row.try_get("block_hash")?; + let from: String = row.try_get("sender")?; + let to: String = row.try_get("receiver")?; + let asset: String = row.try_get("asset")?; + let decimal: i32 = row.try_get("decimal")?; + let amount: String = row.try_get("amount")?; + let height: i64 = row.try_get("height")?; + let timestamp: i64 = row.try_get("timestamp")?; + let value: Value = row.try_get("value")?; + res.push(V2EvmToNativeTx { + tx_hash, + block_hash, + from, + to, + asset, + amount, + decimal, + height, + timestamp, + value, + }) + } + + Ok(V2EvmToNativeTxsResponse::Ok(Json(V2EvmToNativeTxsResult { + code: 200, + message: "".to_string(), + data: V2EvmToNativeTxsData { + total, + page, + page_size, + txs: res, + }, + }))) +} diff --git a/explorer/src/service/v2/mod.rs b/explorer/src/service/v2/mod.rs index 179eda0..9827990 100644 --- a/explorer/src/service/v2/mod.rs +++ b/explorer/src/service/v2/mod.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; pub mod asset; pub mod claim; pub mod delegation; +pub mod evm_to_native; pub mod native_to_evm; pub mod other; pub mod transaction;