gevulot_rs/
gevulot_client.rs1use crate::base_client::BaseClient;
2use crate::error::Result;
3use crate::gov_client::GovClient;
4use crate::pin_client::PinClient;
5use crate::sudo_client::SudoClient;
6use crate::task_client::TaskClient;
7use crate::worker_client::WorkerClient;
8use crate::workflow_client::WorkflowClient;
9use std::sync::Arc;
10use tokio::sync::RwLock;
11
12#[derive(Debug, Clone)]
18pub struct GevulotClient {
19 pub pins: PinClient,
20 pub tasks: TaskClient,
21 pub workflows: WorkflowClient,
22 pub workers: WorkerClient,
23 pub gov: GovClient,
24 pub sudo: SudoClient,
25 pub base_client: Arc<RwLock<BaseClient>>,
27}
28
29pub struct GevulotClientBuilder {
31 endpoint: String,
32 gas_price: f64,
33 gas_multiplier: f64,
34 mnemonic: Option<String>,
35 password: Option<String>,
36}
37
38impl Default for GevulotClientBuilder {
39 fn default() -> Self {
41 Self {
42 endpoint: "http://127.0.0.1:9090".to_string(),
43 gas_price: 0.025,
44 gas_multiplier: 1.2,
45 mnemonic: None,
46 password: None,
47 }
48 }
49}
50
51impl GevulotClientBuilder {
52 pub fn new() -> Self {
54 Self::default()
55 }
56
57 pub fn endpoint(mut self, endpoint: &str) -> Self {
59 self.endpoint = endpoint.to_string();
60 self
61 }
62
63 pub fn gas_price(mut self, gas_price: f64) -> Self {
65 self.gas_price = gas_price;
66 self
67 }
68
69 pub fn gas_multiplier(mut self, gas_multiplier: f64) -> Self {
71 self.gas_multiplier = gas_multiplier;
72 self
73 }
74
75 pub fn mnemonic(mut self, mnemonic: &str) -> Self {
77 self.mnemonic = Some(mnemonic.to_string());
78 self
79 }
80
81 pub fn password(mut self, password: &str) -> Self {
83 self.password = Some(password.to_string());
84 self
85 }
86
87 pub async fn build(self) -> Result<GevulotClient> {
89 let base_client = Arc::new(RwLock::new(
91 BaseClient::new(&self.endpoint, self.gas_price, self.gas_multiplier).await?,
92 ));
93
94 if let Some(mnemonic) = self.mnemonic {
96 base_client
97 .write()
98 .await
99 .set_mnemonic(&mnemonic, self.password.as_deref())?;
100 }
101
102 Ok(GevulotClient {
104 pins: PinClient::new(base_client.clone()),
105 tasks: TaskClient::new(base_client.clone()),
106 workflows: WorkflowClient::new(base_client.clone()),
107 workers: WorkerClient::new(base_client.clone()),
108 gov: GovClient::new(base_client.clone()),
109 sudo: SudoClient::new(base_client.clone()),
110 base_client,
111 })
112 }
113}