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
use solana_sdk::pubkey::Pubkey;

use crate::{
    utils::{
        rpc_config::RpcSignaturesForAddressConfig,
        rpc_response::RpcConfirmedTransactionStatusWithSignature,
    },
    ClientRequest, ClientResponse,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetSignaturesForAddressRequest {
    pubkey: Pubkey,
    #[serde(skip_serializing_if = "Option::is_none")]
    config: Option<RpcSignaturesForAddressConfig>,
}

impl GetSignaturesForAddressRequest {
    pub fn new(pubkey: Pubkey) -> Self {
        Self {
            pubkey,
            config: None,
        }
    }

    pub fn new_with_config(pubkey: Pubkey, config: RpcSignaturesForAddressConfig) -> Self {
        Self {
            pubkey,
            config: Some(config),
        }
    }
}

impl Into<serde_json::Value> for GetSignaturesForAddressRequest {
    fn into(self) -> serde_json::Value {
        let pubkey = self.pubkey.to_string();

        match self.config {
            Some(config) => serde_json::json!([pubkey, config]),
            None => serde_json::json!([pubkey]),
        }
    }
}

impl Into<ClientRequest> for GetSignaturesForAddressRequest {
    fn into(self) -> ClientRequest {
        let mut request = ClientRequest::new("getSignaturesForAddress");
        let params = self.into();

        request.params(params).clone()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetSignaturesForAddressResponse(Vec<RpcConfirmedTransactionStatusWithSignature>);

impl From<ClientResponse> for GetSignaturesForAddressResponse {
    fn from(response: ClientResponse) -> Self {
        serde_json::from_value(response.result).unwrap()
    }
}

impl Into<Vec<RpcConfirmedTransactionStatusWithSignature>> for GetSignaturesForAddressResponse {
    fn into(self) -> Vec<RpcConfirmedTransactionStatusWithSignature> {
        self.0
    }
}