df_hyperliquid_core/client/evm/
tx.rs1#[cfg(feature = "sign")]
14use crate::account::{LocalKey, recover};
15use crate::account::{SignError, Signature};
16use crate::client::evm::rlp;
17use crate::client::evm::u256::{B256, U256};
18use crate::utils::Address;
19use crate::utils::sign::keccak256;
20use serde::Deserialize;
21
22pub const EIP1559: u8 = 0x02;
24
25pub const GAS_HEADROOM: u64 = 130;
28
29const BASE_FEE_TIMES: u64 = 2;
31
32#[derive(Debug, Clone, PartialEq, Eq, Default, Deserialize)]
34#[serde(rename_all = "camelCase")]
35pub struct AccessItem {
36 pub address: Address,
38 pub storage_keys: Vec<B256>,
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, Default)]
46pub struct Eip1559 {
47 pub chain_id: u64,
49 pub nonce: u64,
51 pub max_priority_fee: U256,
53 pub max_fee: U256,
55 pub gas: u64,
57 pub to: Option<Address>,
59 pub value: U256,
61 pub data: Vec<u8>,
63 pub access_list: Vec<AccessItem>,
65}
66
67impl Eip1559 {
68 fn fields(&self) -> Vec<Vec<u8>> {
69 let to = match &self.to {
70 Some(to) => rlp::bytes(to.bytes()),
71 None => rlp::bytes(&[]),
72 };
73 let access: Vec<Vec<u8>> = self
74 .access_list
75 .iter()
76 .map(|item| {
77 let keys: Vec<Vec<u8>> = item.storage_keys.iter().map(|key| rlp::bytes(&key.0)).collect();
78 rlp::list(&[rlp::bytes(item.address.bytes()), rlp::list(&keys)])
79 })
80 .collect();
81 vec![
82 rlp::uint(u128::from(self.chain_id)),
83 rlp::uint(u128::from(self.nonce)),
84 rlp::u256(self.max_priority_fee),
85 rlp::u256(self.max_fee),
86 rlp::uint(u128::from(self.gas)),
87 to,
88 rlp::u256(self.value),
89 rlp::bytes(&self.data),
90 rlp::list(&access),
91 ]
92 }
93
94 #[must_use]
96 pub fn unsigned(&self) -> Vec<u8> {
97 typed(&self.fields())
98 }
99
100 #[must_use]
102 pub fn signing_hash(&self) -> B256 {
103 B256(keccak256(&self.unsigned()))
104 }
105
106 pub fn signed(&self, signature: &Signature) -> Result<Vec<u8>, SignError> {
110 let parity = match signature.recovery_id() {
111 Some(parity @ (0 | 1)) => parity,
112 _ => return Err(SignError::BadV(signature.v())),
113 };
114 let mut fields = self.fields();
115 fields.push(rlp::uint(u128::from(parity)));
116 fields.push(rlp::word(signature.r()));
117 fields.push(rlp::word(signature.s()));
118 Ok(typed(&fields))
119 }
120
121 #[cfg(feature = "sign")]
123 pub fn sign(&self, key: &LocalKey) -> Result<Vec<u8>, SignError> {
124 self.signed(&key.sign_digest(&self.signing_hash().0)?)
125 }
126
127 #[cfg(feature = "sign")]
129 pub fn signer(&self, signature: &Signature) -> Result<Address, SignError> {
130 recover(&self.signing_hash().0, signature)
131 }
132
133 #[must_use]
135 pub fn created(&self, from: Address) -> Option<Address> {
136 self.to.is_none().then(|| create_address(from, self.nonce))
137 }
138
139 #[must_use]
141 pub fn gas_ceiling(&self) -> U256 {
142 self.max_fee.saturating_mul(U256::from_u64(self.gas))
143 }
144
145 #[must_use]
147 pub fn most_spent(&self) -> Option<U256> {
148 self.max_fee.checked_mul(U256::from_u64(self.gas))?.checked_add(self.value)
149 }
150}
151
152fn typed(fields: &[Vec<u8>]) -> Vec<u8> {
153 let mut out = vec![EIP1559];
154 out.extend(rlp::list(fields));
155 out
156}
157
158#[must_use]
160pub fn tx_hash(raw: &[u8]) -> B256 {
161 B256(keccak256(raw))
162}
163
164#[must_use]
167pub fn create_address(from: Address, nonce: u64) -> Address {
168 let hash = keccak256(&rlp::list(&[rlp::bytes(from.bytes()), rlp::uint(u128::from(nonce))]));
169 let mut bytes = [0u8; 20];
170 bytes.copy_from_slice(&hash[12..]);
171 Address::from_bytes(bytes)
172}
173
174#[derive(Debug, Clone, PartialEq, Eq, Default)]
176pub struct Call {
177 pub to: Option<Address>,
179 pub value: U256,
181 pub data: Vec<u8>,
183}
184
185impl Call {
186 #[must_use]
188 pub fn transaction(self, chain_id: u64, nonce: u64, gas: u64, fees: Fees) -> Eip1559 {
189 Eip1559 {
190 chain_id,
191 nonce,
192 max_priority_fee: fees.max_priority_fee,
193 max_fee: fees.max_fee,
194 gas,
195 to: self.to,
196 value: self.value,
197 data: self.data,
198 access_list: Vec::new(),
199 }
200 }
201}
202
203#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
205#[non_exhaustive]
206pub struct Fees {
207 pub max_priority_fee: U256,
209 pub max_fee: U256,
211}
212
213impl Fees {
214 #[must_use]
217 pub fn over_base(base: U256, tip: U256) -> Fees {
218 let max_fee = base.saturating_mul(U256::from_u64(BASE_FEE_TIMES)).saturating_add(tip);
219 Fees { max_priority_fee: tip, max_fee }
220 }
221}
222
223#[must_use]
225pub fn with_headroom(gas: u64) -> u64 {
226 let wide = u128::from(gas) * u128::from(GAS_HEADROOM) / 100;
227 u64::try_from(wide).unwrap_or(u64::MAX)
228}
229
230#[cfg(test)]
231#[path = "tests/tx.rs"]
232mod tests;