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

* fix(abigen): use event name from abi attribute

* rustfmt
This commit is contained in:
Matthias Seitz 2023-02-14 02:11:13 +01:00 committed by GitHub
parent 85d7c07fa8
commit e970f58a8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 4 deletions

View File

@ -21,8 +21,6 @@ pub(crate) fn derive_eth_event_impl(input: DeriveInput) -> Result<TokenStream, E
let name = &input.ident; let name = &input.ident;
let attributes = parse_event_attributes(&input)?; let attributes = parse_event_attributes(&input)?;
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 { let mut event = if let Some((src, span)) = attributes.abi {
// try to parse as a Solidity event // try to parse as a Solidity event
match HumanReadableParser::parse_event(&src) { match HumanReadableParser::parse_event(&src) {
@ -55,7 +53,10 @@ pub(crate) fn derive_eth_event_impl(input: DeriveInput) -> Result<TokenStream, E
derive_abi_event_from_fields(&input) derive_abi_event_from_fields(&input)
}?; }?;
event.name = event_name.clone(); if let Some((attribute_name, _)) = attributes.name {
event.name = attribute_name;
}
if let Some((anon, _)) = attributes.anonymous.as_ref() { if let Some((anon, _)) = attributes.anonymous.as_ref() {
event.anonymous = *anon; event.anonymous = *anon;
} }
@ -71,6 +72,7 @@ pub(crate) fn derive_eth_event_impl(input: DeriveInput) -> Result<TokenStream, E
}; };
let anon = attributes.anonymous.map(|(b, _)| b).unwrap_or_default(); let anon = attributes.anonymous.map(|(b, _)| b).unwrap_or_default();
let event_name = &event.name;
let ethers_core = ethers_core_crate(); let ethers_core = ethers_core_crate();
let ethers_contract = ethers_contract_crate(); let ethers_contract = ethers_contract_crate();
@ -281,7 +283,7 @@ fn derive_decode_from_log_impl(
/// Determine the event's ABI by parsing the AST /// Determine the event's ABI by parsing the AST
fn derive_abi_event_from_fields(input: &DeriveInput) -> Result<Event, Error> { fn derive_abi_event_from_fields(input: &DeriveInput) -> Result<Event, Error> {
let event = Event { let event = Event {
name: "".to_string(), name: input.ident.to_string(),
inputs: utils::derive_abi_inputs_from_fields(input, "EthEvent")? inputs: utils::derive_abi_inputs_from_fields(input, "EthEvent")?
.into_iter() .into_iter()
.map(|(name, kind)| EventParam { name, kind, indexed: false }) .map(|(name, kind)| EventParam { name, kind, indexed: false })

View File

@ -647,3 +647,25 @@ fn can_use_human_readable_error() {
assert_etherror::<MyError>(); 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()
);
}