Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat(abigen): extend ethevent trait methods and decoding #239

Merged

Conversation

mattsse
Copy link
Collaborator

@mattsse mattsse commented Mar 18, 2021

Motivation

This follows up on #234 and also provides a solution for #98

Solution

The EthEvent trait is extended with a method to create an Event builder directly (closes #234).
The EthEvent proc macro now derives a decode_log(log: &RawLog) that parses the log message into an instance of the type it's derived on.
This introduces another trait EthLogDecode which only purpose is to create a common RawLog -> Self interface for all the generated EthEvents of a contract and the generated enum wrapper that is generated for events that have more than 1 event in their abi (closes #98)

Example

if a contract is created as follows

abigen!(
    SimpleContract,
    r#"[
    event ValueChanged(address indexed author, string oldValue, string newValue)
    event OwnerChanged(address indexed author, address indexed author)
    event Log(string msg)
]"#
);

an enum

pub enum SimpleContractEvents {
    LogFilter(LogFilter),
    OwnerChangedFilter(OwnerChangedFilter),
    ValueChangedFilter(ValueChangedFilter),
}

is created.
In addition, if the abi has at least one event the generated contract gets a new method events(&self) -> Event<M, Events>, where Events is either the sole event for contracts with only a single event or the generated enum for contracts with more than one event.
In the latter case, the protocol decoding works in such a way that all variants are tried to be decoded one after the other and the first successful attempt is returned

Breaking changes

  • Renamed the Contract::event(&self, &str) -> Result<Event<_>,Error> to event_for_name and introduced Contract::event<T:EthEvent>(&self) -> Event<_> + another convenience function

Copy link
Owner

@gakonst gakonst left a comment

Choose a reason for hiding this comment

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

1 comment as discussed on the fully qualified syntax, otherwise looks great to me!

#struct_decl

impl<'a, M: Middleware> #name<M> {
impl<'a, M: ethers_providers::Middleware> #name<M> {
Copy link
Owner

Choose a reason for hiding this comment

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

Having fully qualified syntax here requires that we have to declare ethers_providers/core/contract in the consuming crate's cargo toml. It'd be ideal if we avoided that. Could you please revert this change?

Copy link
Collaborator Author

@mattsse mattsse Mar 19, 2021

Choose a reason for hiding this comment

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

Instead of removing the fully qualified syntax, I simply renamed the top level imports to their crate name in e018a70, which achieves the same effect, plus has the benefit, that the EthEvent trait, which resides in ethers_contract is usable with ethers_contract as crate dependency.

Copy link
Owner

Choose a reason for hiding this comment

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

Awesome, thank you, merged.

@gakonst gakonst merged commit 816c5fc into gakonst:master Mar 19, 2021
meetmangukiya pushed a commit to meetmangukiya/ethers-rs that referenced this pull request Mar 21, 2022
* add expectRevert

* fmt

* clippy

* update readme

* typos

* feat: bump ethers for new ethabi types (gakonst#238)

ref: gakonst#700

* custom revert test

* use ethabi to decode

* PR recs

* fmt

Co-authored-by: Georgios Konstantopoulos <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EthEvent::new method for getting typed event data without instantiating a contract Events from Receipts
2 participants