Skip to main content

m10_sdk/block_explorer/
mod.rs

1use serde::Deserialize;
2
3pub struct BlockExplorerClient {
4    address: String,
5    block_explorer_client: reqwest::Client,
6}
7
8#[derive(Debug, Deserialize)]
9pub struct AuditLog {
10    pub tx_id: String,
11    pub action: String,
12    pub entity_type: String,
13    pub status: u8,
14    pub public_key: String,
15    pub timestamp: String,
16}
17
18#[derive(Debug, Deserialize)]
19struct AuditLogResponse {
20    data: AuditLog,
21}
22
23// TODO add query params
24impl BlockExplorerClient {
25    pub fn new(address: impl Into<String>) -> Self {
26        let address = address.into();
27        let block_explorer_client = reqwest::Client::new();
28        Self {
29            address,
30            block_explorer_client,
31        }
32    }
33
34    pub async fn get_block(&mut self, block_id: &str) -> reqwest::Result<String> {
35        let url = format!("{}block-explorer/api/v1/blocks/{}", self.address, block_id);
36        let block = self
37            .block_explorer_client
38            .get(&url)
39            .send()
40            .await?
41            .error_for_status()?
42            .text()
43            .await?;
44        Ok(block)
45    }
46
47    pub async fn get_account_roles(&mut self, account_role_id: &str) -> reqwest::Result<String> {
48        let url = format!(
49            "{}block-explorer/api/v1/account_roles/{}",
50            self.address, account_role_id
51        );
52        let account_roles = self
53            .block_explorer_client
54            .get(&url)
55            .send()
56            .await?
57            .error_for_status()?
58            .text()
59            .await?;
60        Ok(account_roles)
61    }
62
63    pub async fn get_account_stats(&mut self, account_id: &str) -> reqwest::Result<String> {
64        let url = format!(
65            "{}block-explorer/api/v1/account_stats/{}",
66            self.address, account_id
67        );
68        let account_stats = self
69            .block_explorer_client
70            .get(&url)
71            .send()
72            .await?
73            .error_for_status()?
74            .text()
75            .await?;
76        Ok(account_stats)
77    }
78
79    pub async fn get_account(&mut self, account_id: &str) -> reqwest::Result<String> {
80        let url = format!(
81            "{}block-explorer/api/v1/accounts/{}",
82            self.address, account_id
83        );
84        let account = self
85            .block_explorer_client
86            .get(&url)
87            .send()
88            .await?
89            .error_for_status()?
90            .text()
91            .await?;
92        Ok(account)
93    }
94
95    pub async fn get_assets(&mut self) -> reqwest::Result<String> {
96        let url = format!("{}block-explorer/api/v1/assets", self.address);
97        let assets = self
98            .block_explorer_client
99            .get(&url)
100            .send()
101            .await?
102            .error_for_status()?
103            .text()
104            .await?;
105        Ok(assets)
106    }
107
108    pub async fn get_audit_log(&mut self, tx_id: &str) -> reqwest::Result<AuditLog> {
109        let url = format!("{}block-explorer/api/v1/audit_log/{}", self.address, tx_id);
110        let response = self
111            .block_explorer_client
112            .get(&url)
113            .send()
114            .await?
115            .error_for_status()?
116            .json::<AuditLogResponse>()
117            .await?;
118        Ok(response.data)
119    }
120
121    pub async fn get_blocks(&mut self) -> reqwest::Result<String> {
122        let url = format!("{}block-explorer/api/v1/blocks", self.address);
123        let blocks = self
124            .block_explorer_client
125            .get(&url)
126            .send()
127            .await?
128            .error_for_status()?
129            .text()
130            .await?;
131        Ok(blocks)
132    }
133
134    pub async fn get_existing_assets(&mut self) -> reqwest::Result<String> {
135        let url = format!("{}block-explorer/api/v1/existing_assets", self.address);
136        let existing_assets = self
137            .block_explorer_client
138            .get(&url)
139            .send()
140            .await?
141            .error_for_status()?
142            .text()
143            .await?;
144        Ok(existing_assets)
145    }
146
147    pub async fn get_health(&mut self) -> reqwest::Result<String> {
148        let url = format!("{}block-explorer/api/v1/health", self.address);
149        let health = self
150            .block_explorer_client
151            .get(&url)
152            .send()
153            .await?
154            .error_for_status()?
155            .text()
156            .await?;
157        Ok(health)
158    }
159
160    pub async fn get_histogram(&mut self) -> reqwest::Result<String> {
161        let url = format!("{}block-explorer/api/v1/histogram", self.address);
162        let histogram = self
163            .block_explorer_client
164            .get(&url)
165            .send()
166            .await?
167            .error_for_status()?
168            .text()
169            .await?;
170        Ok(histogram)
171    }
172
173    pub async fn get_metrics(&mut self) -> reqwest::Result<String> {
174        let url = format!("{}block-explorer/api/v1/metrics", self.address);
175        let metrics = self
176            .block_explorer_client
177            .get(&url)
178            .send()
179            .await?
180            .error_for_status()?
181            .text()
182            .await?;
183        Ok(metrics)
184    }
185
186    pub async fn get_transaction(&mut self, transaction_id: &str) -> reqwest::Result<String> {
187        let url = format!(
188            "{}block-explorer/api/v1/transactions/{}",
189            self.address, transaction_id
190        );
191        let transaction = self
192            .block_explorer_client
193            .get(&url)
194            .send()
195            .await?
196            .error_for_status()?
197            .text()
198            .await?;
199        Ok(transaction)
200    }
201
202    pub async fn get_transfer(&mut self, transfer_id: &str) -> reqwest::Result<String> {
203        let url = format!(
204            "{}block-explorer/api/v1/transfers/{}",
205            self.address, transfer_id
206        );
207        let transfer = self
208            .block_explorer_client
209            .get(&url)
210            .send()
211            .await?
212            .error_for_status()?
213            .text()
214            .await?;
215        Ok(transfer)
216    }
217}