diff --git a/crates/dyn-abi/benches/abi.rs b/crates/dyn-abi/benches/abi.rs index 5ebfa627a..2ecbf383c 100644 --- a/crates/dyn-abi/benches/abi.rs +++ b/crates/dyn-abi/benches/abi.rs @@ -1,7 +1,7 @@ #![allow(unknown_lints, clippy::incompatible_msrv, missing_docs)] use alloy_dyn_abi::{DynSolType, DynSolValue}; -use alloy_primitives::{hex, U256}; +use alloy_primitives::{hex, Uint, U256}; use alloy_sol_types::{sol, sol_data, SolType, SolValue}; use criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, @@ -142,12 +142,12 @@ fn encode_struct_input() -> Input { Input { tokenIn: hex!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").into(), tokenOut: hex!("955d5c14C8D4944dA1Ea7836bd44D54a8eC35Ba1").into(), - fee: 10000, + fee: Uint::from(10000), recipient: hex!("299A299A22F8C7397d9DB3702439069d951AeA74").into(), deadline: U256::from(1685523099_u64), amountIn: U256::from(10000000000000000000_u128), amountOutMinimum: U256::from(836797564735606450550734848_u128), - sqrtPriceLimitX96: U256::ZERO, + sqrtPriceLimitX96: Uint::ZERO, } } @@ -156,7 +156,7 @@ fn encode_struct_input_tokens() -> [ethabi::Token; 8] { [ ethabi::Token::Address(input.tokenIn.0 .0.into()), ethabi::Token::Address(input.tokenOut.0 .0.into()), - ethabi::Token::Uint(input.fee.into()), + ethabi::Token::Uint(input.fee.to::().into()), ethabi::Token::Address(input.recipient.0 .0.into()), ethabi::Token::Uint(ethabi::Uint::from_big_endian(&input.deadline.to_be_bytes_vec())), ethabi::Token::Uint(ethabi::Uint::from_big_endian(&input.amountIn.to_be_bytes_vec())), @@ -174,12 +174,12 @@ fn encode_struct_sol_values() -> [DynSolValue; 8] { [ input.tokenIn.into(), input.tokenOut.into(), - input.fee.into(), + input.fee.to::().into(), input.recipient.into(), input.deadline.into(), input.amountIn.into(), input.amountOutMinimum.into(), - input.sqrtPriceLimitX96.into(), + input.sqrtPriceLimitX96.to::().into(), ] } diff --git a/crates/primitives/src/aliases.rs b/crates/primitives/src/aliases.rs index a90fcecfc..04b3b768f 100644 --- a/crates/primitives/src/aliases.rs +++ b/crates/primitives/src/aliases.rs @@ -1,15 +1,17 @@ //! Type aliases for common primitive types. -use crate::{FixedBytes, Signed}; +use crate::{FixedBytes, Signed, Uint}; -pub use ruint::aliases::{ - U0, U1, U1024, U128, U16, U160, U192, U2048, U256, U32, U320, U384, U4096, U448, U512, U64, U8, -}; +pub use ruint::aliases::{U0, U1, U1024, U2048, U320, U384, U4096, U448}; macro_rules! int_aliases { - ($($name:ident<$BITS:literal, $LIMBS:literal>),* $(,)?) => {$( + ($($unsigned:ident, $signed:ident<$BITS:literal, $LIMBS:literal>),* $(,)?) => {$( + #[doc = concat!($BITS, "-bit [unsigned integer type][Uint], consisting of ", $LIMBS, ", 64-bit limbs.")] + pub type $unsigned = Uint<$BITS, $LIMBS>; + #[doc = concat!($BITS, "-bit [signed integer type][Signed], consisting of ", $LIMBS, ", 64-bit limbs.")] - pub type $name = Signed<$BITS, $LIMBS>; + pub type $signed = Signed<$BITS, $LIMBS>; + const _: () = assert!($LIMBS == ruint::nlimbs($BITS)); )*}; } @@ -21,15 +23,43 @@ pub type I0 = Signed<0, 0>; pub type I1 = Signed<1, 1>; int_aliases! { - I8<8, 1>, - I16<16, 1>, - I32<32, 1>, - I64<64, 1>, - I128<128, 2>, - I160<160, 3>, - I192<192, 3>, - I256<256, 4>, - I512<512, 8>, + U8, I8< 8, 1>, + U16, I16< 16, 1>, + U24, I24< 24, 1>, + U32, I32< 32, 1>, + U40, I40< 40, 1>, + U48, I48< 48, 1>, + U56, I56< 56, 1>, + U64, I64< 64, 1>, + + U72, I72< 72, 2>, + U80, I80< 80, 2>, + U88, I88< 88, 2>, + U96, I96< 96, 2>, + U104, I104<104, 2>, + U112, I112<112, 2>, + U120, I120<120, 2>, + U128, I128<128, 2>, + + U136, I136<136, 3>, + U144, I144<144, 3>, + U152, I152<152, 3>, + U160, I160<160, 3>, + U168, I168<168, 3>, + U176, I176<176, 3>, + U184, I184<184, 3>, + U192, I192<192, 3>, + + U200, I200<200, 4>, + U208, I208<208, 4>, + U216, I216<216, 4>, + U224, I224<224, 4>, + U232, I232<232, 4>, + U240, I240<240, 4>, + U248, I248<248, 4>, + U256, I256<256, 4>, + + U512, I512<512, 8>, } macro_rules! fixed_bytes_aliases { diff --git a/crates/sol-macro-expander/src/expand/ty.rs b/crates/sol-macro-expander/src/expand/ty.rs index c88b807aa..e2e714e8e 100644 --- a/crates/sol-macro-expander/src/expand/ty.rs +++ b/crates/sol-macro-expander/src/expand/ty.rs @@ -88,12 +88,6 @@ pub fn rec_expand_type(ty: &Type, crates: &ExternCrates, tokens: &mut TokenStrea // IMPORTANT: Keep in sync with `sol-types/src/types/data_type.rs` /// The [`expand_rust_type`] recursive implementation. pub fn rec_expand_rust_type(ty: &Type, crates: &ExternCrates, tokens: &mut TokenStream) { - // Display sizes that match with the Rust type, otherwise we lose information - // (e.g. `uint24` displays the same as `uint32` because both use `u32`) - fn allowed_int_size(size: Option) -> bool { - matches!(size.map_or(256, NonZeroU16::get), 8 | 16 | 32 | 64 | 128 | 256) - } - let alloy_sol_types = &crates.sol_types; let tts = match *ty { Type::Address(span, _) => quote_spanned! {span=> #alloy_sol_types::private::Address }, @@ -106,22 +100,24 @@ pub fn rec_expand_rust_type(ty: &Type, crates: &ExternCrates, tokens: &mut Token let size = Literal::u16_unsuffixed(size.get()); quote_spanned! {span=> #alloy_sol_types::private::FixedBytes<#size> } } - Type::Int(span, size) | Type::Uint(span, size) if allowed_int_size(size) => { + Type::Int(span, size) | Type::Uint(span, size) => { let size = size.map_or(256, NonZeroU16::get); - if size <= 128 { - let name = match ty { + let primitive = matches!(size, 8 | 16 | 32 | 64 | 128); + if primitive { + let prefix = match ty { Type::Int(..) => "i", Type::Uint(..) => "u", _ => unreachable!(), }; - return Ident::new(&format!("{name}{size}"), span).to_tokens(tokens); + return Ident::new(&format!("{prefix}{size}"), span).to_tokens(tokens); } - assert_eq!(size, 256); - match ty { - Type::Int(..) => quote_spanned! {span=> #alloy_sol_types::private::I256 }, - Type::Uint(..) => quote_spanned! {span=> #alloy_sol_types::private::U256 }, + let prefix = match ty { + Type::Int(..) => "I", + Type::Uint(..) => "U", _ => unreachable!(), - } + }; + let name = Ident::new(&format!("{prefix}{size}"), span); + quote_spanned! {span=> #alloy_sol_types::private::primitives::aliases::#name } } Type::Tuple(ref tuple) => { @@ -150,7 +146,7 @@ pub fn rec_expand_rust_type(ty: &Type, crates: &ExternCrates, tokens: &mut Token }, // Exhaustive fallback to `SolType::RustType` - ref ty @ (Type::Int(..) | Type::Uint(..) | Type::Custom(_)) => { + Type::Custom(_) => { let span = ty.span(); let ty = expand_type(ty, crates); quote_spanned! {span=> <#ty as #alloy_sol_types::SolType>::RustType } diff --git a/crates/sol-types/src/lib.rs b/crates/sol-types/src/lib.rs index 97a90d2c2..2f5dcf6df 100644 --- a/crates/sol-types/src/lib.rs +++ b/crates/sol-types/src/lib.rs @@ -66,8 +66,8 @@ pub mod private { vec::Vec, }; pub use alloy_primitives::{ - bytes, keccak256, Address, Bytes, FixedBytes, Function, IntoLogData, LogData, Signed, Uint, - B256, I256, U256, + self as primitives, bytes, keccak256, Address, Bytes, FixedBytes, Function, IntoLogData, + LogData, Signed, Uint, B256, I256, U256, }; pub use core::{ borrow::{Borrow, BorrowMut}, diff --git a/crates/sol-types/src/types/data_type.rs b/crates/sol-types/src/types/data_type.rs index 107700402..30646d80e 100644 --- a/crates/sol-types/src/types/data_type.rs +++ b/crates/sol-types/src/types/data_type.rs @@ -11,13 +11,13 @@ use crate::{abi::token::*, private::SolTypeValue, utils, SolType, Word}; use alloc::{string::String as RustString, vec::Vec}; use alloy_primitives::{ - keccak256, Address as RustAddress, Bytes as RustBytes, FixedBytes as RustFixedBytes, - Function as RustFunction, I256, U256, + aliases::*, keccak256, Address as RustAddress, Bytes as RustBytes, + FixedBytes as RustFixedBytes, Function as RustFunction, I256, U256, }; use core::{borrow::Borrow, fmt::*, hash::Hash, marker::PhantomData, ops::*}; // IMPORTANT: Keep in sync with `rec_expand_rust_type` in -// `sol-macro/src/expand/ty.rs` +// `crates/sol-macro-expander/src/expand/ty.rs` /// Bool - `bool` pub struct Bool; @@ -961,7 +961,7 @@ macro_rules! supported_int { const SKIP_BYTES: usize = (<$i>::BITS as usize - ::BITS) / 8; int_impls2!($i); - int_impls2!($u); + uint_impls2!($u); } )+}; } @@ -1019,7 +1019,8 @@ macro_rules! int_impls { #[inline] fn tokenize_int(int: $ity) -> WordToken { let mut word = [int.is_negative() as u8 * 0xff; 32]; - word[Self::WORD_MSB..].copy_from_slice(&int.to_be_bytes::<32>()[Self::SKIP_BYTES..]); + word[Self::WORD_MSB..] + .copy_from_slice(&int.to_be_bytes::<{ $ity::BYTES }>()[Self::SKIP_BYTES..]); WordToken::new(word) } @@ -1031,19 +1032,20 @@ macro_rules! int_impls { token.0[Self::WORD_MSB - Self::SKIP_BYTES..Self::WORD_MSB].fill(sign_extension); let s = &token.0[Self::WORD_MSB - Self::SKIP_BYTES..]; - <$ity>::from_be_bytes::<32>(s.try_into().unwrap()) + <$ity>::from_be_bytes::<{ $ity::BYTES }>(s.try_into().unwrap()) } #[inline] fn encode_packed_to_int(int: $ity, out: &mut Vec) { - out.extend_from_slice(&int.to_be_bytes::<32>()[Self::SKIP_BYTES..]); + out.extend_from_slice(&int.to_be_bytes::<{ $ity::BYTES }>()[Self::SKIP_BYTES..]); } }; (@big_uint $uty:ident) => { #[inline] fn tokenize_uint(uint: $uty) -> WordToken { let mut word = Word::ZERO; - word[Self::WORD_MSB..].copy_from_slice(&uint.to_be_bytes::<32>()[Self::SKIP_BYTES..]); + word[Self::WORD_MSB..] + .copy_from_slice(&uint.to_be_bytes::<{ $uty::BYTES }>()[Self::SKIP_BYTES..]); WordToken(word) } @@ -1051,12 +1053,13 @@ macro_rules! int_impls { fn detokenize_uint(mut token: WordToken) -> $uty { // zero out bits to ignore token.0[..Self::SKIP_BYTES].fill(0); - <$uty>::from_be_bytes::<32>(token.0 .0) + let s = &token.0[Self::WORD_MSB - Self::SKIP_BYTES..]; + <$uty>::from_be_bytes::<{ $uty::BYTES }>(s.try_into().unwrap()) } #[inline] fn encode_packed_to_uint(uint: $uty, out: &mut Vec) { - out.extend_from_slice(&uint.to_be_bytes::<32>()[Self::SKIP_BYTES..]); + out.extend_from_slice(&uint.to_be_bytes::<{ $uty::BYTES }>()[Self::SKIP_BYTES..]); } }; } @@ -1069,48 +1072,52 @@ macro_rules! int_impls2 { ( i64) => { int_impls! { @primitive_int i64 } }; (i128) => { int_impls! { @primitive_int i128 } }; + ($t:ident) => { int_impls! { @big_int $t } }; +} + +#[rustfmt::skip] +macro_rules! uint_impls2 { ( u8) => { int_impls! { @primitive_uint u8 } }; ( u16) => { int_impls! { @primitive_uint u16 } }; ( u32) => { int_impls! { @primitive_uint u32 } }; ( u64) => { int_impls! { @primitive_uint u64 } }; (u128) => { int_impls! { @primitive_uint u128 } }; - (I256) => { int_impls! { @big_int I256 } }; - (U256) => { int_impls! { @big_uint U256 } }; + ($t:ident) => { int_impls! { @big_uint $t } }; } supported_int!( - 8 => i8, u8; - 16 => i16, u16; - 24 => i32, u32; - 32 => i32, u32; - 40 => i64, u64; - 48 => i64, u64; - 56 => i64, u64; - 64 => i64, u64; - 72 => i128, u128; - 80 => i128, u128; - 88 => i128, u128; - 96 => i128, u128; - 104 => i128, u128; - 112 => i128, u128; - 120 => i128, u128; + 8 => i8, u8; + 16 => i16, u16; + 24 => I24, U24; + 32 => i32, u32; + 40 => I40, U40; + 48 => I48, U48; + 56 => I56, U56; + 64 => i64, u64; + 72 => I72, U72; + 80 => I80, U80; + 88 => I88, U88; + 96 => I96, U96; + 104 => I104, U104; + 112 => I112, U112; + 120 => I120, U120; 128 => i128, u128; - 136 => I256, U256; - 144 => I256, U256; - 152 => I256, U256; - 160 => I256, U256; - 168 => I256, U256; - 176 => I256, U256; - 184 => I256, U256; - 192 => I256, U256; - 200 => I256, U256; - 208 => I256, U256; - 216 => I256, U256; - 224 => I256, U256; - 232 => I256, U256; - 240 => I256, U256; - 248 => I256, U256; + 136 => I136, U136; + 144 => I144, U144; + 152 => I152, U152; + 160 => I160, U160; + 168 => I168, U168; + 176 => I176, U176; + 184 => I184, U184; + 192 => I192, U192; + 200 => I200, U200; + 208 => I208, U208; + 216 => I216, U216; + 224 => I224, U224; + 232 => I232, U232; + 240 => I240, U240; + 248 => I248, U248; 256 => I256, U256; ); @@ -1202,7 +1209,7 @@ impl NameBuffer { mod tests { use super::*; use crate::{sol, SolValue}; - use alloy_primitives::hex; + use alloy_primitives::{hex, Signed}; #[test] fn sol_names() { @@ -1398,7 +1405,7 @@ mod tests { fn tokenize_uint() { macro_rules! test { ($($n:literal: $x:expr => $l:literal),+ $(,)?) => {$( - let uint: as SolType>::RustType = $x.into(); + let uint = as SolType>::RustType::try_from($x).unwrap(); let int = as SolType>::RustType::try_from(uint).unwrap(); assert_eq!( @@ -1412,19 +1419,19 @@ mod tests { )+}; } - let word: Word = Word::new(core::array::from_fn(|i| i as u8 + 1)); + let word = core::array::from_fn::<_, 32, _>(|i| i as u8 + 1); test! { 8: 0x00u8 => "0000000000000000000000000000000000000000000000000000000000000000", 8: 0x01u8 => "0000000000000000000000000000000000000000000000000000000000000001", - 24: 0x01020304u32 => "0000000000000000000000000000000000000000000000000000000000020304", + 24: 0x00020304u32 => "0000000000000000000000000000000000000000000000000000000000020304", 32: 0x01020304u32 => "0000000000000000000000000000000000000000000000000000000001020304", - 56: 0x0102030405060708u64 => "0000000000000000000000000000000000000000000000000002030405060708", + 56: 0x0002030405060708u64 => "0000000000000000000000000000000000000000000000000002030405060708", 64: 0x0102030405060708u64 => "0000000000000000000000000000000000000000000000000102030405060708", - 160: word => "0000000000000000000000000d0e0f101112131415161718191a1b1c1d1e1f20", - 200: word => "0000000000000008090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", - 256: word => "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", + 160: U160::from_be_slice(&word[32 - 160/8..]) => "0000000000000000000000000d0e0f101112131415161718191a1b1c1d1e1f20", + 200: U200::from_be_slice(&word[32 - 200/8..]) => "0000000000000008090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", + 256: U256::from_be_slice(&word[32 - 256/8..]) => "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20", } } @@ -1456,19 +1463,19 @@ mod tests { test! { 8 => 0x0000000000000000000000000000000000000000000000000000000000000020, 16 => 0x0000000000000000000000000000000000000000000000000000000000001f20, - 24 => 0x00000000000000000000000000000000000000000000000000000000001e1f20, + 24 => "0x00000000000000000000000000000000000000000000000000000000001e1f20".parse().unwrap(), 32 => 0x000000000000000000000000000000000000000000000000000000001d1e1f20, - 40 => 0x0000000000000000000000000000000000000000000000000000001c1d1e1f20, - 48 => 0x00000000000000000000000000000000000000000000000000001b1c1d1e1f20, - 56 => 0x000000000000000000000000000000000000000000000000001a1b1c1d1e1f20, + 40 => "0x0000000000000000000000000000000000000000000000000000001c1d1e1f20".parse().unwrap(), + 48 => "0x00000000000000000000000000000000000000000000000000001b1c1d1e1f20".parse().unwrap(), + 56 => "0x000000000000000000000000000000000000000000000000001a1b1c1d1e1f20".parse().unwrap(), 64 => 0x000000000000000000000000000000000000000000000000191a1b1c1d1e1f20, - 72 => 0x000000000000000000000000000000000000000000000018191a1b1c1d1e1f20, - 80 => 0x000000000000000000000000000000000000000000001718191a1b1c1d1e1f20, - 88 => 0x000000000000000000000000000000000000000000161718191a1b1c1d1e1f20, - 96 => 0x000000000000000000000000000000000000000015161718191a1b1c1d1e1f20, - 104 => 0x000000000000000000000000000000000000001415161718191a1b1c1d1e1f20, - 112 => 0x000000000000000000000000000000000000131415161718191a1b1c1d1e1f20, - 120 => 0x000000000000000000000000000000000012131415161718191a1b1c1d1e1f20, + 72 => "0x000000000000000000000000000000000000000000000018191a1b1c1d1e1f20".parse().unwrap(), + 80 => "0x000000000000000000000000000000000000000000001718191a1b1c1d1e1f20".parse().unwrap(), + 88 => "0x000000000000000000000000000000000000000000161718191a1b1c1d1e1f20".parse().unwrap(), + 96 => "0x000000000000000000000000000000000000000015161718191a1b1c1d1e1f20".parse().unwrap(), + 104 => "0x000000000000000000000000000000000000001415161718191a1b1c1d1e1f20".parse().unwrap(), + 112 => "0x000000000000000000000000000000000000131415161718191a1b1c1d1e1f20".parse().unwrap(), + 120 => "0x000000000000000000000000000000000012131415161718191a1b1c1d1e1f20".parse().unwrap(), 128 => 0x000000000000000000000000000000001112131415161718191a1b1c1d1e1f20, 136 => "0x000000000000000000000000000000101112131415161718191a1b1c1d1e1f20".parse().unwrap(), 144 => "0x00000000000000000000000000000f101112131415161718191a1b1c1d1e1f20".parse().unwrap(), @@ -1495,92 +1502,104 @@ mod tests { let token = WordToken::new(word); assert_eq!(>::detokenize(token), -1); assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), -1); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); assert_eq!(>::detokenize(token), -1); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); - assert_eq!(>::detokenize(token), I256::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); + assert_eq!(>::detokenize(token), Signed::MINUS_ONE); } #[test] #[rustfmt::skip] fn detokenize_int() { + use alloy_primitives::Uint; + let word = core::array::from_fn(|i| (i | (0x80 * (i % 2 == 1) as usize)) as u8 + 1); let token = WordToken::new(word); - trait Conv { - fn as_u256_as_i256(&self) -> I256; + trait Conv { + fn as_uint_as_int(&self) -> Signed; } - impl Conv for str { - fn as_u256_as_i256(&self) -> I256 { - I256::from_raw(self.parse::().unwrap()) + impl Conv for str { + fn as_uint_as_int(&self) -> Signed { + Signed::::from_raw(self.parse::>().unwrap()) } } assert_eq!(>::detokenize(token), 0x00000000000000000000000000000000000000000000000000000000000000a0_u8 as i8); assert_eq!(>::detokenize(token), 0x0000000000000000000000000000000000000000000000000000000000001fa0_u16 as i16); - assert_eq!(>::detokenize(token), 0x00000000000000000000000000000000000000000000000000000000ff9e1fa0_u32 as i32); + assert_eq!(>::detokenize(token), "0x00000000000000000000000000000000000000000000000000000000009e1fa0".as_uint_as_int()); assert_eq!(>::detokenize(token), 0x000000000000000000000000000000000000000000000000000000001d9e1fa0_u32 as i32); - assert_eq!(>::detokenize(token), 0x000000000000000000000000000000000000000000000000ffffff9c1d9e1fa0_u64 as i64); - assert_eq!(>::detokenize(token), 0x00000000000000000000000000000000000000000000000000001b9c1d9e1fa0_u64 as i64); - assert_eq!(>::detokenize(token), 0x000000000000000000000000000000000000000000000000ff9a1b9c1d9e1fa0_u64 as i64); + assert_eq!(>::detokenize(token), "0x0000000000000000000000000000000000000000000000000000009c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x00000000000000000000000000000000000000000000000000001b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000000000000000000000009a1b9c1d9e1fa0".as_uint_as_int()); assert_eq!(>::detokenize(token), 0x000000000000000000000000000000000000000000000000199a1b9c1d9e1fa0_u64 as i64); - assert_eq!(>::detokenize(token), 0x00000000000000000000000000000000ffffffffffffff98199a1b9c1d9e1fa0_u128 as i128); - assert_eq!(>::detokenize(token), 0x000000000000000000000000000000000000000000001798199a1b9c1d9e1fa0_u128 as i128); - assert_eq!(>::detokenize(token), 0x00000000000000000000000000000000ffffffffff961798199a1b9c1d9e1fa0_u128 as i128); - assert_eq!(>::detokenize(token), 0x000000000000000000000000000000000000000015961798199a1b9c1d9e1fa0_u128 as i128); - assert_eq!(>::detokenize(token), 0x00000000000000000000000000000000ffffff9415961798199a1b9c1d9e1fa0_u128 as i128); - assert_eq!(>::detokenize(token), 0x000000000000000000000000000000000000139415961798199a1b9c1d9e1fa0_u128 as i128); - assert_eq!(>::detokenize(token), 0x00000000000000000000000000000000ff92139415961798199a1b9c1d9e1fa0_u128 as i128); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000000000000000000098199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000000000000000001798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000000000000000961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000000000000015961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000000000009415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000000000139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000000092139415961798199a1b9c1d9e1fa0".as_uint_as_int()); assert_eq!(>::detokenize(token), 0x000000000000000000000000000000001192139415961798199a1b9c1d9e1fa0_u128 as i128); - assert_eq!(>::detokenize(token), "0xffffffffffffffffffffffffffffff901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0x00000000000000000000000000000f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0xffffffffffffffffffffffffff8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0x0000000000000000000000000d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0xffffffffffffffffffffff8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0x000000000000000000000b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0xffffffffffffffffff8a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0x0000000000000000098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0xffffffffffffff88098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0x0000000000000788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0xffffffffff860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0x0000000005860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0xffffff8405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0x0000038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0xff82038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); - assert_eq!(>::detokenize(token), "0x0182038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_u256_as_i256()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000000000901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x00000000000000000000000000000f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000000008e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000000000000000000000000d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x00000000000000000000008c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x000000000000000000000b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000000000000000008a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000000000000000098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000000000000088098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000000000000788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000000000860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000000005860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000008405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0000038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0082038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); + assert_eq!(>::detokenize(token), "0x0182038405860788098a0b8c0d8e0f901192139415961798199a1b9c1d9e1fa0".as_uint_as_int()); } #[test] fn encode_packed() { - let value = (RustAddress::with_last_byte(1), U256::from(2), 3u32, -3i32, 3u32, -3i32); + use alloy_primitives::Uint; + + let value = ( + RustAddress::with_last_byte(1), + Uint::<160, 3>::from(2), + Uint::from(3u32), + Signed::unchecked_from(-3i32), + 3u32, + -3i32, + ); - let res = + let res_ty = ::abi_encode_packed(&value); + let res_value = value.abi_encode_packed(); let expected = hex!( "0000000000000000000000000000000000000001" "0000000000000000000000000000000000000002" @@ -1589,17 +1608,7 @@ mod tests { "00000003" "fffffffd" ); - assert_eq!(hex::encode(res), hex::encode(expected)); - - // Type is `(address, uint256, uint32, int32, uint32, int32)` - let expected = hex!( - "0000000000000000000000000000000000000001" - "0000000000000000000000000000000000000000000000000000000000000002" - "00000003" - "fffffffd" - "00000003" - "fffffffd" - ); - assert_eq!(hex::encode(value.abi_encode_packed()), hex::encode(expected)); + assert_eq!(hex::encode(res_ty), hex::encode(expected)); + assert_eq!(hex::encode(res_value), hex::encode(expected)); } } diff --git a/crates/sol-types/src/types/value.rs b/crates/sol-types/src/types/value.rs index 826a95162..b3fcad6e3 100644 --- a/crates/sol-types/src/types/value.rs +++ b/crates/sol-types/src/types/value.rs @@ -6,7 +6,7 @@ use crate::{ Result, Word, }; use alloc::{borrow::Cow, string::String, vec::Vec}; -use alloy_primitives::{Address, Bytes, FixedBytes, Function, I256, U256}; +use alloy_primitives::{aliases::*, Address, Bytes, FixedBytes, Function, I256, U256}; /// A Solidity value. /// @@ -185,19 +185,71 @@ impl_sol_value! { // Basic [] bool => sol_data::Bool []; - [] i8 => sol_data::Int::<8> []; - [] i16 => sol_data::Int::<16> []; - [] i32 => sol_data::Int::<32> []; - [] i64 => sol_data::Int::<64> []; + [] i8 => sol_data::Int::<8> []; + [] i16 => sol_data::Int::<16> []; + [] I24 => sol_data::Int::<24> []; + [] i32 => sol_data::Int::<32> []; + [] I40 => sol_data::Int::<40> []; + [] I48 => sol_data::Int::<48> []; + [] I56 => sol_data::Int::<56> []; + [] i64 => sol_data::Int::<64> []; + [] I72 => sol_data::Int::<72> []; + [] I80 => sol_data::Int::<80> []; + [] I88 => sol_data::Int::<88> []; + [] I96 => sol_data::Int::<96> []; + [] I104 => sol_data::Int::<104> []; + [] I112 => sol_data::Int::<112> []; + [] I120 => sol_data::Int::<120> []; [] i128 => sol_data::Int::<128> []; + [] I136 => sol_data::Int::<136> []; + [] I144 => sol_data::Int::<144> []; + [] I152 => sol_data::Int::<152> []; + [] I160 => sol_data::Int::<160> []; + [] I168 => sol_data::Int::<168> []; + [] I176 => sol_data::Int::<176> []; + [] I184 => sol_data::Int::<184> []; + [] I192 => sol_data::Int::<192> []; + [] I200 => sol_data::Int::<200> []; + [] I208 => sol_data::Int::<208> []; + [] I216 => sol_data::Int::<216> []; + [] I224 => sol_data::Int::<224> []; + [] I232 => sol_data::Int::<232> []; + [] I240 => sol_data::Int::<240> []; + [] I248 => sol_data::Int::<248> []; [] I256 => sol_data::Int::<256> []; // TODO: `u8` is specialized to encode as `bytes` or `bytesN` // [] u8 => sol_data::Uint::<8> []; - [] u16 => sol_data::Uint::<16> []; - [] u32 => sol_data::Uint::<32> []; - [] u64 => sol_data::Uint::<64> []; + [] u16 => sol_data::Uint::<16> []; + [] U24 => sol_data::Uint::<24> []; + [] u32 => sol_data::Uint::<32> []; + [] U40 => sol_data::Uint::<40> []; + [] U48 => sol_data::Uint::<48> []; + [] U56 => sol_data::Uint::<56> []; + [] u64 => sol_data::Uint::<64> []; + [] U72 => sol_data::Uint::<72> []; + [] U80 => sol_data::Uint::<80> []; + [] U88 => sol_data::Uint::<88> []; + [] U96 => sol_data::Uint::<96> []; + [] U104 => sol_data::Uint::<104> []; + [] U112 => sol_data::Uint::<112> []; + [] U120 => sol_data::Uint::<120> []; [] u128 => sol_data::Uint::<128> []; + [] U136 => sol_data::Uint::<136> []; + [] U144 => sol_data::Uint::<144> []; + [] U152 => sol_data::Uint::<152> []; + [] U160 => sol_data::Uint::<160> []; + [] U168 => sol_data::Uint::<168> []; + [] U176 => sol_data::Uint::<176> []; + [] U184 => sol_data::Uint::<184> []; + [] U192 => sol_data::Uint::<192> []; + [] U200 => sol_data::Uint::<200> []; + [] U208 => sol_data::Uint::<208> []; + [] U216 => sol_data::Uint::<216> []; + [] U224 => sol_data::Uint::<224> []; + [] U232 => sol_data::Uint::<232> []; + [] U240 => sol_data::Uint::<240> []; + [] U248 => sol_data::Uint::<248> []; [] U256 => sol_data::Uint::<256> []; [] Address => sol_data::Address []; diff --git a/crates/sol-types/tests/macros/sol/json.rs b/crates/sol-types/tests/macros/sol/json.rs index e70ec1289..4f88e5837 100644 --- a/crates/sol-types/tests/macros/sol/json.rs +++ b/crates/sol-types/tests/macros/sol/json.rs @@ -1,5 +1,5 @@ use alloy_json_abi::{Function, JsonAbi, Param, StateMutability}; -use alloy_primitives::{Address, B256, I256, U256}; +use alloy_primitives::{Address, Signed, B256, I256, U256}; use alloy_sol_types::{sol, SolCall, SolError, SolEvent, SolStruct}; use pretty_assertions::assert_eq; use std::borrow::Cow; @@ -97,16 +97,19 @@ fn uniswap_v3_position() { let _ = UniswapV3Position::getLiquidityByRangeCall { pool_: Address::ZERO, self_: Address::ZERO, - lowerTick_: 0, - upperTick_: 0, + lowerTick_: Signed::ZERO, + upperTick_: Signed::ZERO, }; assert_eq!( UniswapV3Position::getLiquidityByRangeCall::SIGNATURE, "getLiquidityByRange(address,address,int24,int24)" ); - let _ = - UniswapV3Position::getPositionIdCall { self_: Address::ZERO, lowerTick_: 0, upperTick_: 0 }; + let _ = UniswapV3Position::getPositionIdCall { + self_: Address::ZERO, + lowerTick_: Signed::ZERO, + upperTick_: Signed::ZERO, + }; assert_eq!( UniswapV3Position::getPositionIdCall::SIGNATURE, "getPositionId(address,int24,int24)"