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)]
216pub enum SpotOut {
217    /// `spotSend`, for an account that keeps spot apart.
218    SpotSend(SpotSend),
219    /// `sendAsset` from spot to spot, for a pooled account or one whose mode is not known.
220    SendAsset(SendAsset),
221}
222
223impl SpotOut {
224    /// User-signed: only the account's own key.
225    pub const KEY: Signers = SpotSend::KEY;
226
227    /// `token` is `NAME:0x<tokenId>`.
228    #[must_use]
229    pub fn new(destination: Address, token: &str, amount: Amount, keeping: Keeping, nonce: u64) -> SpotOut {
230        match keeping {
231            Keeping::Apart | Keeping::Native => SpotOut::SpotSend(SpotSend::new(destination, token, amount, nonce)),
232            Keeping::Unified | Keeping::Portfolio | Keeping::Unknown => {
233                SpotOut::SendAsset(SendAsset::new(destination, SPOT_DEX, SPOT_DEX, token, amount, nonce))
234            }
235        }
236    }
237}
238
239impl UserAction for SpotOut {
240    fn user_signed(&self) -> UserSigned<'_> {
241        match self {
242            SpotOut::SpotSend(send) => send.user_signed(),
243            SpotOut::SendAsset(send) => send.user_signed(),
244        }
245    }
246}
247
248/// `agentSendAsset`: `sendAsset`'s fields as an L1 action, which an agent signs for its master.
249/// The destination is the account itself, so it only moves between the account's own balances.
250/// The action carries its nonce and is signed with that same nonce.
251#[derive(Debug, Clone, PartialEq, Eq)]
252pub struct AgentSendAsset {
253    account: Address,
254    source_dex: String,
255    destination_dex: String,
256    token: String,
257    amount: Amount,
258    nonce: u64,
259}
260
261impl AgentSendAsset {
262    /// L1: an agent the account approved, or the account's own key.
263    pub const KEY: Signers = ActionKind::AgentSendAsset.signers();
264
265    /// From `source_dex` to `destination_dex` of `account`, which is also the
266    /// destination. `token` is `NAME:0x<tokenId>`.
267    #[must_use]
268    pub fn new(
269        account: Address,
270        source_dex: &str,
271        destination_dex: &str,
272        token: &str,
273        amount: Amount,
274        nonce: u64,
275    ) -> AgentSendAsset {
276        AgentSendAsset {
277            account,
278            source_dex: source_dex.to_owned(),
279            destination_dex: destination_dex.to_owned(),
280            token: token.to_owned(),
281            amount,
282            nonce,
283        }
284    }
285
286    /// The nonce inside the action, which is also the one it is signed with.
287    #[must_use]
288    pub const fn nonce(&self) -> u64 {
289        self.nonce
290    }
291
292    /// The amount, in normal form.
293    #[must_use]
294    pub const fn amount(&self) -> &Amount {
295        &self.amount
296    }
297}
298
299impl L1Action for AgentSendAsset {
300    fn action(&self) -> Map {
301        Map::new()
302            .put("type", "agentSendAsset")
303            .put("destination", self.account.to_hex())
304            .put("sourceDex", self.source_dex.clone())
305            .put("destinationDex", self.destination_dex.clone())
306            .put("token", self.token.clone())
307            .put("amount", self.amount.as_str().to_owned())
308            .put("fromSubAccount", "")
309            .put("nonce", self.nonce)
310    }
311}
312
313#[cfg(test)]
314#[path = "tests/actions.rs"]
315mod tests;