Skip to main content

df_hyperliquid_core/client/http/
body.rs

1//! The `/exchange` body, keys in the official Python SDK's order. The venue
2//! reads it as JSON, so only the action's own key order reaches a hash.
3
4use crate::account::Signature;
5use crate::utils::address::Address;
6use crate::utils::json::Json;
7use crate::utils::sign::msgpack::Map;
8
9/// `{"action":..,"nonce":..,"signature":{r,s,v}}`, for a user-signed action.
10#[must_use]
11pub fn user_signed(action: &Map, nonce: u64, signature: &Signature) -> String {
12    Json::Object(vec![
13        ("action".to_owned(), action.to_json()),
14        ("nonce".to_owned(), Json::Number(nonce.to_string())),
15        ("signature".to_owned(), signature.json()),
16    ])
17    .compact()
18}
19
20/// [`user_signed`] plus `"vaultAddress"`, an explicit `null` unless the action
21/// acts for a vault or subaccount, for an L1 action.
22#[must_use]
23pub fn l1(action: &Map, nonce: u64, signature: &Signature, vault: Option<Address>) -> String {
24    Json::Object(vec![
25        ("action".to_owned(), action.to_json()),
26        ("nonce".to_owned(), Json::Number(nonce.to_string())),
27        ("signature".to_owned(), signature.json()),
28        ("vaultAddress".to_owned(), vault.map_or(Json::Null, |vault| Json::String(vault.to_hex()))),
29    ])
30    .compact()
31}