Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 delegations. #233

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions explorer/src/service/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<String>,
page: Query<Option<i64>>,
page_size: Query<Option<i64>>,
) -> poem::Result<V2DelegationTxsResponse> {
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",
Expand Down
84 changes: 84 additions & 0 deletions explorer/src/service/v2/delegation.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -76,3 +77,86 @@ pub async fn v2_get_delegation_tx(
data: Some(res),
})))
}

#[derive(ApiResponse)]
pub enum V2DelegationTxsResponse {
#[oai(status = 200)]
Ok(Json<V2DelegationTxsResult>),
#[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<V2DelegationTxsData>,
}

#[derive(Serialize, Deserialize, Debug, Object)]
pub struct V2DelegationTxsData {
pub page: i64,
pub page_size: i64,
pub total: i64,
pub items: Vec<V2DelegationTx>,
}

pub async fn v2_get_delegation_txs(
api: &Api,
address: Query<String>,
page: Query<Option<i64>>,
page_size: Query<Option<i64>>,
) -> Result<V2DelegationTxsResponse> {
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<V2DelegationTx> = 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,
}),
})))
}
Loading