From 6dbcc0d24f58b4f3bb4d20f66223925090988061 Mon Sep 17 00:00:00 2001 From: ssuchichan Date: Thu, 29 Feb 2024 11:32:01 +0800 Subject: [PATCH] v2 claims. --- explorer/src/service/api.rs | 356 ++++++++++++------------ explorer/src/service/v1/chain.rs | 3 +- explorer/src/service/v1/staking.rs | 6 +- explorer/src/service/v1/transaction.rs | 7 +- explorer/src/service/v2/claim.rs | 96 ++++++- explorer/src/service/v2/delegation.rs | 37 ++- explorer/src/service/v2/undelegation.rs | 60 ++-- 7 files changed, 326 insertions(+), 239 deletions(-) diff --git a/explorer/src/service/api.rs b/explorer/src/service/api.rs index 62115a2..6da58a5 100644 --- a/explorer/src/service/api.rs +++ b/explorer/src/service/api.rs @@ -6,17 +6,15 @@ use crate::service::v1::asset::{ }; use crate::service::v1::block::{BlocksResponse, FullBlockResponse, SimpleBlockResponse}; use crate::service::v1::chain::{ - AddressCountResponse, ChainStatisticsResponse, DelegateAddressNumResponse, DistributeResponse, - PrismSyncResponse, + AddressCountResponse, ChainStatisticsResponse, DistributeResponse, }; use crate::service::v1::price::{MarketChartResponse, SimplePriceResponse}; use crate::service::v1::staking::{ - ClaimResponse, DelegationAmountResponse, DelegationInfoResponse, DelegationResponse, - SimpleDelegationResponse, UnDelegationResponse, UndelegationAmountResponse, + ClaimResponse, DelegationResponse, SimpleDelegationResponse, UnDelegationResponse, UndelegationResponse, }; use crate::service::v1::transaction::{ - ClaimAmountResponse, PmtxsResponse, TxResponse, TxsResponse, V2PrismRecordResponse, + PmtxsResponse, TxResponse, TxsResponse, V2PrismRecordResponse, }; use crate::service::v1::validator::{ CirculatingSupplyResponse, DelegatorListResponse, ValidatorDelegationResponse, @@ -24,9 +22,9 @@ use crate::service::v1::validator::{ ValidatorSignedCountResponse, }; use crate::service::v2::asset::{v2_get_asset, V2AssetTxResponse}; -use crate::service::v2::claim::{v2_get_claim_tx, V2ClaimTxResponse}; +use crate::service::v2::claim::{v2_get_claim, v2_get_claims, V2ClaimResponse, V2ClaimsResponse}; use crate::service::v2::delegation::{ - v2_get_delegation_tx, v2_get_delegation_txs, V2DelegationTxResponse, V2DelegationTxsResponse, + v2_get_delegation, v2_get_delegations, V2DelegationResponse, V2DelegationsResponse, }; use crate::service::v2::native_to_evm::{ v2_get_n2e_tx, v2_get_prism_records_send, V2NativeToEvmTxResponse, @@ -36,8 +34,7 @@ use crate::service::v2::other::{ }; use crate::service::v2::transaction::v2_get_txs; use crate::service::v2::undelegation::{ - v2_get_undelegation_tx, v2_get_undelegation_txs, V2UndelegationTxResponse, - V2UndelegationTxsResponse, + v2_get_undelegation, v2_get_undelegations, V2UndelegationResponse, V2UndelegationsResponse, }; use crate::service::ApiTags; use poem_openapi::param::{Path, Query}; @@ -82,35 +79,35 @@ impl Api { .map_err(handle_fetch_one_err) } - #[oai(path = "/txs/to", method = "get", tag = "ApiTags::Transaction")] - async fn get_txs_send_to( - &self, - /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v - address: Query, - /// page index, the default is 1. - page: Query>, - /// page size, the default is 10. - page_size: Query>, - ) -> poem::Result { - service::v1::transaction::get_txs_send_to(self, address, page, page_size) - .await - .map_err(handle_fetch_one_err) - } - - #[oai(path = "/txs/from", method = "get", tag = "ApiTags::Transaction")] - async fn get_txs_receive_from( - &self, - /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v - address: Query, - /// page index, the default is 1. - page: Query>, - /// page size, the default is 10. - page_size: Query>, - ) -> poem::Result { - service::v1::transaction::get_txs_receive_from(self, address, page, page_size) - .await - .map_err(handle_fetch_one_err) - } + // #[oai(path = "/txs/to", method = "get", tag = "ApiTags::Transaction")] + // async fn get_txs_send_to( + // &self, + // /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v + // address: Query, + // /// page index, the default is 1. + // page: Query>, + // /// page size, the default is 10. + // page_size: Query>, + // ) -> poem::Result { + // service::v1::transaction::get_txs_send_to(self, address, page, page_size) + // .await + // .map_err(handle_fetch_one_err) + // } + + // #[oai(path = "/txs/from", method = "get", tag = "ApiTags::Transaction")] + // async fn get_txs_receive_from( + // &self, + // /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v + // address: Query, + // /// page index, the default is 1. + // page: Query>, + // /// page size, the default is 10. + // page_size: Query>, + // ) -> poem::Result { + // service::v1::transaction::get_txs_receive_from(self, address, page, page_size) + // .await + // .map_err(handle_fetch_one_err) + // } #[allow(clippy::too_many_arguments)] #[oai(path = "/txs", method = "get", tag = "ApiTags::Transaction")] @@ -144,39 +141,39 @@ impl Api { .map_err(handle_fetch_one_err) } - #[allow(clippy::too_many_arguments)] - #[oai(path = "/txs/raw", method = "get", tag = "ApiTags::Transaction")] - async fn get_txs_no_wrap( - &self, - /// block hash, e.g. 4B7C22FA8FC6913E091DC324830181BBA1F01EBFF53049F958EA5AA65327BFE0 - block_id: Query>, - /// block height. - height: Query>, - /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v - address: Query>, - /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v - from: Query>, - /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v - to: Query>, - /// transaction type. - /// 0 - Findora native tx. - /// 1 - EVM tx. - ty: Query>, - /// starting timestamp. - start_time: Query>, - /// ending timestamp. - end_time: Query>, - /// page index, the default is 1. - page: Query>, - /// page size, the default is 10. - page_size: Query>, - ) -> poem::Result { - service::v1::transaction::get_txs_raw( - self, block_id, height, address, from, to, ty, start_time, end_time, page, page_size, - ) - .await - .map_err(handle_fetch_one_err) - } + // #[allow(clippy::too_many_arguments)] + // #[oai(path = "/txs/raw", method = "get", tag = "ApiTags::Transaction")] + // async fn get_txs_no_wrap( + // &self, + // /// block hash, e.g. 4B7C22FA8FC6913E091DC324830181BBA1F01EBFF53049F958EA5AA65327BFE0 + // block_id: Query>, + // /// block height. + // height: Query>, + // /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v + // address: Query>, + // /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v + // from: Query>, + // /// bech32 address, e.g. fra1p4vy5n9mlkdys7xczegj398xtyvw2nawz00nnfh4yr7fpjh297cqsxfv7v + // to: Query>, + // /// transaction type. + // /// 0 - Findora native tx. + // /// 1 - EVM tx. + // ty: Query>, + // /// starting timestamp. + // start_time: Query>, + // /// ending timestamp. + // end_time: Query>, + // /// page index, the default is 1. + // page: Query>, + // /// page size, the default is 10. + // page_size: Query>, + // ) -> poem::Result { + // service::v1::transaction::get_txs_raw( + // self, block_id, height, address, from, to, ty, start_time, end_time, page, page_size, + // ) + // .await + // .map_err(handle_fetch_one_err) + // } #[allow(clippy::too_many_arguments)] #[oai( @@ -652,27 +649,27 @@ impl Api { .map_err(handle_fetch_one_err) } - #[oai( - path = "/chain/claim/:address", - method = "get", - tag = "ApiTags::BlockChain" - )] - async fn get_claim_amount(&self, address: Path) -> poem::Result { - service::v1::transaction::get_claims_amount(self, address) - .await - .map_err(handle_fetch_one_err) - } + // #[oai( + // path = "/chain/claim/:address", + // method = "get", + // tag = "ApiTags::BlockChain" + // )] + // async fn get_claim_amount(&self, address: Path) -> poem::Result { + // service::v1::transaction::get_claims_amount(self, address) + // .await + // .map_err(handle_fetch_one_err) + // } - #[oai( - path = "/staking/delegation_info/:address", - method = "get", - tag = "ApiTags::Staking" - )] - async fn get_delegation(&self, address: Path) -> poem::Result { - service::v1::staking::delegation(self, address) - .await - .map_err(handle_fetch_one_err) - } + // #[oai( + // path = "/staking/delegation_info/:address", + // method = "get", + // tag = "ApiTags::Staking" + // )] + // async fn get_delegation(&self, address: Path) -> poem::Result { + // service::v1::staking::delegation(self, address) + // .await + // .map_err(handle_fetch_one_err) + // } #[oai( path = "/staking/undelegation", @@ -716,132 +713,143 @@ impl Api { .map_err(handle_fetch_one_err) } + // #[oai( + // path = "/staking/delegation/amount", + // method = "get", + // tag = "ApiTags::Staking" + // )] + // async fn get_delegation_amount( + // &self, + // /// base64 pubkey, e.g. OmZMrZBVsPjQwvHsROCI3mRw2pdVnYER8Xa5lzQ3Ek0= + // pubkey: Query>, + // /// starting timestamp. + // start: Query>, + // /// ending timestamp. + // end: Query>, + // ) -> poem::Result { + // service::v1::staking::get_delegation_amount(self, pubkey, start, end) + // .await + // .map_err(handle_fetch_one_err) + // } + + // #[oai( + // path = "/staking/undelegation/amount", + // method = "get", + // tag = "ApiTags::Staking" + // )] + // async fn get_undelegation_amount( + // &self, + // /// base64 pubkey, e.g. OmZMrZBVsPjQwvHsROCI3mRw2pdVnYER8Xa5lzQ3Ek0= + // pubkey: Query>, + // /// starting timestamp. + // start: Query>, + // /// ending timestamp. + // end: Query>, + // ) -> poem::Result { + // service::v1::staking::get_undelegation_amount(self, pubkey, start, end) + // .await + // .map_err(handle_fetch_one_err) + // } + + // #[oai( + // path = "/chain/prism/sync", + // method = "get", + // tag = "ApiTags::BlockChain" + // )] + // async fn prism_sync_info(&self) -> poem::Result { + // service::v1::chain::prism_sync_info(self) + // .await + // .map_err(handle_fetch_one_err) + // } + + // #[oai( + // path = "/chain/delegation/address/num", + // method = "get", + // tag = "ApiTags::BlockChain" + // )] + // async fn delegation_address_num(&self) -> poem::Result { + // service::v1::chain::delegation_address_num(self) + // .await + // .map_err(handle_fetch_one_err) + // } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // V2 + /////////////////////////////////////////////////////////////////////////////////////////////// #[oai( - path = "/staking/delegation/amount", + path = "/v2/staking/delegation/:tx_hash", method = "get", tag = "ApiTags::Staking" )] - async fn get_delegation_amount( - &self, - /// base64 pubkey, e.g. OmZMrZBVsPjQwvHsROCI3mRw2pdVnYER8Xa5lzQ3Ek0= - pubkey: Query>, - /// starting timestamp. - start: Query>, - /// ending timestamp. - end: Query>, - ) -> poem::Result { - service::v1::staking::get_delegation_amount(self, pubkey, start, end) + async fn v2_get_delegation(&self, tx_hash: Path) -> poem::Result { + v2_get_delegation(self, tx_hash) .await .map_err(handle_fetch_one_err) } - #[oai( - path = "/staking/undelegation/amount", + path = "/v2/staking/delegations", method = "get", tag = "ApiTags::Staking" )] - async fn get_undelegation_amount( + async fn v2_get_delegations( &self, - /// base64 pubkey, e.g. OmZMrZBVsPjQwvHsROCI3mRw2pdVnYER8Xa5lzQ3Ek0= - pubkey: Query>, - /// starting timestamp. - start: Query>, - /// ending timestamp. - end: Query>, - ) -> poem::Result { - service::v1::staking::get_undelegation_amount(self, pubkey, start, end) - .await - .map_err(handle_fetch_one_err) - } - - #[oai( - path = "/chain/prism/sync", - method = "get", - tag = "ApiTags::BlockChain" - )] - async fn prism_sync_info(&self) -> poem::Result { - service::v1::chain::prism_sync_info(self) - .await - .map_err(handle_fetch_one_err) - } - - #[oai( - path = "/chain/delegation/address/num", - method = "get", - tag = "ApiTags::BlockChain" - )] - async fn delegation_address_num(&self) -> poem::Result { - service::v1::chain::delegation_address_num(self) + address: Query, + page: Query>, + page_size: Query>, + ) -> poem::Result { + v2_get_delegations(self, address, page, page_size) .await .map_err(handle_fetch_one_err) } - - /////////////////////////////////////////////////////////////////////////////////////////////// - // V2 - /////////////////////////////////////////////////////////////////////////////////////////////// #[oai( - path = "/v2/tx/delegation/:tx_hash", + path = "/v2/staking/undelegation/:tx_hash", method = "get", - tag = "ApiTags::Transaction" + tag = "ApiTags::Staking" )] - async fn v2_get_delegation_tx( + async fn v2_get_undelegation( &self, tx_hash: Path, - ) -> poem::Result { - v2_get_delegation_tx(self, tx_hash) + ) -> poem::Result { + v2_get_undelegation(self, tx_hash) .await .map_err(handle_fetch_one_err) } + #[oai( - path = "/v2/tx/delegations", + path = "/v2/staking/undelegations", method = "get", - tag = "ApiTags::Transaction" + tag = "ApiTags::Staking" )] - async fn v2_get_delegation_txs( + async fn v2_get_undelegations( &self, address: Query, page: Query>, page_size: Query>, - ) -> poem::Result { - v2_get_delegation_txs(self, address, page, page_size) + ) -> poem::Result { + v2_get_undelegations(self, address, page, page_size) .await .map_err(handle_fetch_one_err) } + #[oai( - path = "/v2/tx/undelegation/:tx_hash", + path = "/v2/staking/claim/:tx_hash", method = "get", - tag = "ApiTags::Transaction" + tag = "ApiTags::Staking" )] - async fn v2_get_undelegation_tx( - &self, - tx_hash: Path, - ) -> poem::Result { - v2_get_undelegation_tx(self, tx_hash) + async fn v2_get_claim(&self, tx_hash: Path) -> poem::Result { + v2_get_claim(self, tx_hash) .await .map_err(handle_fetch_one_err) } - #[oai( - path = "/v2/tx/undelegations", - method = "get", - tag = "ApiTags::Transaction" - )] - async fn v2_get_undelegation_txs( + + #[oai(path = "/v2/staking/claims", method = "get", tag = "ApiTags::Staking")] + async fn v2_get_claims( &self, address: Query, page: Query>, page_size: Query>, - ) -> poem::Result { - v2_get_undelegation_txs(self, address, page, page_size) - .await - .map_err(handle_fetch_one_err) - } - #[oai( - path = "/v2/tx/claim/:tx_hash", - method = "get", - tag = "ApiTags::Transaction" - )] - async fn v2_get_claim_tx(&self, tx_hash: Path) -> poem::Result { - v2_get_claim_tx(self, tx_hash) + ) -> poem::Result { + v2_get_claims(self, address, page, page_size) .await .map_err(handle_fetch_one_err) } diff --git a/explorer/src/service/v1/chain.rs b/explorer/src/service/v1/chain.rs index fea417b..fdc004c 100644 --- a/explorer/src/service/v1/chain.rs +++ b/explorer/src/service/v1/chain.rs @@ -256,7 +256,7 @@ pub struct PrismResult { pub struct PrismInfo { pub height: i64, } - +#[allow(dead_code)] pub async fn prism_sync_info(api: &Api) -> Result { let mut conn = api.storage.lock().await.acquire().await?; @@ -292,6 +292,7 @@ pub struct DelegateAddressNum { num: u64, } +#[allow(dead_code)] pub async fn delegation_address_num(api: &Api) -> Result { let mut conn = api.storage.lock().await.acquire().await?; diff --git a/explorer/src/service/v1/staking.rs b/explorer/src/service/v1/staking.rs index a329bf4..e428dc3 100644 --- a/explorer/src/service/v1/staking.rs +++ b/explorer/src/service/v1/staking.rs @@ -339,7 +339,7 @@ pub struct DelegationInfoResult { pub message: String, pub data: Option, } - +#[allow(dead_code)] pub async fn delegation(api: &Api, pubkey: Path) -> Result { let delegation_info_url = api .platform @@ -607,7 +607,7 @@ pub struct DelegationAmountResult { pub struct DelegationAmountData { pub amount: u64, } - +#[allow(dead_code)] pub async fn get_delegation_amount( api: &Api, pubkey: Query>, @@ -668,7 +668,7 @@ pub struct UndelegationAmountResult { pub struct UndelegationAmountData { pub amount: u64, } - +#[allow(dead_code)] pub async fn get_undelegation_amount( api: &Api, pubkey: Query>, diff --git a/explorer/src/service/v1/transaction.rs b/explorer/src/service/v1/transaction.rs index 0adb2c8..8ddf15c 100644 --- a/explorer/src/service/v1/transaction.rs +++ b/explorer/src/service/v1/transaction.rs @@ -481,6 +481,7 @@ pub async fn get_tx(api: &Api, tx_hash: Path) -> Result { }))) } +#[allow(dead_code)] pub async fn get_txs_receive_from( api: &Api, address: Query, @@ -548,7 +549,7 @@ pub async fn get_txs_receive_from( }), }))) } - +#[allow(dead_code)] pub async fn get_txs_send_to( api: &Api, address: Query, @@ -744,7 +745,7 @@ pub async fn get_txs( }), }))) } - +#[allow(dead_code)] #[allow(clippy::too_many_arguments)] pub async fn get_txs_raw( api: &Api, @@ -1120,7 +1121,7 @@ pub struct ClaimAmountResult { pub struct ClaimAmount { pub amount: u64, } - +#[allow(dead_code)] pub async fn get_claims_amount(api: &Api, address: Path) -> Result { let mut conn = api.storage.lock().await.acquire().await?; diff --git a/explorer/src/service/v2/claim.rs b/explorer/src/service/v2/claim.rs index 09de8f6..9bc96df 100644 --- a/explorer/src/service/v2/claim.rs +++ b/explorer/src/service/v2/claim.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}; @@ -7,9 +8,9 @@ use serde_json::Value; use sqlx::Row; #[derive(ApiResponse)] -pub enum V2ClaimTxResponse { +pub enum V2ClaimResponse { #[oai(status = 200)] - Ok(Json), + Ok(Json), #[oai(status = 404)] NotFound, #[oai(status = 500)] @@ -17,14 +18,14 @@ pub enum V2ClaimTxResponse { } #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2ClaimTxResult { +pub struct V2ClaimResult { pub code: u16, pub message: String, - pub data: Option, + pub data: Option, } #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2ClaimTx { +pub struct V2Claim { pub tx_hash: String, pub block_hash: String, pub from: String, @@ -34,7 +35,7 @@ pub struct V2ClaimTx { pub value: Value, } -pub async fn v2_get_claim_tx(api: &Api, tx_hash: Path) -> Result { +pub async fn v2_get_claim(api: &Api, tx_hash: Path) -> Result { let mut conn = api.storage.lock().await.acquire().await?; let sql_query = format!( "SELECT * FROM claims WHERE tx='{}'", @@ -51,7 +52,7 @@ pub async fn v2_get_claim_tx(api: &Api, tx_hash: Path) -> Result) -> Result), + #[oai(status = 404)] + NotFound, + #[oai(status = 500)] + InternalError, +} + +#[derive(Serialize, Deserialize, Debug, Object)] +pub struct V2ClaimsResult { + pub code: u16, + pub message: String, + pub data: Option, +} + +#[derive(Serialize, Deserialize, Debug, Object)] +pub struct V2ClaimsData { + pub page: i64, + pub page_size: i64, + pub total: i64, + pub data: Option>, +} + +pub async fn v2_get_claims( + 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 claims 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,height,timestamp,content FROM claims 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 sender: String = row.try_get("sender")?; + let amount: i64 = row.try_get("amount")?; + let height: i64 = row.try_get("height")?; + let timestamp: i64 = row.try_get("timestamp")?; + let value: Value = row.try_get("content")?; + res.push(V2Claim { + tx_hash: tx, + block_hash: block, + from: sender, + amount: amount as u64, + height, + timestamp, + value, + }); + } + + Ok(V2ClaimsResponse::Ok(Json(V2ClaimsResult { + code: 200, + message: "".to_string(), + data: Some(V2ClaimsData { + page, + page_size, + total, + data: Some(res), + }), + }))) +} diff --git a/explorer/src/service/v2/delegation.rs b/explorer/src/service/v2/delegation.rs index 284bd66..3b96540 100644 --- a/explorer/src/service/v2/delegation.rs +++ b/explorer/src/service/v2/delegation.rs @@ -8,9 +8,9 @@ use serde_json::Value; use sqlx::Row; #[derive(ApiResponse)] -pub enum V2DelegationTxResponse { +pub enum V2DelegationResponse { #[oai(status = 200)] - Ok(Json), + Ok(Json), #[oai(status = 404)] NotFound, #[oai(status = 500)] @@ -18,14 +18,14 @@ pub enum V2DelegationTxResponse { } #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2DelegationTxResult { +pub struct V2DelegationResult { pub code: u16, pub message: String, - pub data: Option, + pub data: Option, } #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2DelegationTx { +pub struct V2Delegation { pub tx_hash: String, pub block_hash: String, pub amount: i64, @@ -37,10 +37,7 @@ pub struct V2DelegationTx { pub value: Value, } -pub async fn v2_get_delegation_tx( - api: &Api, - tx_hash: Path, -) -> Result { +pub async fn v2_get_delegation(api: &Api, tx_hash: Path) -> Result { let mut conn = api.storage.lock().await.acquire().await?; let sql_query = format!( "SELECT * FROM delegations WHERE tx='{}'", @@ -59,7 +56,7 @@ pub async fn v2_get_delegation_tx( let timestamp: i64 = row.try_get("timestamp")?; let value: Value = row.try_get("content")?; - let res = V2DelegationTx { + let res = V2Delegation { tx_hash: tx, block_hash: block, from: sender, @@ -71,7 +68,7 @@ pub async fn v2_get_delegation_tx( value, }; - Ok(V2DelegationTxResponse::Ok(Json(V2DelegationTxResult { + Ok(V2DelegationResponse::Ok(Json(V2DelegationResult { code: StatusCode::OK.as_u16(), message: "".to_string(), data: Some(res), @@ -79,9 +76,9 @@ pub async fn v2_get_delegation_tx( } #[derive(ApiResponse)] -pub enum V2DelegationTxsResponse { +pub enum V2DelegationsResponse { #[oai(status = 200)] - Ok(Json), + Ok(Json), #[oai(status = 404)] NotFound, #[oai(status = 500)] @@ -89,7 +86,7 @@ pub enum V2DelegationTxsResponse { } #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2DelegationTxsResult { +pub struct V2DelegationsResult { pub code: u16, pub message: String, pub data: Option, @@ -100,15 +97,15 @@ pub struct V2DelegationTxsData { pub page: i64, pub page_size: i64, pub total: i64, - pub items: Vec, + pub items: Vec, } -pub async fn v2_get_delegation_txs( +pub async fn v2_get_delegations( api: &Api, address: Query, page: Query>, page_size: Query>, -) -> Result { +) -> 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?; @@ -124,7 +121,7 @@ pub async fn v2_get_delegation_txs( address.0.to_lowercase(), page_size, (page-1)*page_size ); - let mut res: Vec = vec![]; + 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")?; @@ -136,7 +133,7 @@ pub async fn v2_get_delegation_txs( let height: i64 = row.try_get("height")?; let timestamp: i64 = row.try_get("timestamp")?; let value: Value = row.try_get("content")?; - res.push(V2DelegationTx { + res.push(V2Delegation { tx_hash: tx, block_hash: block, from: sender, @@ -149,7 +146,7 @@ pub async fn v2_get_delegation_txs( }); } - Ok(V2DelegationTxsResponse::Ok(Json(V2DelegationTxsResult { + Ok(V2DelegationsResponse::Ok(Json(V2DelegationsResult { code: 200, message: "".to_string(), data: Some(V2DelegationTxsData { diff --git a/explorer/src/service/v2/undelegation.rs b/explorer/src/service/v2/undelegation.rs index 7aa4443..4accc88 100644 --- a/explorer/src/service/v2/undelegation.rs +++ b/explorer/src/service/v2/undelegation.rs @@ -8,9 +8,9 @@ use serde_json::Value; use sqlx::Row; #[derive(ApiResponse)] -pub enum V2UndelegationTxResponse { +pub enum V2UndelegationResponse { #[oai(status = 200)] - Ok(Json), + Ok(Json), #[oai(status = 404)] NotFound, #[oai(status = 500)] @@ -18,14 +18,14 @@ pub enum V2UndelegationTxResponse { } #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2UndelegationTxResult { +pub struct V2UndelegationResult { pub code: u16, pub message: String, - pub data: Option, + pub data: Option, } #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2UndelegationTx { +pub struct V2Undelegation { pub tx_hash: String, pub block_hash: String, pub from: String, @@ -37,10 +37,10 @@ pub struct V2UndelegationTx { pub value: Value, } -pub async fn v2_get_undelegation_tx( +pub async fn v2_get_undelegation( api: &Api, tx_hash: Path, -) -> Result { +) -> Result { let mut conn = api.storage.lock().await.acquire().await?; let sql_query = format!( "SELECT tx,block,sender,amount,validator,new_validator,timestamp,height,content FROM undelegations WHERE tx='{}'", @@ -59,7 +59,7 @@ pub async fn v2_get_undelegation_tx( let timestamp: i64 = row.try_get("timestamp")?; let value: Value = row.try_get("content")?; - let res = V2UndelegationTx { + let res = V2Undelegation { tx_hash: tx, block_hash: block, from: sender, @@ -71,42 +71,44 @@ pub async fn v2_get_undelegation_tx( value, }; - Ok(V2UndelegationTxResponse::Ok(Json(V2UndelegationTxResult { + Ok(V2UndelegationResponse::Ok(Json(V2UndelegationResult { code: StatusCode::OK.as_u16(), message: "".to_string(), data: Some(res), }))) } + #[derive(ApiResponse)] -pub enum V2UndelegationTxsResponse { +pub enum V2UndelegationsResponse { #[oai(status = 200)] - Ok(Json), + Ok(Json), #[oai(status = 404)] NotFound, #[oai(status = 500)] InternalError, } + #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2UndelegationTxsResult { +pub struct V2UndelegationsResult { pub code: u16, pub message: String, - pub data: V2UndelegationTxsData, + pub data: V2UndelegationsData, } #[derive(Serialize, Deserialize, Debug, Object)] -pub struct V2UndelegationTxsData { +pub struct V2UndelegationsData { pub page: i64, pub page_size: i64, pub total: i64, - pub items: Option>, + pub items: Option>, } -pub async fn v2_get_undelegation_txs( +pub async fn v2_get_undelegations( api: &Api, address: Query, page: Query>, page_size: Query>, -) -> Result { +) -> 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); @@ -122,7 +124,7 @@ pub async fn v2_get_undelegation_txs( address.0.to_lowercase(), page_size, (page-1)*page_size ); - let mut res: Vec = vec![]; + 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")?; @@ -134,7 +136,7 @@ pub async fn v2_get_undelegation_txs( let height: i64 = row.try_get("height")?; let timestamp: i64 = row.try_get("timestamp")?; let value: Value = row.try_get("content")?; - res.push(V2UndelegationTx { + res.push(V2Undelegation { tx_hash: tx, block_hash: block, from: sender, @@ -147,16 +149,14 @@ pub async fn v2_get_undelegation_txs( }); } - Ok(V2UndelegationTxsResponse::Ok(Json( - V2UndelegationTxsResult { - code: 200, - message: "".to_string(), - data: V2UndelegationTxsData { - page, - page_size, - total, - items: Some(res), - }, + Ok(V2UndelegationsResponse::Ok(Json(V2UndelegationsResult { + code: 200, + message: "".to_string(), + data: V2UndelegationsData { + page, + page_size, + total, + items: Some(res), }, - ))) + }))) }