Skip to content

Commit

Permalink
optimize api. (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogefoodcoin authored Jul 10, 2024
1 parent 72666a1 commit e5c2b7a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
6 changes: 5 additions & 1 deletion indexer/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use sqlx::Error::RowNotFound;

#[derive(Debug)]
pub enum IndexerError {
Expand Down Expand Up @@ -60,7 +61,10 @@ impl IntoResponse for IndexerError {
fn into_response(self) -> Response {
let err_msg = match self {
IndexerError::IndexerCustom(e) => e,
IndexerError::IndexerDBError(e) => e.to_string(),
IndexerError::IndexerDBError(e) => match e {
RowNotFound => return (StatusCode::NOT_FOUND, "not found").into_response(),
_ => e.to_string(),
},
IndexerError::IndexerIOError(e) => e.to_string(),
IndexerError::IndexerTomlDeError(e) => e.to_string(),
IndexerError::IndexerHexError(e) => e.to_string(),
Expand Down
79 changes: 40 additions & 39 deletions indexer/src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,46 +158,9 @@ pub async fn get_validators(
);
}

let row = sqlx::query(&sql_total).fetch_one(&mut *pool).await?;
let mut total: i64 = row.try_get("count")?;

let mut validators: Vec<ValidatorResponse> = vec![];
let rows = sqlx::query(&sql_query).fetch_all(&mut *pool).await?;
for r in rows {
let validator: String = r.try_get("validator")?;
let staker: String = r.try_get("staker")?;
let active: bool = r.try_get("active")?;
let jailed: bool = r.try_get("jailed")?;
let should_vote: i32 = r.try_get("should_vote")?;
let voted: i32 = r.try_get("voted")?;
let pubkey: String = r.try_get("pubkey")?;
let pubkey_type: i32 = r.try_get("pubkey_type")?;
let rate: BigDecimal = r.try_get("rate")?;
let power: BigDecimal = r.try_get("power")?;
let unbound_amount: BigDecimal = r.try_get("unbound")?;
let punish_rate: BigDecimal = r.try_get("punish_rate")?;
let begin_block: i64 = r.try_get("begin_block")?;
let unjail_time: NaiveDateTime = r.try_get("unjail_time")?;
let memo: Value = r.try_get("memo")?;
validators.push(ValidatorResponse {
validator,
staker,
active,
jailed,
should_vote,
voted,
pubkey,
pubkey_type,
rate: rate.to_string(),
power: power.to_string(),
unbound_amount: unbound_amount.to_string(),
punish_rate: punish_rate.to_string(),
begin_block,
unjail_time: unjail_time.and_utc().timestamp(),
memo,
})
}
if single && validators.len() == 0 {
let mut total: i64 = 0;
if single {
let sql_query_memo = r#"SELECT memo from evm_stakes WHERE validator=$1"#;
let row = sqlx::query(&sql_query_memo)
.bind(&params.0.validator)
Expand Down Expand Up @@ -244,6 +207,44 @@ pub async fn get_validators(
unjail_time: unjail_time.and_utc().timestamp(),
memo,
})
} else {
let row = sqlx::query(&sql_total).fetch_one(&mut *pool).await?;
total = row.try_get("count")?;
let rows = sqlx::query(&sql_query).fetch_all(&mut *pool).await?;
for r in rows {
let validator: String = r.try_get("validator")?;
let staker: String = r.try_get("staker")?;
let active: bool = r.try_get("active")?;
let jailed: bool = r.try_get("jailed")?;
let should_vote: i32 = r.try_get("should_vote")?;
let voted: i32 = r.try_get("voted")?;
let pubkey: String = r.try_get("pubkey")?;
let pubkey_type: i32 = r.try_get("pubkey_type")?;
let rate: BigDecimal = r.try_get("rate")?;
let power: BigDecimal = r.try_get("power")?;
let unbound_amount: BigDecimal = r.try_get("unbound")?;
let punish_rate: BigDecimal = r.try_get("punish_rate")?;
let begin_block: i64 = r.try_get("begin_block")?;
let unjail_time: NaiveDateTime = r.try_get("unjail_time")?;
let memo: Value = r.try_get("memo")?;
validators.push(ValidatorResponse {
validator,
staker,
active,
jailed,
should_vote,
voted,
pubkey,
pubkey_type,
rate: rate.to_string(),
power: power.to_string(),
unbound_amount: unbound_amount.to_string(),
punish_rate: punish_rate.to_string(),
begin_block,
unjail_time: unjail_time.and_utc().timestamp(),
memo,
})
}
}

Ok(Json(QueryResult {
Expand Down

0 comments on commit e5c2b7a

Please sign in to comment.