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")]
21pub struct PreTransferCheck {
22    /// A send to it is refused.
23    pub is_sanctioned: bool,
24    /// Whether the venue has seen the address before (`userExists`).
25    pub user_exists: bool,
26    /// The activation fee a first transfer to it pays, in USDC; zero when none.
27    pub fee: Dec,
28    /// Whether the address has sent a transaction of its own (`userHasSentTx`).
29    pub user_has_sent_tx: bool,
30}
31
32impl PreTransferCheck {
33    /// The fee a first transfer to this address pays, when there is one.
34    #[must_use]
35    pub fn activation_fee(&self) -> Option<Dec> {
36        self.fee.is_positive().then_some(self.fee)
37    }
38}
39
40/// Reads `preTransferCheck`; see [`pre_transfer_check_request`].
41pub async fn pre_transfer_check(
42    transport: &impl Transport,
43    recipient: Address,
44    sender: Address,
45) -> Result<PreTransferCheck, ApiError> {
46    info(transport, pre_transfer_check_request(recipient, sender)).await
47}
48
49#[cfg(test)]
50mod tests {
51    use std::collections::BTreeMap;
52
53    use super::*;
54
55    /// Both answers in `pre_transfer_check_mainnet.json`, live on mainnet, 2026-10-18.
56    #[test]
57    fn a_fresh_recipient_costs_a_fee_and_a_known_one_does_not() {
58        let recorded: BTreeMap<String, PreTransferCheck> = serde_json::from_str(include_str!(concat!(
59            env!("CARGO_MANIFEST_DIR"),
60            "/fixtures/transfer/pre_transfer_check_mainnet.json"
61        )))
62        .unwrap();
63        let fresh = &recorded["fresh"];
64        assert_eq!(
65            *fresh,
66            PreTransferCheck { is_sanctioned: false, user_exists: false, fee: Dec::ONE, user_has_sent_tx: false }
67        );
68        assert_eq!(fresh.activation_fee(), Some(Dec::ONE));
69        let known = &recorded["known"];
70        assert!(known.user_exists && known.user_has_sent_tx && !known.is_sanctioned);
71        assert_eq!(known.activation_fee(), None);
72    }
73}