Skip to content

Commit

Permalink
feat(sol-macro): add event filters to contracts (#563)
Browse files Browse the repository at this point in the history
* feat(sol-macro): add event filters to contracts

* refs

* fix

* fix

* fix

* docs
  • Loading branch information
DaniPopes authored Mar 13, 2024
1 parent 02e42ac commit 4f752b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
37 changes: 34 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,29 @@ 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> {
self.event_filter::<#event_name>()
}
}
});

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 +381,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 +425,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 +438,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 event filter using this contract instance's provider and address.
///
/// Note that the type can be any event, not just those defined in this contract.
/// Prefer using the other methods for building type-safe event filters.
pub fn event_filter<E: alloy_sol_types::SolEvent>(&self)
-> alloy_contract::Event<N, T, &P, E>
{
alloy_contract::Event::new_sol(&self.provider, &self.address)
}

#(#filter_methods)*
}
}
});

Expand Down
1 change: 1 addition & 0 deletions crates/sol-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ mod json;
/// other methods when possible
/// - `pub fn <functionName>(&self, <parameters>...) -> CallBuilder<P, functionReturn>` for each
/// function in the contract
/// - `pub fn <eventName>_filter(&self) -> Event<P, eventName>` for each event in the contract
/// - `pub fn new ...`, same as above just as a free function in the contract module
/// - `abi [ = <bool = false>]`: generates functions which return the dynamic ABI representation
/// (provided by [`alloy_json_abi`](https://docs.rs/alloy-json-abi)) of all the generated items.
Expand Down

0 comments on commit 4f752b9

Please sign in to comment.