df_hyperliquid_core/transfer/
evm.rs1use crate::account::{ActionKind, Signers};
9use crate::client::evm::tx::Call;
10use crate::client::evm::u256::{BadAmount, NATIVE_DECIMALS, U256};
11use crate::transfer::erc20;
12use crate::transfer::send::Amount;
13use crate::utils::sign::{Field, NonceKey, UserAction, UserSigned};
14use crate::utils::{Address, Dec};
15
16pub const HYPE_SYSTEM: Address = Address::from_bytes([0x22; 20]);
18
19#[must_use]
22pub fn system_address(index: u64) -> Address {
23 let mut bytes = [0u8; 20];
24 bytes[0] = 0x20;
25 bytes[12..].copy_from_slice(&index.to_be_bytes());
26 Address::from_bytes(bytes)
27}
28
29#[must_use]
32pub fn is_system_address(address: Address) -> bool {
33 address == HYPE_SYSTEM || system_token(address).is_some()
34}
35
36#[must_use]
39pub fn system_token(address: Address) -> Option<u64> {
40 let bytes = address.bytes();
41 if bytes[0] != 0x20 || bytes[1..12].iter().any(|byte| *byte != 0) {
42 return None;
43 }
44 let mut index = [0u8; 8];
45 index.copy_from_slice(&bytes[12..]);
46 Some(u64::from_be_bytes(index))
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct Linked {
54 pub index: u64,
56 pub contract: Option<Address>,
58 pub wei_decimals: u32,
60 pub evm_extra_wei_decimals: i32,
62}
63
64impl Linked {
65 #[must_use]
67 pub const fn hype(wei_decimals: u32) -> Linked {
68 Linked { index: 0, contract: None, wei_decimals, evm_extra_wei_decimals: 0 }
69 }
70
71 #[must_use]
73 pub const fn erc20(index: u64, contract: Address, wei_decimals: u32, evm_extra_wei_decimals: i32) -> Linked {
74 Linked { index, contract: Some(contract), wei_decimals, evm_extra_wei_decimals }
75 }
76
77 #[must_use]
80 pub fn system(&self) -> Address {
81 match self.contract {
82 None => HYPE_SYSTEM,
83 Some(_) => system_address(self.index),
84 }
85 }
86
87 #[must_use]
89 pub fn evm_decimals(&self) -> Option<u32> {
90 match self.contract {
91 None => Some(NATIVE_DECIMALS),
92 Some(_) => u32::try_from(i64::from(self.wei_decimals) + i64::from(self.evm_extra_wei_decimals)).ok(),
93 }
94 }
95
96 #[must_use]
99 pub fn core_places(&self) -> Option<u32> {
100 self.evm_decimals().map(|evm| evm.min(self.wei_decimals))
101 }
102
103 pub fn to_core(&self, amount: Dec) -> Result<Call, BadAmount> {
107 let places = self.core_places().ok_or(BadAmount::TooLarge)?;
108 let decimals = self.evm_decimals().ok_or(BadAmount::TooLarge)?;
109 if !amount.is_positive() {
110 return Err(BadAmount::NotPositive(amount));
111 }
112 if amount.places() > places {
113 return Err(BadAmount::TooFine { value: amount, decimals: places });
114 }
115 let units = U256::from_dec(amount, decimals)?;
116 Ok(match self.contract {
117 None => Call { to: Some(HYPE_SYSTEM), value: units, data: Vec::new() },
118 Some(contract) => {
119 Call { to: Some(contract), value: U256::ZERO, data: erc20::transfer(self.system(), units) }
120 }
121 })
122 }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127#[non_exhaustive]
128pub enum AddressEncoding {
129 Hex,
131 Base58,
133}
134
135impl AddressEncoding {
136 #[must_use]
138 pub const fn wire(self) -> &'static str {
139 match self {
140 AddressEncoding::Hex => "hex",
141 AddressEncoding::Base58 => "base58",
142 }
143 }
144}
145
146#[derive(Debug, Clone, PartialEq, Eq)]
149pub struct Destination {
150 recipient: String,
151 encoding: AddressEncoding,
152 chain_id: u32,
153}
154
155impl Destination {
156 #[must_use]
158 pub fn evm(recipient: Address, chain_id: u32) -> Destination {
159 Destination { recipient: recipient.to_hex(), encoding: AddressEncoding::Hex, chain_id }
160 }
161
162 #[must_use]
164 pub fn base58(recipient: &str, chain_id: u32) -> Destination {
165 Destination { recipient: recipient.to_owned(), encoding: AddressEncoding::Base58, chain_id }
166 }
167
168 #[must_use]
170 pub fn recipient(&self) -> &str {
171 &self.recipient
172 }
173
174 #[must_use]
176 pub const fn encoding(&self) -> AddressEncoding {
177 self.encoding
178 }
179
180 #[must_use]
182 pub const fn chain_id(&self) -> u32 {
183 self.chain_id
184 }
185}
186
187#[derive(Debug, Clone, PartialEq, Eq)]
194pub struct SendToEvmWithData {
195 token: String,
196 amount: Amount,
197 source_dex: String,
198 destination: Destination,
199 gas_limit: u64,
200 data: Vec<u8>,
201 nonce: u64,
202}
203
204impl SendToEvmWithData {
205 pub const KEY: Signers = ActionKind::SendToEvmWithData.signers();
207
208 #[must_use]
212 pub fn new(
213 token: &str,
214 amount: Amount,
215 source_dex: &str,
216 destination: Destination,
217 gas_limit: u64,
218 data: Vec<u8>,
219 nonce: u64,
220 ) -> SendToEvmWithData {
221 SendToEvmWithData {
222 token: token.to_owned(),
223 amount,
224 source_dex: source_dex.to_owned(),
225 destination,
226 gas_limit,
227 data,
228 nonce,
229 }
230 }
231
232 #[must_use]
234 pub const fn amount(&self) -> &Amount {
235 &self.amount
236 }
237
238 #[must_use]
240 pub const fn destination(&self) -> &Destination {
241 &self.destination
242 }
243
244 #[must_use]
246 pub fn data(&self) -> &[u8] {
247 &self.data
248 }
249}
250
251impl UserAction for SendToEvmWithData {
252 fn user_signed(&self) -> UserSigned<'_> {
253 UserSigned::new("sendToEvmWithData", "HyperliquidTransaction:SendToEvmWithData", NonceKey::Nonce, self.nonce)
254 .field("token", Field::String(&self.token))
255 .field("amount", Field::String(self.amount.as_str()))
256 .field("sourceDex", Field::String(&self.source_dex))
257 .field("destinationRecipient", Field::String(&self.destination.recipient))
258 .field("addressEncoding", Field::String(self.destination.encoding.wire()))
259 .field("destinationChainId", Field::Uint32(self.destination.chain_id))
260 .field("gasLimit", Field::Uint64(self.gas_limit))
261 .field("data", Field::Bytes(&self.data))
262 }
263}
264
265#[cfg(test)]
266#[path = "tests/evm.rs"]
267mod tests;