Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contract): remove fake transport and provider traits #1996

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ serial_test = "3.0"
similar-asserts = "1.5"
tempfile = "3.10"
tower-http = "0.6.1"

[patch.crates-io]
alloy-core = { git = "https://github.com/alloy-rs/core", branch = "yash/rm-fake-transport" }
alloy-dyn-abi = { git = "https://github.com/alloy-rs/core", branch = "yash/rm-fake-transport" }
alloy-json-abi = { git = "https://github.com/alloy-rs/core", branch = "yash/rm-fake-transport" }
alloy-primitives = { git = "https://github.com/alloy-rs/core", branch = "yash/rm-fake-transport" }
alloy-sol-types = { git = "https://github.com/alloy-rs/core", branch = "yash/rm-fake-transport" }
54 changes: 23 additions & 31 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use std::{self, marker::PhantomData};

/// [`CallBuilder`] using a [`SolCall`] type as the call decoder.
// NOTE: please avoid changing this type due to its use in the `sol!` macro.
pub type SolCallBuilder<T, P, C, N = Ethereum> = CallBuilder<T, P, PhantomData<C>, N>;
pub type SolCallBuilder<P, C, N = Ethereum> = CallBuilder<P, PhantomData<C>, N>;

/// [`CallBuilder`] using a [`Function`] as the call decoder.
pub type DynCallBuilder<T, P, N = Ethereum> = CallBuilder<T, P, Function, N>;
pub type DynCallBuilder<P, N = Ethereum> = CallBuilder<P, Function, N>;

/// [`CallBuilder`] that does not have a call decoder.
pub type RawCallBuilder<T, P, N = Ethereum> = CallBuilder<T, P, (), N>;
pub type RawCallBuilder<P, N = Ethereum> = CallBuilder<P, (), N>;

/// A builder for sending a transaction via `eth_sendTransaction`, or calling a contract via
/// `eth_call`.
Expand Down Expand Up @@ -70,13 +70,13 @@ pub type RawCallBuilder<T, P, N = Ethereum> = CallBuilder<T, P, (), N>;
/// // Through `contract.<function_name>(args...)`
/// let a = U256::ZERO;
/// let b = true;
/// let builder: SolCallBuilder<_, _, MyContract::doStuffCall, _> = contract.doStuff(a, b);
/// let builder: SolCallBuilder<_, MyContract::doStuffCall, _> = contract.doStuff(a, b);
/// let MyContract::doStuffReturn { c: _, d: _ } = builder.call().await?;
///
/// // Through `contract.call_builder(&<FunctionCall { args... }>)`:
/// // (note that this is discouraged because it's inherently less type-safe)
/// let call = MyContract::doStuffCall { a, b };
/// let builder: SolCallBuilder<_, _, MyContract::doStuffCall, _> = contract.call_builder(&call);
/// let builder: SolCallBuilder<_, MyContract::doStuffCall, _> = contract.call_builder(&call);
/// let MyContract::doStuffReturn { c: _, d: _ } = builder.call().await?;
/// # Ok(())
/// # }
Expand All @@ -102,13 +102,13 @@ pub type RawCallBuilder<T, P, N = Ethereum> = CallBuilder<T, P, (), N>;
/// let contract: ContractInstance<_, _> = interface.connect(address, &provider);
///
/// // Build and call the function:
/// let call_builder: DynCallBuilder<(), _, _> = contract.function("doStuff", &[U256::ZERO.into(), true.into()])?;
/// let call_builder: DynCallBuilder<_, _> = contract.function("doStuff", &[U256::ZERO.into(), true.into()])?;
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved
/// let result: Vec<DynSolValue> = call_builder.call().await?;
///
/// // You can also decode the output manually. Get the raw bytes:
/// let raw_result: Bytes = call_builder.call_raw().await?;
/// // Or, equivalently:
/// let raw_builder: RawCallBuilder<(), _, _> = call_builder.clone().clear_decoder();
/// let raw_builder: RawCallBuilder<_, _> = call_builder.clone().clear_decoder();
/// let raw_result: Bytes = raw_builder.call().await?;
/// // Decode the raw bytes:
/// let decoded_result: Vec<DynSolValue> = call_builder.decode_output(raw_result, false)?;
Expand All @@ -119,32 +119,31 @@ pub type RawCallBuilder<T, P, N = Ethereum> = CallBuilder<T, P, (), N>;
/// [sol]: alloy_sol_types::sol
#[derive(Clone)]
#[must_use = "call builders do nothing unless you `.call`, `.send`, or `.await` them"]
pub struct CallBuilder<T, P, D, N: Network = Ethereum> {
pub struct CallBuilder<P, D, N: Network = Ethereum> {
request: N::TransactionRequest,
block: BlockId,
state: Option<StateOverride>,
/// The provider.
// NOTE: This is public due to usage in `sol!`, please avoid changing it.
pub provider: P,
decoder: D,
fake_transport: PhantomData<T>,
}

impl<T, P, D, N: Network> CallBuilder<T, P, D, N> {
impl<P, D, N: Network> CallBuilder<P, D, N> {
/// Converts the call builder to the inner transaction request
pub fn into_transaction_request(self) -> N::TransactionRequest {
self.request
}
}

impl<T, P, D, N: Network> AsRef<N::TransactionRequest> for CallBuilder<T, P, D, N> {
impl<P, D, N: Network> AsRef<N::TransactionRequest> for CallBuilder<P, D, N> {
fn as_ref(&self) -> &N::TransactionRequest {
&self.request
}
}

// See [`ContractInstance`].
impl<T, P: Provider<N>, N: Network> DynCallBuilder<T, P, N> {
impl<P: Provider<N>, N: Network> DynCallBuilder<P, N> {
pub(crate) fn new_dyn(
provider: P,
address: &Address,
Expand All @@ -161,43 +160,41 @@ impl<T, P: Provider<N>, N: Network> DynCallBuilder<T, P, N> {

/// Clears the decoder, returning a raw call builder.
#[inline]
pub fn clear_decoder(self) -> RawCallBuilder<T, P, N> {
pub fn clear_decoder(self) -> RawCallBuilder<P, N> {
RawCallBuilder {
request: self.request,
block: self.block,
state: self.state,
provider: self.provider,
decoder: (),
fake_transport: PhantomData,
}
}
}

#[doc(hidden)]
impl<'a, T, P: Provider<N>, C: SolCall, N: Network> SolCallBuilder<T, &'a P, C, N> {
impl<'a, P: Provider<N>, C: SolCall, N: Network> SolCallBuilder<&'a P, C, N> {
// `sol!` macro constructor, see `#[sol(rpc)]`. Not public API.
// NOTE: please avoid changing this function due to its use in the `sol!` macro.
pub fn new_sol(provider: &'a P, address: &Address, call: &C) -> Self {
Self::new_inner_call(provider, call.abi_encode().into(), PhantomData::<C>).to(*address)
}
}

impl<T, P: Provider<N>, C: SolCall, N: Network> SolCallBuilder<T, P, C, N> {
impl<P: Provider<N>, C: SolCall, N: Network> SolCallBuilder<P, C, N> {
/// Clears the decoder, returning a raw call builder.
#[inline]
pub fn clear_decoder(self) -> RawCallBuilder<T, P, N> {
pub fn clear_decoder(self) -> RawCallBuilder<P, N> {
RawCallBuilder {
request: self.request,
block: self.block,
state: self.state,
provider: self.provider,
decoder: (),
fake_transport: PhantomData,
}
}
}

impl<T, P: Provider<N>, N: Network> RawCallBuilder<T, P, N> {
impl<P: Provider<N>, N: Network> RawCallBuilder<P, N> {
/// Sets the decoder to the provided [`SolCall`].
///
/// Converts the raw call builder into a sol call builder.
Expand Down Expand Up @@ -250,19 +247,18 @@ impl<T, P: Provider<N>, N: Network> RawCallBuilder<T, P, N> {
/// # }
/// ```
#[inline]
pub fn with_sol_decoder<C: SolCall>(self) -> SolCallBuilder<T, P, C, N> {
pub fn with_sol_decoder<C: SolCall>(self) -> SolCallBuilder<P, C, N> {
SolCallBuilder {
request: self.request,
block: self.block,
state: self.state,
provider: self.provider,
decoder: PhantomData::<C>,
fake_transport: PhantomData,
}
}
}

impl<T, P: Provider<N>, N: Network> RawCallBuilder<T, P, N> {
impl<P: Provider<N>, N: Network> RawCallBuilder<P, N> {
/// Creates a new call builder with the provided provider and ABI encoded input.
///
/// Will not decode the output of the call, meaning that [`call`](Self::call) will behave the
Expand All @@ -282,15 +278,14 @@ impl<T, P: Provider<N>, N: Network> RawCallBuilder<T, P, N> {
}
}

impl<T, P: Provider<N>, D: CallDecoder, N: Network> CallBuilder<T, P, D, N> {
impl<P: Provider<N>, D: CallDecoder, N: Network> CallBuilder<P, D, N> {
fn new_inner_deploy(provider: P, input: Bytes, decoder: D) -> Self {
Self {
request: <N::TransactionRequest>::default().with_deploy_code(input),
decoder,
provider,
block: BlockId::default(),
state: None,
fake_transport: PhantomData,
}
}

Expand All @@ -301,7 +296,6 @@ impl<T, P: Provider<N>, D: CallDecoder, N: Network> CallBuilder<T, P, D, N> {
provider,
block: BlockId::default(),
state: None,
fake_transport: PhantomData,
}
}

Expand Down Expand Up @@ -504,21 +498,20 @@ impl<T, P: Provider<N>, D: CallDecoder, N: Network> CallBuilder<T, P, D, N> {
}
}

impl<T, P: Clone, D, N: Network> CallBuilder<T, &P, D, N> {
impl<P: Clone, D, N: Network> CallBuilder<&P, D, N> {
/// Clones the provider and returns a new builder with the cloned provider.
pub fn with_cloned_provider(self) -> CallBuilder<T, P, D, N> {
pub fn with_cloned_provider(self) -> CallBuilder<P, D, N> {
CallBuilder {
request: self.request,
block: self.block,
state: self.state,
provider: self.provider.clone(),
decoder: self.decoder,
fake_transport: PhantomData,
}
}
}

impl<T, P, D: CallDecoder, N: Network> std::fmt::Debug for CallBuilder<T, P, D, N> {
impl<P, D: CallDecoder, N: Network> std::fmt::Debug for CallBuilder<P, D, N> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CallBuilder")
Expand Down Expand Up @@ -586,8 +579,7 @@ mod tests {

/// Creates a new call_builder to test field modifications, taken from [call_encoding]
#[allow(clippy::type_complexity)]
fn build_call_builder() -> CallBuilder<(), impl Provider, PhantomData<MyContract::doStuffCall>>
{
fn build_call_builder() -> CallBuilder<impl Provider, PhantomData<MyContract::doStuffCall>> {
let provider = ProviderBuilder::new().on_anvil();
let contract = MyContract::new(Address::ZERO, provider);
let call_builder = contract.doStuff(U256::ZERO, true).with_cloned_provider();
Expand Down
22 changes: 10 additions & 12 deletions crates/contract/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use std::{fmt, marker::PhantomData};

/// Helper for managing the event filter before querying or streaming its logs
#[must_use = "event filters do nothing unless you `query`, `watch`, or `stream` them"]
pub struct Event<T, P, E, N = Ethereum> {
pub struct Event<P, E, N = Ethereum> {
/// The provider to use for querying or streaming logs.
pub provider: P,
/// The filter to use for querying or streaming logs.
pub filter: Filter,
_phantom: PhantomData<(T, E, N)>,
_phantom: PhantomData<(E, N)>,
}

impl<T, P: fmt::Debug, E, N> fmt::Debug for Event<T, P, E, N> {
impl<P: fmt::Debug, E, N> fmt::Debug for Event<P, E, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Event")
.field("provider", &self.provider)
Expand All @@ -30,9 +30,7 @@ impl<T, P: fmt::Debug, E, N> fmt::Debug for Event<T, P, E, N> {
}

#[doc(hidden)]
impl<'a, T: crate::private::Transport, P: Provider<N>, E: SolEvent, N: Network>
Event<T, &'a P, E, N>
{
impl<'a, P: Provider<N>, E: SolEvent, N: Network> Event<&'a P, E, N> {
// `sol!` macro constructor, see `#[sol(rpc)]`. Not public API.
// NOTE: please avoid changing this function due to its use in the `sol!` macro.
pub fn new_sol(provider: &'a P, address: &Address) -> Self {
Expand All @@ -46,7 +44,7 @@ impl<'a, T: crate::private::Transport, P: Provider<N>, E: SolEvent, N: Network>
}
}

impl<T, P: Provider<N>, E: SolEvent, N: Network> Event<T, P, E, N> {
impl<P: Provider<N>, E: SolEvent, N: Network> Event<P, E, N> {
/// Creates a new event with the provided provider and filter.
pub const fn new(provider: P, filter: Filter) -> Self {
Self { provider, filter, _phantom: PhantomData }
Expand Down Expand Up @@ -163,9 +161,9 @@ impl<T, P: Provider<N>, E: SolEvent, N: Network> Event<T, P, E, N> {
}
}

impl<T, P: Clone, E, N> Event<T, &P, E, N> {
impl<P: Clone, E, N> Event<&P, E, N> {
/// Clones the provider and returns a new event with the cloned provider.
pub fn with_cloned_provider(self) -> Event<T, P, E, N> {
pub fn with_cloned_provider(self) -> Event<P, E, N> {
Event { provider: self.provider.clone(), filter: self.filter, _phantom: PhantomData }
}
}
Expand Down Expand Up @@ -322,7 +320,7 @@ mod tests {
// let from = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
let contract = MyContract::deploy(&provider).await.unwrap();

let event: Event<(), _, MyContract::MyEvent, _> = Event::new(&provider, Filter::new());
let event: Event<_, MyContract::MyEvent, _> = Event::new(&provider, Filter::new());
let all = event.query().await.unwrap();
assert_eq!(all.len(), 0);

Expand Down Expand Up @@ -429,7 +427,7 @@ mod tests {

let contract = MyContract::deploy(&provider).await.unwrap();

let event: Event<(), _, MyContract::MyEvent, _> = Event::new(&provider, Filter::new())
let event: Event<_, MyContract::MyEvent, _> = Event::new(&provider, Filter::new())
.address(*contract.address())
.event_signature(MyContract::MyEvent::SIGNATURE_HASH);
let all = event.query().await.unwrap();
Expand Down Expand Up @@ -486,7 +484,7 @@ mod tests {
.unwrap();

let contract = MyContract::new(*contract.address(), &provider);
let event: Event<(), _, MyContract::MyEvent, _> = Event::new(&provider, Filter::new())
let event: Event<_, MyContract::MyEvent, _> = Event::new(&provider, Filter::new())
.address(*contract.address())
.event_signature(MyContract::MyEvent::SIGNATURE_HASH);

Expand Down
6 changes: 3 additions & 3 deletions crates/contract/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<P: Provider<N>, N: Network> ContractInstance<P, N> {
&self,
name: &str,
args: &[DynSolValue],
) -> Result<CallBuilder<(), &P, Function, N>> {
) -> Result<CallBuilder<&P, Function, N>> {
let function = self.interface.get_from_name(name)?;
CallBuilder::new_dyn(&self.provider, &self.address, function, args)
}
Expand All @@ -92,13 +92,13 @@ impl<P: Provider<N>, N: Network> ContractInstance<P, N> {
&self,
selector: &Selector,
args: &[DynSolValue],
) -> Result<CallBuilder<(), &P, Function, N>> {
) -> Result<CallBuilder<&P, Function, N>> {
let function = self.interface.get_from_selector(selector)?;
CallBuilder::new_dyn(&self.provider, &self.address, function, args)
}

/// Returns an [`Event`] builder with the provided filter.
pub const fn event<E: SolEvent>(&self, filter: Filter) -> Event<(), &P, E, N> {
pub const fn event<E: SolEvent>(&self, filter: Filter) -> Event<&P, E, N> {
Event::new(&self.provider, filter)
}
}
Expand Down
10 changes: 1 addition & 9 deletions crates/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,5 @@ pub use call::*;
#[doc(hidden)]
pub mod private {
pub use alloy_network::{Ethereum, Network};

// Fake traits to mitigate `sol!` macro breaking changes.
pub trait Provider<T, N: Network>: alloy_provider::Provider<N> {}
impl<N: Network, P: alloy_provider::Provider<N>> Provider<(), N> for P {}

// This is done so that the compiler can infer the `T` type to be `()`, which is the only type
// that implements this fake `Transport` trait.
pub trait Transport {}
impl Transport for () {}
pub use alloy_provider::Provider;
}
Loading