solana_trader_client_rust/provider/http/
general.rs

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::provider::http::HTTPClient;
use anyhow::{anyhow, Result};
use solana_trader_proto::api;
use solana_trader_proto::api::GetAccountBalanceRequest;

impl HTTPClient {
    pub async fn get_transaction(
        &self,
        request: &api::GetTransactionRequest,
    ) -> anyhow::Result<api::GetTransactionResponse> {
        let url = format!(
            "{}/api/v2/transaction?signature={}",
            self.base_url, request.signature
        );

        println!("{}", url);

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        let response_text = response.text().await?;

        println!("{}", response_text);

        // let mut value: serde_json::Value = serde_json::from_str(&response_text)
        //     .map_err(|e| anyhow::anyhow!("Failed to parse response as JSON: {}", e))?;
        //
        // convert_string_enums(&mut value);
        //
        // serde_json::from_value(value)
        //     .map_err(|e| anyhow::anyhow!("Failed to parse response into GetTransactionResponse: {}", e))

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        self.handle_response(response).await
    }
    pub async fn get_recent_block_hash(&self) -> anyhow::Result<api::GetRecentBlockHashResponse> {
        let url = format!("{}/api/v1/system/blockhash", self.base_url);

        println!("{}", url);

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        self.handle_response(response).await
    }

    pub async fn get_recent_block_hash_v2(
        &self,
        request: &api::GetRecentBlockHashRequestV2,
    ) -> anyhow::Result<api::GetRecentBlockHashResponseV2> {
        let url = format!(
            "{}/api/v2/system/blockhash?offset={}",
            self.base_url, request.offset
        );

        println!("{}", url);

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        self.handle_response(response).await
    }

    pub async fn get_rate_limit(&self) -> anyhow::Result<api::GetRateLimitResponse> {
        let url = format!("{}/api/v2/rate-limit", self.base_url);

        println!("{}", url);

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        self.handle_response(response).await
    }

    pub async fn get_account_balance_v2(
        &self,
        request: GetAccountBalanceRequest,
    ) -> anyhow::Result<api::GetAccountBalanceResponse> {
        println!("here1");

        let url = format!(
            "{}/api/v2/balance?ownerAddress={}",
            self.base_url, request.owner_address
        );

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        self.handle_response(response).await
    }

    pub async fn get_priority_fee(
        &self,
        project: api::Project,
        percentile: Option<f64>,
    ) -> Result<api::GetPriorityFeeResponse> {
        let mut url = format!(
            "{}/api/v2/system/priority-fee?project={}",
            self.base_url, project as i32
        );
        if let Some(p) = percentile {
            url = format!(
                "{}/api/v2/system/priority-fee?project={}&percentile={}",
                self.base_url, project as i32, p
            );
        }

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        self.handle_response(response).await
    }

    pub async fn get_token_accounts(
        &self,
        owner_address: String,
    ) -> Result<api::GetTokenAccountsResponse> {
        let url = format!(
            "{}/api/v1/account/token-accounts?ownerAddress={}",
            self.base_url, owner_address
        );

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        self.handle_response(response).await
    }

    pub async fn get_account_balance(
        &self,
        owner_address: String,
    ) -> Result<api::GetAccountBalanceResponse> {
        let url = format!(
            "{}/api/v2/balance?ownerAddress={}",
            self.base_url, owner_address
        );

        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| anyhow!("HTTP GET request failed: {}", e))?;

        self.handle_response(response).await
    }
}