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
use crate::interop::Interop;
use crate::{TonCrypto, TonContracts, TonResult};
#[derive(Default, Serialize)]
#[serde(rename_all="camelCase")]
pub struct TonClientConfig {
pub default_workchain: Option<i32>,
pub base_url: Option<String>,
pub requests_url: Option<String>,
pub queries_url: Option<String>,
pub subscriptions_url: Option<String>,
}
pub struct TonClient {
context: u32,
pub crypto: TonCrypto,
pub contracts: TonContracts,
}
impl TonClient {
pub fn new(config: &TonClientConfig) -> TonResult<TonClient> {
let context = Interop::create_context();
let client = TonClient {
context,
crypto: TonCrypto::new(context),
contracts: TonContracts::new(context),
};
client.setup(config)?;
Ok(client)
}
pub fn new_with_base_url(base_url: &str) -> TonResult<TonClient> {
Self::new(&TonClientConfig {
base_url: Some(base_url.to_string()),
requests_url: None,
queries_url: None,
subscriptions_url: None,
default_workchain: Some(0)
})
}
pub fn default() -> TonResult<TonClient> {
Self::new(&TonClientConfig::default())
}
pub fn get_client_version(&self) -> String {
Interop::json_request_no_args(self.context, "version").unwrap()
}
pub fn setup(&self, config: &TonClientConfig) -> TonResult<()> {
Interop::json_request(self.context, "setup", config)
}
}
impl Drop for TonClient {
fn drop(&mut self) {
Interop::destroy_context(self.context);
}
}