Skip to content

Commit

Permalink
feat: stakes.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinyuchan committed Mar 18, 2024
1 parent 14c471b commit f5b05c4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
72 changes: 71 additions & 1 deletion indexer/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::{IndexerError, Result};
use crate::types::{
ClaimRecord, DelegationRecord, DelegatorDebt, DelegatorInfo, DelegatorReward, DelegatorSum,
QueryResult, UndelegationRecord, ValidatorDataResponse, ValidatorResponse,
QueryResult, StakeRecord, UndelegationRecord, ValidatorDataResponse, ValidatorResponse,
ValidatorStatusResponse,
};
use crate::AppState;
Expand All @@ -16,6 +16,76 @@ use sqlx::Row;
use std::str::FromStr;
use std::sync::Arc;

#[derive(Serialize, Deserialize)]
pub struct StakeRecordParams {
pub validator: Option<String>,
pub staker: Option<String>,
pub page: Option<i32>,
pub page_size: Option<i32>,
}

pub async fn get_stake_records(
State(state): State<Arc<AppState>>,
params: Query<StakeRecordParams>,
) -> Result<Json<QueryResult<Vec<StakeRecord>>>> {
let mut pool = state.pool.acquire().await?;
let page = params.page.unwrap_or(1);
let page_size = params.page_size.unwrap_or(10);

let (sql_total, sql_query) = if let Some(addr) = params.0.validator {
let addr = if addr[0..2].eq("0x") {
&addr[2..]
} else {
&addr
};
(
format!("select count(*) as cnt from evm_e_stake where validator='{}'", addr),
format!("select tx,block_num,validator,public_key,ty,staker,amount,memo,rate from evm_e_stake where validator='{}' order by block_num desc limit {} offset {}", addr, page_size, (page-1)*page_size)
)
} else {
(
"select count(*) as cnt from evm_e_stake".to_string(),
format!("select tx,block_num,validator,public_key,ty,staker,amount,memo,rate from evm_e_stake order by block_num desc limit {} offset {}",page_size, (page-1)*page_size)
)
};

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

let rows = sqlx::query(&sql_query).fetch_all(&mut *pool).await?;
let mut stakes: Vec<StakeRecord> = vec![];
for r in rows {
let tx: String = r.try_get("tx")?;
let block_num: i64 = r.try_get("block_num")?;
let validator: String = r.try_get("validator")?;
let public_key: String = r.try_get("public_key")?;
let ty: i32 = r.try_get("ty")?;
let staker: String = r.try_get("staker")?;
let amount: BigDecimal = r.try_get("amount").unwrap_or_default();
let memo: String = r.try_get("memo")?;
let rate: BigDecimal = r.try_get("rate").unwrap_or_default();

stakes.push(StakeRecord {
tx,
block_num,
validator,
public_key,
ty,
staker,
amount: amount.to_string(),
memo,
rate: rate.to_string(),
})
}

Ok(Json(QueryResult {
total,
page,
page_size,
data: stakes,
}))
}

#[derive(Serialize, Deserialize)]
pub struct ClaimRecordsParams {
pub delegator: Option<String>,
Expand Down
6 changes: 4 additions & 2 deletions indexer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ mod types;

use crate::api::{
get_claim_records, get_delegation_records, get_delegator_bound, get_delegator_debt,
get_delegator_reward, get_delegators_of_validator, get_sum, get_undelegation_records,
get_validator_detail, get_validator_status, get_validators, get_validators_of_delegator,
get_delegator_reward, get_delegators_of_validator, get_stake_records, get_sum,
get_undelegation_records, get_validator_detail, get_validator_status, get_validators,
get_validators_of_delegator,
};
use axum::http::Method;
use axum::routing::get;
Expand Down Expand Up @@ -88,6 +89,7 @@ async fn main() -> Result<()> {
.route("/api/validator/list", get(get_validators))
.route("/api/validator/detail", get(get_validator_detail))
.route("/api/validator/status", get(get_validator_status))
.route("/api/stakes", get(get_stake_records))
.route("/api/claims", get(get_claim_records))
.route("/api/delegations", get(get_delegation_records))
.route("/api/undelegations", get(get_undelegation_records))
Expand Down
13 changes: 13 additions & 0 deletions indexer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,16 @@ pub struct ClaimRecord {
pub delegator: String,
pub amount: String,
}

#[derive(Serialize, Deserialize)]
pub struct StakeRecord {
pub tx: String,
pub block_num: i64,
pub validator: String,
pub public_key: String,
pub ty: i32,
pub staker: String,
pub amount: String,
pub memo: String,
pub rate: String,
}

0 comments on commit f5b05c4

Please sign in to comment.