renegade_sdk/renegade_wallet_client/actions/
sync_account.rs

1//! Sync an account with onchain state
2
3use renegade_external_api::http::account::{
4    SYNC_ACCOUNT_ROUTE, SyncAccountRequest, SyncAccountResponse,
5};
6
7use crate::{
8    RenegadeClientError,
9    actions::{NON_BLOCKING_PARAM, construct_http_path},
10    client::RenegadeClient,
11    websocket::{DEFAULT_TASK_TIMEOUT, TaskWaiter},
12};
13
14// --- Public Actions --- //
15impl RenegadeClient {
16    /// Sync an account with onchain state. Awaits the completion of the sync
17    /// task before returning.
18    pub async fn sync_account(&self) -> Result<(), RenegadeClientError> {
19        let request = self.build_sync_account_request();
20
21        let path = self.build_sync_account_request_path(false)?;
22
23        self.relayer_client.post::<_, SyncAccountResponse>(&path, request).await?;
24
25        Ok(())
26    }
27
28    /// Enqueues a sync task in the relayer. Returns a `TaskWaiter` that can be
29    /// used to await task completion.
30    pub async fn enqueue_sync_account(&self) -> Result<TaskWaiter, RenegadeClientError> {
31        let request = self.build_sync_account_request();
32
33        let path = self.build_sync_account_request_path(true)?;
34
35        let SyncAccountResponse { task_id, .. } = self.relayer_client.post(&path, request).await?;
36
37        let task_waiter = self.watch_task(task_id, DEFAULT_TASK_TIMEOUT).await?;
38
39        Ok(task_waiter)
40    }
41}
42
43// --- Private Helpers --- //
44impl RenegadeClient {
45    /// Builds the sync account request
46    fn build_sync_account_request(&self) -> SyncAccountRequest {
47        SyncAccountRequest {
48            account_id: self.get_account_id(),
49            master_view_seed: self.get_master_view_seed(),
50            auth_hmac_key: self.get_auth_hmac_key().into(),
51            schnorr_public_key: self.get_schnorr_public_key(),
52        }
53    }
54
55    /// Builds the request path for the sync account endpoint
56    fn build_sync_account_request_path(
57        &self,
58        non_blocking: bool,
59    ) -> Result<String, RenegadeClientError> {
60        let path = construct_http_path!(SYNC_ACCOUNT_ROUTE, "account_id" => self.get_account_id());
61        let query_string =
62            serde_urlencoded::to_string(&[(NON_BLOCKING_PARAM, non_blocking.to_string())])
63                .map_err(RenegadeClientError::serde)?;
64
65        Ok(format!("{path}?{query_string}"))
66    }
67}