diff --git a/crates/dyn-abi/src/eip712/coerce.rs b/crates/dyn-abi/src/eip712/coerce.rs index a8286fc5c..193ff67b5 100644 --- a/crates/dyn-abi/src/eip712/coerce.rs +++ b/crates/dyn-abi/src/eip712/coerce.rs @@ -107,7 +107,7 @@ fn bytes(value: &serde_json::Value) -> Option> { let arr = value.as_array()?; let mut vec = Vec::with_capacity(arr.len()); - for elem in arr.into_iter() { + for elem in arr.iter() { vec.push(elem.as_u64()?.try_into().ok()?); } Some(vec) diff --git a/crates/dyn-abi/src/eip712/typed_data.rs b/crates/dyn-abi/src/eip712/typed_data.rs index c288c46db..a98b37836 100644 --- a/crates/dyn-abi/src/eip712/typed_data.rs +++ b/crates/dyn-abi/src/eip712/typed_data.rs @@ -101,6 +101,7 @@ impl<'de> Deserialize<'de> for TypedData { message: serde_json::Value, } + #[allow(non_local_definitions)] impl From for TypedData { fn from(value: TypedDataHelper) -> Self { Self { diff --git a/crates/primitives/src/common.rs b/crates/primitives/src/common.rs new file mode 100644 index 000000000..51062ead0 --- /dev/null +++ b/crates/primitives/src/common.rs @@ -0,0 +1,96 @@ +use crate::Address; + +#[cfg(feature = "rlp")] +use alloy_rlp::{Buf, BufMut, Decodable, Encodable, EMPTY_STRING_CODE}; + +/// The `to` field of a transaction. Either a target address, or empty for a +/// contract creation. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] +pub enum TxKind { + /// A transaction that creates a contract. + #[default] + Create, + /// A transaction that calls a contract or transfer. + Call(Address), +} + +impl From> for TxKind { + /// Creates a `TxKind::Call` with the `Some` address, `None` otherwise. + #[inline] + fn from(value: Option
) -> Self { + match value { + None => TxKind::Create, + Some(addr) => TxKind::Call(addr), + } + } +} + +impl From
for TxKind { + /// Creates a `TxKind::Call` with the given address. + #[inline] + fn from(value: Address) -> Self { + TxKind::Call(value) + } +} + +impl TxKind { + /// Returns the address of the contract that will be called or will receive the transfer. + pub const fn to(self) -> Option
{ + match self { + TxKind::Create => None, + TxKind::Call(to) => Some(to), + } + } + + /// Returns true if the transaction is a contract creation. + #[inline] + pub const fn is_create(self) -> bool { + matches!(self, TxKind::Create) + } + + /// Returns true if the transaction is a contract call. + #[inline] + pub const fn is_call(self) -> bool { + matches!(self, TxKind::Call(_)) + } + + /// Calculates a heuristic for the in-memory size of this object. + #[inline] + pub const fn size(self) -> usize { + core::mem::size_of::() + } +} + +#[cfg(feature = "rlp")] +impl Encodable for TxKind { + fn encode(&self, out: &mut dyn BufMut) { + match self { + TxKind::Call(to) => to.encode(out), + TxKind::Create => out.put_u8(EMPTY_STRING_CODE), + } + } + + fn length(&self) -> usize { + match self { + TxKind::Call(to) => to.length(), + TxKind::Create => 1, // EMPTY_STRING_CODE is a single byte + } + } +} + +#[cfg(feature = "rlp")] +impl Decodable for TxKind { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + if let Some(&first) = buf.first() { + if first == EMPTY_STRING_CODE { + buf.advance(1); + Ok(TxKind::Create) + } else { + let addr =
::decode(buf)?; + Ok(TxKind::Call(addr)) + } + } else { + Err(alloy_rlp::Error::InputTooShort) + } + } +} diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index f5f1a42a0..0359acf1f 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -16,6 +16,8 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(feature = "std", allow(unused_imports))] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +// TODO: remove when https://github.com/proptest-rs/proptest/pull/427 is merged +#![allow(unknown_lints, non_local_definitions)] #[macro_use] extern crate alloc; @@ -44,6 +46,9 @@ pub use bits::{ mod bytes_; pub use self::bytes_::Bytes; +mod common; +pub use common::TxKind; + mod log; pub use log::{Log, LogData}; diff --git a/crates/sol-types/src/lib.rs b/crates/sol-types/src/lib.rs index 83be2c9d2..c9d6bfd9b 100644 --- a/crates/sol-types/src/lib.rs +++ b/crates/sol-types/src/lib.rs @@ -159,6 +159,8 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(feature = "std", allow(unused_imports))] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +// TODO: remove when https://github.com/proptest-rs/proptest/pull/427 is merged +#![allow(unknown_lints, non_local_definitions)] #[allow(unused_extern_crates)] extern crate self as alloy_sol_types; diff --git a/crates/sol-types/tests/macros/main.rs b/crates/sol-types/tests/macros/main.rs index c80ec7d43..1d0228245 100644 --- a/crates/sol-types/tests/macros/main.rs +++ b/crates/sol-types/tests/macros/main.rs @@ -1 +1,4 @@ +// TODO: remove when https://github.com/proptest-rs/proptest/pull/427 is merged +#![allow(unknown_lints, non_local_definitions)] + mod sol;