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
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
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>()
}
}
Comment on lines +341 to +346
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make these default snake case? I realize that for function doStuff we codegen foo.doStuff().send() atm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we default everything to the same name as declared in solidity/ABI. It's possible in the future to implement renaming like in serde but currently there is no real demand.

});

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