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

fix(abigen): use event name from abi attribute #2144

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions ethers-contract/ethers-contract-derive/src/event.rs
Expand Up @@ -28,8 +28,6 @@ pub(crate) fn derive_eth_event_impl(input: DeriveInput) -> TokenStream {
Err(errors) => return errors,
};

let event_name = attributes.name.map(|(s, _)| s).unwrap_or_else(|| input.ident.to_string());

let mut event = if let Some((src, span)) = attributes.abi {
// try to parse as solidity event
if let Ok(event) = HumanReadableParser::parse_event(&src) {
Expand Down Expand Up @@ -60,7 +58,10 @@ pub(crate) fn derive_eth_event_impl(input: DeriveInput) -> TokenStream {
}
};

event.name = event_name.clone();
if let Some((attribute_name, _)) = attributes.name {
event.name = attribute_name;
}

if let Some((anon, _)) = attributes.anonymous.as_ref() {
event.anonymous = *anon;
}
Expand All @@ -79,6 +80,7 @@ pub(crate) fn derive_eth_event_impl(input: DeriveInput) -> TokenStream {
};

let anon = attributes.anonymous.map(|(b, _)| b).unwrap_or_default();
let event_name = &event.name;

let ethevent_impl = quote! {
impl #contract_crate::EthEvent for #name {
Expand Down Expand Up @@ -286,7 +288,7 @@ fn derive_decode_from_log_impl(
/// Determine the event's ABI by parsing the AST
fn derive_abi_event_from_fields(input: &DeriveInput) -> Result<Event, Error> {
let event = Event {
name: "".to_string(),
name: input.ident.to_string(),
inputs: utils::derive_abi_inputs_from_fields(input, "EthEvent")?
.into_iter()
.map(|(name, kind)| EventParam { name, kind, indexed: false })
Expand Down
22 changes: 22 additions & 0 deletions ethers-contract/tests/it/common/derive.rs
Expand Up @@ -647,3 +647,25 @@ fn can_use_human_readable_error() {

assert_etherror::<MyError>();
}

// <https://github.com/gakonst/ethers-rs/issues/2142>
#[test]
fn derives_abi_name() {
#[derive(Debug, EthEvent)]
#[ethevent(abi = "Transfer(address,address,uint256)")]
struct Erc20TransferEvent {
#[ethevent(indexed, name = "_from")]
from: Address,
#[ethevent(indexed, name = "_to")]
to: Address,
#[ethevent(name = "_value")]
value: U256,
}

assert_eq!(Erc20TransferEvent::abi_signature(), "Transfer(address,address,uint256)");

assert_eq!(
Erc20TransferEvent::signature(),
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef".parse().unwrap()
);
}