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)]
216pub enum SpotOut {
217 SpotSend(SpotSend),
219 SendAsset(SendAsset),
221}
222
223impl SpotOut {
224 pub const KEY: Signers = SpotSend::KEY;
226
227 #[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#[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 pub const KEY: Signers = ActionKind::AgentSendAsset.signers();
264
265 #[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 #[must_use]
288 pub const fn nonce(&self) -> u64 {
289 self.nonce
290 }
291
292 #[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;