Skip to main content

df_hyperliquid_core/transfer/send/
check.rs

1//! What the venue says about a recipient before a send (`preTransferCheck`): whether it is
2//! sanctioned, whether it exists, and the fee a first transfer to it pays.
3
4use crate::client::{ApiError, Transport, info, info_request};
5use crate::utils::{Address, Dec, Json};
6use serde::Deserialize;
7
8/// `{"type":"preTransferCheck","user":<recipient>,"source":<sender>}`.
9#[must_use]
10pub fn pre_transfer_check_request(recipient: Address, sender: Address) -> String {
11    info_request(
12        "preTransferCheck",
13        &[("user", Json::String(recipient.to_hex())), ("source", Json::String(sender.to_hex()))],
14    )
15}
16
17/// The venue's answer about a recipient. A fresh one costs an activation fee (live: `"1.0"`),
18/// charged to the sender on top of the amount as its own `accountActivationGas` ledger row.
19#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
20#[serde(rename_all = "camelCase")]
21#[non_exhaustive]
22pub struct PreTransferCheck {
23    /// A send to it is refused.
24    pub is_sanctioned: bool,
25    /// Whether the venue has seen the address before (`userExists`).
26    pub user_exists: bool,
27    /// The activation fee a first transfer to it pays, in USDC; zero when none.
28    pub fee: Dec,
29    /// Whether the address has sent a transaction of its own (`userHasSentTx`).
30    pub user_has_sent_tx: bool,
31}
32
33impl PreTransferCheck {
34    /// The fee a first transfer to this address pays, when there is one.
35    #[must_use]
36    pub fn activation_fee(&self) -> Option<Dec> {
37        self.fee.is_positive().then_some(self.fee)
38    }
39}
40
41/// Reads `preTransferCheck`; see [`pre_transfer_check_request`].
42pub async fn pre_transfer_check(
43    transport: &impl Transport,
44    recipient: Address,
45    sender: Address,
46) -> Result<PreTransferCheck, ApiError> {
47    info(transport, pre_transfer_check_request(recipient, sender)).await
48}
49
50#[cfg(test)]
51mod tests {
52    use std::collections::BTreeMap;
53
54    use super::*;
55
56    /// Both answers in `pre_transfer_check_mainnet.json`, live on mainnet, 2026-10-18.
57    #[test]
58    fn a_fresh_recipient_costs_a_fee_and_a_known_one_does_not() {
59        let recorded: BTreeMap<String, PreTransferCheck> = serde_json::from_str(include_str!(concat!(
60            env!("CARGO_MANIFEST_DIR"),
61            "/fixtures/transfer/pre_transfer_check_mainnet.json"
62        )))
63        .unwrap();
64        let fresh = &recorded["fresh"];
65        assert_eq!(
66            *fresh,
67            PreTransferCheck { is_sanctioned: false, user_exists: false, fee: Dec::ONE, user_has_sent_tx: false }
68        );
69        assert_eq!(fresh.activation_fee(), Some(Dec::ONE));
70        let known = &recorded["known"];
71        assert!(known.user_exists && known.user_has_sent_tx && !known.is_sanctioned);
72        assert_eq!(known.activation_fee(), None);
73    }
74}