Skip to content

Commit

Permalink
Feat: validator status. (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowbeer authored Apr 1, 2024
1 parent 75f34cd commit 31c85e3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
24 changes: 23 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* [2.2 获取reward数量](#2.2)
* [2.3 获取debt数量](#2.3)
* [2.4 获取validator数据](#2.4)
*
* [2.5 获取validator状态](#2.5)
## [Other](#3)
* [3.1 统计delegate,undelegate,claim总量](#3.1)

Expand Down Expand Up @@ -568,6 +568,28 @@
}
```

<h3 id="2.5">2.5 获取validator状态</h3>

* `GET /api/vstatus`
* 参数

| 参数 | 类型 | 必传 | 说明 |
|---------|--------|----|-------------|
| address | string | Y | validator地址 |


* Request: `http://localhost/api/vstatus?address=0xd518c4f95a3f39ed853a2614566897c4ad5a008f`
* Response:
```json
{
"heap_index_off1": "54",
"is_active": true,
"jailed": false,
"unjail_datetime": 1697279406,
"should_vote": 1445,
"voted": 1445
}
```

<h3 id="3.1">3.1 统计delegate,undelegate,claim总量</h3>

Expand Down
29 changes: 29 additions & 0 deletions indexer/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::{IndexerError, Result};
use crate::types::{
BoundResponse, DebtResponse, DelegatorSumResponse, RewardResponse, ValidatorDataResponse,
ValidatorStatusResponse,
};
use crate::AppState;
use axum::extract::{Query, State};
Expand All @@ -13,6 +14,34 @@ use sqlx::Row;
use std::str::FromStr;
use std::sync::Arc;

#[derive(Serialize, Deserialize)]
pub struct ValidatorStatusParams {
pub address: String,
}

pub async fn get_validator_status(
State(state): State<Arc<AppState>>,
params: Query<ValidatorStatusParams>,
) -> Result<Json<ValidatorStatusResponse>> {
let staking = state.staking.clone();
let validator = H160::from_str(&params.address)?;

match staking.validator_status(validator).call().await {
Ok(data) => {
let res = ValidatorStatusResponse {
heap_index_off1: data.0.to_string(),
is_active: data.1,
jailed: data.2,
unjail_datetime: data.3,
should_vote: data.4,
voted: data.5,
};
Ok(Json(res))
}
Err(e) => Err(IndexerError::Custom(e.to_string())),
}
}

#[derive(Serialize, Deserialize)]
pub struct ValidatorDataParams {
pub address: String,
Expand Down
3 changes: 2 additions & 1 deletion indexer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod validators;

use crate::contract::{
get_delegator_bound, get_delegator_debt, get_delegator_reward, get_delegator_sum,
get_validator_data,
get_validator_data, get_validator_status,
};
use crate::delegate::get_delegate_records;
use crate::receipt::get_receipts;
Expand Down Expand Up @@ -116,6 +116,7 @@ async fn main() -> Result<()> {
.route("/api/debt", get(get_delegator_debt))
.route("/api/sum", get(get_delegator_sum))
.route("/api/vdata", get(get_validator_data))
.route("/api/vstatus", get(get_validator_status))
.layer(cors)
.with_state(app_state);

Expand Down
10 changes: 10 additions & 0 deletions indexer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,13 @@ pub struct ValidatorDataResponse {
pub total_unbound_amount: String,
pub begin_block: u64,
}

#[derive(Serialize, Deserialize)]
pub struct ValidatorStatusResponse {
pub heap_index_off1: String,
pub is_active: bool,
pub jailed: bool,
pub unjail_datetime: u64,
pub should_vote: u16,
pub voted: u16,
}

0 comments on commit 31c85e3

Please sign in to comment.