Skip to content

Commit

Permalink
fix: query validator. (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowbeer authored Jul 10, 2024
1 parent ed37518 commit e0fce29
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
49 changes: 48 additions & 1 deletion indexer/src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub async fn get_validators(
let mut pool = state.pool.acquire().await?;
let page = params.page.unwrap_or(1);
let page_size = params.page_size.unwrap_or(10);

let mut single = false;
let mut sql_total = "SELECT count(ev.block_num) \
FROM evm_stakes es \
LEFT JOIN evm_validators ev \
Expand All @@ -129,6 +129,7 @@ pub async fn get_validators(
let mut query_params: Vec<String> = vec![];
if let Some(ref validator) = params.0.validator {
query_params.push(format!("ev.validator='{}' ", validator));
single = true;
}
if let Some(online) = params.0.online {
query_params.push(format!("ev.active={} ", online))
Expand Down Expand Up @@ -196,6 +197,52 @@ pub async fn get_validators(
memo,
})
}
if single && validators.len() == 0 {
let slq_query_single = r#"SELECT ev.validator,ev.pubkey,ev.pubkey_type,ev.rate,ev.staker,
ev.power,ev.unbound,ev.punish_rate,ev.begin_block,ev.active,ev.jailed,ev.unjail_time,
ev.should_vote,ev.voted,es.memo
FROM evm_validators ev
LEFT JOIN evm_stakes es
ON ev.validator = es.validator
WHERE ev.validator=$1
ORDER BY ev.block_num DESC LIMIT 1"#;
let r = sqlx::query(&slq_query_single)
.bind(&params.0.validator)
.fetch_one(&mut *pool)
.await?;
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 {
total,
Expand Down
2 changes: 1 addition & 1 deletion scanner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use sqlx::{Pool, Postgres};
use std::env;
use std::time::Duration;

const DEFAULT_INTERVAL: u64 = 6; // 6s
const DEFAULT_INTERVAL: u64 = 4; // 4s
const DEFAULT_RPC_RETRIES: usize = 3;

#[derive(Parser, Debug)]
Expand Down

0 comments on commit e0fce29

Please sign in to comment.