From 4a7c0e968f80793e786f6cc86ab225f14962843a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=94=A6=E4=BD=91?= Date: Wed, 28 Feb 2024 18:43:17 +0800 Subject: [PATCH] v2 delegations. (#233) --- explorer/src/service/api.rs | 20 ++++++- explorer/src/service/v2/delegation.rs | 84 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/explorer/src/service/api.rs b/explorer/src/service/api.rs index 1fb74a6..62115a2 100644 --- a/explorer/src/service/api.rs +++ b/explorer/src/service/api.rs @@ -25,7 +25,9 @@ use crate::service::v1::validator::{ }; use crate::service::v2::asset::{v2_get_asset, V2AssetTxResponse}; use crate::service::v2::claim::{v2_get_claim_tx, V2ClaimTxResponse}; -use crate::service::v2::delegation::{v2_get_delegation_tx, V2DelegationTxResponse}; +use crate::service::v2::delegation::{ + v2_get_delegation_tx, v2_get_delegation_txs, V2DelegationTxResponse, V2DelegationTxsResponse, +}; use crate::service::v2::native_to_evm::{ v2_get_n2e_tx, v2_get_prism_records_send, V2NativeToEvmTxResponse, }; @@ -790,7 +792,21 @@ impl Api { .await .map_err(handle_fetch_one_err) } - + #[oai( + path = "/v2/tx/delegations", + method = "get", + tag = "ApiTags::Transaction" + )] + async fn v2_get_delegation_txs( + &self, + address: Query, + page: Query>, + page_size: Query>, + ) -> poem::Result { + v2_get_delegation_txs(self, address, page, page_size) + .await + .map_err(handle_fetch_one_err) + } #[oai( path = "/v2/tx/undelegation/:tx_hash", method = "get", diff --git a/explorer/src/service/v2/delegation.rs b/explorer/src/service/v2/delegation.rs index c6b2e6f..284bd66 100644 --- a/explorer/src/service/v2/delegation.rs +++ b/explorer/src/service/v2/delegation.rs @@ -1,5 +1,6 @@ use crate::service::api::Api; use anyhow::Result; +use poem_openapi::param::Query; use poem_openapi::{param::Path, payload::Json, ApiResponse, Object}; use reqwest::StatusCode; use serde::{Deserialize, Serialize}; @@ -76,3 +77,86 @@ pub async fn v2_get_delegation_tx( data: Some(res), }))) } + +#[derive(ApiResponse)] +pub enum V2DelegationTxsResponse { + #[oai(status = 200)] + Ok(Json), + #[oai(status = 404)] + NotFound, + #[oai(status = 500)] + InternalError, +} + +#[derive(Serialize, Deserialize, Debug, Object)] +pub struct V2DelegationTxsResult { + pub code: u16, + pub message: String, + pub data: Option, +} + +#[derive(Serialize, Deserialize, Debug, Object)] +pub struct V2DelegationTxsData { + pub page: i64, + pub page_size: i64, + pub total: i64, + pub items: Vec, +} + +pub async fn v2_get_delegation_txs( + api: &Api, + address: Query, + page: Query>, + page_size: Query>, +) -> Result { + let page = page.0.unwrap_or(1); + let page_size = page_size.0.unwrap_or(10); + let mut conn = api.storage.lock().await.acquire().await?; + let sql_count = format!( + "SELECT count(*) AS cnt FROM delegations WHERE sender='{}'", + address.0.to_lowercase() + ); + let row_cnt = sqlx::query(sql_count.as_str()).fetch_one(&mut conn).await?; + let total: i64 = row_cnt.try_get("cnt")?; + + let sql_query = format!( + "SELECT tx,block,sender,amount,validator,new_validator,timestamp,height,content FROM delegations WHERE sender='{}' ORDER BY timestamp DESC LIMIT {} OFFSET {}", + address.0.to_lowercase(), 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: String = row.try_get("tx")?; + let block: String = row.try_get("block")?; + let amount: i64 = row.try_get("amount")?; + let sender: String = row.try_get("sender")?; + let validator: String = row.try_get("validator")?; + let new_validator: String = row.try_get("new_validator")?; + let height: i64 = row.try_get("height")?; + let timestamp: i64 = row.try_get("timestamp")?; + let value: Value = row.try_get("content")?; + res.push(V2DelegationTx { + tx_hash: tx, + block_hash: block, + from: sender, + amount, + validator, + new_validator, + height, + timestamp, + value, + }); + } + + Ok(V2DelegationTxsResponse::Ok(Json(V2DelegationTxsResult { + code: 200, + message: "".to_string(), + data: Some(V2DelegationTxsData { + page, + page_size, + total, + items: res, + }), + }))) +}