Skip to main content

hypercall/client/wallet_client/
margin.rs

1use super::WalletClient;
2use sonic_rs::{json, Value};
3
4impl WalletClient {
5    /// Set margin mode for the wallet
6    pub async fn set_margin_mode(
7        &self,
8        base_url: &str,
9        margin_mode: &str,
10    ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
11        let nonce = self.next_nonce();
12
13        let signature = self.sign_set_margin_mode(margin_mode, nonce).await?;
14
15        let response = self
16            .http_client
17            .post(format!("{}/margin-mode", base_url))
18            .json(&json!({
19                "wallet": self.wallet_address,
20                "margin_mode": margin_mode,
21                "nonce": nonce,
22                "signature": signature,
23            }))
24            .send()
25            .await?;
26
27        let status = response.status();
28        let body = response.text().await?;
29
30        if !status.is_success() && status != reqwest::StatusCode::OK {
31            // Still parse the body as it may contain useful error info
32            if let Ok(json) = sonic_rs::from_str::<Value>(&body) {
33                return Ok(json);
34            }
35            return Err(format!("Failed to set margin mode: {} - {}", status, body).into());
36        }
37
38        Ok(sonic_rs::from_str(&body)?)
39    }
40}