This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
feat: add EthEvent proc macro derive support #227
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR builds on top of #226 with support for deriving custom solidity events
Motivation
This is the first to make working with events more convenient by allowing the user to declare their own types.
Solution
Two new proc macros are introduced:
EthAbiType
: that automatically derives theTokenizable
and therefor automatically theDeTokenizeable
trait for custom structs as long as all members areTokenizable
, see thederive
test file for examples.EthEvent
: derives the newEthEvent
trait, that was introduced based on what is already generated for Event filters at the momentname()
,signature()
,abi_signature()
and derivesTokenizable
as well,Thanks to the
sync
crateEthEvent
tries to derive the ABI from the target rust struct without further information, however this only checks against the names of the types.For example this is all that is necessary to derive a
EthEvent
implementation for ABIEvent(address,string)
:Supported are the rust types
Address
,i<bits>
,u<bits>
,String
,bool
,H160
,H256
,H512
, as well as theirVec<>
version.The
can_detect_various_event_abi_types
test provides a demonstration.The
EthEvent
has several attributes:[ethevent(name= "..."]
: overrides what is returned asEthEvent::name
[ethevent(signature = "66b73eb17a..."]
: overrides theEthEvent::signature
where the providedsignature
is a hex string[ethevent(abi = "Event(address,string)"]
: overrides theEthEvent::abi_signature
Current limitations:
Deriving automatic types only works for fields of elementary types, because there is currently no way to get the ABI for custom types (e.g. structs that derive
EthAbiType
) based on their types alone.This could be mitigated by making the
abi_signature
a trait constant and computing the ABI for such cases at runtime withstatic Lazy::
.Due to a limitation in the argument parser introduced in #226 providing an ABI with tuples does not work yet, since a signature of e.g.
Event((int,int),address)
isn't technically a valid solidity syntax, however support for this could be added to theAbiParser
The
derive.rs
test provides an overview of the features