Skip to main content

df_papertrade_core/trade/writes/
withdraw.rs

1//! Withdrawing USDC to the wallet's own HyperCore spot account, signed by the wallet.
2
3use df_hyperliquid_core::client::Http;
4
5use crate::account::Account;
6use crate::account::store::Store;
7use crate::account::wallet::{Purpose, Wallet};
8use crate::client::{Accepted, Client, Error};
9use crate::trade::{Intent, WithdrawToCore, deadline};
10use crate::utils::Lane;
11
12impl<H: Http + Clone> Client<H> {
13    /// Withdraws `usdc` (6 decimals, at least 10 USDC) of the available balance to the wallet's
14    /// own HyperCore spot account, never anywhere else. The wallet signs.
15    pub async fn withdraw(
16        &self,
17        store: &impl Store,
18        wallet: &impl Wallet,
19        account: Option<&Account>,
20        usdc: u128,
21    ) -> Result<Accepted, Error> {
22        let owner = wallet.address();
23        if let Some(account) = account
24            && usdc.saturating_mul(crate::utils::units::COLLATERAL_PER_USDC) > account.funds.available_balance
25        {
26            return Err(Error::InsufficientFunds("more than the available balance".into()));
27        }
28        WithdrawToCore::check(usdc)?;
29        let nonce = self.take_nonce(store, owner, Lane::Exchange).await?;
30        let intent = WithdrawToCore::new(owner, usdc, nonce, deadline(self.now_ms()))?;
31        let signed = self.wallet_sign(wallet, owner, Intent::WithdrawToCore(intent), Purpose::Withdraw).await?;
32        self.submit(&signed).await
33    }
34}