gevulot_rs/
gevulot_client.rs

1use 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/// GevulotClient exposes all gevulot specific functionality
13/// * pins
14/// * tasks
15/// * workers
16/// * workflows
17#[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    // raw access to base functionality so we don't lock out ourselves
26    pub base_client: Arc<RwLock<BaseClient>>,
27}
28
29/// Builder for GevulotClient
30pub 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    /// Provides default values for GevulotClientBuilder
40    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    /// Creates a new GevulotClientBuilder with default values
53    pub fn new() -> Self {
54        Self::default()
55    }
56
57    /// Sets the endpoint for the GevulotClient
58    pub fn endpoint(mut self, endpoint: &str) -> Self {
59        self.endpoint = endpoint.to_string();
60        self
61    }
62
63    /// Sets the gas price for the GevulotClient
64    pub fn gas_price(mut self, gas_price: f64) -> Self {
65        self.gas_price = gas_price;
66        self
67    }
68
69    /// Sets the gas multiplier for the GevulotClient
70    pub fn gas_multiplier(mut self, gas_multiplier: f64) -> Self {
71        self.gas_multiplier = gas_multiplier;
72        self
73    }
74
75    /// Sets the mnemonic for the GevulotClient
76    pub fn mnemonic(mut self, mnemonic: &str) -> Self {
77        self.mnemonic = Some(mnemonic.to_string());
78        self
79    }
80
81    /// Sets the password for the GevulotClient
82    pub fn password(mut self, password: &str) -> Self {
83        self.password = Some(password.to_string());
84        self
85    }
86
87    /// Builds the GevulotClient with the provided configuration
88    pub async fn build(self) -> Result<GevulotClient> {
89        // Create a new BaseClient with the provided endpoint, gas price, and gas multiplier
90        let base_client = Arc::new(RwLock::new(
91            BaseClient::new(&self.endpoint, self.gas_price, self.gas_multiplier).await?,
92        ));
93
94        // If a mnemonic is provided, set it in the BaseClient
95        if let Some(mnemonic) = self.mnemonic {
96            base_client
97                .write()
98                .await
99                .set_mnemonic(&mnemonic, self.password.as_deref())?;
100        }
101
102        // Create and return the GevulotClient with the initialized clients
103        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}