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)]
52pub struct Linked {
53 pub index: u64,
55 pub contract: Option<Address>,
57 pub wei_decimals: u32,
59 pub evm_extra_wei_decimals: i32,
61}
62
63impl Linked {
64 #[must_use]
66 pub const fn hype(wei_decimals: u32) -> Linked {
67 Linked { index: 0, contract: None, wei_decimals, evm_extra_wei_decimals: 0 }
68 }
69
70 #[must_use]
72 pub const fn erc20(index: u64, contract: Address, wei_decimals: u32, evm_extra_wei_decimals: i32) -> Linked {
73 Linked { index, contract: Some(contract), wei_decimals, evm_extra_wei_decimals }
74 }
75
76 #[must_use]
79 pub fn system(&self) -> Address {
80 match self.contract {
81 None => HYPE_SYSTEM,
82 Some(_) => system_address(self.index),
83 }
84 }
85
86 #[must_use]
88 pub fn evm_decimals(&self) -> Option<u32> {
89 match self.contract {
90 None => Some(NATIVE_DECIMALS),
91 Some(_) => u32::try_from(i64::from(self.wei_decimals) + i64::from(self.evm_extra_wei_decimals)).ok(),
92 }
93 }
94
95 #[must_use]
98 pub fn core_places(&self) -> Option<u32> {
99 self.evm_decimals().map(|evm| evm.min(self.wei_decimals))
100 }
101
102 pub fn to_core(&self, amount: Dec) -> Result<Call, BadAmount> {
106 let places = self.core_places().ok_or(BadAmount::TooLarge)?;
107 let decimals = self.evm_decimals().ok_or(BadAmount::TooLarge)?;
108 if !amount.is_positive() {
109 return Err(BadAmount::NotPositive(amount));
110 }
111 if amount.places() > places {
112 return Err(BadAmount::TooFine { value: amount, decimals: places });
113 }
114 let units = U256::from_dec(amount, decimals)?;
115 Ok(match self.contract {
116 None => Call { to: Some(HYPE_SYSTEM), value: units, data: Vec::new() },
117 Some(contract) => {
118 Call { to: Some(contract), value: U256::ZERO, data: erc20::transfer(self.system(), units) }
119 }
120 })
121 }
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126pub enum AddressEncoding {
127 Hex,
129 Base58,
131}
132
133impl AddressEncoding {
134 #[must_use]
136 pub const fn wire(self) -> &'static str {
137 match self {
138 AddressEncoding::Hex => "hex",
139 AddressEncoding::Base58 => "base58",
140 }
141 }
142}
143
144#[derive(Debug, Clone, PartialEq, Eq)]
147pub struct Destination {
148 recipient: String,
149 encoding: AddressEncoding,
150 chain_id: u32,
151}
152
153impl Destination {
154 #[must_use]
156 pub fn evm(recipient: Address, chain_id: u32) -> Destination {
157 Destination { recipient: recipient.to_hex(), encoding: AddressEncoding::Hex, chain_id }
158 }
159
160 #[must_use]
162 pub fn base58(recipient: &str, chain_id: u32) -> Destination {
163 Destination { recipient: recipient.to_owned(), encoding: AddressEncoding::Base58, chain_id }
164 }
165
166 #[must_use]
168 pub fn recipient(&self) -> &str {
169 &self.recipient
170 }
171
172 #[must_use]
174 pub const fn encoding(&self) -> AddressEncoding {
175 self.encoding
176 }
177
178 #[must_use]
180 pub const fn chain_id(&self) -> u32 {
181 self.chain_id
182 }
183}
184
185#[derive(Debug, Clone, PartialEq, Eq)]
192pub struct SendToEvmWithData {
193 token: String,
194 amount: Amount,
195 source_dex: String,
196 destination: Destination,
197 gas_limit: u64,
198 data: Vec<u8>,
199 nonce: u64,
200}
201
202impl SendToEvmWithData {
203 pub const KEY: Signers = ActionKind::SendToEvmWithData.signers();
205
206 #[must_use]
210 pub fn new(
211 token: &str,
212 amount: Amount,
213 source_dex: &str,
214 destination: Destination,
215 gas_limit: u64,
216 data: Vec<u8>,
217 nonce: u64,
218 ) -> SendToEvmWithData {
219 SendToEvmWithData {
220 token: token.to_owned(),
221 amount,
222 source_dex: source_dex.to_owned(),
223 destination,
224 gas_limit,
225 data,
226 nonce,
227 }
228 }
229
230 #[must_use]
232 pub const fn amount(&self) -> &Amount {
233 &self.amount
234 }
235
236 #[must_use]
238 pub const fn destination(&self) -> &Destination {
239 &self.destination
240 }
241
242 #[must_use]
244 pub fn data(&self) -> &[u8] {
245 &self.data
246 }
247}
248
249impl UserAction for SendToEvmWithData {
250 fn user_signed(&self) -> UserSigned<'_> {
251 UserSigned::new("sendToEvmWithData", "HyperliquidTransaction:SendToEvmWithData", NonceKey::Nonce, self.nonce)
252 .field("token", Field::String(&self.token))
253 .field("amount", Field::String(self.amount.as_str()))
254 .field("sourceDex", Field::String(&self.source_dex))
255 .field("destinationRecipient", Field::String(&self.destination.recipient))
256 .field("addressEncoding", Field::String(self.destination.encoding.wire()))
257 .field("destinationChainId", Field::Uint32(self.destination.chain_id))
258 .field("gasLimit", Field::Uint64(self.gas_limit))
259 .field("data", Field::Bytes(&self.data))
260 }
261}
262
263#[cfg(test)]
264#[path = "tests/evm.rs"]
265mod tests;