hypercall/client/wallet_client/
margin.rs1use super::WalletClient;
2use sonic_rs::{json, Value};
3
4impl WalletClient {
5 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 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}