1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::error::CosmosClientError;
use cosmos_sdk_proto::cosmos::base::query::v1beta1::PageRequest;
use cosmos_sdk_proto::cosmos::slashing::v1beta1::{
    QueryParamsRequest, QueryParamsResponse, QuerySigningInfoRequest, QuerySigningInfoResponse,
    QuerySigningInfosRequest, QuerySigningInfosResponse,
};
use prost::Message;
use std::rc::Rc;
use tendermint_rpc::{Client, HttpClient};

pub struct SlashingModule {
    rpc: Rc<HttpClient>,
}

impl SlashingModule {
    pub fn new(rpc: Rc<HttpClient>) -> Self {
        SlashingModule { rpc }
    }

    pub async fn params(&self) -> Result<QueryParamsResponse, CosmosClientError> {
        let query = QueryParamsRequest {};
        let query = self
            .rpc
            .abci_query(
                Some("/cosmos.slashing.v1beta1.Query/Params".to_string()),
                query.encode_to_vec(),
                None,
                false,
            )
            .await
            .unwrap();

        let resp = QueryParamsResponse::decode(query.value.as_slice())?;
        Ok(resp)
    }

    pub async fn signing_info(
        &self,
        cons_address: &str,
    ) -> Result<QuerySigningInfoResponse, CosmosClientError> {
        let query = QuerySigningInfoRequest {
            cons_address: cons_address.to_string(),
        };
        let query = self
            .rpc
            .abci_query(
                Some("/cosmos.slashing.v1beta1.Query/SigningInfo".to_string()),
                query.encode_to_vec(),
                None,
                false,
            )
            .await
            .unwrap();

        let resp = QuerySigningInfoResponse::decode(query.value.as_slice())?;
        Ok(resp)
    }

    pub async fn signing_infos(
        &self,
        pagination: Option<PageRequest>,
    ) -> Result<QuerySigningInfosResponse, CosmosClientError> {
        let query = QuerySigningInfosRequest { pagination };
        let query = self
            .rpc
            .abci_query(
                Some("/cosmos.slashing.v1beta1.Query/SigningInfos".to_string()),
                query.encode_to_vec(),
                None,
                false,
            )
            .await
            .unwrap();

        let resp = QuerySigningInfosResponse::decode(query.value.as_slice())?;
        Ok(resp)
    }
}