Skip to main content

df_hyperliquid_core/transfer/send/
actions.rs

1//! The actions that move money, keys in the `@nktkas/hyperliquid` 0.33.2 order, amounts in normal
2//! form, addresses lowercase. The user-signed ones are signed by the account's own wallet only;
3//! [`AgentSendAsset`], an L1 action, by an agent for its master.
4
5use crate::account::{ActionKind, Keeping, Signers};
6use crate::client::Network;
7use crate::transfer::send::amount::Amount;
8use crate::utils::Address;
9use crate::utils::sign::{Field, L1Action, Map, NonceKey, UserAction, UserSigned};
10
11/// A balance as `sendAsset` names it: spot.
12pub const SPOT_DEX: &str = "spot";
13/// The main perp dex's balance, which `sendAsset` names by the empty string.
14pub const PERP_DEX: &str = "";
15
16/// `sendAsset`: any token between any two balances of any two accounts, and the one transfer a
17/// pooled account takes.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct SendAsset {
20    destination: String,
21    source_dex: String,
22    destination_dex: String,
23    token: String,
24    amount: Amount,
25    from_sub_account: String,
26    nonce: u64,
27}
28
29impl SendAsset {
30    /// User-signed: only the account's own key.
31    pub const KEY: Signers = ActionKind::SendAsset.signers();
32
33    /// `token` is `NAME:0x<tokenId>`, as `spotMeta` lists it.
34    #[must_use]
35    pub fn new(
36        destination: Address,
37        source_dex: &str,
38        destination_dex: &str,
39        token: &str,
40        amount: Amount,
41        nonce: u64,
42    ) -> SendAsset {
43        SendAsset {
44            destination: destination.to_hex(),
45            source_dex: source_dex.to_owned(),
46            destination_dex: destination_dex.to_owned(),
47            token: token.to_owned(),
48            amount,
49            from_sub_account: String::new(),
50            nonce,
51        }
52    }
53
54    /// USDC from one balance to another: to another account from spot (or,
55    /// on a standard account, from the perp balance), or to the account itself
56    /// to combine its two balances. USDC's token id is the network's own.
57    #[must_use]
58    pub fn usdc(
59        network: Network,
60        destination: Address,
61        source_dex: &str,
62        destination_dex: &str,
63        amount: Amount,
64        nonce: u64,
65    ) -> SendAsset {
66        SendAsset::new(destination, source_dex, destination_dex, network.usdc().wire, amount, nonce)
67    }
68
69    /// Sent from one of the account's subaccounts rather than the account.
70    #[must_use]
71    pub fn from_sub_account(mut self, sub_account: Address) -> SendAsset {
72        self.from_sub_account = sub_account.to_hex();
73        self
74    }
75
76    /// The amount, in normal form.
77    #[must_use]
78    pub const fn amount(&self) -> &Amount {
79        &self.amount
80    }
81}
82
83impl UserAction for SendAsset {
84    fn user_signed(&self) -> UserSigned<'_> {
85        UserSigned::new("sendAsset", "HyperliquidTransaction:SendAsset", NonceKey::Nonce, self.nonce)
86            .field("destination", Field::String(&self.destination))
87            .field("sourceDex", Field::String(&self.source_dex))
88            .field("destinationDex", Field::String(&self.destination_dex))
89            .field("token", Field::String(&self.token))
90            .field("amount", Field::String(self.amount.as_str()))
91            .field("fromSubAccount", Field::String(&self.from_sub_account))
92    }
93}
94
95/// `spotSend`: a spot token to another account. A pooled account refuses it.
96#[derive(Debug, Clone, PartialEq, Eq)]
97pub struct SpotSend {
98    destination: String,
99    token: String,
100    amount: Amount,
101    time: u64,
102}
103
104impl SpotSend {
105    /// User-signed: only the account's own key.
106    pub const KEY: Signers = ActionKind::SpotSend.signers();
107
108    /// `token` (`NAME:0x<tokenId>`) to `destination`, with `time` in milliseconds as its nonce.
109    #[must_use]
110    pub fn new(destination: Address, token: &str, amount: Amount, time: u64) -> SpotSend {
111        SpotSend { destination: destination.to_hex(), token: token.to_owned(), amount, time }
112    }
113}
114
115impl UserAction for SpotSend {
116    fn user_signed(&self) -> UserSigned<'_> {
117        UserSigned::new("spotSend", "HyperliquidTransaction:SpotSend", NonceKey::Time, self.time)
118            .field("destination", Field::String(&self.destination))
119            .field("token", Field::String(&self.token))
120            .field("amount", Field::String(self.amount.as_str()))
121    }
122}
123
124/// `usdSend`: USDC out of the perp balance to another account.
125#[derive(Debug, Clone, PartialEq, Eq)]
126pub struct UsdSend {
127    destination: String,
128    amount: Amount,
129    time: u64,
130}
131
132impl UsdSend {
133    /// User-signed: only the account's own key.
134    pub const KEY: Signers = ActionKind::UsdSend.signers();
135
136    /// USDC to `destination`, with `time` in milliseconds as its nonce.
137    #[must_use]
138    pub fn new(destination: Address, amount: Amount, time: u64) -> UsdSend {
139        UsdSend { destination: destination.to_hex(), amount, time }
140    }
141}
142
143impl UserAction for UsdSend {
144    fn user_signed(&self) -> UserSigned<'_> {
145        UserSigned::new("usdSend", "HyperliquidTransaction:UsdSend", NonceKey::Time, self.time)
146            .field("destination", Field::String(&self.destination))
147            .field("amount", Field::String(self.amount.as_str()))
148    }
149}
150
151/// `usdClassTransfer`: USDC between the account's own spot and perp balances.
152#[derive(Debug, Clone, PartialEq, Eq)]
153pub struct UsdClassTransfer {
154    amount: Amount,
155    to_perp: bool,
156    nonce: u64,
157}
158
159impl UsdClassTransfer {
160    /// User-signed: only the account's own key.
161    pub const KEY: Signers = ActionKind::UsdClassTransfer.signers();
162
163    /// USDC to the perp balance when `to_perp`, else to spot, signed at `nonce`.
164    #[must_use]
165    pub const fn new(amount: Amount, to_perp: bool, nonce: u64) -> UsdClassTransfer {
166        UsdClassTransfer { amount, to_perp, nonce }
167    }
168}
169
170impl UserAction for UsdClassTransfer {
171    fn user_signed(&self) -> UserSigned<'_> {
172        UserSigned::new("usdClassTransfer", "HyperliquidTransaction:UsdClassTransfer", NonceKey::Nonce, self.nonce)
173            .field("amount", Field::String(self.amount.as_str()))
174            .field("toPerp", Field::Bool(self.to_perp))
175    }
176}
177
178/// `withdraw3`: USDC out of the perp balance to an address on Arbitrum. The venue takes its fee
179/// (1 USDC in live `withdraw` rows) out of the amount.
180#[derive(Debug, Clone, PartialEq, Eq)]
181pub struct Withdraw3 {
182    destination: String,
183    amount: Amount,
184    time: u64,
185}
186
187impl Withdraw3 {
188    /// User-signed: only the account's own key.
189    pub const KEY: Signers = ActionKind::Withdraw3.signers();
190
191    /// USDC to `destination` on Arbitrum, with `time` in milliseconds as its nonce.
192    #[must_use]
193    pub fn new(destination: Address, amount: Amount, time: u64) -> Withdraw3 {
194        Withdraw3 { destination: destination.to_hex(), amount, time }
195    }
196
197    /// The amount, in normal form, the venue's fee included.
198    #[must_use]
199    pub const fn amount(&self) -> &Amount {
200        &self.amount
201    }
202}
203
204impl UserAction for Withdraw3 {
205    fn user_signed(&self) -> UserSigned<'_> {
206        UserSigned::new("withdraw3", "HyperliquidTransaction:Withdraw", NonceKey::Time, self.time)
207            .field("destination", Field::String(&self.destination))
208            .field("amount", Field::String(self.amount.as_str()))
209    }
210}
211
212/// A spot token out of the account: `spotSend` for a separate account, and `sendAsset` from spot
213/// to spot for a pooled one (which refuses `spotSend`) or one whose mode is not known (every kind
214/// takes it). Either goes to a person, or through a system address to HyperEVM.
215#[derive(Debug, Clone, PartialEq, Eq)]
216#[non_exhaustive]
217pub enum SpotOut {
218    /// `spotSend`, for an account that keeps spot apart.
219    SpotSend(SpotSend),
220    /// `sendAsset` from spot to spot, for a pooled account or one whose mode is not known.
221    SendAsset(SendAsset),
222}
223
224impl SpotOut {
225    /// User-signed: only the account's own key.
226    pub const KEY: Signers = SpotSend::KEY;
227
228    /// `token` is `NAME:0x<tokenId>`.
229    #[must_use]
230    pub fn new(destination: Address, token: &str, amount: Amount, keeping: Keeping, nonce: u64) -> SpotOut {
231        match keeping {
232            Keeping::Apart | Keeping::Native => SpotOut::SpotSend(SpotSend::new(destination, token, amount, nonce)),
233            Keeping::Unified | Keeping::Portfolio | Keeping::Unknown => {
234                SpotOut::SendAsset(SendAsset::new(destination, SPOT_DEX, SPOT_DEX, token, amount, nonce))
235            }
236        }
237    }
238}
239
240impl UserAction for SpotOut {
241    fn user_signed(&self) -> UserSigned<'_> {
242        match self {
243            SpotOut::SpotSend(send) => send.user_signed(),
244            SpotOut::SendAsset(send) => send.user_signed(),
245        }
246    }
247}
248
249/// `agentSendAsset`: `sendAsset`'s fields as an L1 action, which an agent signs for its master.
250/// The destination is the account itself, so it only moves between the account's own balances.
251/// The action carries its nonce and is signed with that same nonce.
252#[derive(Debug, Clone, PartialEq, Eq)]
253pub struct AgentSendAsset {
254    account: Address,
255    source_dex: String,
256    destination_dex: String,
257    token: String,
258    amount: Amount,
259    nonce: u64,
260}
261
262impl AgentSendAsset {
263    /// L1: an agent the account approved, or the account's own key.
264    pub const KEY: Signers = ActionKind::AgentSendAsset.signers();
265
266    /// From `source_dex` to `destination_dex` of `account`, which is also the
267    /// destination. `token` is `NAME:0x<tokenId>`.
268    #[must_use]
269    pub fn new(
270        account: Address,
271        source_dex: &str,
272        destination_dex: &str,
273        token: &str,
274        amount: Amount,
275        nonce: u64,
276    ) -> AgentSendAsset {
277        AgentSendAsset {
278            account,
279            source_dex: source_dex.to_owned(),
280            destination_dex: destination_dex.to_owned(),
281            token: token.to_owned(),
282            amount,
283            nonce,
284        }
285    }
286
287    /// The nonce inside the action, which is also the one it is signed with.
288    #[must_use]
289    pub const fn nonce(&self) -> u64 {
290        self.nonce
291    }
292
293    /// The amount, in normal form.
294    #[must_use]
295    pub const fn amount(&self) -> &Amount {
296        &self.amount
297    }
298}
299
300impl L1Action for AgentSendAsset {
301    fn action(&self) -> Map {
302        Map::new()
303            .put("type", "agentSendAsset")
304            .put("destination", self.account.to_hex())
305            .put("sourceDex", self.source_dex.clone())
306            .put("destinationDex", self.destination_dex.clone())
307            .put("token", self.token.clone())
308            .put("amount", self.amount.as_str().to_owned())
309            .put("fromSubAccount", "")
310            .put("nonce", self.nonce)
311    }
312}
313
314#[cfg(test)]
315#[path = "tests/actions.rs"]
316mod tests;