Skip to content

Commit

Permalink
feat(primitives): update Bytes formatting, add UpperHex
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Dec 5, 2023
1 parent 0f5afd0 commit f31dce2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
19 changes: 9 additions & 10 deletions crates/primitives/src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ pub struct Bytes(pub bytes::Bytes);

impl fmt::Debug for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Bytes(")?;
f.write_str(&self.hex_encode())?;
f.write_str(")")
fmt::LowerHex::fmt(self, f)
}
}

impl fmt::Display for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.hex_encode())
fmt::LowerHex::fmt(self, f)
}
}

impl fmt::LowerHex for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.hex_encode())
f.pad(&hex::encode_prefixed(self.as_ref()))
}
}

impl fmt::UpperHex for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&hex::encode_upper_prefixed(self.as_ref()))
}
}

Expand Down Expand Up @@ -315,11 +319,6 @@ impl Bytes {
pub fn split_to(&mut self, at: usize) -> Self {
Self(self.0.split_to(at))
}

#[inline]
fn hex_encode(&self) -> String {
hex::encode_prefixed(self.0.as_ref())
}
}

#[cfg(feature = "arbitrary")]
Expand Down
19 changes: 19 additions & 0 deletions crates/sol-types/src/types/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ supported_int!(
mod tests {
use super::*;
use crate::sol;
use alloy_primitives::hex;

macro_rules! assert_encoded_size {
($t:ty, $sz:expr) => {
Expand Down Expand Up @@ -1427,4 +1428,22 @@ mod tests {
assert_eq!(<Int<248>>::detokenize(token), "0xff82038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256());
assert_eq!(<Int<256>>::detokenize(token), "0x0182038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256());
}

#[test]
fn encode_packed() {
let value = (RustAddress::with_last_byte(1), U256::from(2), 3, -3, 3, -3);
let res =
<sol! { (address, uint160, uint24, int24, uint32, int32) }>::abi_encode_packed(&value);
assert_eq!(
res,
hex!(
"0000000000000000000000000000000000000001"
"0000000000000000000000000000000000000002"
"000003"
"fffffd"
"00000003"
"fffffffd"
)
);
}
}

0 comments on commit f31dce2

Please sign in to comment.