Skip to main content

hypercall/client/wallet_client/
withdrawals.rs

1use super::WalletClient;
2use hypercall_types::WalletAddress;
3use sonic_rs::{json, Value};
4
5impl WalletClient {
6    pub async fn withdraw_option_with_nonce(
7        &self,
8        base_url: &str,
9        account: WalletAddress,
10        symbol: &str,
11        amount: &str,
12        nonce: u64,
13    ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
14        let signature = self
15            .sign_withdraw_option(account, symbol, amount, nonce)
16            .await?;
17        let response = self
18            .http_client
19            .post(format!("{}/withdraw/option", base_url))
20            .json(&json!({
21                "wallet": self.wallet_address,
22                "account": account,
23                "symbol": symbol,
24                "amount": amount,
25                "nonce": nonce,
26                "signature": signature,
27            }))
28            .send()
29            .await?;
30
31        let status = response.status();
32        let body = response.text().await?;
33        let parsed = sonic_rs::from_str::<Value>(&body).unwrap_or_else(|_| json!({ "raw": body }));
34        if !status.is_success() {
35            return Err(format!("Failed to withdraw option: {} - {}", status, parsed).into());
36        }
37
38        Ok(parsed)
39    }
40
41    pub async fn withdraw_option(
42        &self,
43        base_url: &str,
44        account: WalletAddress,
45        symbol: &str,
46        amount: &str,
47    ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
48        let nonce = self.next_nonce();
49        self.withdraw_option_with_nonce(base_url, account, symbol, amount, nonce)
50            .await
51    }
52
53    pub async fn withdraw_usdc_with_nonce(
54        &self,
55        base_url: &str,
56        account: WalletAddress,
57        destination: WalletAddress,
58        amount: &str,
59        nonce: u64,
60    ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
61        let signature = self
62            .sign_withdraw_usdc(account, destination, amount, nonce)
63            .await?;
64        let response = self
65            .http_client
66            .post(format!("{}/withdraw/usdc", base_url))
67            .json(&json!({
68                "wallet": self.wallet_address,
69                "account": account,
70                "destination": destination,
71                "amount": amount,
72                "nonce": nonce,
73                "signature": signature,
74            }))
75            .send()
76            .await?;
77
78        let status = response.status();
79        let body = response.text().await?;
80        let parsed = sonic_rs::from_str::<Value>(&body).unwrap_or_else(|_| json!({ "raw": body }));
81        if !status.is_success() {
82            return Err(format!("Failed to withdraw USDC: {} - {}", status, parsed).into());
83        }
84
85        Ok(parsed)
86    }
87
88    pub async fn withdraw_usdc(
89        &self,
90        base_url: &str,
91        account: WalletAddress,
92        destination: WalletAddress,
93        amount: &str,
94    ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
95        let nonce = self.next_nonce();
96        self.withdraw_usdc_with_nonce(base_url, account, destination, amount, nonce)
97            .await
98    }
99}