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
use crate::client::{Client, ClientType, CryptoClient};
use crate::types::{ClientOptions, SyncItem, SyncStats};
use async_std::sync::Arc;
use async_trait::async_trait;
#[derive(Clone)]
pub struct SiacoinClient {
url: String,
}
pub fn get_client(client_type: ClientType, options: ClientOptions) -> crate::Result<Box<Client>> {
match client_type {
ClientType::HTTP => Ok(Box::new(SiacoinClient::new(options))),
_ => Err(crate::error::ClientError::UnsupportedType(client_type)),
}
}
impl SiacoinClient {
pub fn new(options: ClientOptions) -> Self {
SiacoinClient {
url: options.url.clone(),
}
}
}
#[async_trait]
impl CryptoClient for SiacoinClient {
async fn sync_stats(&self) -> SyncStats {
let result: serde_json::Value = match surf::get(&self.url).recv_json().await {
Ok(r) => r,
Err(_e) => {
async_std::task::sleep(std::time::Duration::from_secs(60)).await;
return SyncStats {
current_block: 0,
syncing: true,
sync_item: SyncItem::VerificationProgress,
estimated_sync_item_remaining: 0.0,
};
}
};
let syncing = result
.as_object()
.unwrap()
.get("synced")
.unwrap()
.as_bool()
.unwrap();
let height = result
.as_object()
.unwrap()
.get("height")
.unwrap()
.as_u64()
.unwrap();
SyncStats {
current_block: height,
syncing,
sync_item: SyncItem::VerificationProgress,
estimated_sync_item_remaining: 0.0,
}
}
fn client_type(&self) -> ClientType {
ClientType::HTTP
}
}