1use 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
11pub const SPOT_DEX: &str = "spot";
13pub const PERP_DEX: &str = "";
15
16#[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 pub const KEY: Signers = ActionKind::SendAsset.signers();
32
33 #[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 #[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 #[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 #[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#[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 pub const KEY: Signers = ActionKind::SpotSend.signers();
107
108 #[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#[derive(Debug, Clone, PartialEq, Eq)]
126pub struct UsdSend {
127 destination: String,
128 amount: Amount,
129 time: u64,
130}
131
132impl UsdSend {
133 pub const KEY: Signers = ActionKind::UsdSend.signers();
135
136 #[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#[derive(Debug, Clone, PartialEq, Eq)]
153pub struct UsdClassTransfer {
154 amount: Amount,
155 to_perp: bool,
156 nonce: u64,
157}
158
159impl UsdClassTransfer {
160 pub const KEY: Signers = ActionKind::UsdClassTransfer.signers();
162
163 #[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#[derive(Debug, Clone, PartialEq, Eq)]
181pub struct Withdraw3 {
182 destination: String,
183 amount: Amount,
184 time: u64,
185}
186
187impl Withdraw3 {
188 pub const KEY: Signers = ActionKind::Withdraw3.signers();
190
191 #[must_use]
193 pub fn new(destination: Address, amount: Amount, time: u64) -> Withdraw3 {
194 Withdraw3 { destination: destination.to_hex(), amount, time }
195 }
196
197 #[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#[derive(Debug, Clone, PartialEq, Eq)]
216#[non_exhaustive]
217pub enum SpotOut {
218 SpotSend(SpotSend),
220 SendAsset(SendAsset),
222}
223
224impl SpotOut {
225 pub const KEY: Signers = SpotSend::KEY;
227
228 #[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#[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 pub const KEY: Signers = ActionKind::AgentSendAsset.signers();
265
266 #[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 #[must_use]
289 pub const fn nonce(&self) -> u64 {
290 self.nonce
291 }
292
293 #[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;