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(sol-macro): add event filters to contracts #563

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Changes from 1 commit
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
Next Next commit
feat(sol-macro): add event filters to contracts
  • Loading branch information
DaniPopes committed Mar 11, 2024
commit 45caef401fdb62caa00dc76e8b02411367078188
38 changes: 35 additions & 3 deletions crates/sol-macro/src/expand/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,30 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS
)
}));

let filter_methods = events.iter().map(|&e| {
let event_name = cx.overloaded_name(e.into());
let name = format_ident!("{name}_filter");
let doc = format!(
"Creates a new event filter for the [`{event_name}`] event.",
);
quote! {
#[doc = #doc]
pub fn #name(&self) -> alloy_contract::Event<N, T, &P, #event_name> {
alloy_contract::private::Event::new(&self.provider)
alloy_contract::SolEventBuilder::new_sol(&self.provider, &self.address)
}
}
});

let alloy_contract = &cx.crates.contract;
let generics_n_t_p = quote!(<N: alloy_contract::private::Network, T: alloy_contract::private::Transport + ::core::clone::Clone, P: alloy_contract::private::Provider<N, T>>);

quote! {
use #alloy_contract as alloy_contract;

#[doc = #new_fn_doc]
#[inline]
pub const fn new<N: alloy_contract::private::Network, T: alloy_contract::private::Transport + ::core::clone::Clone, P: alloy_contract::private::Provider<N, T>>(
pub const fn new #generics_n_t_p(
address: alloy_sol_types::private::Address,
provider: P,
) -> #name<N, T, P> {
Expand All @@ -366,7 +382,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS

/// Instantiation and getters/setters.
#[automatically_derived]
impl<N: alloy_contract::private::Network, T: alloy_contract::private::Transport + ::core::clone::Clone, P: alloy_contract::private::Provider<N, T>> #name<N, T, P> {
impl #generics_n_t_p #name<N, T, P> {
#[doc = #new_fn_doc]
#[inline]
pub const fn new(address: alloy_sol_types::private::Address, provider: P) -> Self {
Expand Down Expand Up @@ -410,7 +426,7 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS

/// Function calls.
#[automatically_derived]
impl<N: alloy_contract::private::Network, T: alloy_contract::private::Transport + ::core::clone::Clone, P: alloy_contract::private::Provider<N, T>> #name<N, T, P> {
impl #generics_n_t_p #name<N, T, P> {
/// Creates a new call builder using this contract instance's provider and address.
///
/// Note that the call can be any function call, not just those defined in this
Expand All @@ -423,6 +439,22 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, contract: &ItemContract) -> Result<TokenS

#(#methods)*
}

/// Event filters.
#[automatically_derived]
impl #generics_n_t_p #name<N, T, P> {
/// Creates a new call builder using this contract instance's provider and address.
///
/// Note that the call can be any function call, not just those defined in this
/// contract. Prefer using the other methods for building type-safe contract calls.
pub fn event_filter<E: alloy_sol_types::SolEvent>(&self, call: &C)
-> alloy_contract::Event<N, T, &P, E>
{
alloy_contract::Event::new_sol(&self.provider, self.address)
}

#(#filter_methods)*
}
}
});

Expand Down