tycho_ethereum/
lib.rs

1#[cfg(test)]
2#[macro_use]
3extern crate pretty_assertions;
4
5pub mod erc20;
6pub mod errors;
7pub mod rpc;
8pub mod services;
9
10#[cfg(test)]
11pub mod test_fixtures;
12
13use alloy::primitives::{Address, B256, U256};
14pub(crate) use errors::*;
15use tycho_common::Bytes;
16
17/// A trait for converting types to and from `Bytes`.
18///
19/// This trait provides methods to convert a type into a `Bytes` object,
20/// as well as reconstruct the original type from a `Bytes` object.
21///
22/// # Examples
23/// ```
24/// use alloy::primitives::Address;
25/// use tycho_ethereum::BytesCodec;
26/// use tycho_common::Bytes;
27///
28/// let address_value = Address::ZERO;
29/// let bytes: Bytes = address_value.to_bytes(); // Converts Address to Bytes
30/// let new_address = Address::from_bytes(&bytes);  // Converts Bytes back to Address
31/// ```
32pub trait BytesCodec {
33    /// Converts the current type into a `Bytes` object.
34    fn to_bytes(self) -> Bytes;
35
36    /// Reconstructs the type from a `Bytes` object.
37    ///
38    /// # Arguments
39    ///
40    /// * `bytes` - The `Bytes` object to convert into the original type.
41    ///
42    /// # Returns
43    ///
44    /// The type that was converted from `Bytes`.
45    fn from_bytes(bytes: &Bytes) -> Self;
46}
47
48// Implementing `BytesCodec` for `Address` (H160 equivalent).
49impl BytesCodec for Address {
50    /// Converts `Address` to `Bytes`.
51    fn to_bytes(self) -> Bytes {
52        Bytes::from(self.0.to_vec())
53    }
54
55    /// Converts `Bytes` to `Address`.
56    ///
57    /// # Panics
58    ///
59    /// Will panic if the length of `Bytes` is not 20 (which is the size of an `Address`).
60    fn from_bytes(bytes: &Bytes) -> Self {
61        Address::from_slice(bytes.as_ref())
62    }
63}
64
65// Implementing `BytesCodec` for `B256`
66impl BytesCodec for B256 {
67    /// Converts `B256` to `Bytes`.
68    fn to_bytes(self) -> Bytes {
69        Bytes::from(self.0.to_vec())
70    }
71
72    /// Converts `Bytes` to `B256`.
73    ///
74    /// # Panics
75    ///
76    /// Will panic if the length of `Bytes` is not 32 (which is the size of a `B256`).
77    fn from_bytes(bytes: &Bytes) -> Self {
78        B256::from_slice(bytes.as_ref())
79    }
80}
81
82// Implementing `BytesCodec` for `U256`.
83impl BytesCodec for U256 {
84    /// Converts `U256` to `Bytes`.
85    fn to_bytes(self) -> Bytes {
86        let buf = self.to_be_bytes::<32>();
87        Bytes::from(buf.to_vec())
88    }
89
90    /// Converts `Bytes` to `U256` using big-endian.
91    ///
92    /// # Panics
93    ///
94    /// Will panic if the length of `Bytes` is larger than 32.
95    fn from_bytes(bytes: &Bytes) -> Self {
96        let bytes_slice = bytes.as_ref();
97
98        // Create an array with zeros.
99        let mut u256_bytes: [u8; 32] = [0; 32];
100
101        // Copy bytes from `bytes_slice` to `u256_bytes`.
102        u256_bytes[32 - bytes_slice.len()..].copy_from_slice(bytes_slice);
103
104        // Convert the byte array to `U256` using big-endian.
105        U256::from_be_bytes(u256_bytes)
106    }
107}